| 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_INTERNAL_LAST_ACCESSED_HPP |
| 23 |
#define LRU_INTERNAL_LAST_ACCESSED_HPP |
| 24 |
|
| 25 |
#include <algorithm> |
| 26 |
#include <functional> |
| 27 |
#include <iterator> |
| 28 |
|
| 29 |
#include <lru/internal/utility.hpp> |
| 30 |
|
| 31 |
namespace LRU { |
| 32 |
namespace Internal { |
| 33 |
|
| 34 |
/// Provides a simple iterator-compatible pointer object for a key and |
| 35 |
/// information. |
| 36 |
/// |
| 37 |
/// The easisest idea for this class, theoretically, would be to just store an s |
| 38 |
/// iterator to the internal cache map (i.e. template the class on the iterator |
| 39 |
/// type). However, the major trouble with that approach is that this class |
| 40 |
/// should be 100% *mutable*, as in "always non-const", so that keys and |
| 41 |
/// informations |
| 42 |
/// we store for fast access can be (quickly) retrieved as either const or |
| 43 |
/// non-const (iterators for example). This is not possible, since the |
| 44 |
/// const-ness of `const_iterators` are not the usual idea of const in C++, |
| 45 |
/// meaning especially it cannot be cast away with a `const_cast` as is required |
| 46 |
/// for the mutability. As such, we *must* store the plain keys and |
| 47 |
/// informations. |
| 48 |
/// This, however, means that iterators cannot be stored efficiently, since a |
| 49 |
/// new hash table lookup would be required to go from a key to its iterator. |
| 50 |
/// However, since the main use case of this class is to avoid a second lookup |
| 51 |
/// in the usual `if (cache.contains(key)) return cache.lookup(key)`, which is |
| 52 |
/// not an issue for iterators since they can be compared to the `end` iterator |
| 53 |
/// in constant time (equivalent to the call to `contains()`). |
| 54 |
/// |
| 55 |
/// WARNING: This class stores *pointers* to keys and informations. As such |
| 56 |
/// lifetime |
| 57 |
/// of the pointed-to objects must be cared for by the user of this class. |
| 58 |
/// |
| 59 |
/// \tparam Key The type of key being accessed. |
| 60 |
/// \tparam InformationType The type of information being accessed. |
| 61 |
/// \tparam KeyEqual The type of the key comparison function. |
| 62 |
template <typename Key, |
| 63 |
typename InformationType, |
| 64 |
typename KeyEqual = std::equal_to<Key>> |
| 65 |
class LastAccessed { |
| 66 |
public: |
| 67 |
/// Constructor. |
| 68 |
/// |
| 69 |
/// \param key_equal The function to compare keys with. |
| 70 |
explicit LastAccessed(const KeyEqual& key_equal = KeyEqual()) |
| 71 |
: _key(nullptr) |
| 72 |
, _information(nullptr) |
| 73 |
, _is_valid(false) |
| 74 |
, _key_equal(key_equal) { |
| 75 |
} |
| 76 |
|
| 77 |
/// Constructor. |
| 78 |
/// |
| 79 |
/// \param key The key to store a reference to. |
| 80 |
/// \param information The information to store a reference to. |
| 81 |
/// \param key_equal The function to compare keys with. |
| 82 |
LastAccessed(const Key& key, |
| 83 |
const InformationType& information, |
| 84 |
const KeyEqual& key_equal = KeyEqual()) |
| 85 |
: _key(const_cast<Key*>(&key)) |
| 86 |
, _information(const_cast<InformationType*>(&information)) |
| 87 |
, _is_valid(true) |
| 88 |
, _key_equal(key_equal) { |
| 89 |
} |
| 90 |
|
| 91 |
/// Constructor. |
| 92 |
/// |
| 93 |
/// \param iterator An iterator pointing to a key and information to use for |
| 94 |
/// constructing the instance. |
| 95 |
/// \param key_equal The function to compare keys with. |
| 96 |
template <typename Iterator> |
| 97 |
explicit LastAccessed(Iterator iterator, |
| 98 |
const KeyEqual& key_equal = KeyEqual()) |
| 99 |
: LastAccessed(iterator->first, iterator->second, key_equal) { |
| 100 |
} |
| 101 |
|
| 102 |
/// Copy assignment operator for iterators. |
| 103 |
/// |
| 104 |
/// \param iterator An iterator pointing to a key and value to use for the |
| 105 |
/// `LastAccessed` instance. |
| 106 |
/// \return The resulting `LastAccessed` instance. |
| 107 |
template <typename Iterator> |
| 108 |
LastAccessed& operator=(Iterator iterator) { |
| 109 |
_key = const_cast<Key*>(&(iterator->first)); |
| 110 |
_information = const_cast<InformationType*>(&(iterator->second)); |
| 111 |
_is_valid = true; |
| 112 |
|
| 113 |
return *this; |
| 114 |
} |
| 115 |
|
| 116 |
/// Compares a `LastAccessed` object for equality with a key. |
| 117 |
/// |
| 118 |
/// \param last_accessed The `LastAccessed` instance to compare. |
| 119 |
/// \param key The key instance to compare. |
| 120 |
/// \returns True if the key of the `LastAccessed` object's key equals the |
| 121 |
/// given key, else false. |
| 122 |
friend bool |
| 123 |
operator==(const LastAccessed& last_accessed, const Key& key) noexcept { |
| 124 |
if (!last_accessed._is_valid) return false; |
| 125 |
return last_accessed._key_equal(key, last_accessed.key()); |
| 126 |
} |
| 127 |
|
| 128 |
/// \copydoc operator==(const LastAccessed&,const Key&) |
| 129 |
friend bool |
| 130 |
operator==(const Key& key, const LastAccessed& last_accessed) noexcept { |
| 131 |
return last_accessed == key; |
| 132 |
} |
| 133 |
|
| 134 |
/// Compares a `LastAccessed` object for equality with an iterator. |
| 135 |
/// |
| 136 |
/// \param last_accessed The `LastAccessed` instance to compare. |
| 137 |
/// \param iterator The iterator to compare with. |
| 138 |
/// \returns True if the `LastAccessed` object's key equals that of the |
| 139 |
/// iterator, else false. |
| 140 |
template <typename Iterator, typename = enable_if_iterator<Iterator>> |
| 141 |
friend bool |
| 142 |
operator==(const LastAccessed& last_accessed, Iterator iterator) noexcept { |
| 143 |
/// Fast comparisons to an iterator (not relying on implicit conversion) |
| 144 |
return last_accessed == iterator->first; |
| 145 |
} |
| 146 |
|
| 147 |
/// \copydoc operator==(const LastAccessed&,Iterator) |
| 148 |
template <typename Iterator, typename = enable_if_iterator<Iterator>> |
| 149 |
friend bool |
| 150 |
operator==(Iterator iterator, const LastAccessed& last_accessed) noexcept { |
| 151 |
return last_accessed == iterator; |
| 152 |
} |
| 153 |
|
| 154 |
/// Compares a `LastAccessed` object for inequality with something. |
| 155 |
/// |
| 156 |
/// \param last_accessed The `LastAccessed` instance to compare. |
| 157 |
/// \param other Something else to compare to. |
| 158 |
/// \returns True if the key of the `LastAccessed` object's key does not equal |
| 159 |
/// the given other object's key, else false. |
| 160 |
template <typename T> |
| 161 |
friend bool |
| 162 |
operator!=(const LastAccessed& last_accessed, const T& other) noexcept { |
| 163 |
return !(last_accessed == other); |
| 164 |
} |
| 165 |
|
| 166 |
/// \copydoc operator!=(const LastAccessed&,const T&) |
| 167 |
template <typename T> |
| 168 |
friend bool |
| 169 |
operator!=(const T& other, const LastAccessed& last_accessed) noexcept { |
| 170 |
return !(other == last_accessed); |
| 171 |
} |
| 172 |
|
| 173 |
/// \returns The last accessed key. |
| 174 |
Key& key() noexcept { |
| 175 |
assert(is_valid()); |
| 176 |
return *_key; |
| 177 |
} |
| 178 |
|
| 179 |
/// \returns The last accessed key. |
| 180 |
const Key& key() const noexcept { |
| 181 |
assert(is_valid()); |
| 182 |
return *_key; |
| 183 |
} |
| 184 |
|
| 185 |
/// \returns The last accessed information. |
| 186 |
InformationType& information() noexcept { |
| 187 |
assert(is_valid()); |
| 188 |
return *_information; |
| 189 |
} |
| 190 |
|
| 191 |
/// \returns The last accessed information. |
| 192 |
const InformationType& information() const noexcept { |
| 193 |
assert(is_valid()); |
| 194 |
return *_information; |
| 195 |
} |
| 196 |
|
| 197 |
/// \returns The last accessed information. |
| 198 |
auto& iterator() noexcept { |
| 199 |
assert(is_valid()); |
| 200 |
return _information->order; |
| 201 |
} |
| 202 |
|
| 203 |
/// \returns The last accessed value. |
| 204 |
auto& value() noexcept { |
| 205 |
assert(is_valid()); |
| 206 |
return _information->value; |
| 207 |
} |
| 208 |
|
| 209 |
/// \returns The last accessed value. |
| 210 |
const auto& value() const noexcept { |
| 211 |
assert(is_valid()); |
| 212 |
return _information->value; |
| 213 |
} |
| 214 |
|
| 215 |
/// \returns True if the key and information of the instance may be accessed, |
| 216 |
/// else false. |
| 217 |
bool is_valid() const noexcept { |
| 218 |
return _is_valid; |
| 219 |
} |
| 220 |
|
| 221 |
/// \copydoc is_valid() |
| 222 |
explicit operator bool() const noexcept { |
| 223 |
return is_valid(); |
| 224 |
} |
| 225 |
|
| 226 |
/// Invalidates the instance. |
| 227 |
void invalidate() noexcept { |
| 228 |
_is_valid = false; |
| 229 |
_key = nullptr; |
| 230 |
_information = nullptr; |
| 231 |
} |
| 232 |
|
| 233 |
/// \returns The key comparison function used. |
| 234 |
const KeyEqual& key_equal() const noexcept { |
| 235 |
return _key_equal; |
| 236 |
} |
| 237 |
|
| 238 |
private: |
| 239 |
/// A pointer to the key that was last accessed (if any). |
| 240 |
Key* _key; |
| 241 |
|
| 242 |
/// A pointer to the information that was last accessed (if any). |
| 243 |
InformationType* _information; |
| 244 |
|
| 245 |
/// True if the key and information pointers are valid, else false. |
| 246 |
bool _is_valid; |
| 247 |
|
| 248 |
/// The function used to compare keys. |
| 249 |
KeyEqual _key_equal; |
| 250 |
}; |
| 251 |
} // namespace Internal |
| 252 |
} // namespace LRU |
| 253 |
|
| 254 |
#endif // LRU_INTERNAL_LAST_ACCESSED_HPP |