1 /* 2 This file is part of MADNESS. 3 4 Copyright (C) 2007,2010 Oak Ridge National Laboratory 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program; if not, write to the Free Software 18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 20 For more information please contact: 21 22 Robert J. Harrison 23 Oak Ridge National Laboratory 24 One Bethel Valley Road 25 P.O. Box 2008, MS-6367 26 27 email: harrisonrj@ornl.gov 28 tel: 865-241-3937 29 fax: 865-572-0680 30 31 $Id$ 32 */ 33 #ifndef MADNESS_MRA_SIMPLECACHE_H__INCLUDED 34 #define MADNESS_MRA_SIMPLECACHE_H__INCLUDED 35 36 #include <madness/mra/key.h> 37 38 namespace madness { 39 /// Simplified interface around hash_map to cache stuff for 1D 40 41 /// This is a write once cache --- subsequent writes of elements 42 /// have no effect (so that pointers/references to cached data 43 /// cannot be invalidated) 44 template <typename Q, std::size_t NDIM> 45 class SimpleCache { 46 private: 47 typedef ConcurrentHashMap< Key<NDIM>, Q > mapT; 48 typedef std::pair<Key<NDIM>, Q> pairT; 49 mapT cache; 50 51 public: SimpleCache()52 SimpleCache() : cache() {}; 53 SimpleCache(const SimpleCache & c)54 SimpleCache(const SimpleCache& c) : cache(c.cache) {}; 55 56 SimpleCache& operator=(const SimpleCache& c) { 57 if (this != &c) { 58 cache.clear(); 59 cache = c.cache; 60 } 61 return *this; 62 } 63 64 /// If key is present return pointer to cached value, otherwise return NULL getptr(const Key<NDIM> & key)65 inline const Q* getptr(const Key<NDIM>& key) const { 66 typename mapT::const_iterator test = cache.find(key); 67 if (test == cache.end()) return 0; 68 return &(test->second); 69 } 70 71 72 /// If key=(n,l) is present return pointer to cached value, otherwise return NULL 73 74 /// This for the convenience (backward compatibility) of 1D routines getptr(Level n,Translation l)75 inline const Q* getptr(Level n, Translation l) const { 76 Key<NDIM> key(n,Vector<Translation,NDIM>(l)); 77 return getptr(key); 78 } 79 80 81 /// If key=(n,l) is present return pointer to cached value, otherwise return NULL 82 83 /// This for the convenience (backward compatibility) of 1D routines getptr(Level n,const Key<NDIM> & disp)84 inline const Q* getptr(Level n, const Key<NDIM>& disp) const { 85 Key<NDIM> key(n,disp.translation()); 86 return getptr(key); 87 } 88 89 90 /// Set value associated with key ... gives ownership of a new copy to the container set(const Key<NDIM> & key,const Q & val)91 inline void set(const Key<NDIM>& key, const Q& val) { 92 cache.insert(pairT(key,val)); 93 } 94 set(Level n,Translation l,const Q & val)95 inline void set(Level n, Translation l, const Q& val) { 96 Key<NDIM> key(n,Vector<Translation,NDIM>(l)); 97 set(key, val); 98 } 99 set(Level n,const Key<NDIM> & disp,const Q & val)100 inline void set(Level n, const Key<NDIM>& disp, const Q& val) { 101 Key<NDIM> key(n,disp.translation()); 102 set(key, val); 103 } 104 }; 105 } 106 #endif // MADNESS_MRA_SIMPLECACHE_H__INCLUDED 107