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_SET_H_ | 5 #ifndef BASE_CONTAINERS_FLAT_SET_H_ |
| 6 #define BASE_CONTAINERS_FLAT_SET_H_ | 6 #define BASE_CONTAINERS_FLAT_SET_H_ |
| 7 | 7 |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <functional> | 9 #include <functional> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 26 // | 26 // |
| 27 // Usage guidance: | 27 // Usage guidance: |
| 28 // Prefer base::flat_set for: | 28 // Prefer base::flat_set for: |
| 29 // * Very small sets, something that is an easy fit for cache. Consider | 29 // * Very small sets, something that is an easy fit for cache. Consider |
| 30 // "very small" to be under a 100 32bit integers. | 30 // "very small" to be under a 100 32bit integers. |
| 31 // * Sets that are built once (using flat_set::flat_set(first, last)). Consider | 31 // * Sets that are built once (using flat_set::flat_set(first, last)). Consider |
| 32 // collecting all data in a vector and then building flat_set out of it. | 32 // collecting all data in a vector and then building flat_set out of it. |
| 33 // TODO(dyaroshev): improve the interface to better support this pattern | 33 // TODO(dyaroshev): improve the interface to better support this pattern |
| 34 // (crbug.com/682254). | 34 // (crbug.com/682254). |
| 35 // * Sets where mutating happens in big bulks: use erase(std::remove()) idiom | 35 // * Sets where mutating happens in big bulks: use erase(std::remove()) idiom |
| 36 // for erasing many elements. Insertion is harder - consider set operations | 36 // for erasing many elements. If possible - use supplied for flat_set |
| 37 // or building a new vector. Set operations can be slow if one of the sets | 37 // overload of base::EraseIf. Insertion is harder - consider set operations |
|
dyaroshev
2017/03/03 23:16:54
I'm not sure how to formulate this thought.
Peter Kasting
2017/03/03 23:27:30
"Sets where mutating happens in bulk operations: t
dyaroshev
2017/03/04 00:32:49
Done.
| |
| 38 // is considerably bigger. Also be aware that beating performance of | 38 // or building a new vector. Set operations can be slow if one of the sets is |
| 39 // sort + unique (implementation of flat_set's constructor) is hard, clever | 39 // considerably bigger. Also be aware that beating performance of sort + |
| 40 // merge of many sets might not win. Generally avoid inserting into flat set | 40 // unique (implementation of flat_set's constructor) is hard, clever merge of |
| 41 // without benchmarks. | 41 // many sets might not win. Generally avoid inserting into flat set without |
| 42 // benchmarks. | |
| 42 // * Copying and iterating. | 43 // * Copying and iterating. |
| 43 // * Set operations (union/intersect etc). | 44 // * Set operations (union/intersect etc). |
| 44 // | 45 // |
| 45 // Prefer to build a new flat_set from a std::vector (or similar) instead of | 46 // Prefer to build a new flat_set from a std::vector (or similar) instead of |
| 46 // calling insert() repeatedly, which would have O(size^2) complexity. | 47 // calling insert() repeatedly, which would have O(size^2) complexity. |
| 47 // | 48 // |
| 48 // TODO(dyaroshev): develop standalone benchmarks to find performance boundaries | 49 // TODO(dyaroshev): develop standalone benchmarks to find performance boundaries |
| 49 // for different types of sets crbug.com/682215. | 50 // for different types of sets crbug.com/682215. |
| 50 // | 51 // |
| 51 // If you do write a benchmark that significantly depends on using sets please | 52 // If you do write a benchmark that significantly depends on using sets please |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 209 iterator emplace_hint(const_iterator position_hint, Args&&... args); | 210 iterator emplace_hint(const_iterator position_hint, Args&&... args); |
| 210 | 211 |
| 211 // -------------------------------------------------------------------------- | 212 // -------------------------------------------------------------------------- |
| 212 // Erase operations. | 213 // Erase operations. |
| 213 // | 214 // |
| 214 // Assume that every operation invalidates iterators and references. | 215 // Assume that every operation invalidates iterators and references. |
| 215 // | 216 // |
| 216 // erase(position), erase(first, last) can take O(size). | 217 // erase(position), erase(first, last) can take O(size). |
| 217 // erase(key) may take O(size) + O(log(size)). | 218 // erase(key) may take O(size) + O(log(size)). |
| 218 // | 219 // |
| 219 // Prefer the erase(std::remove(), end()) idiom for deleting multiple | 220 // Prefer the erase(std::remove(), end()) idiom for deleting multiple |
|
danakj
2017/03/03 23:51:22
Like peter said above just say to use base::EraseI
dyaroshev
2017/03/04 00:32:49
Slightly extended it. Is it ok?
| |
| 220 // elements. | 221 // elements. If possible use supplied base::EraseIf overload. |
| 222 // | |
| 221 | 223 |
| 222 iterator erase(const_iterator position); | 224 iterator erase(const_iterator position); |
| 223 iterator erase(const_iterator first, const_iterator last); | 225 iterator erase(const_iterator first, const_iterator last); |
| 224 size_type erase(const key_type& key); | 226 size_type erase(const key_type& key); |
| 225 | 227 |
| 226 // -------------------------------------------------------------------------- | 228 // -------------------------------------------------------------------------- |
| 227 // Comparators. | 229 // Comparators. |
| 228 | 230 |
| 229 key_compare key_comp() const; | 231 key_compare key_comp() const; |
| 230 value_compare value_comp() const; | 232 value_compare value_comp() const; |
| (...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 650 } | 652 } |
| 651 | 653 |
| 652 // ---------------------------------------------------------------------------- | 654 // ---------------------------------------------------------------------------- |
| 653 // General operations. | 655 // General operations. |
| 654 | 656 |
| 655 template <class Key, class Compare> | 657 template <class Key, class Compare> |
| 656 void flat_set<Key, Compare>::swap(flat_set& other) { | 658 void flat_set<Key, Compare>::swap(flat_set& other) { |
| 657 std::swap(impl_, other.impl_); | 659 std::swap(impl_, other.impl_); |
| 658 } | 660 } |
| 659 | 661 |
| 662 // ---------------------------------------------------------------------------- | |
| 663 // Free functions. | |
| 664 | |
| 665 // Erases all elements that match predicate. It has O(size) complexity. | |
| 666 | |
|
danakj
2017/03/03 23:51:22
no whitespace here
dyaroshev
2017/03/04 00:32:49
Done.
| |
| 667 template <typename Key, typename Compare, typename Predicate> | |
| 668 void EraseIf(base::flat_set<Key, Compare>& container, Predicate pred) { | |
| 669 container.erase( | |
| 670 std::remove_if(std::begin(container), std::end(container), pred), | |
|
danakj
2017/03/03 23:51:22
it's a bit less verbose to use container.begin() a
dyaroshev
2017/03/04 00:32:49
Done.
| |
| 671 std::end(container)); | |
| 672 } | |
| 673 | |
| 660 } // namespace base | 674 } // namespace base |
| 661 | 675 |
| 662 #endif // BASE_CONTAINERS_FLAT_SET_H_ | 676 #endif // BASE_CONTAINERS_FLAT_SET_H_ |
| OLD | NEW |