| 1 |
/// The MIT License (MIT) |
| 2 |
/// Copyright (c) 2016 Peter Goldsborough |
| 3 |
/// |
| 4 |
/// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
/// of this software and associated documentation files (the "Software"), to |
| 6 |
/// deal in the Software without restriction, including without limitation the |
| 7 |
/// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 8 |
/// sell copies of the Software, and to permit persons to whom the Software is |
| 9 |
/// furnished to do so, subject to the following conditions: |
| 10 |
/// |
| 11 |
/// The above copyright notice and this permission notice shall be included in |
| 12 |
/// all copies or substantial portions of the Software. |
| 13 |
/// |
| 14 |
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 |
/// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 |
/// IN THE SOFTWARE. |
| 21 |
|
| 22 |
#ifndef LRU_CACHE_HPP |
| 23 |
#define LRU_CACHE_HPP |
| 24 |
|
| 25 |
#include <cassert> |
| 26 |
#include <chrono> |
| 27 |
#include <cstddef> |
| 28 |
#include <functional> |
| 29 |
#include <iterator> |
| 30 |
#include <list> |
| 31 |
#include <stdexcept> |
| 32 |
#include <unordered_map> |
| 33 |
|
| 34 |
#include <lru/cache-tags.hpp> |
| 35 |
#include <lru/error.hpp> |
| 36 |
#include <lru/internal/base-cache.hpp> |
| 37 |
#include <lru/internal/information.hpp> |
| 38 |
#include <lru/internal/last-accessed.hpp> |
| 39 |
|
| 40 |
namespace LRU { |
| 41 |
namespace Internal { |
| 42 |
template <typename Key, |
| 43 |
typename Value, |
| 44 |
typename HashFunction, |
| 45 |
typename KeyEqual> |
| 46 |
using UntimedCacheBase = Internal::BaseCache<Key, |
| 47 |
Value, |
| 48 |
Internal::Information, |
| 49 |
HashFunction, |
| 50 |
KeyEqual, |
| 51 |
Tag::BasicCache>; |
| 52 |
} // namespace Internal |
| 53 |
|
| 54 |
/// A basic LRU cache implementation. |
| 55 |
/// |
| 56 |
/// An LRU cache is a fixed-size cache that remembers the order in which |
| 57 |
/// elements were inserted into it. When the size of the cache exceeds its |
| 58 |
/// capacity, the "least-recently-used" (LRU) element is erased. In our |
| 59 |
/// implementation, usage is defined as insertion, but not lookup. That is, |
| 60 |
/// looking up an element does not move it to the "front" of the cache (making |
| 61 |
/// the operation faster). Only insertions (and erasures) can change the order |
| 62 |
/// of elements. The capacity of the cache can be modified at any time. |
| 63 |
/// |
| 64 |
/// \see LRU::TimedCache |
| 65 |
template <typename Key, |
| 66 |
typename Value, |
| 67 |
typename HashFunction = std::hash<Key>, |
| 68 |
typename KeyEqual = std::equal_to<Key>> |
| 69 |
class Cache |
| 70 |
: public Internal::UntimedCacheBase<Key, Value, HashFunction, KeyEqual> { |
| 71 |
private: |
| 72 |
using super = Internal::UntimedCacheBase<Key, Value, HashFunction, KeyEqual>; |
| 73 |
using PRIVATE_BASE_CACHE_MEMBERS; |
| 74 |
|
| 75 |
public: |
| 76 |
using PUBLIC_BASE_CACHE_MEMBERS; |
| 77 |
using typename super::size_t; |
| 78 |
|
| 79 |
/// \copydoc BaseCache::BaseCache(size_t,const HashFunction&,const KeyEqual&) |
| 80 |
/// \detailss The capacity defaults to an internal constant, currently 128. |
| 81 |
explicit Cache(size_t capacity = Internal::DEFAULT_CAPACITY, |
| 82 |
const HashFunction& hash = HashFunction(), |
| 83 |
const KeyEqual& equal = KeyEqual()) |
| 84 |
: super(capacity, hash, equal) { |
| 85 |
} |
| 86 |
|
| 87 |
/// \copydoc BaseCache(size_t,Iterator,Iterator,const HashFunction&,const |
| 88 |
/// KeyEqual&) |
| 89 |
template <typename Iterator> |
| 90 |
Cache(size_t capacity, |
| 91 |
Iterator begin, |
| 92 |
Iterator end, |
| 93 |
const HashFunction& hash = HashFunction(), |
| 94 |
const KeyEqual& equal = KeyEqual()) |
| 95 |
: super(capacity, begin, end, hash, equal) { |
| 96 |
} |
| 97 |
|
| 98 |
/// \copydoc BaseCache(Iterator,Iterator,const HashFunction&,const |
| 99 |
/// KeyEqual&) |
| 100 |
template <typename Iterator> |
| 101 |
Cache(Iterator begin, |
| 102 |
Iterator end, |
| 103 |
const HashFunction& hash = HashFunction(), |
| 104 |
const KeyEqual& equal = KeyEqual()) |
| 105 |
: super(begin, end, hash, equal) { |
| 106 |
} |
| 107 |
|
| 108 |
/// Constructor. |
| 109 |
/// |
| 110 |
/// \param capacity The capacity of the cache. |
| 111 |
/// \param range A range to construct the cache with. |
| 112 |
/// \param hash The hash function to use for the internal map. |
| 113 |
/// \param key_equal The key equality function to use for the internal map. |
| 114 |
template <typename Range, typename = Internal::enable_if_range<Range>> |
| 115 |
Cache(size_t capacity, |
| 116 |
Range&& range, |
| 117 |
const HashFunction& hash = HashFunction(), |
| 118 |
const KeyEqual& equal = KeyEqual()) |
| 119 |
: super(capacity, std::forward<Range>(range), hash, equal) { |
| 120 |
} |
| 121 |
|
| 122 |
/// Constructor. |
| 123 |
/// |
| 124 |
/// \param range A range to construct the cache with. |
| 125 |
/// \param hash The hash function to use for the internal map. |
| 126 |
/// \param key_equal The key equality function to use for the internal map. |
| 127 |
template <typename Range, typename = Internal::enable_if_range<Range>> |
| 128 |
explicit Cache(Range&& range, |
| 129 |
const HashFunction& hash = HashFunction(), |
| 130 |
const KeyEqual& equal = KeyEqual()) |
| 131 |
: super(std::forward<Range>(range), hash, equal) { |
| 132 |
} |
| 133 |
|
| 134 |
/// \copydoc BaseCache(InitializerList,const HashFunction&,const |
| 135 |
/// KeyEqual&) |
| 136 |
Cache(InitializerList list, |
| 137 |
const HashFunction& hash = HashFunction(), |
| 138 |
const KeyEqual& equal = KeyEqual()) // NOLINT(runtime/explicit) |
| 139 |
: super(list, hash, equal) { |
| 140 |
} |
| 141 |
|
| 142 |
/// \copydoc BaseCache(size_t,InitializerList,const HashFunction&,const |
| 143 |
/// KeyEqual&) |
| 144 |
Cache(size_t capacity, |
| 145 |
InitializerList list, |
| 146 |
const HashFunction& hash = HashFunction(), |
| 147 |
const KeyEqual& equal = KeyEqual()) // NOLINT(runtime/explicit) |
| 148 |
: super(capacity, list, hash, equal) { |
| 149 |
} |
| 150 |
|
| 151 |
/// \copydoc BaseCache::find(const Key&) |
| 152 |
UnorderedIterator find(const Key& key) override { |
| 153 |
auto iterator = _map.find(key); |
| 154 |
if (iterator != _map.end()) { |
| 155 |
_register_hit(key, iterator->second.value); |
| 156 |
_move_to_front(iterator->second.order); |
| 157 |
_last_accessed = iterator; |
| 158 |
} else { |
| 159 |
_register_miss(key); |
| 160 |
} |
| 161 |
|
| 162 |
return {*this, iterator}; |
| 163 |
} |
| 164 |
|
| 165 |
/// \copydoc BaseCache::find(const Key&) const |
| 166 |
UnorderedConstIterator find(const Key& key) const override { |
| 167 |
auto iterator = _map.find(key); |
| 168 |
if (iterator != _map.end()) { |
| 169 |
_register_hit(key, iterator->second.value); |
| 170 |
_move_to_front(iterator->second.order); |
| 171 |
_last_accessed = iterator; |
| 172 |
} else { |
| 173 |
_register_miss(key); |
| 174 |
} |
| 175 |
|
| 176 |
return {*this, iterator}; |
| 177 |
} |
| 178 |
|
| 179 |
/// \returns The most-recently inserted element. |
| 180 |
const Key& front() const noexcept { |
| 181 |
if (is_empty()) { |
| 182 |
throw LRU::Error::EmptyCache("front"); |
| 183 |
} else { |
| 184 |
// The queue is reversed for natural order of iteration. |
| 185 |
return _order.back(); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
/// \returns The least-recently inserted element. |
| 190 |
const Key& back() const noexcept { |
| 191 |
if (is_empty()) { |
| 192 |
throw LRU::Error::EmptyCache("back"); |
| 193 |
} else { |
| 194 |
// The queue is reversed for natural order of iteration. |
| 195 |
return _order.front(); |
| 196 |
} |
| 197 |
} |
| 198 |
}; |
| 199 |
|
| 200 |
namespace Lowercase { |
| 201 |
template <typename... Ts> |
| 202 |
using cache = Cache<Ts...>; |
| 203 |
} // namespace Lowercase |
| 204 |
|
| 205 |
} // namespace LRU |
| 206 |
|
| 207 |
#endif // LRU_CACHE_HPP |