| 6 | #define BASE_CONTAINERS_FLAT_SET_H_ |
| 7 |
| 8 | #include <functional> |
| 9 |
| 10 | #include "base/containers/flat_tree.h" |
| 11 | #include "base/template_util.h" |
| 12 |
| 13 | namespace base { |
| 14 |
| 15 | // flat_set is a container with a std::set-like interface that stores its |
| 16 | // contents in a sorted vector. |
| 17 | // |
| 18 | // Please see //base/containers/README.md for an overview of which container |
| 19 | // to select. |
| 20 | // |
| 21 | // PROS |
| 22 | // |
| 23 | // - Good memory locality. |
| 24 | // - Low overhead, especially for smaller sets. |
| 25 | // - Performance is good for more workloads than you might expect (see |
| 26 | // overview link above). |
| 27 | // - Supports C++14 set interface. |
| 28 | // |
| 29 | // CONS |
| 30 | // |
| 31 | // - Inserts and removals are O(n). |
| 32 | // |
| 33 | // IMPORTANT NOTES |
| 34 | // |
| 35 | // - Iterators are invalidated across mutations. |
| 36 | // - If possible, construct a flat_set in one operation by inserting into |
| 37 | // a std::vector and moving that vector into the flat_set constructor. |
| 38 | // - For multiple removals use base::EraseIf() which is O(n) rather than |
| 39 | // O(n * removed_items). |
| 40 | // |
| 41 | // QUICK REFERENCE |
| 42 | // |
| 43 | // Most of the core functionality is inherited from flat_tree. Please see |
| 44 | // flat_tree.h for more details for most of these functions. As a quick |
| 45 | // reference, the functions available are: |
| 46 | // |
| 47 | // Constructors (inputs need not be sorted): |
| 48 | // flat_set(InputIterator first, InputIterator last, |
| 49 | // const Compare& compare = Compare()); |
| 50 | // flat_set(const flat_set&); |
| 51 | // flat_set(flat_set&&); |
| 52 | // flat_set(const std::vector<Key>& items, |
| 53 | // const Compare& compare = Compare()); |
| 54 | // flat_set(std::vector<Key>&& items, |
| 55 | // const Compare& compare = Compare()); // Re-use storage. |
| 56 | // flat_set(std::initializer_list<value_type> ilist, |
| 57 | // const Compare& comp = Compare()); |
| 58 | // |
| 59 | // Assignment functions: |
| 60 | // flat_set& operator=(const flat_set&); |
| 61 | // flat_set& operator=(flat_set&&); |
| 62 | // flat_set& operator=(initializer_list<Key>); |
| 63 | // |
| 64 | // Memory management functions: |
| 65 | // void reserve(size_t); |
| 66 | // size_t capacity() const; |
| 67 | // void shrink_to_fit(); |
| 68 | // |
| 69 | // Size management functions: |
| 70 | // void clear(); |
| 71 | // size_t size() const; |
| 72 | // size_t max_size() const; |
| 73 | // bool empty() const; |
| 74 | // |
| 75 | // Iterator functions: |
| 76 | // iterator begin(); |
| 77 | // const_iterator begin() const; |
| 78 | // const_iterator cbegin() const; |
| 79 | // iterator end(); |
| 80 | // const_iterator end() const; |
| 81 | // const_iterator cend() const; |
| 82 | // reverse_iterator rbegin(); |
| 83 | // const reverse_iterator rbegin() const; |
| 84 | // const_reverse_iterator crbegin() const; |
| 85 | // reverse_iterator rend(); |
| 86 | // const_reverse_iterator rend() const; |
| 87 | // const_reverse_iterator crend() const; |
| 88 | // |
| 89 | // Insert and accessor functions: |
| 90 | // pair<iterator, bool> insert(const key_type&); |
| 91 | // pair<iterator, bool> insert(key_type&&); |
| 92 | // void insert(InputIterator first, InputIterator last); |
| 93 | // iterator insert(const_iterator hint, const key_type&); |
| 94 | // iterator insert(const_iterator hint, key_type&&); |
| 95 | // pair<iterator, bool> emplace(Args&&...); |
| 96 | // iterator emplace_hint(const_iterator, Args&&...); |
| 97 | // |
| 98 | // Underlying type functions: |
| 99 | // underlying_type extract() &&; |
| 100 | // void replace(underlying_type&&); |
| 101 | // |
| 102 | // Erase functions: |
| 103 | // iterator erase(iterator); |
| 104 | // iterator erase(const_iterator); |
| 105 | // iterator erase(const_iterator first, const_iterator& last); |
| 106 | // template <typename K> size_t erase(const K& key); |
| 107 | // |
| 108 | // Comparators (see std::set documentation). |
| 109 | // key_compare key_comp() const; |
| 110 | // value_compare value_comp() const; |
| 111 | // |
| 112 | // Search functions: |
| 113 | // template <typename K> size_t count(const K&) const; |
| 114 | // template <typename K> iterator find(const K&); |
| 115 | // template <typename K> const_iterator find(const K&) const; |
| 116 | // template <typename K> bool contains(const K&) const; |
| 117 | // template <typename K> pair<iterator, iterator> equal_range(K&); |
| 118 | // template <typename K> iterator lower_bound(const K&); |
| 119 | // template <typename K> const_iterator lower_bound(const K&) const; |
| 120 | // template <typename K> iterator upper_bound(const K&); |
| 121 | // template <typename K> const_iterator upper_bound(const K&) const; |
| 122 | // |
| 123 | // General functions: |
| 124 | // void swap(flat_set&&); |
| 125 | // |
| 126 | // Non-member operators: |
| 127 | // bool operator==(const flat_set&, const flat_set); |
| 128 | // bool operator!=(const flat_set&, const flat_set); |
| 129 | // bool operator<(const flat_set&, const flat_set); |
| 130 | // bool operator>(const flat_set&, const flat_set); |
| 131 | // bool operator>=(const flat_set&, const flat_set); |
| 132 | // bool operator<=(const flat_set&, const flat_set); |
| 133 | // |
| 134 | template <class Key, class Compare = std::less<>> |
| 135 | using flat_set = typename ::base::internal::flat_tree< |
| 136 | Key, |
| 137 | Key, |
| 138 | ::base::internal::GetKeyFromValueIdentity<Key>, |
| 139 | Compare>; |
| 140 |
| 141 | } // namespace base |
| 142 |
| 143 | #endif // BASE_CONTAINERS_FLAT_SET_H_ |
| 144 |