Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(802)

Side by Side Diff: base/containers/flat_tree.h

Issue 2859593002: base: Add bulk insert to flat_tree. (Closed)
Patch Set: update Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | base/containers/flat_tree_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <vector> 9 #include <vector>
10 10
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 const_reverse_iterator crbegin() const; 177 const_reverse_iterator crbegin() const;
178 178
179 reverse_iterator rend(); 179 reverse_iterator rend();
180 const_reverse_iterator rend() const; 180 const_reverse_iterator rend() const;
181 const_reverse_iterator crend() const; 181 const_reverse_iterator crend() const;
182 182
183 // -------------------------------------------------------------------------- 183 // --------------------------------------------------------------------------
184 // Insert operations. 184 // Insert operations.
185 // 185 //
186 // Assume that every operation invalidates iterators and references. 186 // Assume that every operation invalidates iterators and references.
187 // Insertion of one element can take O(size). See the Notes section in the 187 // Insertion of one element can take O(size).
188 // class comments on why we do not currently implement range insertion.
189 // Capacity of flat_tree grows in an implementation-defined manner. 188 // Capacity of flat_tree grows in an implementation-defined manner.
190 // 189 //
191 // NOTE: Prefer to build a new flat_tree from a std::vector (or similar) 190 // NOTE: Prefer to build a new flat_tree from a std::vector (or similar)
192 // instead of calling insert() repeatedly. 191 // instead of calling insert() repeatedly.
193 192
194 std::pair<iterator, bool> insert(const value_type& val); 193 std::pair<iterator, bool> insert(const value_type& val);
195 std::pair<iterator, bool> insert(value_type&& val); 194 std::pair<iterator, bool> insert(value_type&& val);
196 195
197 iterator insert(const_iterator position_hint, const value_type& x); 196 iterator insert(const_iterator position_hint, const value_type& x);
198 iterator insert(const_iterator position_hint, value_type&& x); 197 iterator insert(const_iterator position_hint, value_type&& x);
199 198
199 template <class InputIterator>
200 void insert(InputIterator first, InputIterator last);
201
200 template <class... Args> 202 template <class... Args>
201 std::pair<iterator, bool> emplace(Args&&... args); 203 std::pair<iterator, bool> emplace(Args&&... args);
202 204
203 template <class... Args> 205 template <class... Args>
204 iterator emplace_hint(const_iterator position_hint, Args&&... args); 206 iterator emplace_hint(const_iterator position_hint, Args&&... args);
205 207
206 // -------------------------------------------------------------------------- 208 // --------------------------------------------------------------------------
207 // Erase operations. 209 // Erase operations.
208 // 210 //
209 // Assume that every operation invalidates iterators and references. 211 // Assume that every operation invalidates iterators and references.
(...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after
593 if (position_hint == end() || impl_.get_value_comp()(val, *position_hint)) { 595 if (position_hint == end() || impl_.get_value_comp()(val, *position_hint)) {
594 if (position_hint == begin() || 596 if (position_hint == begin() ||
595 impl_.get_value_comp()(*(position_hint - 1), val)) 597 impl_.get_value_comp()(*(position_hint - 1), val))
596 // We have to cast away const because of crbug.com/677044. 598 // We have to cast away const because of crbug.com/677044.
597 return impl_.body_.insert(const_cast_it(position_hint), std::move(val)); 599 return impl_.body_.insert(const_cast_it(position_hint), std::move(val));
598 } 600 }
599 return insert(std::move(val)).first; 601 return insert(std::move(val)).first;
600 } 602 }
601 603
602 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> 604 template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
605 template <class InputIterator>
606 void flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::insert(
607 InputIterator first,
608 InputIterator last) {
609 // Insert the values at the end.
610 auto size = impl_.body_.size();
611 impl_.body_.insert(end(), first, last);
612 auto mid = begin() + size;
613
614 // Sort the newly inserted elements.
615 std::stable_sort(mid, end(), impl_.get_value_comp());
616
617 // Merge the two sublists and unique them.
618 std::inplace_merge(begin(), mid, end(), impl_.get_value_comp());
619 auto comparator = [this](const value_type& lhs, const value_type& rhs) {
620 // lhs is already <= rhs due to sort, therefore
621 // !(lhs < rhs) <=> lhs == rhs.
622 return !impl_.get_value_comp()(lhs, rhs);
623 };
624 auto erase_after = std::unique(begin(), end(), comparator);
625 erase(erase_after, end());
626 }
627
628 template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
603 template <class... Args> 629 template <class... Args>
604 auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::emplace(Args&&... args) 630 auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::emplace(Args&&... args)
605 -> std::pair<iterator, bool> { 631 -> std::pair<iterator, bool> {
606 return insert(value_type(std::forward<Args>(args)...)); 632 return insert(value_type(std::forward<Args>(args)...));
607 } 633 }
608 634
609 template <class Key, class Value, class GetKeyFromValue, class KeyCompare> 635 template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
610 template <class... Args> 636 template <class... Args>
611 auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::emplace_hint( 637 auto flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::emplace_hint(
612 const_iterator position_hint, 638 const_iterator position_hint,
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
765 void EraseIf(base::internal::flat_tree<Key, Value, GetKeyFromValue, KeyCompare>& 791 void EraseIf(base::internal::flat_tree<Key, Value, GetKeyFromValue, KeyCompare>&
766 container, 792 container,
767 Predicate pred) { 793 Predicate pred) {
768 container.erase(std::remove_if(container.begin(), container.end(), pred), 794 container.erase(std::remove_if(container.begin(), container.end(), pred),
769 container.end()); 795 container.end());
770 } 796 }
771 797
772 } // namespace base 798 } // namespace base
773 799
774 #endif // BASE_CONTAINERS_FLAT_TREE_H_ 800 #endif // BASE_CONTAINERS_FLAT_TREE_H_
OLDNEW
« no previous file with comments | « no previous file | base/containers/flat_tree_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698