LSSTApplications
17.0+11,17.0+34,17.0+56,17.0+57,17.0+59,17.0+7,17.0-1-g377950a+33,17.0.1-1-g114240f+2,17.0.1-1-g4d4fbc4+28,17.0.1-1-g55520dc+49,17.0.1-1-g5f4ed7e+52,17.0.1-1-g6dd7d69+17,17.0.1-1-g8de6c91+11,17.0.1-1-gb9095d2+7,17.0.1-1-ge9fec5e+5,17.0.1-1-gf4e0155+55,17.0.1-1-gfc65f5f+50,17.0.1-1-gfc6fb1f+20,17.0.1-10-g87f9f3f+1,17.0.1-11-ge9de802+16,17.0.1-16-ga14f7d5c+4,17.0.1-17-gc79d625+1,17.0.1-17-gdae4c4a+8,17.0.1-2-g26618f5+29,17.0.1-2-g54f2ebc+9,17.0.1-2-gf403422+1,17.0.1-20-g2ca2f74+6,17.0.1-23-gf3eadeb7+1,17.0.1-3-g7e86b59+39,17.0.1-3-gb5ca14a,17.0.1-3-gd08d533+40,17.0.1-30-g596af8797,17.0.1-4-g59d126d+4,17.0.1-4-gc69c472+5,17.0.1-6-g5afd9b9+4,17.0.1-7-g35889ee+1,17.0.1-7-gc7c8782+18,17.0.1-9-gc4bbfb2+3,w.2019.22
LSSTDataManagementBasePackage
|
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 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/utils/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.
Definition at line 84 of file Cache.h.
|
default |
|
default |
|
default |
Dtor.
void lsst::utils::Cache< Key, Value, KeyHash, KeyPred >::add | ( | Key const & | key, |
Value const & | value | ||
) |
|
inline |
|
inline |
void lsst::utils::Cache< Key, Value, KeyHash, KeyPred >::flush | ( | ) |
Empty the cache.
Definition at line 318 of file Cache.h.
std::vector< Key > lsst::utils::Cache< Key, Value, KeyHash, KeyPred >::keys | ( | ) | const |
Return all keys in the cache, most recent first.
Definition at line 308 of file Cache.h.
Value lsst::utils::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.
Definition at line 276 of file Cache.h.
|
default |
|
default |
Value lsst::utils::Cache< Key, Value, KeyHash, KeyPred >::operator[] | ( | Key const & | key | ) |
Definition at line 290 of file Cache.h.
|
inline |
|
inline |