| 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 14 matching lines...) Expand all Loading... |
| 25 // be very fast for set operations and for small number of elements. | 25 // be very fast for set operations and for small number of elements. |
| 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: to erase multiple elements, use |
| 36 // for erasing many elements. Insertion is harder - consider set operations | 36 // base::EraseIf() rather than repeated single-element removal. Insertion is |
| 37 // or building a new vector. Set operations can be slow if one of the sets | 37 // harder - consider set operations or building a new vector. Set operations |
| 38 // is considerably bigger. Also be aware that beating performance of | 38 // can be slow if one of the sets is considerably bigger. Also be aware that |
| 39 // sort + unique (implementation of flat_set's constructor) is hard, clever | 39 // beating performance of sort + unique (implementation of flat_set's |
| 40 // merge of many sets might not win. Generally avoid inserting into flat set | 40 // constructor) is hard, clever merge of many sets might not win. Generally |
| 41 // without benchmarks. | 41 // avoid inserting into flat set without benchmarks. |
| 42 // * Copying and iterating. | 42 // * Copying and iterating. |
| 43 // * Set operations (union/intersect etc). | 43 // * Set operations (union/intersect etc). |
| 44 // | 44 // |
| 45 // Prefer to build a new flat_set from a std::vector (or similar) instead of | 45 // 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. | 46 // calling insert() repeatedly, which would have O(size^2) complexity. |
| 47 // | 47 // |
| 48 // TODO(dyaroshev): develop standalone benchmarks to find performance boundaries | 48 // TODO(dyaroshev): develop standalone benchmarks to find performance boundaries |
| 49 // for different types of sets crbug.com/682215. | 49 // for different types of sets crbug.com/682215. |
| 50 // | 50 // |
| 51 // If you do write a benchmark that significantly depends on using sets please | 51 // 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); | 209 iterator emplace_hint(const_iterator position_hint, Args&&... args); |
| 210 | 210 |
| 211 // -------------------------------------------------------------------------- | 211 // -------------------------------------------------------------------------- |
| 212 // Erase operations. | 212 // Erase operations. |
| 213 // | 213 // |
| 214 // Assume that every operation invalidates iterators and references. | 214 // Assume that every operation invalidates iterators and references. |
| 215 // | 215 // |
| 216 // erase(position), erase(first, last) can take O(size). | 216 // erase(position), erase(first, last) can take O(size). |
| 217 // erase(key) may take O(size) + O(log(size)). | 217 // erase(key) may take O(size) + O(log(size)). |
| 218 // | 218 // |
| 219 // Prefer the erase(std::remove(), end()) idiom for deleting multiple | 219 // Prefer base::EraseIf() or some other variation on erase(remove(), end()) |
| 220 // elements. | 220 // idiom when deleting multiple non-consecutive elements. |
| 221 | 221 |
| 222 iterator erase(const_iterator position); | 222 iterator erase(const_iterator position); |
| 223 iterator erase(const_iterator first, const_iterator last); | 223 iterator erase(const_iterator first, const_iterator last); |
| 224 size_type erase(const key_type& key); | 224 size_type erase(const key_type& key); |
| 225 | 225 |
| 226 // -------------------------------------------------------------------------- | 226 // -------------------------------------------------------------------------- |
| 227 // Comparators. | 227 // Comparators. |
| 228 | 228 |
| 229 key_compare key_comp() const; | 229 key_compare key_comp() const; |
| 230 value_compare value_comp() const; | 230 value_compare value_comp() const; |
| (...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 650 } | 650 } |
| 651 | 651 |
| 652 // ---------------------------------------------------------------------------- | 652 // ---------------------------------------------------------------------------- |
| 653 // General operations. | 653 // General operations. |
| 654 | 654 |
| 655 template <class Key, class Compare> | 655 template <class Key, class Compare> |
| 656 void flat_set<Key, Compare>::swap(flat_set& other) { | 656 void flat_set<Key, Compare>::swap(flat_set& other) { |
| 657 std::swap(impl_, other.impl_); | 657 std::swap(impl_, other.impl_); |
| 658 } | 658 } |
| 659 | 659 |
| 660 // ---------------------------------------------------------------------------- |
| 661 // Free functions. |
| 662 |
| 663 // Erases all elements that match predicate. It has O(size) complexity. |
| 664 template <typename Key, typename Compare, typename Predicate> |
| 665 void EraseIf(base::flat_set<Key, Compare>& container, Predicate pred) { |
| 666 container.erase(std::remove_if(container.begin(), container.end(), pred), |
| 667 container.end()); |
| 668 } |
| 669 |
| 660 } // namespace base | 670 } // namespace base |
| 661 | 671 |
| 662 #endif // BASE_CONTAINERS_FLAT_SET_H_ | 672 #endif // BASE_CONTAINERS_FLAT_SET_H_ |
| OLD | NEW |