| 6 | #define BASE_CONTAINERS_FLAT_TREE_H_ |
| 7 |
| 8 | #include <algorithm> |
| 9 | #include <iterator> |
| 10 | #include <type_traits> |
| 11 | #include <utility> |
| 12 | #include <vector> |
| 13 |
| 14 | #include "base/stl_util.h" |
| 15 | #include "base/template_util.h" |
| 16 |
| 17 | namespace base { |
| 18 |
| 19 | namespace internal { |
| 20 |
| 21 | // This is a convenience method returning true if Iterator is at least a |
| 22 | // ForwardIterator and thus supports multiple passes over a range. |
| 23 | template <class Iterator> |
| 24 | constexpr bool is_multipass() { |
| 25 | return std::is_base_of< |
| 26 | std::forward_iterator_tag, |
| 27 | typename std::iterator_traits<Iterator>::iterator_category>::value; |
| 28 | } |
| 29 |
| 30 | // Uses SFINAE to detect whether type has is_transparent member. |
| 31 | template <typename T, typename = void> |
| 32 | struct IsTransparentCompare : std::false_type {}; |
| 33 | template <typename T> |
| 34 | struct IsTransparentCompare<T, void_t<typename T::is_transparent>> |
| 35 | : std::true_type {}; |
| 36 |
| 37 | // Implementation ------------------------------------------------------------- |
| 38 |
| 39 | // Implementation of a sorted vector for backing flat_set and flat_map. Do not |
| 40 | // use directly. |
| 41 | // |
| 42 | // The use of "value" in this is like std::map uses, meaning it's the thing |
| 43 | // contained (in the case of map it's a <Kay, Mapped> pair). The Key is how |
| 44 | // things are looked up. In the case of a set, Key == Value. In the case of |
| 45 | // a map, the Key is a component of a Value. |
| 46 | // |
| 47 | // The helper class GetKeyFromValue provides the means to extract a key from a |
| 48 | // value for comparison purposes. It should implement: |
| 49 | // const Key& operator()(const Value&). |
| 50 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 51 | class flat_tree { |
| 52 | protected: |
| 53 | using underlying_type = std::vector<Value>; |
| 54 |
| 55 | public: |
| 56 | // -------------------------------------------------------------------------- |
| 57 | // Types. |
| 58 | // |
| 59 | using key_type = Key; |
| 60 | using key_compare = KeyCompare; |
| 61 | using value_type = Value; |
| 62 |
| 63 | // Wraps the templated key comparison to compare values. |
| 64 | class value_compare : public key_compare { |
| 65 | public: |
| 66 | value_compare() = default; |
| 67 |
| 68 | template <class Cmp> |
| 69 | explicit value_compare(Cmp&& compare_arg) |
| 70 | : KeyCompare(std::forward<Cmp>(compare_arg)) {} |
| 71 |
| 72 | bool operator()(const value_type& left, const value_type& right) const { |
| 73 | GetKeyFromValue extractor; |
| 74 | return key_compare::operator()(extractor(left), extractor(right)); |
| 75 | } |
| 76 | }; |
| 77 |
| 78 | using pointer = typename underlying_type::pointer; |
| 79 | using const_pointer = typename underlying_type::const_pointer; |
| 80 | using reference = typename underlying_type::reference; |
| 81 | using const_reference = typename underlying_type::const_reference; |
| 82 | using size_type = typename underlying_type::size_type; |
| 83 | using difference_type = typename underlying_type::difference_type; |
| 84 | using iterator = typename underlying_type::iterator; |
| 85 | using const_iterator = typename underlying_type::const_iterator; |
| 86 | using reverse_iterator = typename underlying_type::reverse_iterator; |
| 87 | using const_reverse_iterator = |
| 88 | typename underlying_type::const_reverse_iterator; |
| 89 |
| 90 | // -------------------------------------------------------------------------- |
| 91 | // Lifetime. |
| 92 | // |
| 93 | // Constructors that take range guarantee O(N * log^2(N)) + O(N) complexity |
| 94 | // and take O(N * log(N)) + O(N) if extra memory is available (N is a range |
| 95 | // length). |
| 96 | // |
| 97 | // Assume that move constructors invalidate iterators and references. |
| 98 | // |
| 99 | // The constructors that take ranges, lists, and vectors do not require that |
| 100 | // the input be sorted. |
| 101 |
| 102 | flat_tree(); |
| 103 | explicit flat_tree(const key_compare& comp); |
| 104 |
| 105 | template <class InputIterator> |
| 106 | flat_tree(InputIterator first, |
| 107 | InputIterator last, |
| 108 | const key_compare& comp = key_compare()); |
| 109 |
| 110 | flat_tree(const flat_tree&); |
| 111 | flat_tree(flat_tree&&) noexcept = default; |
| 112 |
| 113 | flat_tree(const underlying_type& items, |
| 114 | const key_compare& comp = key_compare()); |
| 115 | flat_tree(underlying_type&& items, const key_compare& comp = key_compare()); |
| 116 |
| 117 | flat_tree(std::initializer_list<value_type> ilist, |
| 118 | const key_compare& comp = key_compare()); |
| 119 |
| 120 | ~flat_tree(); |
| 121 |
| 122 | // -------------------------------------------------------------------------- |
| 123 | // Assignments. |
| 124 | // |
| 125 | // Assume that move assignment invalidates iterators and references. |
| 126 |
| 127 | flat_tree& operator=(const flat_tree&); |
| 128 | flat_tree& operator=(flat_tree&&); |
| 129 | // Takes the first if there are duplicates in the initializer list. |
| 130 | flat_tree& operator=(std::initializer_list<value_type> ilist); |
| 131 |
| 132 | // -------------------------------------------------------------------------- |
| 133 | // Memory management. |
| 134 | // |
| 135 | // Beware that shrink_to_fit() simply forwards the request to the |
| 136 | // underlying_type and its implementation is free to optimize otherwise and |
| 137 | // leave capacity() to be greater that its size. |
| 138 | // |
| 139 | // reserve() and shrink_to_fit() invalidate iterators and references. |
| 140 |
| 141 | void reserve(size_type new_capacity); |
| 142 | size_type capacity() const; |
| 143 | void shrink_to_fit(); |
| 144 |
| 145 | // -------------------------------------------------------------------------- |
| 146 | // Size management. |
| 147 | // |
| 148 | // clear() leaves the capacity() of the flat_tree unchanged. |
| 149 |
| 150 | void clear(); |
| 151 |
| 152 | size_type size() const; |
| 153 | size_type max_size() const; |
| 154 | bool empty() const; |
| 155 |
| 156 | // -------------------------------------------------------------------------- |
| 157 | // Iterators. |
| 158 |
| 159 | iterator begin(); |
| 160 | const_iterator begin() const; |
| 161 | const_iterator cbegin() const; |
| 162 |
| 163 | iterator end(); |
| 164 | const_iterator end() const; |
| 165 | const_iterator cend() const; |
| 166 |
| 167 | reverse_iterator rbegin(); |
| 168 | const_reverse_iterator rbegin() const; |
| 169 | const_reverse_iterator crbegin() const; |
| 170 |
| 171 | reverse_iterator rend(); |
| 172 | const_reverse_iterator rend() const; |
| 173 | const_reverse_iterator crend() const; |
| 174 |
| 175 | // -------------------------------------------------------------------------- |
| 176 | // Insert operations. |
| 177 | // |
| 178 | // Assume that every operation invalidates iterators and references. |
| 179 | // Insertion of one element can take O(size). Capacity of flat_tree grows in |
| 180 | // an implementation-defined manner. |
| 181 | // |
| 182 | // NOTE: Prefer to build a new flat_tree from a std::vector (or similar) |
| 183 | // instead of calling insert() repeatedly. |
| 184 |
| 185 | std::pair<iterator, bool> insert(const value_type& val); |
| 186 | std::pair<iterator, bool> insert(value_type&& val); |
| 187 |
| 188 | iterator insert(const_iterator position_hint, const value_type& x); |
| 189 | iterator insert(const_iterator position_hint, value_type&& x); |
| 190 |
| 191 | // This method inserts the values from the range [first, last) into the |
| 192 | // current tree. |
| 193 | template <class InputIterator> |
| 194 | void insert(InputIterator first, InputIterator last); |
| 195 |
| 196 | template <class... Args> |
| 197 | std::pair<iterator, bool> emplace(Args&&... args); |
| 198 |
| 199 | template <class... Args> |
| 200 | iterator emplace_hint(const_iterator position_hint, Args&&... args); |
| 201 |
| 202 | // -------------------------------------------------------------------------- |
| 203 | // Underlying type operations. |
| 204 | // |
| 205 | // Assume that either operation invalidates iterators and references. |
| 206 |
| 207 | // Extracts the underlying_type and returns it to the caller. Ensures that |
| 208 | // `this` is `empty()` afterwards. |
| 209 | underlying_type extract() &&; |
| 210 |
| 211 | // Replaces the underlying_type with `body`. Expects that `body` is sorted |
| 212 | // and has no repeated elements with regard to value_comp(). |
| 213 | void replace(underlying_type&& body); |
| 214 |
| 215 | // -------------------------------------------------------------------------- |
| 216 | // Erase operations. |
| 217 | // |
| 218 | // Assume that every operation invalidates iterators and references. |
| 219 | // |
| 220 | // erase(position), erase(first, last) can take O(size). |
| 221 | // erase(key) may take O(size) + O(log(size)). |
| 222 | // |
| 223 | // Prefer base::EraseIf() or some other variation on erase(remove(), end()) |
| 224 | // idiom when deleting multiple non-consecutive elements. |
| 225 |
| 226 | iterator erase(iterator position); |
| 227 | iterator erase(const_iterator position); |
| 228 | iterator erase(const_iterator first, const_iterator last); |
| 229 | template <typename K> |
| 230 | size_type erase(const K& key); |
| 231 |
| 232 | // -------------------------------------------------------------------------- |
| 233 | // Comparators. |
| 234 |
| 235 | key_compare key_comp() const; |
| 236 | value_compare value_comp() const; |
| 237 |
| 238 | // -------------------------------------------------------------------------- |
| 239 | // Search operations. |
| 240 | // |
| 241 | // Search operations have O(log(size)) complexity. |
| 242 |
| 243 | template <typename K> |
| 244 | size_type count(const K& key) const; |
| 245 |
| 246 | template <typename K> |
| 247 | iterator find(const K& key); |
| 248 |
| 249 | template <typename K> |
| 250 | const_iterator find(const K& key) const; |
| 251 |
| 252 | template <typename K> |
| 253 | bool contains(const K& key) const; |
| 254 |
| 255 | template <typename K> |
| 256 | std::pair<iterator, iterator> equal_range(const K& key); |
| 257 |
| 258 | template <typename K> |
| 259 | std::pair<const_iterator, const_iterator> equal_range(const K& key) const; |
| 260 |
| 261 | template <typename K> |
| 262 | iterator lower_bound(const K& key); |
| 263 |
| 264 | template <typename K> |
| 265 | const_iterator lower_bound(const K& key) const; |
| 266 |
| 267 | template <typename K> |
| 268 | iterator upper_bound(const K& key); |
| 269 |
| 270 | template <typename K> |
| 271 | const_iterator upper_bound(const K& key) const; |
| 272 |
| 273 | // -------------------------------------------------------------------------- |
| 274 | // General operations. |
| 275 | // |
| 276 | // Assume that swap invalidates iterators and references. |
| 277 | // |
| 278 | // Implementation note: currently we use operator==() and operator<() on |
| 279 | // std::vector, because they have the same contract we need, so we use them |
| 280 | // directly for brevity and in case it is more optimal than calling equal() |
| 281 | // and lexicograhpical_compare(). If the underlying container type is changed, |
| 282 | // this code may need to be modified. |
| 283 |
| 284 | void swap(flat_tree& other) noexcept; |
| 285 |
| 286 | friend bool operator==(const flat_tree& lhs, const flat_tree& rhs) { |
| 287 | return lhs.impl_.body_ == rhs.impl_.body_; |
| 288 | } |
| 289 |
| 290 | friend bool operator!=(const flat_tree& lhs, const flat_tree& rhs) { |
| 291 | return !(lhs == rhs); |
| 292 | } |
| 293 |
| 294 | friend bool operator<(const flat_tree& lhs, const flat_tree& rhs) { |
| 295 | return lhs.impl_.body_ < rhs.impl_.body_; |
| 296 | } |
| 297 |
| 298 | friend bool operator>(const flat_tree& lhs, const flat_tree& rhs) { |
| 299 | return rhs < lhs; |
| 300 | } |
| 301 |
| 302 | friend bool operator>=(const flat_tree& lhs, const flat_tree& rhs) { |
| 303 | return !(lhs < rhs); |
| 304 | } |
| 305 |
| 306 | friend bool operator<=(const flat_tree& lhs, const flat_tree& rhs) { |
| 307 | return !(lhs > rhs); |
| 308 | } |
| 309 |
| 310 | friend void swap(flat_tree& lhs, flat_tree& rhs) noexcept { lhs.swap(rhs); } |
| 311 |
| 312 | protected: |
| 313 | // Emplaces a new item into the tree that is known not to be in it. This |
| 314 | // is for implementing map operator[]. |
| 315 | template <class... Args> |
| 316 | iterator unsafe_emplace(const_iterator position, Args&&... args); |
| 317 |
| 318 | // Attempts to emplace a new element with key |key|. Only if |key| is not yet |
| 319 | // present, construct value_type from |args| and insert it. Returns an |
| 320 | // iterator to the element with key |key| and a bool indicating whether an |
| 321 | // insertion happened. |
| 322 | template <class K, class... Args> |
| 323 | std::pair<iterator, bool> emplace_key_args(const K& key, Args&&... args); |
| 324 |
| 325 | // Similar to |emplace_key_args|, but checks |hint| first as a possible |
| 326 | // insertion position. |
| 327 | template <class K, class... Args> |
| 328 | std::pair<iterator, bool> emplace_hint_key_args(const_iterator hint, |
| 329 | const K& key, |
| 330 | Args&&... args); |
| 331 |
| 332 | private: |
| 333 | // Helper class for e.g. lower_bound that can compare a value on the left |
| 334 | // to a key on the right. |
| 335 | struct KeyValueCompare { |
| 336 | // The key comparison object must outlive this class. |
| 337 | explicit KeyValueCompare(const key_compare& key_comp) |
| 338 | : key_comp_(key_comp) {} |
| 339 |
| 340 | template <typename T, typename U> |
| 341 | bool operator()(const T& lhs, const U& rhs) const { |
| 342 | return key_comp_(extract_if_value_type(lhs), extract_if_value_type(rhs)); |
| 343 | } |
| 344 |
| 345 | private: |
| 346 | const key_type& extract_if_value_type(const value_type& v) const { |
| 347 | GetKeyFromValue extractor; |
| 348 | return extractor(v); |
| 349 | } |
| 350 |
| 351 | template <typename K> |
| 352 | const K& extract_if_value_type(const K& k) const { |
| 353 | return k; |
| 354 | } |
| 355 |
| 356 | const key_compare& key_comp_; |
| 357 | }; |
| 358 |
| 359 | iterator const_cast_it(const_iterator c_it) { |
| 360 | auto distance = std::distance(cbegin(), c_it); |
| 361 | return std::next(begin(), distance); |
| 362 | } |
| 363 |
| 364 | // This method is inspired by both std::map::insert(P&&) and |
| 365 | // std::map::insert_or_assign(const K&, V&&). It inserts val if an equivalent |
| 366 | // element is not present yet, otherwise it overwrites. It returns an iterator |
| 367 | // to the modified element and a flag indicating whether insertion or |
| 368 | // assignment happened. |
| 369 | template <class V> |
| 370 | std::pair<iterator, bool> insert_or_assign(V&& val) { |
| 371 | auto position = lower_bound(GetKeyFromValue()(val)); |
| 372 |
| 373 | if (position == end() || value_comp()(val, *position)) |
| 374 | return {impl_.body_.emplace(position, std::forward<V>(val)), true}; |
| 375 |
| 376 | *position = std::forward<V>(val); |
| 377 | return {position, false}; |
| 378 | } |
| 379 |
| 380 | // This method is similar to insert_or_assign, with the following differences: |
| 381 | // - Instead of searching [begin(), end()) it only searches [first, last). |
| 382 | // - In case no equivalent element is found, val is appended to the end of the |
| 383 | // underlying body and an iterator to the next bigger element in [first, |
| 384 | // last) is returned. |
| 385 | template <class V> |
| 386 | std::pair<iterator, bool> append_or_assign(iterator first, |
| 387 | iterator last, |
| 388 | V&& val) { |
| 389 | auto position = std::lower_bound(first, last, val, value_comp()); |
| 390 |
| 391 | if (position == last || value_comp()(val, *position)) { |
| 392 | // emplace_back might invalidate position, which is why distance needs to |
| 393 | // be cached. |
| 394 | const difference_type distance = std::distance(begin(), position); |
| 395 | impl_.body_.emplace_back(std::forward<V>(val)); |
| 396 | return {std::next(begin(), distance), true}; |
| 397 | } |
| 398 |
| 399 | *position = std::forward<V>(val); |
| 400 | return {position, false}; |
| 401 | } |
| 402 |
| 403 | // This method is similar to insert, with the following differences: |
| 404 | // - Instead of searching [begin(), end()) it only searches [first, last). |
| 405 | // - In case no equivalent element is found, val is appended to the end of the |
| 406 | // underlying body and an iterator to the next bigger element in [first, |
| 407 | // last) is returned. |
| 408 | template <class V> |
| 409 | std::pair<iterator, bool> append_unique(iterator first, |
| 410 | iterator last, |
| 411 | V&& val) { |
| 412 | auto position = std::lower_bound(first, last, val, value_comp()); |
| 413 |
| 414 | if (position == last || value_comp()(val, *position)) { |
| 415 | // emplace_back might invalidate position, which is why distance needs to |
| 416 | // be cached. |
| 417 | const difference_type distance = std::distance(begin(), position); |
| 418 | impl_.body_.emplace_back(std::forward<V>(val)); |
| 419 | return {std::next(begin(), distance), true}; |
| 420 | } |
| 421 |
| 422 | return {position, false}; |
| 423 | } |
| 424 |
| 425 | void sort_and_unique(iterator first, iterator last) { |
| 426 | // Preserve stability for the unique code below. |
| 427 | std::stable_sort(first, last, value_comp()); |
| 428 |
| 429 | auto equal_comp = [this](const value_type& lhs, const value_type& rhs) { |
| 430 | // lhs is already <= rhs due to sort, therefore |
| 431 | // !(lhs < rhs) <=> lhs == rhs. |
| 432 | return !value_comp()(lhs, rhs); |
| 433 | }; |
| 434 |
| 435 | erase(std::unique(first, last, equal_comp), last); |
| 436 | } |
| 437 |
| 438 | // To support comparators that may not be possible to default-construct, we |
| 439 | // have to store an instance of Compare. Using this to store all internal |
| 440 | // state of flat_tree and using private inheritance to store compare lets us |
| 441 | // take advantage of an empty base class optimization to avoid extra space in |
| 442 | // the common case when Compare has no state. |
| 443 | struct Impl : private value_compare { |
| 444 | Impl() = default; |
| 445 |
| 446 | template <class Cmp, class... Body> |
| 447 | explicit Impl(Cmp&& compare_arg, Body&&... underlying_type_args) |
| 448 | : value_compare(std::forward<Cmp>(compare_arg)), |
| 449 | body_(std::forward<Body>(underlying_type_args)...) {} |
| 450 |
| 451 | const value_compare& get_value_comp() const { return *this; } |
| 452 | const key_compare& get_key_comp() const { return *this; } |
| 453 |
| 454 | underlying_type body_; |
| 455 | } impl_; |
| 456 |
| 457 | // If the compare is not transparent we want to construct key_type once. |
| 458 | template <typename K> |
| 459 | using KeyTypeOrK = typename std:: |
| 460 | conditional<IsTransparentCompare<key_compare>::value, K, key_type>::type; |
| 461 | }; |
| 462 |
| 463 | // ---------------------------------------------------------------------------- |
| 464 | // Lifetime. |
| 465 |
| 466 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 467 | flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::flat_tree() = default; |
| 468 |
| 469 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 470 | flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::flat_tree( |
| 471 | const KeyCompare& comp) |
| 472 | : impl_(comp) {} |
| 473 |
| 474 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 475 | template <class InputIterator> |
| 476 | flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::flat_tree( |
| 477 | InputIterator first, |
| 478 | InputIterator last, |
| 479 | const KeyCompare& comp) |
| 480 | : impl_(comp, first, last) { |
| 481 | sort_and_unique(begin(), end()); |
| 482 | } |
| 483 |
| 484 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 485 | flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::flat_tree( |
| 486 | const flat_tree&) = default; |
| 487 |
| 488 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 489 | flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::flat_tree( |
| 490 | const underlying_type& items, |
| 491 | const KeyCompare& comp) |
| 492 | : impl_(comp, items) { |
| 493 | sort_and_unique(begin(), end()); |
| 494 | } |
| 495 |
| 496 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 497 | flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::flat_tree( |
| 498 | underlying_type&& items, |
| 499 | const KeyCompare& comp) |
| 500 | : impl_(comp, std::move(items)) { |
| 501 | sort_and_unique(begin(), end()); |
| 502 | } |
| 503 |
| 504 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 505 | flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::flat_tree( |
| 506 | std::initializer_list<value_type> ilist, |
| 507 | const KeyCompare& comp) |
| 508 | : flat_tree(std::begin(ilist), std::end(ilist), comp) {} |
| 509 |
| 510 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 511 | flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::~flat_tree() = default; |
| 512 |
| 513 | // ---------------------------------------------------------------------------- |
| 514 | // Assignments. |
| 515 |
| 516 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 517 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::operator=( |
| 518 | const flat_tree&) -> flat_tree& = default; |
| 519 |
| 520 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 521 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::operator=(flat_tree &&) |
| 522 | -> flat_tree& = default; |
| 523 |
| 524 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 525 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::operator=( |
| 526 | std::initializer_list<value_type> ilist) -> flat_tree& { |
| 527 | impl_.body_ = ilist; |
| 528 | sort_and_unique(begin(), end()); |
| 529 | return *this; |
| 530 | } |
| 531 |
| 532 | // ---------------------------------------------------------------------------- |
| 533 | // Memory management. |
| 534 |
| 535 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 536 | void flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::reserve( |
| 537 | size_type new_capacity) { |
| 538 | impl_.body_.reserve(new_capacity); |
| 539 | } |
| 540 |
| 541 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 542 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::capacity() const |
| 543 | -> size_type { |
| 544 | return impl_.body_.capacity(); |
| 545 | } |
| 546 |
| 547 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 548 | void flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::shrink_to_fit() { |
| 549 | impl_.body_.shrink_to_fit(); |
| 550 | } |
| 551 |
| 552 | // ---------------------------------------------------------------------------- |
| 553 | // Size management. |
| 554 |
| 555 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 556 | void flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::clear() { |
| 557 | impl_.body_.clear(); |
| 558 | } |
| 559 |
| 560 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 561 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::size() const |
| 562 | -> size_type { |
| 563 | return impl_.body_.size(); |
| 564 | } |
| 565 |
| 566 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 567 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::max_size() const |
| 568 | -> size_type { |
| 569 | return impl_.body_.max_size(); |
| 570 | } |
| 571 |
| 572 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 573 | bool flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::empty() const { |
| 574 | return impl_.body_.empty(); |
| 575 | } |
| 576 |
| 577 | // ---------------------------------------------------------------------------- |
| 578 | // Iterators. |
| 579 |
| 580 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 581 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::begin() -> iterator { |
| 582 | return impl_.body_.begin(); |
| 583 | } |
| 584 |
| 585 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 586 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::begin() const |
| 587 | -> const_iterator { |
| 588 | return impl_.body_.begin(); |
| 589 | } |
| 590 |
| 591 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 592 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::cbegin() const |
| 593 | -> const_iterator { |
| 594 | return impl_.body_.cbegin(); |
| 595 | } |
| 596 |
| 597 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 598 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::end() -> iterator { |
| 599 | return impl_.body_.end(); |
| 600 | } |
| 601 |
| 602 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 603 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::end() const |
| 604 | -> const_iterator { |
| 605 | return impl_.body_.end(); |
| 606 | } |
| 607 |
| 608 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 609 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::cend() const |
| 610 | -> const_iterator { |
| 611 | return impl_.body_.cend(); |
| 612 | } |
| 613 |
| 614 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 615 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::rbegin() |
| 616 | -> reverse_iterator { |
| 617 | return impl_.body_.rbegin(); |
| 618 | } |
| 619 |
| 620 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 621 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::rbegin() const |
| 622 | -> const_reverse_iterator { |
| 623 | return impl_.body_.rbegin(); |
| 624 | } |
| 625 |
| 626 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 627 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::crbegin() const |
| 628 | -> const_reverse_iterator { |
| 629 | return impl_.body_.crbegin(); |
| 630 | } |
| 631 |
| 632 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 633 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::rend() |
| 634 | -> reverse_iterator { |
| 635 | return impl_.body_.rend(); |
| 636 | } |
| 637 |
| 638 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 639 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::rend() const |
| 640 | -> const_reverse_iterator { |
| 641 | return impl_.body_.rend(); |
| 642 | } |
| 643 |
| 644 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 645 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::crend() const |
| 646 | -> const_reverse_iterator { |
| 647 | return impl_.body_.crend(); |
| 648 | } |
| 649 |
| 650 | // ---------------------------------------------------------------------------- |
| 651 | // Insert operations. |
| 652 | // |
| 653 | // Currently we use position_hint the same way as eastl or boost: |
| 654 | // https://github.com/electronicarts/EASTL/blob/master/include/EASTL/vector_set.h#L493 |
| 655 |
| 656 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 657 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::insert( |
| 658 | const value_type& val) -> std::pair<iterator, bool> { |
| 659 | return emplace_key_args(GetKeyFromValue()(val), val); |
| 660 | } |
| 661 |
| 662 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 663 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::insert( |
| 664 | value_type&& val) -> std::pair<iterator, bool> { |
| 665 | return emplace_key_args(GetKeyFromValue()(val), std::move(val)); |
| 666 | } |
| 667 |
| 668 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 669 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::insert( |
| 670 | const_iterator position_hint, |
| 671 | const value_type& val) -> iterator { |
| 672 | return emplace_hint_key_args(position_hint, GetKeyFromValue()(val), val) |
| 673 | .first; |
| 674 | } |
| 675 |
| 676 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 677 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::insert( |
| 678 | const_iterator position_hint, |
| 679 | value_type&& val) -> iterator { |
| 680 | return emplace_hint_key_args(position_hint, GetKeyFromValue()(val), |
| 681 | std::move(val)) |
| 682 | .first; |
| 683 | } |
| 684 |
| 685 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 686 | template <class InputIterator> |
| 687 | void flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::insert( |
| 688 | InputIterator first, |
| 689 | InputIterator last) { |
| 690 | if (first == last) |
| 691 | return; |
| 692 |
| 693 | // Dispatch to single element insert if the input range contains a single |
| 694 | // element. |
| 695 | if (is_multipass<InputIterator>() && std::next(first) == last) { |
| 696 | insert(end(), *first); |
| 697 | return; |
| 698 | } |
| 699 |
| 700 | // Provide a convenience lambda to obtain an iterator pointing past the last |
| 701 | // old element. This needs to be dymanic due to possible re-allocations. |
| 702 | auto middle = [this, size = size()] { return std::next(begin(), size); }; |
| 703 |
| 704 | // For batch updates initialize the first insertion point. |
| 705 | difference_type pos_first_new = size(); |
| 706 |
| 707 | // Loop over the input range while appending new values and overwriting |
| 708 | // existing ones, if applicable. Keep track of the first insertion point. |
| 709 | for (; first != last; ++first) { |
| 710 | std::pair<iterator, bool> result = append_unique(begin(), middle(), *first); |
| 711 | if (result.second) { |
| 712 | pos_first_new = |
| 713 | std::min(pos_first_new, std::distance(begin(), result.first)); |
| 714 | } |
| 715 | } |
| 716 |
| 717 | // The new elements might be unordered and contain duplicates, so post-process |
| 718 | // the just inserted elements and merge them with the rest, inserting them at |
| 719 | // the previously found spot. |
| 720 | sort_and_unique(middle(), end()); |
| 721 | std::inplace_merge(std::next(begin(), pos_first_new), middle(), end(), |
| 722 | value_comp()); |
| 723 | } |
| 724 |
| 725 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 726 | template <class... Args> |
| 727 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::emplace(Args&&... args) |
| 728 | -> std::pair<iterator, bool> { |
| 729 | return insert(value_type(std::forward<Args>(args)...)); |
| 730 | } |
| 731 |
| 732 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 733 | template <class... Args> |
| 734 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::emplace_hint( |
| 735 | const_iterator position_hint, |
| 736 | Args&&... args) -> iterator { |
| 737 | return insert(position_hint, value_type(std::forward<Args>(args)...)); |
| 738 | } |
| 739 |
| 740 | // ---------------------------------------------------------------------------- |
| 741 | // Underlying type operations. |
| 742 |
| 743 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 744 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>:: |
| 745 | extract() && -> underlying_type { |
| 746 | return std::exchange(impl_.body_, underlying_type()); |
| 747 | } |
| 748 |
| 749 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 750 | void flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::replace( |
| 751 | underlying_type&& body) { |
| 752 | // Ensure that |body| is sorted and has no repeated elements. |
| 753 | DCHECK(std::is_sorted(body.begin(), body.end(), value_comp())); |
| 754 | DCHECK(std::adjacent_find(body.begin(), body.end(), |
| 755 | [this](const auto& lhs, const auto& rhs) { |
| 756 | return !value_comp()(lhs, rhs); |
| 757 | }) == body.end()); |
| 758 | impl_.body_ = std::move(body); |
| 759 | } |
| 760 |
| 761 | // ---------------------------------------------------------------------------- |
| 762 | // Erase operations. |
| 763 |
| 764 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 765 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::erase( |
| 766 | iterator position) -> iterator { |
| 767 | CHECK(position != impl_.body_.end()); |
| 768 | return impl_.body_.erase(position); |
| 769 | } |
| 770 |
| 771 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 772 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::erase( |
| 773 | const_iterator position) -> iterator { |
| 774 | CHECK(position != impl_.body_.end()); |
| 775 | return impl_.body_.erase(position); |
| 776 | } |
| 777 |
| 778 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 779 | template <typename K> |
| 780 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::erase(const K& val) |
| 781 | -> size_type { |
| 782 | auto eq_range = equal_range(val); |
| 783 | auto res = std::distance(eq_range.first, eq_range.second); |
| 784 | erase(eq_range.first, eq_range.second); |
| 785 | return res; |
| 786 | } |
| 787 |
| 788 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 789 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::erase( |
| 790 | const_iterator first, |
| 791 | const_iterator last) -> iterator { |
| 792 | return impl_.body_.erase(first, last); |
| 793 | } |
| 794 |
| 795 | // ---------------------------------------------------------------------------- |
| 796 | // Comparators. |
| 797 |
| 798 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 799 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::key_comp() const |
| 800 | -> key_compare { |
| 801 | return impl_.get_key_comp(); |
| 802 | } |
| 803 |
| 804 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 805 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::value_comp() const |
| 806 | -> value_compare { |
| 807 | return impl_.get_value_comp(); |
| 808 | } |
| 809 |
| 810 | // ---------------------------------------------------------------------------- |
| 811 | // Search operations. |
| 812 |
| 813 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 814 | template <typename K> |
| 815 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::count( |
| 816 | const K& key) const -> size_type { |
| 817 | auto eq_range = equal_range(key); |
| 818 | return std::distance(eq_range.first, eq_range.second); |
| 819 | } |
| 820 |
| 821 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 822 | template <typename K> |
| 823 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::find(const K& key) |
| 824 | -> iterator { |
| 825 | return const_cast_it(base::as_const(*this).find(key)); |
| 826 | } |
| 827 |
| 828 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 829 | template <typename K> |
| 830 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::find( |
| 831 | const K& key) const -> const_iterator { |
| 832 | auto eq_range = equal_range(key); |
| 833 | return (eq_range.first == eq_range.second) ? end() : eq_range.first; |
| 834 | } |
| 835 |
| 836 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 837 | template <typename K> |
| 838 | bool flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::contains( |
| 839 | const K& key) const { |
| 840 | auto lower = lower_bound(key); |
| 841 | return lower != end() && !key_comp()(key, GetKeyFromValue()(*lower)); |
| 842 | } |
| 843 |
| 844 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 845 | template <typename K> |
| 846 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::equal_range( |
| 847 | const K& key) -> std::pair<iterator, iterator> { |
| 848 | auto res = base::as_const(*this).equal_range(key); |
| 849 | return {const_cast_it(res.first), const_cast_it(res.second)}; |
| 850 | } |
| 851 |
| 852 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 853 | template <typename K> |
| 854 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::equal_range( |
| 855 | const K& key) const -> std::pair<const_iterator, const_iterator> { |
| 856 | auto lower = lower_bound(key); |
| 857 |
| 858 | GetKeyFromValue extractor; |
| 859 | if (lower == end() || impl_.get_key_comp()(key, extractor(*lower))) |
| 860 | return {lower, lower}; |
| 861 |
| 862 | return {lower, std::next(lower)}; |
| 863 | } |
| 864 |
| 865 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 866 | template <typename K> |
| 867 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::lower_bound( |
| 868 | const K& key) -> iterator { |
| 869 | return const_cast_it(base::as_const(*this).lower_bound(key)); |
| 870 | } |
| 871 |
| 872 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 873 | template <typename K> |
| 874 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::lower_bound( |
| 875 | const K& key) const -> const_iterator { |
| 876 | static_assert(std::is_convertible<const KeyTypeOrK<K>&, const K&>::value, |
| 877 | "Requested type cannot be bound to the container's key_type " |
| 878 | "which is required for a non-transparent compare."); |
| 879 |
| 880 | const KeyTypeOrK<K>& key_ref = key; |
| 881 |
| 882 | KeyValueCompare key_value(impl_.get_key_comp()); |
| 883 | return std::lower_bound(begin(), end(), key_ref, key_value); |
| 884 | } |
| 885 |
| 886 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 887 | template <typename K> |
| 888 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::upper_bound( |
| 889 | const K& key) -> iterator { |
| 890 | return const_cast_it(base::as_const(*this).upper_bound(key)); |
| 891 | } |
| 892 |
| 893 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 894 | template <typename K> |
| 895 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::upper_bound( |
| 896 | const K& key) const -> const_iterator { |
| 897 | static_assert(std::is_convertible<const KeyTypeOrK<K>&, const K&>::value, |
| 898 | "Requested type cannot be bound to the container's key_type " |
| 899 | "which is required for a non-transparent compare."); |
| 900 |
| 901 | const KeyTypeOrK<K>& key_ref = key; |
| 902 |
| 903 | KeyValueCompare key_value(impl_.get_key_comp()); |
| 904 | return std::upper_bound(begin(), end(), key_ref, key_value); |
| 905 | } |
| 906 |
| 907 | // ---------------------------------------------------------------------------- |
| 908 | // General operations. |
| 909 |
| 910 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 911 | void flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::swap( |
| 912 | flat_tree& other) noexcept { |
| 913 | std::swap(impl_, other.impl_); |
| 914 | } |
| 915 |
| 916 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 917 | template <class... Args> |
| 918 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::unsafe_emplace( |
| 919 | const_iterator position, |
| 920 | Args&&... args) -> iterator { |
| 921 | return impl_.body_.emplace(position, std::forward<Args>(args)...); |
| 922 | } |
| 923 |
| 924 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 925 | template <class K, class... Args> |
| 926 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::emplace_key_args( |
| 927 | const K& key, |
| 928 | Args&&... args) -> std::pair<iterator, bool> { |
| 929 | auto lower = lower_bound(key); |
| 930 | if (lower == end() || key_comp()(key, GetKeyFromValue()(*lower))) |
| 931 | return {unsafe_emplace(lower, std::forward<Args>(args)...), true}; |
| 932 | return {lower, false}; |
| 933 | } |
| 934 |
| 935 | template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 936 | template <class K, class... Args> |
| 937 | auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::emplace_hint_key_args( |
| 938 | const_iterator hint, |
| 939 | const K& key, |
| 940 | Args&&... args) -> std::pair<iterator, bool> { |
| 941 | GetKeyFromValue extractor; |
| 942 | if ((hint == begin() || key_comp()(extractor(*std::prev(hint)), key))) { |
| 943 | if (hint == end() || key_comp()(key, extractor(*hint))) { |
| 944 | // *(hint - 1) < key < *hint => key did not exist and hint is correct. |
| 945 | return {unsafe_emplace(hint, std::forward<Args>(args)...), true}; |
| 946 | } |
| 947 | if (!key_comp()(extractor(*hint), key)) { |
| 948 | // key == *hint => no-op, return correct hint. |
| 949 | return {const_cast_it(hint), false}; |
| 950 | } |
| 951 | } |
| 952 | // hint was not helpful, dispatch to hintless version. |
| 953 | return emplace_key_args(key, std::forward<Args>(args)...); |
| 954 | } |
| 955 |
| 956 | // For containers like sets, the key is the same as the value. This implements |
| 957 | // the GetKeyFromValue template parameter to flat_tree for this case. |
| 958 | template <class Key> |
| 959 | struct GetKeyFromValueIdentity { |
| 960 | const Key& operator()(const Key& k) const { return k; } |
| 961 | }; |
| 962 |
| 963 | } // namespace internal |
| 964 |
| 965 | // ---------------------------------------------------------------------------- |
| 966 | // Free functions. |
| 967 |
| 968 | // Erases all elements that match predicate. It has O(size) complexity. |
| 969 | template <class Key, |
| 970 | class Value, |
| 971 | class GetKeyFromValue, |
| 972 | class KeyCompare, |
| 973 | typename Predicate> |
| 974 | size_t EraseIf( |
| 975 | base::internal::flat_tree<Key, Value, GetKeyFromValue, KeyCompare>& |
| 976 | container, |
| 977 | Predicate pred) { |
| 978 | auto it = std::remove_if(container.begin(), container.end(), pred); |
| 979 | size_t removed = std::distance(it, container.end()); |
| 980 | container.erase(it, container.end()); |
| 981 | return removed; |
| 982 | } |
| 983 |
| 984 | } // namespace base |
| 985 |
| 986 | #endif // BASE_CONTAINERS_FLAT_TREE_H_ |
| 987 |