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 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
549 // | 568 // |
550 // Currently we use position_hint the same way as eastl or boost: | 569 // Currently we use position_hint the same way as eastl or boost: |
551 // https://github.com/electronicarts/EASTL/blob/master/include/EASTL/vector_set. h#L493 | 570 // https://github.com/electronicarts/EASTL/blob/master/include/EASTL/vector_set. h#L493 |
552 // | 571 // |
553 // We duplicate code between copy and move version so that we can avoid | 572 // We duplicate code between copy and move version so that we can avoid |
554 // creating a temporary value. | 573 // creating a temporary value. |
555 | 574 |
556 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> | 575 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
557 auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::insert( | 576 auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::insert( |
558 const value_type& val) -> std::pair<iterator, bool> { | 577 const value_type& val) -> std::pair<iterator, bool> { |
559 auto position = lower_bound(val); | 578 GetKeyFromValue extractor; |
579 auto position = lower_bound(extractor(val)); | |
560 | 580 |
561 if (position == end() || impl_.get_value_comp()(val, *position)) | 581 if (position == end() || impl_.get_value_comp()(val, *position)) |
562 return {impl_.body_.insert(position, val), true}; | 582 return {impl_.body_.insert(position, val), true}; |
563 | 583 |
564 return {position, false}; | 584 return {position, false}; |
565 } | 585 } |
566 | 586 |
567 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> | 587 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
568 auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::insert( | 588 auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::insert( |
569 value_type&& val) -> std::pair<iterator, bool> { | 589 value_type&& val) -> std::pair<iterator, bool> { |
(...skipping 26 matching lines...) Expand all Loading... | |
596 if (position_hint == end() || impl_.get_value_comp()(val, *position_hint)) { | 616 if (position_hint == end() || impl_.get_value_comp()(val, *position_hint)) { |
597 if (position_hint == begin() || | 617 if (position_hint == begin() || |
598 impl_.get_value_comp()(*(position_hint - 1), val)) | 618 impl_.get_value_comp()(*(position_hint - 1), val)) |
599 // We have to cast away const because of crbug.com/677044. | 619 // We have to cast away const because of crbug.com/677044. |
600 return impl_.body_.insert(const_cast_it(position_hint), std::move(val)); | 620 return impl_.body_.insert(const_cast_it(position_hint), std::move(val)); |
601 } | 621 } |
602 return insert(std::move(val)).first; | 622 return insert(std::move(val)).first; |
603 } | 623 } |
604 | 624 |
605 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> | 625 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
626 template <class InputIterator> | |
627 auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::insert( | |
628 InputIterator first, | |
629 InputIterator last, | |
630 FlatContainerDupes dupes) -> void { | |
631 if (first == last) | |
632 return; | |
633 | |
634 // Cache results whether existing elements should be overwritten and whether | |
635 // inserting new elements happens immediately or will be done in a batch. | |
636 const bool overwrite_existing = dupes == KEEP_LAST_OF_DUPES; | |
637 const bool insert_inplace = | |
638 is_multipass<InputIterator>() && std::next(first) == last; | |
639 | |
640 if (insert_inplace) { | |
dyaroshev
2017/05/12 22:10:03
Maybe factor this piece out? Function seems quite
jdoerrie
2017/05/17 06:41:15
Done.
| |
641 if (overwrite_existing) { | |
642 std::for_each(first, last, [this](const value_type& val) { | |
dyaroshev
2017/05/12 22:10:03
This won't work for move only types, and you will
jdoerrie
2017/05/17 06:41:16
Done.
| |
643 std::pair<iterator, bool> result = insert(val); | |
644 if (!result.second) | |
645 *result.first = val; | |
646 }); | |
647 } else { | |
648 std::copy(first, last, std::inserter(*this, end())); | |
jdoerrie
2017/05/17 06:41:16
I dropped the call to copy in favor of an explicit
dyaroshev
2017/05/17 10:15:19
Nah, it assigns the iterator. Smth like:
pos = con
jdoerrie
2017/05/17 11:32:05
You are right, my bad. Here's libc++'s implementat
| |
649 } | |
650 | |
651 return; | |
652 } | |
653 | |
654 // For batch updates initialize the first insertion point. | |
655 const size_type original_size = size(); | |
656 difference_type pos_first_new = original_size; | |
jdoerrie
2017/05/17 06:41:15
I dropped this optimization, because it would have
dyaroshev
2017/05/17 10:15:19
I think that inserting small ranges is actually an
jdoerrie
2017/05/17 11:32:05
Acknowledged.
| |
657 | |
658 // Lambda that tries to find |val| in the sorted range [first, last). If |val| | |
659 // is not present, it is appened to the underlying body while potentially | |
660 // updating |pos_first_new|. | |
661 // It returns an iterator and bool pair with the same semantics as |insert|'s | |
662 // return value. | |
663 auto append_new = [this, &pos_first_new](iterator first, iterator last, | |
664 const value_type& val) { | |
dyaroshev
2017/05/12 22:10:03
Again, move is problematic. If you take the push_b
jdoerrie
2017/05/17 06:41:15
Done.
| |
665 iterator lower = std::lower_bound(first, last, val, value_comp()); | |
666 if (lower == last || value_comp()(val, *lower)) { | |
667 pos_first_new = std::min(pos_first_new, std::distance(begin(), lower)); | |
668 impl_.body_.push_back(val); | |
669 return std::make_pair(std::prev(end()), true); | |
670 } | |
671 | |
672 return std::make_pair(lower, false); | |
673 }; | |
674 | |
675 // Provide a convenience lambda to obtain an iterator pointing past the last | |
676 // old element. This needs to be dymanic due to possible re-allocations. | |
677 auto middle = [this, original_size]() { | |
678 return std::next(begin(), original_size); | |
679 }; | |
680 | |
681 // Loop over the input range and append and track new values. Use the returned | |
682 // iterator to overwrite existing values if applicable. | |
683 if (overwrite_existing) { | |
684 std::for_each( | |
685 first, last, [this, append_new, middle](const value_type& val) { | |
686 std::pair<iterator, bool> result = append_new(begin(), middle(), val); | |
687 if (!result.second) | |
688 *result.first = val; | |
689 }); | |
690 } else { | |
691 std::for_each(first, last, | |
692 [this, append_new, middle](const value_type& val) { | |
693 append_new(begin(), middle(), val); | |
694 }); | |
695 } | |
696 | |
697 // The new elements might be unordered and contain duplicates, so post-process | |
698 // the just inserted elements and merge them with the rest, inserting them at | |
699 // the previously found spot. | |
700 sort_and_unique(middle(), end(), dupes); | |
701 std::inplace_merge(std::next(begin(), pos_first_new), middle(), end(), | |
702 value_comp()); | |
703 } | |
704 | |
705 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> | |
606 template <class... Args> | 706 template <class... Args> |
607 auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::emplace(Args&&... args) | 707 auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::emplace(Args&&... args) |
608 -> std::pair<iterator, bool> { | 708 -> std::pair<iterator, bool> { |
609 return insert(value_type(std::forward<Args>(args)...)); | 709 return insert(value_type(std::forward<Args>(args)...)); |
610 } | 710 } |
611 | 711 |
612 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> | 712 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> |
613 template <class... Args> | 713 template <class... Args> |
614 auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::emplace_hint( | 714 auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::emplace_hint( |
615 const_iterator position_hint, | 715 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>& | 868 void EraseIf(base::internal::flat_tree<Key, Value, GetKeyFromValue, KeyCompare>& |
769 container, | 869 container, |
770 Predicate pred) { | 870 Predicate pred) { |
771 container.erase(std::remove_if(container.begin(), container.end(), pred), | 871 container.erase(std::remove_if(container.begin(), container.end(), pred), |
772 container.end()); | 872 container.end()); |
773 } | 873 } |
774 | 874 |
775 } // namespace base | 875 } // namespace base |
776 | 876 |
777 #endif // BASE_CONTAINERS_FLAT_TREE_H_ | 877 #endif // BASE_CONTAINERS_FLAT_TREE_H_ |
OLD | NEW |