Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_CONTAINERS_FLAT_TREE_H_ | 5 #ifndef BASE_CONTAINERS_FLAT_TREE_H_ |
| 6 #define BASE_CONTAINERS_FLAT_TREE_H_ | 6 #define BASE_CONTAINERS_FLAT_TREE_H_ |
| 7 | 7 |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <iterator> | |
| 9 #include <vector> | 10 #include <vector> |
| 10 | 11 |
| 11 namespace base { | 12 namespace base { |
| 12 | 13 |
| 13 enum FlatContainerDupes { | 14 enum FlatContainerDupes { |
| 14 KEEP_FIRST_OF_DUPES, | 15 KEEP_FIRST_OF_DUPES, |
| 15 KEEP_LAST_OF_DUPES, | 16 KEEP_LAST_OF_DUPES, |
| 16 }; | 17 }; |
| 17 | 18 |
| 18 namespace internal { | 19 namespace internal { |
| 19 | 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 | |
| 20 // This algorithm is like unique() from the standard library except it | 30 // This algorithm is like unique() from the standard library except it |
| 21 // selects only the last of consecutive values instead of the first. | 31 // selects only the last of consecutive values instead of the first. |
| 22 template <class Iterator, class BinaryPredicate> | 32 template <class Iterator, class BinaryPredicate> |
| 23 Iterator LastUnique(Iterator first, Iterator last, BinaryPredicate compare) { | 33 Iterator LastUnique(Iterator first, Iterator last, BinaryPredicate compare) { |
| 24 Iterator replacable = std::adjacent_find(first, last, compare); | 34 Iterator replacable = std::adjacent_find(first, last, compare); |
| 25 | 35 |
| 26 // No duplicate elements found. | 36 // No duplicate elements found. |
| 27 if (replacable == last) | 37 if (replacable == last) |
| 28 return last; | 38 return last; |
| 29 | 39 |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 180 const_reverse_iterator crbegin() const; | 190 const_reverse_iterator crbegin() const; |
| 181 | 191 |
| 182 reverse_iterator rend(); | 192 reverse_iterator rend(); |
| 183 const_reverse_iterator rend() const; | 193 const_reverse_iterator rend() const; |
| 184 const_reverse_iterator crend() const; | 194 const_reverse_iterator crend() const; |
| 185 | 195 |
| 186 // -------------------------------------------------------------------------- | 196 // -------------------------------------------------------------------------- |
| 187 // Insert operations. | 197 // Insert operations. |
| 188 // | 198 // |
| 189 // Assume that every operation invalidates iterators and references. | 199 // Assume that every operation invalidates iterators and references. |
| 190 // Insertion of one element can take O(size). See the Notes section in the | 200 // Insertion of one element can take O(size). Capacity of flat_tree grows in |
| 191 // class comments on why we do not currently implement range insertion. | 201 // an implementation-defined manner. |
| 192 // Capacity of flat_tree grows in an implementation-defined manner. | |
| 193 // | 202 // |
| 194 // NOTE: Prefer to build a new flat_tree from a std::vector (or similar) | 203 // NOTE: Prefer to build a new flat_tree from a std::vector (or similar) |
| 195 // instead of calling insert() repeatedly. | 204 // instead of calling insert() repeatedly. |
| 196 | 205 |
| 197 std::pair<iterator, bool> insert(const value_type& val); | 206 std::pair<iterator, bool> insert(const value_type& val); |
| 198 std::pair<iterator, bool> insert(value_type&& val); | 207 std::pair<iterator, bool> insert(value_type&& val); |
| 199 | 208 |
| 200 iterator insert(const_iterator position_hint, const value_type& x); | 209 iterator insert(const_iterator position_hint, const value_type& x); |
| 201 iterator insert(const_iterator position_hint, value_type&& x); | 210 iterator insert(const_iterator position_hint, value_type&& x); |
| 202 | 211 |
| 212 // This method inserts the values from the range [first, last) into the | |
| 213 // current tree. In case of KEEP_LAST_OF_DUPES newly added elements can | |
| 214 // overwrite existing values. | |
| 215 template <class InputIterator> | |
| 216 void insert(InputIterator first, | |
| 217 InputIterator last, | |
| 218 FlatContainerDupes dupes); | |
| 219 | |
| 203 template <class... Args> | 220 template <class... Args> |
| 204 std::pair<iterator, bool> emplace(Args&&... args); | 221 std::pair<iterator, bool> emplace(Args&&... args); |
| 205 | 222 |
| 206 template <class... Args> | 223 template <class... Args> |
| 207 iterator emplace_hint(const_iterator position_hint, Args&&... args); | 224 iterator emplace_hint(const_iterator position_hint, Args&&... args); |
| 208 | 225 |
| 209 // -------------------------------------------------------------------------- | 226 // -------------------------------------------------------------------------- |
| 210 // Erase operations. | 227 // Erase operations. |
| 211 // | 228 // |
| 212 // Assume that every operation invalidates iterators and references. | 229 // Assume that every operation invalidates iterators and references. |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 309 const key_compare& key_comp_; | 326 const key_compare& key_comp_; |
| 310 }; | 327 }; |
| 311 | 328 |
| 312 const flat_tree& as_const() { return *this; } | 329 const flat_tree& as_const() { return *this; } |
| 313 | 330 |
| 314 iterator const_cast_it(const_iterator c_it) { | 331 iterator const_cast_it(const_iterator c_it) { |
| 315 auto distance = std::distance(cbegin(), c_it); | 332 auto distance = std::distance(cbegin(), c_it); |
| 316 return std::next(begin(), distance); | 333 return std::next(begin(), distance); |
| 317 } | 334 } |
| 318 | 335 |
| 319 void sort_and_unique(FlatContainerDupes dupes) { | 336 void sort_and_unique(iterator first, |
| 337 iterator last, | |
| 338 FlatContainerDupes dupes) { | |
| 320 // Preserve stability for the unique code below. | 339 // Preserve stability for the unique code below. |
| 321 std::stable_sort(begin(), end(), impl_.get_value_comp()); | 340 std::stable_sort(first, last, impl_.get_value_comp()); |
| 322 | 341 |
| 323 auto comparator = [this](const value_type& lhs, const value_type& rhs) { | 342 auto comparator = [this](const value_type& lhs, const value_type& rhs) { |
| 324 // lhs is already <= rhs due to sort, therefore | 343 // lhs is already <= rhs due to sort, therefore |
| 325 // !(lhs < rhs) <=> lhs == rhs. | 344 // !(lhs < rhs) <=> lhs == rhs. |
| 326 return !impl_.get_value_comp()(lhs, rhs); | 345 return !impl_.get_value_comp()(lhs, rhs); |
| 327 }; | 346 }; |
| 328 | 347 |
| 329 iterator erase_after; | 348 iterator erase_after; |
| 330 switch (dupes) { | 349 switch (dupes) { |
| 331 case KEEP_FIRST_OF_DUPES: | 350 case KEEP_FIRST_OF_DUPES: |
| 332 erase_after = std::unique(begin(), end(), comparator); | 351 erase_after = std::unique(first, last, comparator); |
| 333 break; | 352 break; |
| 334 case KEEP_LAST_OF_DUPES: | 353 case KEEP_LAST_OF_DUPES: |
| 335 erase_after = LastUnique(begin(), end(), comparator); | 354 erase_after = LastUnique(first, last, comparator); |
| 336 break; | 355 break; |
| 337 } | 356 } |
| 338 erase(erase_after, end()); | 357 erase(erase_after, last); |
| 339 } | 358 } |
| 340 | 359 |
| 341 // To support comparators that may not be possible to default-construct, we | 360 // To support comparators that may not be possible to default-construct, we |
| 342 // have to store an instance of Compare. Using this to store all internal | 361 // have to store an instance of Compare. Using this to store all internal |
| 343 // state of flat_tree and using private inheritance to store compare lets us | 362 // state of flat_tree and using private inheritance to store compare lets us |
| 344 // take advantage of an empty base class optimization to avoid extra space in | 363 // take advantage of an empty base class optimization to avoid extra space in |
| 345 // the common case when Compare has no state. | 364 // the common case when Compare has no state. |
| 346 struct Impl : private value_compare { | 365 struct Impl : private value_compare { |
| 347 Impl() = default; | 366 Impl() = default; |
| 348 | 367 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 370 : impl_(comp) {} | 389 : impl_(comp) {} |
| 371 | 390 |
| 372 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> | 391 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 373 template <class InputIterator> | 392 template <class InputIterator> |
| 374 flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::flat_tree( | 393 flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::flat_tree( |
| 375 InputIterator first, | 394 InputIterator first, |
| 376 InputIterator last, | 395 InputIterator last, |
| 377 FlatContainerDupes dupe_handling, | 396 FlatContainerDupes dupe_handling, |
| 378 const KeyCompare& comp) | 397 const KeyCompare& comp) |
| 379 : impl_(comp, first, last) { | 398 : impl_(comp, first, last) { |
| 380 sort_and_unique(dupe_handling); | 399 sort_and_unique(begin(), end(), dupe_handling); |
| 381 } | 400 } |
| 382 | 401 |
| 383 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> | 402 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 384 flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::flat_tree( | 403 flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::flat_tree( |
| 385 const flat_tree&) = default; | 404 const flat_tree&) = default; |
| 386 | 405 |
| 387 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> | 406 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 388 flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::flat_tree(flat_tree&&) = | 407 flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::flat_tree(flat_tree&&) = |
| 389 default; | 408 default; |
| 390 | 409 |
| 391 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> | 410 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 392 flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::flat_tree( | 411 flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::flat_tree( |
| 393 std::vector<value_type> items, | 412 std::vector<value_type> items, |
| 394 FlatContainerDupes dupe_handling, | 413 FlatContainerDupes dupe_handling, |
| 395 const KeyCompare& comp) | 414 const KeyCompare& comp) |
| 396 : impl_(comp, std::move(items)) { | 415 : impl_(comp, std::move(items)) { |
| 397 sort_and_unique(dupe_handling); | 416 sort_and_unique(begin(), end(), dupe_handling); |
| 398 } | 417 } |
| 399 | 418 |
| 400 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> | 419 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 401 flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::flat_tree( | 420 flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::flat_tree( |
| 402 std::initializer_list<value_type> ilist, | 421 std::initializer_list<value_type> ilist, |
| 403 FlatContainerDupes dupe_handling, | 422 FlatContainerDupes dupe_handling, |
| 404 const KeyCompare& comp) | 423 const KeyCompare& comp) |
| 405 : flat_tree(std::begin(ilist), std::end(ilist), dupe_handling, comp) {} | 424 : flat_tree(std::begin(ilist), std::end(ilist), dupe_handling, comp) {} |
| 406 | 425 |
| 407 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> | 426 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 408 flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::~flat_tree() = default; | 427 flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::~flat_tree() = default; |
| 409 | 428 |
| 410 // ---------------------------------------------------------------------------- | 429 // ---------------------------------------------------------------------------- |
| 411 // Assignments. | 430 // Assignments. |
| 412 | 431 |
| 413 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> | 432 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 414 auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::operator=( | 433 auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::operator=( |
| 415 const flat_tree&) -> flat_tree& = default; | 434 const flat_tree&) -> flat_tree& = default; |
| 416 | 435 |
| 417 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> | 436 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 418 auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::operator=(flat_tree &&) | 437 auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::operator=(flat_tree &&) |
| 419 -> flat_tree& = default; | 438 -> flat_tree& = default; |
| 420 | 439 |
| 421 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> | 440 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 422 auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::operator=( | 441 auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::operator=( |
| 423 std::initializer_list<value_type> ilist) -> flat_tree& { | 442 std::initializer_list<value_type> ilist) -> flat_tree& { |
| 424 impl_.body_ = ilist; | 443 impl_.body_ = ilist; |
| 425 sort_and_unique(KEEP_FIRST_OF_DUPES); | 444 sort_and_unique(begin(), end(), KEEP_FIRST_OF_DUPES); |
| 426 return *this; | 445 return *this; |
| 427 } | 446 } |
| 428 | 447 |
| 429 // ---------------------------------------------------------------------------- | 448 // ---------------------------------------------------------------------------- |
| 430 // Memory management. | 449 // Memory management. |
| 431 | 450 |
| 432 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> | 451 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 433 void flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::reserve( | 452 void flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::reserve( |
| 434 size_type new_capacity) { | 453 size_type new_capacity) { |
| 435 impl_.body_.reserve(new_capacity); | 454 impl_.body_.reserve(new_capacity); |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 596 if (position_hint == end() || impl_.get_value_comp()(val, *position_hint)) { | 615 if (position_hint == end() || impl_.get_value_comp()(val, *position_hint)) { |
| 597 if (position_hint == begin() || | 616 if (position_hint == begin() || |
| 598 impl_.get_value_comp()(*(position_hint - 1), val)) | 617 impl_.get_value_comp()(*(position_hint - 1), val)) |
| 599 // We have to cast away const because of crbug.com/677044. | 618 // We have to cast away const because of crbug.com/677044. |
| 600 return impl_.body_.insert(const_cast_it(position_hint), std::move(val)); | 619 return impl_.body_.insert(const_cast_it(position_hint), std::move(val)); |
| 601 } | 620 } |
| 602 return insert(std::move(val)).first; | 621 return insert(std::move(val)).first; |
| 603 } | 622 } |
| 604 | 623 |
| 605 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> | 624 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 625 template <class InputIterator> | |
| 626 auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::insert( | |
| 627 InputIterator first, | |
| 628 InputIterator last, | |
| 629 FlatContainerDupes dupes) -> void { | |
| 630 if (first == last) | |
| 631 return; | |
| 632 | |
| 633 // In case of multipass iterators check whether there is only one element in | |
| 634 // the range. If applicable, either insert a new element in the correct | |
| 635 // position or overwrite an existing one when KEEP_LAST_OF_DUPES is passed. | |
| 636 if (is_multipass<InputIterator>() && std::next(first) == last) { | |
|
dyaroshev
2017/05/11 09:46:43
We probably need this to be smth along the lines o
jdoerrie
2017/05/12 10:32:57
Acknowledged, although even doing inplace inserts
| |
| 637 GetKeyFromValue extractor; | |
| 638 iterator lower = lower_bound(extractor(*first)); | |
| 639 bool is_unique = lower == end() || value_comp()(*first, *lower); | |
| 640 switch (dupes) { | |
| 641 case KEEP_LAST_OF_DUPES: | |
| 642 if (!is_unique) { | |
| 643 *lower = *first; | |
| 644 return; | |
| 645 } | |
| 646 default: | |
| 647 if (is_unique) | |
| 648 impl_.body_.insert(lower, *first); | |
| 649 } | |
| 650 return; | |
| 651 } | |
| 652 | |
| 653 // Initialize the first insertion point and provide a convenience lambda to | |
| 654 // obtain an iterator pointing past the old element. This needs to be dymanic | |
| 655 // due to possible re-allocations. | |
| 656 difference_type pos_first_new = size(); | |
|
dyaroshev
2017/05/11 09:46:43
Nit: declare pos_first_new where you need it.
jdoerrie
2017/05/12 10:32:57
Done.
| |
| 657 size_type original_size = size(); | |
| 658 auto middle = [this, original_size]() { | |
| 659 return std::next(begin(), original_size); | |
| 660 }; | |
| 661 | |
| 662 // Loop over all elements in the range, inserting new values at the end of the | |
| 663 // tree, while overwriting existing ones in case of KEEP_LAST_OF_DUPES. Keep | |
| 664 // track of the first insertion point for the following merge step. | |
| 665 for (; first != last; ++first) { | |
| 666 iterator lower = std::lower_bound(begin(), middle(), *first, value_comp()); | |
| 667 bool is_unique = lower == middle() || value_comp()(*first, *lower); | |
| 668 switch (dupes) { | |
| 669 case KEEP_LAST_OF_DUPES: | |
|
dyaroshev
2017/05/11 09:46:43
Don't do this check inside a loop. Even if it's op
jdoerrie
2017/05/12 10:32:57
Done.
| |
| 670 if (!is_unique) { | |
| 671 *lower = *first; | |
| 672 continue; | |
| 673 } | |
| 674 default: | |
| 675 if (is_unique) { | |
| 676 pos_first_new = | |
| 677 std::min(pos_first_new, std::distance(begin(), lower)); | |
| 678 impl_.body_.push_back(*first); | |
| 679 } | |
| 680 } | |
| 681 } | |
| 682 | |
| 683 // The new elements might be unordered and contain duplicates, so post-process | |
| 684 // the just inserted elements and merge them with the rest, inserting them at | |
| 685 // the previously found spot. | |
| 686 sort_and_unique(middle(), end(), dupes); | |
| 687 std::inplace_merge(std::next(begin(), pos_first_new), middle(), end(), | |
| 688 value_comp()); | |
| 689 } | |
| 690 | |
| 691 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> | |
| 606 template <class... Args> | 692 template <class... Args> |
| 607 auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::emplace(Args&&... args) | 693 auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::emplace(Args&&... args) |
| 608 -> std::pair<iterator, bool> { | 694 -> std::pair<iterator, bool> { |
| 609 return insert(value_type(std::forward<Args>(args)...)); | 695 return insert(value_type(std::forward<Args>(args)...)); |
| 610 } | 696 } |
| 611 | 697 |
| 612 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> | 698 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
| 613 template <class... Args> | 699 template <class... Args> |
| 614 auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::emplace_hint( | 700 auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::emplace_hint( |
| 615 const_iterator position_hint, | 701 const_iterator position_hint, |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 768 void EraseIf(base::internal::flat_tree<Key, Value, GetKeyFromValue, KeyCompare>& | 854 void EraseIf(base::internal::flat_tree<Key, Value, GetKeyFromValue, KeyCompare>& |
| 769 container, | 855 container, |
| 770 Predicate pred) { | 856 Predicate pred) { |
| 771 container.erase(std::remove_if(container.begin(), container.end(), pred), | 857 container.erase(std::remove_if(container.begin(), container.end(), pred), |
| 772 container.end()); | 858 container.end()); |
| 773 } | 859 } |
| 774 | 860 |
| 775 } // namespace base | 861 } // namespace base |
| 776 | 862 |
| 777 #endif // BASE_CONTAINERS_FLAT_TREE_H_ | 863 #endif // BASE_CONTAINERS_FLAT_TREE_H_ |
| OLD | NEW |