LSST Applications
21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
LSST Data Management Base Package
|
Cache of most recently used values. More...
#include <Cache.h>
Public Member Functions | |
Cache (std::size_t maxElements=0) | |
Ctor. More... | |
Cache (Cache const &)=default | |
Cache (Cache &&)=default | |
Cache & | operator= (Cache const &)=default |
Cache & | operator= (Cache &&)=default |
~Cache ()=default | |
Dtor. More... | |
template<typename Generator > | |
Value | operator() (Key const &key, Generator func) |
Lookup or generate a value. More... | |
Value | operator[] (Key const &key) |
void | add (Key const &key, Value const &value) |
Add a value to the cache. More... | |
std::size_t | size () const |
Return the number of values in the cache. More... | |
std::vector< Key > | keys () const |
Return all keys in the cache, most recent first. More... | |
bool | contains (Key const &key) |
Does the cache contain the key? More... | |
std::size_t | capacity () const |
Return the capacity of the cache. More... | |
void | reserve (std::size_t maxElements) |
Change the capacity of the cache. More... | |
void | flush () |
Empty the cache. More... | |
Cache (std::size_t maxElements=0) | |
Ctor. More... | |
Cache (Cache const &)=default | |
Cache (Cache &&)=default | |
Cache & | operator= (Cache const &)=default |
Cache & | operator= (Cache &&)=default |
~Cache ()=default | |
Dtor. More... | |
template<typename Generator > | |
Value | operator() (Key const &key, Generator func) |
Lookup or generate a value. More... | |
Value | operator[] (Key const &key) |
void | add (Key const &key, Value const &value) |
Add a value to the cache. More... | |
std::size_t | size () const |
Return the number of values in the cache. More... | |
std::vector< Key > | keys () const |
Return all keys in the cache, most recent first. More... | |
bool | contains (Key const &key) |
Does the cache contain the key? More... | |
std::size_t | capacity () const |
Return the capacity of the cache. More... | |
void | reserve (std::size_t maxElements) |
Change the capacity of the cache. More... | |
void | flush () |
Empty the cache. More... | |
Cache of most recently used values.
This object stores the most recent maxElements
values, where maxElements
is set in the constructor. Objects (of type Value
) are stored by a key (of type Key
; hence the need to provide a KeyHash
and KeyPred
), and the class presents a dict-like interface. Objects may be added to (add
) and retrieved from (operator[]
) the cache. For ease of use, an interface (operator()
) is also provided that will check the cache for an existing key, and if the key is not present, generate it with a function provided by the user.
Value
and Key
must be copyable.Cache.h
) should generally only be included in source files, not other header files, because you probably don't want all of the boost::multi_index
includes in your header. We suggest you se the CacheFwd.h
file in your header instead, and hold the Cache
as a std::unique_ptr
.lsst/cpputils/python/Cache.h
.
|
inline |
Ctor.
The maximum number of elements may be zero (default), in which case the cache is permitted to grow without limit.
|
default |
|
default |
|
default |
Dtor.
|
inline |
Ctor.
The maximum number of elements may be zero (default), in which case the cache is permitted to grow without limit.
|
default |
|
default |
|
default |
Dtor.
void lsst::cpputils::Cache< Key, Value, KeyHash, KeyPred >::add | ( | Key const & | key, |
Value const & | value | ||
) |
void lsst::cpputils::Cache< Key, Value, KeyHash, KeyPred >::add | ( | Key const & | key, |
Value const & | value | ||
) |
Add a value to the cache.
If the key is already in the cache, the existing value will be promoted to the most recently used value.
|
inline |
|
inline |
|
inline |
|
inline |
void lsst::cpputils::Cache< Key, Value, KeyHash, KeyPred >::flush |
Empty the cache.
Definition at line 318 of file Cache.h.
void lsst::cpputils::Cache< Key, Value, KeyHash, KeyPred >::flush | ( | ) |
Empty the cache.
std::vector< Key > lsst::cpputils::Cache< Key, Value, KeyHash, KeyPred >::keys |
std::vector<Key> lsst::cpputils::Cache< Key, Value, KeyHash, KeyPred >::keys | ( | ) | const |
Return all keys in the cache, most recent first.
Value lsst::cpputils::Cache< Key, Value, KeyHash, KeyPred >::operator() | ( | Key const & | key, |
Generator | func | ||
) |
Lookup or generate a value.
If the key is in the cache, the corresponding value is returned. Otherwise, a value is generated by the provided function which is cached and returned. Thus, the (expensive) Generator
function only fires if the corresponding value is not already cached.
The Generator
function signature should be:
Value func(Key const& key);
Given the possibility of lambdas, we could have made the required function signature so that it took no arguments. However, it's possible the user has some function that produces a value when given a key, so chose to adopt that signature; any other signature would likely require use of a lambda always.
Value lsst::cpputils::Cache< Key, Value, KeyHash, KeyPred >::operator() | ( | Key const & | key, |
Generator | func | ||
) |
Lookup or generate a value.
If the key is in the cache, the corresponding value is returned. Otherwise, a value is generated by the provided function which is cached and returned. Thus, the (expensive) Generator
function only fires if the corresponding value is not already cached.
The Generator
function signature should be:
Value func(Key const& key);
Given the possibility of lambdas, we could have made the required function signature so that it took no arguments. However, it's possible the user has some function that produces a value when given a key, so chose to adopt that signature; any other signature would likely require use of a lambda always.
|
default |
|
default |
|
default |
|
default |
Value lsst::cpputils::Cache< Key, Value, KeyHash, KeyPred >::operator[] | ( | Key const & | key | ) |
Definition at line 290 of file Cache.h.
Value lsst::cpputils::Cache< Key, Value, KeyHash, KeyPred >::operator[] | ( | Key const & | key | ) |
|
inline |
|
inline |
|
inline |
|
inline |