Skip to content
Snippets Groups Projects
Cache.h 4.35 KiB
Newer Older
Nick Draper's avatar
Nick Draper committed
#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 &copy; 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>
Nick Draper's avatar
Nick Draper committed
    template< class KEYTYPE, class VALUETYPE >
    class DLLExport Cache
    {
    public:

      /// No-arg Constructor
      Cache():m_cacheHit(0),m_cacheMiss(0)
      {
      }

      ///Clears the cache
Nick Draper's avatar
Nick Draper committed
        m_cacheMap.clear();
      }

      ///The number of cache entries
      int size() {return static_cast<int>(m_cacheMap.size()); }
Nick Draper's avatar
Nick Draper committed

      ///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; 
      }

      ///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
Nick Draper's avatar
Nick Draper committed
      bool getCache(const KEYTYPE key, VALUETYPE& value) const
      {
        bool found = getCacheNoStats(key,value);
        if (found) 
        {
          m_cacheHit++;
        }
        else
        {
          m_cacheMiss++;
        }
Nick Draper's avatar
Nick Draper committed
        return found;
      }

Nick Draper's avatar
Nick Draper committed
      void removeCache(const KEYTYPE key)
      {
        CacheMapIterator it_found = m_cacheMap.find(key);
        if (it_found != m_cacheMap.end()) 
        {
          m_cacheMap.erase(it_found);
Nick Draper's avatar
Nick Draper committed
        }
      }

    private:
      ///Attempts to retreive a value from the cache
Nick Draper's avatar
Nick Draper committed
      bool getCacheNoStats(const KEYTYPE key, VALUETYPE& value) const
      {
	CacheMapConstIterator it_found = m_cacheMap.find(key);
        if (it_found == m_cacheMap.end()) 
        {
          return false; // did not find the component
        }
        value = it_found->second;
Nick Draper's avatar
Nick Draper committed
        return true;
      }


      ///total number of times the cache has contained the requested information
      mutable int m_cacheHit;
      ///total number of times the cache has not contained the requested information
Nick Draper's avatar
Nick Draper committed
      mutable int m_cacheMiss;
      /// 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;
Nick Draper's avatar
Nick Draper committed
    };

  } // namespace Kernel
} // namespace Mantid

#endif /*MANTID_KERNEL_CACHE_H_*/