Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#ifndef MANTID_KERNEL_CACHE_H_
#define MANTID_KERNEL_CACHE_H_
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include <map>
#include "MantidKernel/System.h"
namespace Mantid
{
namespace Kernel
{
/** @class Cache Cache.h Kernel/Cache.h
Cache is a generic caching storage class.
@author Nick Draper, Tessella Support Services plc
@date 20/10/2009
Copyright © 2007-9 STFC Rutherford Appleton Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>.
Code Documentation is available at: <http://doxygen.mantidproject.org>
Gigg, Martyn Anthony
committed
*/
template< class KEYTYPE, class VALUETYPE >
class DLLExport Cache
{
public:
/// No-arg Constructor
Cache():m_cacheHit(0),m_cacheMiss(0)
{
}
///Clears the cache
Gigg, Martyn Anthony
committed
void clear()
{
m_cacheHit = 0;
m_cacheMiss = 0;
int size() {return static_cast<int>(m_cacheMap.size()); }
///total number of times the cache has contained the requested information
int hitCount() { return m_cacheHit; }
///total number of times the cache has contained the requested information
int missCount() { return m_cacheMiss; }
///total number of times the cache has contained the requested information/the total number of requests
double hitRatio()
{
double hitRatio = 0.0;
if ((m_cacheHit+m_cacheMiss)>0)
{
hitRatio = 100.0*(m_cacheHit*1.0)/(m_cacheHit+m_cacheMiss);
}
return hitRatio;
}
Gigg, Martyn Anthony
committed
///Sets a cached value on the rotation cache
void setCache(const KEYTYPE key, VALUETYPE value)
{
if (getCacheNoStats(key,value))
{
//remove key
removeCache(key);
}
m_cacheMap.insert(std::make_pair(key,value));
}
///Attempts to retreive a value from the cache
Gigg, Martyn Anthony
committed
bool found = getCacheNoStats(key,value);
if (found)
{
m_cacheHit++;
}
else
{
m_cacheMiss++;
}
Gigg, Martyn Anthony
committed
///removes the value associated with a key
Gigg, Martyn Anthony
committed
CacheMapIterator it_found = m_cacheMap.find(key);
if (it_found != m_cacheMap.end())
{
m_cacheMap.erase(it_found);
Gigg, Martyn Anthony
committed
///Attempts to retreive a value from the cache
bool getCacheNoStats(const KEYTYPE key, VALUETYPE& value) const
{
Gigg, Martyn Anthony
committed
CacheMapConstIterator it_found = m_cacheMap.find(key);
if (it_found == m_cacheMap.end())
{
return false; // did not find the component
}
value = it_found->second;
return true;
}
///total number of times the cache has contained the requested information
Gigg, Martyn Anthony
committed
mutable int m_cacheHit;
///total number of times the cache has not contained the requested information
Gigg, Martyn Anthony
committed
/// internal cache map
std::map<const KEYTYPE,VALUETYPE > m_cacheMap;
/// iterator typedef
typedef typename std::map<const KEYTYPE,VALUETYPE >::iterator CacheMapIterator;
/// const_iterator typedef
typedef typename std::map<const KEYTYPE,VALUETYPE >::const_iterator CacheMapConstIterator;