|
8 | 8 | #include <iostream> |
9 | 9 | #include <set> |
10 | 10 | #include <vector> |
| 11 | +#include <ranges> |
11 | 12 |
|
12 | 13 | #include <boost/container_hash/hash.hpp> |
13 | 14 |
|
@@ -122,43 +123,90 @@ public: |
122 | 123 | return &cs[1]; |
123 | 124 | } |
124 | 125 |
|
125 | | - struct Iterator |
| 126 | + class Iterator |
126 | 127 | { |
| 128 | + /** |
| 129 | + * Helper class with overloaded operator-> for "drill-down" behavior. |
| 130 | + * This was a "temporary" string_view doesn't have to be stored anywhere. |
| 131 | + */ |
| 132 | + class PointerProxy |
| 133 | + { |
| 134 | + std::string_view segment; |
| 135 | + |
| 136 | + public: |
| 137 | + PointerProxy(std::string_view segment_) |
| 138 | + : segment(segment_) |
| 139 | + { |
| 140 | + } |
| 141 | + |
| 142 | + const std::string_view * operator->() const |
| 143 | + { |
| 144 | + return &segment; |
| 145 | + } |
| 146 | + }; |
| 147 | + |
| 148 | + public: |
| 149 | + using value_type = std::string_view; |
| 150 | + using reference_type = const std::string_view; |
| 151 | + using pointer_type = PointerProxy; |
| 152 | + using difference_type = std::ptrdiff_t; |
| 153 | + using iterator_category = std::forward_iterator_tag; |
| 154 | + |
127 | 155 | std::string_view remaining; |
128 | 156 | size_t slash; |
129 | 157 |
|
| 158 | + /** |
| 159 | + * Dummy default constructor required for forward iterators. Doesn't return |
| 160 | + * a usable iterator. |
| 161 | + */ |
| 162 | + Iterator() |
| 163 | + : remaining() |
| 164 | + , slash(0) |
| 165 | + { |
| 166 | + } |
| 167 | + |
130 | 168 | Iterator(std::string_view remaining) |
131 | 169 | : remaining(remaining) |
132 | 170 | , slash(remaining.find('/')) |
133 | 171 | { |
134 | 172 | } |
135 | 173 |
|
136 | | - bool operator!=(const Iterator & x) const |
| 174 | + bool operator==(const Iterator & x) const |
137 | 175 | { |
138 | | - return remaining.data() != x.remaining.data(); |
| 176 | + return remaining.data() == x.remaining.data(); |
139 | 177 | } |
140 | 178 |
|
141 | | - bool operator==(const Iterator & x) const |
| 179 | + reference_type operator*() const |
142 | 180 | { |
143 | | - return !(*this != x); |
| 181 | + return remaining.substr(0, slash); |
144 | 182 | } |
145 | 183 |
|
146 | | - const std::string_view operator*() const |
| 184 | + pointer_type operator->() const |
147 | 185 | { |
148 | | - return remaining.substr(0, slash); |
| 186 | + return PointerProxy(**this); |
149 | 187 | } |
150 | 188 |
|
151 | | - void operator++() |
| 189 | + Iterator & operator++() |
152 | 190 | { |
153 | 191 | if (slash == remaining.npos) |
154 | 192 | remaining = remaining.substr(remaining.size()); |
155 | 193 | else { |
156 | 194 | remaining = remaining.substr(slash + 1); |
157 | 195 | slash = remaining.find('/'); |
158 | 196 | } |
| 197 | + return *this; |
| 198 | + } |
| 199 | + |
| 200 | + Iterator operator++(int) |
| 201 | + { |
| 202 | + auto tmp = *this; |
| 203 | + ++*this; |
| 204 | + return tmp; |
159 | 205 | } |
160 | 206 | }; |
161 | 207 |
|
| 208 | + static_assert(std::forward_iterator<Iterator>); |
| 209 | + |
162 | 210 | Iterator begin() const |
163 | 211 | { |
164 | 212 | return Iterator(rel()); |
@@ -265,6 +313,8 @@ public: |
265 | 313 | friend std::size_t hash_value(const CanonPath &); |
266 | 314 | }; |
267 | 315 |
|
| 316 | +static_assert(std::ranges::forward_range<CanonPath>); |
| 317 | + |
268 | 318 | std::ostream & operator<<(std::ostream & stream, const CanonPath & path); |
269 | 319 |
|
270 | 320 | inline std::size_t hash_value(const CanonPath & path) |
|
0 commit comments