| 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 BASE_ORDERED_ITERATOR_HPP |
| 23 |
#define BASE_ORDERED_ITERATOR_HPP |
| 24 |
|
| 25 |
#include <algorithm> |
| 26 |
#include <functional> |
| 27 |
#include <iterator> |
| 28 |
#include <type_traits> |
| 29 |
|
| 30 |
#include <lru/entry.hpp> |
| 31 |
#include <lru/error.hpp> |
| 32 |
#include <lru/internal/base-iterator.hpp> |
| 33 |
#include <lru/internal/base-unordered-iterator.hpp> |
| 34 |
#include <lru/internal/definitions.hpp> |
| 35 |
#include <lru/internal/optional.hpp> |
| 36 |
#include <lru/iterator-tags.hpp> |
| 37 |
|
| 38 |
namespace LRU { |
| 39 |
namespace Internal { |
| 40 |
|
| 41 |
template <typename Key, typename Value, typename Cache> |
| 42 |
using BaseForBaseOrderedIterator = |
| 43 |
BaseIterator<std::bidirectional_iterator_tag, |
| 44 |
Key, |
| 45 |
Value, |
| 46 |
Cache, |
| 47 |
typename Queue<Key>::const_iterator>; |
| 48 |
|
| 49 |
/// The base class for all const and non-const ordered iterators. |
| 50 |
/// |
| 51 |
/// Ordered iterators are bidirectional iterators that iterate over the keys of |
| 52 |
/// a cache in the order in which they were inserted into the cache. As they |
| 53 |
/// only iterate over the keys, they must perform hash lookups to retrieve the |
| 54 |
/// value the first time they are dereferenced. This makes them slightly less |
| 55 |
/// efficient than unordered iterators. However, they also have the additional |
| 56 |
/// property that they may be constructed from unordered iterators, and that |
| 57 |
/// they can be decremented. |
| 58 |
/// |
| 59 |
/// \tparam Key The key type over which instances of the iterator iterate. |
| 60 |
/// \tparam Value The value type over which instances of the iterator iterate. |
| 61 |
/// \tparam Cache The type of the cache instances of the iterator point into. |
| 62 |
template <typename Key, typename Value, typename Cache> |
| 63 |
class BaseOrderedIterator |
| 64 |
: public BaseForBaseOrderedIterator<Key, Value, Cache> { |
| 65 |
protected: |
| 66 |
using super = BaseForBaseOrderedIterator<Key, Value, Cache>; |
| 67 |
using PRIVATE_BASE_ITERATOR_MEMBERS; |
| 68 |
using UnderlyingIterator = typename Queue<Key>::const_iterator; |
| 69 |
|
| 70 |
public: |
| 71 |
using Tag = LRU::Tag::OrderedIterator; |
| 72 |
using PUBLIC_BASE_ITERATOR_MEMBERS; |
| 73 |
|
| 74 |
/// Constructor. |
| 75 |
BaseOrderedIterator() noexcept = default; |
| 76 |
|
| 77 |
/// \copydoc BaseIterator::BaseIterator(Cache,UnderlyingIterator) |
| 78 |
BaseOrderedIterator(Cache& cache, UnderlyingIterator iterator) |
| 79 |
: super(cache, iterator) { |
| 80 |
} |
| 81 |
|
| 82 |
/// Generalized copy constructor. |
| 83 |
/// |
| 84 |
/// \param other The ordered iterator to contruct from. |
| 85 |
template <typename AnyKey, typename AnyValue, typename AnyCache> |
| 86 |
BaseOrderedIterator( |
| 87 |
const BaseOrderedIterator<AnyKey, AnyValue, AnyCache>& other) |
| 88 |
: super(other) { |
| 89 |
} |
| 90 |
|
| 91 |
/// Generalized move constructor. |
| 92 |
/// |
| 93 |
/// \param other The ordered iterator to move into this one. |
| 94 |
template <typename AnyKey, typename AnyValue, typename AnyCache> |
| 95 |
BaseOrderedIterator(BaseOrderedIterator<AnyKey, AnyValue, AnyCache>&& other) |
| 96 |
: super(std::move(other)) { |
| 97 |
} |
| 98 |
|
| 99 |
/// Generalized conversion copy constructor. |
| 100 |
/// |
| 101 |
/// \param unordered_iterator The unordered iterator to construct from. |
| 102 |
template < |
| 103 |
typename AnyCache, |
| 104 |
typename UnderlyingIterator, |
| 105 |
typename = std::enable_if_t< |
| 106 |
std::is_same<std::decay_t<AnyCache>, std::decay_t<Cache>>::value>> |
| 107 |
BaseOrderedIterator(const BaseUnorderedIterator<AnyCache, UnderlyingIterator>& |
| 108 |
unordered_iterator) { |
| 109 |
// Atomicity |
| 110 |
_throw_if_at_invalid(unordered_iterator); |
| 111 |
_cache = unordered_iterator._cache; |
| 112 |
_iterator = unordered_iterator._iterator->second.order; |
| 113 |
} |
| 114 |
|
| 115 |
/// Generalized conversion move constructor. |
| 116 |
/// |
| 117 |
/// \param unordered_iterator The unordered iterator to move-construct from. |
| 118 |
template < |
| 119 |
typename AnyCache, |
| 120 |
typename UnderlyingIterator, |
| 121 |
typename = std::enable_if_t< |
| 122 |
std::is_same<std::decay_t<AnyCache>, std::decay_t<Cache>>::value>> |
| 123 |
BaseOrderedIterator(BaseUnorderedIterator<AnyCache, UnderlyingIterator>&& |
| 124 |
unordered_iterator) { |
| 125 |
// Atomicity |
| 126 |
_throw_if_at_invalid(unordered_iterator); |
| 127 |
_cache = std::move(unordered_iterator._cache); |
| 128 |
_entry = std::move(unordered_iterator._entry); |
| 129 |
_iterator = std::move(unordered_iterator._iterator->second.order); |
| 130 |
} |
| 131 |
|
| 132 |
/// Copy constructor. |
| 133 |
BaseOrderedIterator(const BaseOrderedIterator& other) = default; |
| 134 |
|
| 135 |
/// Move constructor. |
| 136 |
BaseOrderedIterator(BaseOrderedIterator&& other) = default; |
| 137 |
|
| 138 |
/// Copy assignment operator. |
| 139 |
BaseOrderedIterator& operator=(const BaseOrderedIterator& other) = default; |
| 140 |
|
| 141 |
/// Move assignment operator. |
| 142 |
BaseOrderedIterator& operator=(BaseOrderedIterator&& other) = default; |
| 143 |
|
| 144 |
/// Destructor. |
| 145 |
virtual ~BaseOrderedIterator() = default; |
| 146 |
|
| 147 |
/// Checks for equality between this iterator and another ordered iterator. |
| 148 |
/// |
| 149 |
/// \param other The other ordered iterator. |
| 150 |
/// \returns True if both iterators point to the same entry, else false. |
| 151 |
bool operator==(const BaseOrderedIterator& other) const noexcept { |
| 152 |
return this->_iterator == other._iterator; |
| 153 |
} |
| 154 |
|
| 155 |
/// Checks for inequality between this iterator another ordered iterator. |
| 156 |
/// |
| 157 |
/// \param other The other ordered iterator. |
| 158 |
/// \returns True if the iterators point to different entries, else false. |
| 159 |
bool operator!=(const BaseOrderedIterator& other) const noexcept { |
| 160 |
return !(*this == other); |
| 161 |
} |
| 162 |
|
| 163 |
/// Checks for inequality between this iterator and another unordered |
| 164 |
/// iterator. |
| 165 |
/// |
| 166 |
/// \param other The other unordered iterator. |
| 167 |
/// \returns True if both iterators point to the end of the same cache, else |
| 168 |
/// the result of comparing with the unordered iterator, converted to an |
| 169 |
/// ordered iterator. |
| 170 |
template <typename AnyCache, typename AnyUnderlyingIterator> |
| 171 |
bool operator==( |
| 172 |
const BaseUnorderedIterator<AnyCache, AnyUnderlyingIterator>& other) const |
| 173 |
noexcept { |
| 174 |
if (this->_cache != other._cache) return false; |
| 175 |
|
| 176 |
// The past-the-end iterators of the same cache should compare equal. |
| 177 |
// This is an exceptional guarantee we make. This is also the reason |
| 178 |
// why we can't rely on the conversion from unordered to ordered iterators |
| 179 |
// because construction of an ordered iterator from the past-the-end |
| 180 |
// unordered iterator will fail (with an InvalidIteratorConversion error) |
| 181 |
if (other == other._cache->unordered_end()) { |
| 182 |
return *this == this->_cache->ordered_end(); |
| 183 |
} |
| 184 |
|
| 185 |
// Will call the other overload |
| 186 |
return *this == static_cast<BaseOrderedIterator>(other); |
| 187 |
} |
| 188 |
|
| 189 |
/// Checks for equality between an unordered iterator and an ordered iterator. |
| 190 |
/// |
| 191 |
/// \param first The unordered iterator. |
| 192 |
/// \param second The ordered iterator. |
| 193 |
/// \returns True if both iterators point to the end of the same cache, else |
| 194 |
/// the result of comparing with the unordered iterator, converted to an |
| 195 |
/// ordered iterator. |
| 196 |
template <typename AnyCache, typename AnyUnderlyingIterator> |
| 197 |
friend bool operator==( |
| 198 |
const BaseUnorderedIterator<AnyCache, AnyUnderlyingIterator>& first, |
| 199 |
const BaseOrderedIterator& second) noexcept { |
| 200 |
return second == first; |
| 201 |
} |
| 202 |
|
| 203 |
/// Checks for inequality between an unordered |
| 204 |
/// iterator and an ordered iterator. |
| 205 |
/// |
| 206 |
/// \param first The ordered iterator. |
| 207 |
/// \param second The unordered iterator. |
| 208 |
/// \returns True if the iterators point to different entries, else false. |
| 209 |
template <typename AnyCache, typename AnyUnderlyingIterator> |
| 210 |
friend bool |
| 211 |
operator!=(const BaseOrderedIterator& first, |
| 212 |
const BaseUnorderedIterator<AnyCache, AnyUnderlyingIterator>& |
| 213 |
second) noexcept { |
| 214 |
return !(first == second); |
| 215 |
} |
| 216 |
|
| 217 |
/// Checks for inequality between an unordered |
| 218 |
/// iterator and an ordered iterator. |
| 219 |
/// |
| 220 |
/// \param first The unordered iterator. |
| 221 |
/// \param second The ordered iterator. |
| 222 |
/// \returns True if the iterators point to different entries, else false. |
| 223 |
template <typename AnyCache, typename AnyUnderlyingIterator> |
| 224 |
friend bool operator!=( |
| 225 |
const BaseUnorderedIterator<AnyCache, AnyUnderlyingIterator>& first, |
| 226 |
const BaseOrderedIterator& second) noexcept { |
| 227 |
return second != first; |
| 228 |
} |
| 229 |
|
| 230 |
/// Increments the iterator to the next entry. |
| 231 |
/// |
| 232 |
/// If the iterator already pointed to the end any number of increments |
| 233 |
/// before, behavior is undefined. |
| 234 |
/// |
| 235 |
/// \returns The resulting iterator. |
| 236 |
BaseOrderedIterator& operator++() { |
| 237 |
++_iterator; |
| 238 |
_entry.reset(); |
| 239 |
return *this; |
| 240 |
} |
| 241 |
|
| 242 |
/// Increments the iterator and returns a copy of the previous one. |
| 243 |
/// |
| 244 |
/// If the iterator already pointed to the end any number of increments |
| 245 |
/// before, behavior is undefined. |
| 246 |
/// |
| 247 |
/// \returns A copy of the previous iterator. |
| 248 |
BaseOrderedIterator operator++(int) { |
| 249 |
auto previous = *this; |
| 250 |
++*this; |
| 251 |
return previous; |
| 252 |
} |
| 253 |
|
| 254 |
/// Decrements the iterator to the previous entry. |
| 255 |
/// |
| 256 |
/// \returns The resulting iterator. |
| 257 |
BaseOrderedIterator& operator--() { |
| 258 |
--_iterator; |
| 259 |
_entry.reset(); |
| 260 |
return *this; |
| 261 |
} |
| 262 |
|
| 263 |
/// Decrements the iterator and returns a copy of the previous entry. |
| 264 |
/// |
| 265 |
/// \returns The previous iterator. |
| 266 |
BaseOrderedIterator operator--(int) { |
| 267 |
auto previous = *this; |
| 268 |
--*this; |
| 269 |
return previous; |
| 270 |
} |
| 271 |
|
| 272 |
Entry& operator*() noexcept override { |
| 273 |
return _maybe_lookup(); |
| 274 |
} |
| 275 |
|
| 276 |
/// \returns A reference to the entry the iterator points to. |
| 277 |
/// \details If the iterator is invalid, behavior is undefined. |
| 278 |
Entry& entry() override { |
| 279 |
_cache->throw_if_invalid(*this); |
| 280 |
return _maybe_lookup(); |
| 281 |
} |
| 282 |
|
| 283 |
/// \returns A reference to the value the iterator points to. |
| 284 |
/// \details If the iterator is invalid, behavior is undefined. |
| 285 |
Value& value() override { |
| 286 |
return entry().value(); |
| 287 |
} |
| 288 |
|
| 289 |
/// \returns A reference to the key the iterator points to. |
| 290 |
/// \details If the iterator is invalid, behavior is undefined. |
| 291 |
const Key& key() override { |
| 292 |
// No lookup required |
| 293 |
_cache->throw_if_invalid(*this); |
| 294 |
return *_iterator; |
| 295 |
} |
| 296 |
|
| 297 |
protected: |
| 298 |
template <typename, typename, typename> |
| 299 |
friend class BaseOrderedIterator; |
| 300 |
|
| 301 |
/// Looks up the entry for a key if this was not done already. |
| 302 |
/// |
| 303 |
/// \returns The entry, which was possibly newly looked up. |
| 304 |
Entry& _maybe_lookup() { |
| 305 |
if (!_entry.has_value()) { |
| 306 |
_lookup(); |
| 307 |
} |
| 308 |
|
| 309 |
return *_entry; |
| 310 |
} |
| 311 |
|
| 312 |
/// Looks up the entry for a key and sets the internal entry member. |
| 313 |
void _lookup() { |
| 314 |
auto iterator = _cache->_map.find(*_iterator); |
| 315 |
_entry.emplace(iterator->first, iterator->second.value); |
| 316 |
} |
| 317 |
|
| 318 |
private: |
| 319 |
/// Throws an exception if the given unordered iterator is invalid. |
| 320 |
/// |
| 321 |
/// \param unordered_iterator The iterator to check. |
| 322 |
/// \throws LRU::Error::InvalidIteratorConversion if the iterator is invalid. |
| 323 |
template <typename UnorderedIterator> |
| 324 |
void _throw_if_at_invalid(const UnorderedIterator& unordered_iterator) { |
| 325 |
// For atomicity of the copy assignment, we assign the cache pointer only |
| 326 |
// after this check in the copy/move constructor and use the iterator's |
| 327 |
// cache. If an exception is thrown, the state of the ordered iterator is |
| 328 |
// unchanged compared to before the assignment. |
| 329 |
if (unordered_iterator == unordered_iterator._cache->unordered_end()) { |
| 330 |
throw LRU::Error::InvalidIteratorConversion(); |
| 331 |
} |
| 332 |
} |
| 333 |
}; |
| 334 |
|
| 335 |
} // namespace Internal |
| 336 |
} // namespace LRU |
| 337 |
|
| 338 |
#endif // BASE_ORDERED_ITERATOR_HPP |