Debian Codegraph @

src/3rdParty/lru-cache/include/lru/internal/base-unordered-iterator.hpp

raw
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_UNORDERED_ITERATOR_HPP
23 #define BASE_UNORDERED_ITERATOR_HPP
24
25 #include <algorithm>
26 #include <iterator>
27 #include <type_traits>
28
29 #include <lru/entry.hpp>
30 #include <lru/internal/base-iterator.hpp>
31 #include <lru/internal/definitions.hpp>
32 #include <lru/internal/optional.hpp>
33 #include <lru/iterator-tags.hpp>
34
35
36 namespace LRU {
37
38 // Forward declaration.
39 template <typename, typename, typename, typename, typename>
40 class TimedCache;
41
42 namespace Internal {
43 template <typename Cache, typename UnderlyingIterator>
44 using BaseForBaseUnorderedIterator =
45 BaseIterator<std::forward_iterator_tag,
46 decltype(UnderlyingIterator()->first),
47 decltype(UnderlyingIterator()->second.value),
48 Cache,
49 UnderlyingIterator>;
50
51 /// The base class for all const and non-const unordered iterators.
52 ///
53 /// An unordered iterator is a wrapper around an `unordered_map` iterator with
54 /// ForwardIterator category. As such, it is (nearly) as fast to access the pair
55 /// as through the unordered iterator as through the map iterator directly.
56 /// However, the order of keys is unspecified. For this reason, unordered
57 /// iterators have the special property that they may be used to construct
58 /// ordered iterators, after which the order of insertion is respected.
59 ///
60 /// \tparam Cache The type of the cache instances of the iterator point into.
61 /// \tparam UnderlyingIterator The underlying iterator class used to implement
62 /// the iterator.
63 template <typename Cache, typename UnderlyingIterator>
64 class BaseUnorderedIterator
65 : public BaseForBaseUnorderedIterator<Cache, UnderlyingIterator> {
66 protected:
67 using super = BaseForBaseUnorderedIterator<Cache, UnderlyingIterator>;
68 using PRIVATE_BASE_ITERATOR_MEMBERS;
69 // These are the key and value types the BaseIterator extracts
70 using Key = typename super::KeyType;
71 using Value = typename super::ValueType;
72
73 public:
74 using Tag = LRU::Tag::UnorderedIterator;
75 using PUBLIC_BASE_ITERATOR_MEMBERS;
76
77 /// Constructor.
78 BaseUnorderedIterator() noexcept = default;
79
80 /// \copydoc BaseIterator::BaseIterator(Cache,UnderlyingIterator)
81 explicit BaseUnorderedIterator(Cache& cache,
82 const UnderlyingIterator& iterator) noexcept
83 : super(cache, iterator) {
84 }
85
86 /// Generalized copy constructor.
87 ///
88 /// Useful mainly for non-const to const conversion.
89 ///
90 /// \param other The iterator to copy from.
91 template <typename AnyCache, typename AnyUnderlyingIterator>
92 BaseUnorderedIterator(
93 const BaseUnorderedIterator<AnyCache, AnyUnderlyingIterator>&
94 other) noexcept
95 : super(other) {
96 }
97
98 /// Copy constructor.
99 BaseUnorderedIterator(const BaseUnorderedIterator& other) noexcept = default;
100
101 /// Move constructor.
102 BaseUnorderedIterator(BaseUnorderedIterator&& other) noexcept = default;
103
104 /// Copy assignment operator.
105 BaseUnorderedIterator&
106 operator=(const BaseUnorderedIterator& other) noexcept = default;
107
108 /// Move assignment operator.
109 template <typename AnyCache, typename AnyUnderlyingIterator>
110 BaseUnorderedIterator&
111 operator=(BaseUnorderedIterator<AnyCache, AnyUnderlyingIterator>
112 unordered_iterator) noexcept {
113 swap(unordered_iterator);
114 return *this;
115 }
116
117 /// Destructor.
118 virtual ~BaseUnorderedIterator() = default;
119
120 /// Compares this iterator for equality with another unordered iterator.
121 ///
122 /// \param other Another unordered iterator.
123 /// \returns True if both iterators point to the same entry, else false.
124 template <typename AnyCache, typename AnyIterator>
125 bool
126 operator==(const BaseUnorderedIterator<AnyCache, AnyIterator>& other) const
127 noexcept {
128 return this->_iterator == other._iterator;
129 }
130
131 /// Compares this iterator for inequality with another unordered iterator.
132 ///
133 /// \param other Another unordered iterator.
134 /// \returns True if the iterators point to different entries, else false.
135 template <typename AnyCache, typename AnyIterator>
136 bool
137 operator!=(const BaseUnorderedIterator<AnyCache, AnyIterator>& other) const
138 noexcept {
139 return !(*this == other);
140 }
141
142 /// Increments the iterator to the next entry.
143 ///
144 /// If the iterator already pointed to the end any number of increments
145 /// before, behavior is undefined.
146 ///
147 /// \returns The resulting iterator.
148 BaseUnorderedIterator& operator++() {
149 ++_iterator;
150 _entry.reset();
151 return *this;
152 }
153
154 /// Increments the iterator and returns a copy of the previous one.
155 ///
156 /// If the iterator already pointed to the end any number of increments
157 /// before, behavior is undefined.
158 ///
159 /// \returns A copy of the previous iterator.
160 BaseUnorderedIterator operator++(int) {
161 auto previous = *this;
162 ++*this;
163 return previous;
164 }
165
166 /// \copydoc BaseIterator::operator*
167 /// \details If the iterator is invalid, behavior is undefined. No exception
168 /// handling is performed.
169 Entry& operator*() noexcept override {
170 if (!_entry.has_value()) {
171 _entry.emplace(_iterator->first, _iterator->second.value);
172 }
173
174 return *_entry;
175 }
176
177 /// \returns A reference to the entry the iterator points to.
178 /// \throws LRU::Error::InvalidIterator if the iterator is the end iterator.
179 /// \throws LRU::Error::KeyExpired if the key pointed to by the iterator has
180 /// expired.
181 Entry& entry() override {
182 if (!_entry.has_value()) {
183 _entry.emplace(_iterator->first, _iterator->second.value);
184 }
185
186 _cache->throw_if_invalid(*this);
187 return *_entry;
188 }
189
190 /// \returns A reference to the key the iterator points to.
191 /// \throws LRU::Error::InvalidIterator if the iterator is the end iterator.
192 /// \throws LRU::Error::KeyExpired if the key pointed to by the iterator has
193 /// expired.
194 const Key& key() override {
195 return entry().key();
196 }
197
198 /// \returns A reference to the value the iterator points to.
199 /// \throws LRU::Error::InvalidIterator if the iterator is the end iterator.
200 /// \throws LRU::Error::KeyExpired if the key pointed to by the iterator has
201 /// expired.
202 Value& value() override {
203 return entry().value();
204 }
205
206 protected:
207 template <typename, typename, typename>
208 friend class BaseOrderedIterator;
209
210 template <typename, typename, typename, typename, typename>
211 friend class LRU::TimedCache;
212 };
213 } // namespace Internal
214 } // namespace LRU
215
216 #endif // BASE_UNORDERED_ITERATOR_HPP