22#ifndef LSST_CPPUTILS_CACHE_H
23#define LSST_CPPUTILS_CACHE_H
28#include "boost/multi_index_container.hpp"
29#include "boost/multi_index/sequenced_index.hpp"
30#include "boost/multi_index/hashed_index.hpp"
31#include "boost/multi_index/composite_key.hpp"
32#include "boost/multi_index/member.hpp"
33#include "boost/format.hpp"
41#ifdef LSST_CACHE_DEBUG
73template <
typename Key,
typename Value,
typename KeyHash,
typename KeyPred>
85 _container.template get<Hash>().reserve(maxElements);
86#ifdef LSST_CACHE_DEBUG
87 _debuggingEnabled =
false;
90 _requests.reserve(maxElements);
101#ifdef LSST_CACHE_DEBUG
129 template <
typename Generator>
153 void add(Key
const&
key, Value
const& value);
199#ifdef LSST_CACHE_DEBUG
200 void enableDebugging() { _debuggingEnabled =
true; }
209 _container.template get<Sequence>().pop_back();
221 typedef boost::multi_index_container<
223 boost::multi_index::indexed_by<
224 boost::multi_index::sequenced<boost::multi_index::tag<Sequence>>,
225 boost::multi_index::hashed_unique<
226 boost::multi_index::tag<Hash>,
227 boost::multi_index::member<Element, Key, &Element::first>,
228 KeyHash>>> Container;
236 auto const& hashContainer = _container.template get<Hash>();
237 auto it = hashContainer.find(key);
238 bool found = (it != hashContainer.end());
240 _container.relocate(_container.template get<Sequence>().begin(),
241 _container.template project<Sequence>(it));
243#ifdef LSST_CACHE_DEBUG
244 if (_debuggingEnabled) {
245 _requests.push_back(key);
254 void _addNew(Key
const& key, Value
const& value) {
255 _container.template get<Sequence>().emplace_front(key, value);
260 Container _container;
261#ifdef LSST_CACHE_DEBUG
262 bool _debuggingEnabled;
274template <
typename Key,
typename Value,
typename KeyHash,
typename KeyPred>
275template <
typename Generator>
280 auto result = _lookup(
key);
282 return result.first->second;
284 Value value = func(
key);
289template <
typename Key,
typename Value,
typename KeyHash,
typename KeyPred>
291 auto result = _lookup(
key);
293 return result.first->second;
296 (boost::format(
"Unable to find key: %s") %
key).
str());
299template <
typename Key,
typename Value,
typename KeyHash,
typename KeyPred>
301 auto result = _lookup(
key);
302 if (!result.second) {
307template <
typename Key,
typename Value,
typename KeyHash,
typename KeyPred>
311 for (
auto & keyValue : _container.template get<Sequence>()) {
317template <
typename Key,
typename Value,
typename KeyHash,
typename KeyPred>
320 _container.template get<Sequence>().pop_back();
324#ifdef LSST_CACHE_DEBUG
325template <
typename Key,
typename Value,
typename KeyHash,
typename KeyPred>
327 if (!_debuggingEnabled) {
330 std::string filename = (boost::format(
"lsst-cache-%s-%d.dat") %
333 for (
auto const& key : _requests) {
337 std::cerr <<
"Wrote cache requests to " << filename <<
": " << _hits <<
"/" << _total <<
" hits";
343namespace utils = cpputils;
#define LSST_EXCEPT(type,...)
Cache of most recently used values.
std::vector< Key > keys() const
Return all keys in the cache, most recent first.
std::size_t size() const
Return the number of values in the cache.
Value operator()(Key const &key, Generator func)
Lookup or generate a value.
bool contains(Key const &key)
Does the cache contain the key?
Cache(Cache const &)=default
std::size_t capacity() const
Return the capacity of the cache.
void flush()
Empty the cache.
void add(Key const &key, Value const &value)
Add a value to the cache.
Cache(std::size_t maxElements=0)
Ctor.
Cache & operator=(Cache &&)=default
Value operator[](Key const &key)
void reserve(std::size_t maxElements)
Change the capacity of the cache.
Cache & operator=(Cache const &)=default
std::string demangleType(std::string const _typeName)
Forward declarations for lsst::cpputils::Cache.