MADNESS 0.10.1
simplecache.h
Go to the documentation of this file.
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>
38
39namespace madness {
40 /// Simplified interface around hash_map to cache stuff for 1D
41
42 /// This is a write once cache --- subsequent writes of elements
43 /// have no effect (so that pointers/references to cached data
44 /// cannot be invalidated)
45 template <typename Q, std::size_t NDIM>
47 private:
49 typedef std::pair<Key<NDIM>, Q> pairT;
51
52 public:
54
56
58 if (this != &c) {
59 cache.clear();
60 cache = c.cache;
61 }
62 return *this;
63 }
64
65 /// If key is present return pointer to cached value, otherwise return NULL
66 inline const Q* getptr(const Key<NDIM>& key) const {
67 typename mapT::const_iterator test = cache.find(key);
68 if (test == cache.end()) return 0;
69 return &(test->second);
70 }
71
72
73 /// If key=(n,l) is present return pointer to cached value, otherwise return NULL
74
75 /// This for the convenience (backward compatibility) of 1D routines
76 inline const Q* getptr(Level n, Translation l) const {
78 return getptr(key);
79 }
80
81
82 /// If key=(n,l) is present return pointer to cached value, otherwise return NULL
83
84 /// This for the convenience (backward compatibility) of 1D routines
85 inline const Q* getptr(Level n, const Key<NDIM>& disp) const {
86 Key<NDIM> key(n,disp.translation());
87 return getptr(key);
88 }
89
90
91 /// Set value associated with key ... gives ownership of a new copy to the container
92 inline void set(const Key<NDIM>& key, const Q& val) {
93 [[maybe_unused]] auto&& [it, inserted] = cache.insert(pairT(key,val));
94 }
95
96 inline void set(Level n, Translation l, const Q& val) {
98 set(key, val);
99 }
100
101 inline void set(Level n, const Key<NDIM>& disp, const Q& val) {
102 Key<NDIM> key(n,disp.translation());
103 set(key, val);
104 }
105 };
106}
107#endif // MADNESS_MRA_SIMPLECACHE_H__INCLUDED
Definition worldhashmap.h:396
std::pair< iterator, bool > insert(const datumT &datum)
Definition worldhashmap.h:468
iterator end()
Definition worldhashmap.h:583
iterator find(const keyT &key)
Definition worldhashmap.h:524
void clear()
Definition worldhashmap.h:556
iterator for hash
Definition worldhashmap.h:188
Key is the index for a node of the 2^NDIM-tree.
Definition key.h:66
const Vector< Translation, NDIM > & translation() const
Definition key.h:164
Simplified interface around hash_map to cache stuff for 1D.
Definition simplecache.h:46
const Q * getptr(Level n, const Key< NDIM > &disp) const
If key=(n,l) is present return pointer to cached value, otherwise return NULL.
Definition simplecache.h:85
ConcurrentHashMap< Key< NDIM >, Q > mapT
Definition simplecache.h:48
void set(Level n, const Key< NDIM > &disp, const Q &val)
Definition simplecache.h:101
SimpleCache()
Definition simplecache.h:53
mapT cache
Definition simplecache.h:50
SimpleCache & operator=(const SimpleCache &c)
Definition simplecache.h:57
const Q * getptr(Level n, Translation l) const
If key=(n,l) is present return pointer to cached value, otherwise return NULL.
Definition simplecache.h:76
std::pair< Key< NDIM >, Q > pairT
Definition simplecache.h:49
SimpleCache(const SimpleCache &c)
Definition simplecache.h:55
void set(const Key< NDIM > &key, const Q &val)
Set value associated with key ... gives ownership of a new copy to the container.
Definition simplecache.h:92
const Q * getptr(const Key< NDIM > &key) const
If key is present return pointer to cached value, otherwise return NULL.
Definition simplecache.h:66
void set(Level n, Translation l, const Q &val)
Definition simplecache.h:96
A simple, fixed dimension vector.
Definition vector.h:64
Multidimension Key for MRA tree and associated iterators.
Namespace for all elements and tools of MADNESS.
Definition DFParameters.h:10
int64_t Translation
Definition key.h:54
int Level
Definition key.h:55
double Q(double a)
Definition relops.cc:20
static const double c
Definition relops.cc:10
Defines and implements a concurrent hashmap.
void test()
Definition y.cc:696