| 6 | #define BASE_CONTAINERS_FLAT_MAP_H_ |
| 7 |
| 8 | #include <functional> |
| 9 | #include <tuple> |
| 10 | #include <utility> |
| 11 |
| 12 | #include "base/containers/flat_tree.h" |
| 13 | #include "base/logging.h" |
| 14 | #include "base/template_util.h" |
| 15 |
| 16 | namespace base { |
| 17 |
| 18 | namespace internal { |
| 19 |
| 20 | // An implementation of the flat_tree GetKeyFromValue template parameter that |
| 21 | // extracts the key as the first element of a pair. |
| 22 | template <class Key, class Mapped> |
| 23 | struct GetKeyFromValuePairFirst { |
| 24 | const Key& operator()(const std::pair<Key, Mapped>& p) const { |
| 25 | return p.first; |
| 26 | } |
| 27 | }; |
| 28 |
| 29 | } // namespace internal |
| 30 |
| 31 | // flat_map is a container with a std::map-like interface that stores its |
| 32 | // contents in a sorted vector. |
| 33 | // |
| 34 | // Please see //base/containers/README.md for an overview of which container |
| 35 | // to select. |
| 36 | // |
| 37 | // PROS |
| 38 | // |
| 39 | // - Good memory locality. |
| 40 | // - Low overhead, especially for smaller maps. |
| 41 | // - Performance is good for more workloads than you might expect (see |
| 42 | // overview link above). |
| 43 | // - Supports C++14 map interface. |
| 44 | // |
| 45 | // CONS |
| 46 | // |
| 47 | // - Inserts and removals are O(n). |
| 48 | // |
| 49 | // IMPORTANT NOTES |
| 50 | // |
| 51 | // - Iterators are invalidated across mutations. |
| 52 | // - If possible, construct a flat_map in one operation by inserting into |
| 53 | // a std::vector and moving that vector into the flat_map constructor. |
| 54 | // |
| 55 | // QUICK REFERENCE |
| 56 | // |
| 57 | // Most of the core functionality is inherited from flat_tree. Please see |
| 58 | // flat_tree.h for more details for most of these functions. As a quick |
| 59 | // reference, the functions available are: |
| 60 | // |
| 61 | // Constructors (inputs need not be sorted): |
| 62 | // flat_map(InputIterator first, InputIterator last, |
| 63 | // const Compare& compare = Compare()); |
| 64 | // flat_map(const flat_map&); |
| 65 | // flat_map(flat_map&&); |
| 66 | // flat_map(const std::vector<value_type>& items, |
| 67 | // const Compare& compare = Compare()); |
| 68 | // flat_map(std::vector<value_type>&& items, |
| 69 | // const Compare& compare = Compare()); // Re-use storage. |
| 70 | // flat_map(std::initializer_list<value_type> ilist, |
| 71 | // const Compare& comp = Compare()); |
| 72 | // |
| 73 | // Assignment functions: |
| 74 | // flat_map& operator=(const flat_map&); |
| 75 | // flat_map& operator=(flat_map&&); |
| 76 | // flat_map& operator=(initializer_list<value_type>); |
| 77 | // |
| 78 | // Memory management functions: |
| 79 | // void reserve(size_t); |
| 80 | // size_t capacity() const; |
| 81 | // void shrink_to_fit(); |
| 82 | // |
| 83 | // Size management functions: |
| 84 | // void clear(); |
| 85 | // size_t size() const; |
| 86 | // size_t max_size() const; |
| 87 | // bool empty() const; |
| 88 | // |
| 89 | // Iterator functions: |
| 90 | // iterator begin(); |
| 91 | // const_iterator begin() const; |
| 92 | // const_iterator cbegin() const; |
| 93 | // iterator end(); |
| 94 | // const_iterator end() const; |
| 95 | // const_iterator cend() const; |
| 96 | // reverse_iterator rbegin(); |
| 97 | // const reverse_iterator rbegin() const; |
| 98 | // const_reverse_iterator crbegin() const; |
| 99 | // reverse_iterator rend(); |
| 100 | // const_reverse_iterator rend() const; |
| 101 | // const_reverse_iterator crend() const; |
| 102 | // |
| 103 | // Insert and accessor functions: |
| 104 | // mapped_type& operator[](const key_type&); |
| 105 | // mapped_type& operator[](key_type&&); |
| 106 | // mapped_type& at(const K&); |
| 107 | // const mapped_type& at(const K&) const; |
| 108 | // pair<iterator, bool> insert(const value_type&); |
| 109 | // pair<iterator, bool> insert(value_type&&); |
| 110 | // iterator insert(const_iterator hint, const value_type&); |
| 111 | // iterator insert(const_iterator hint, value_type&&); |
| 112 | // void insert(InputIterator first, InputIterator last); |
| 113 | // pair<iterator, bool> insert_or_assign(K&&, M&&); |
| 114 | // iterator insert_or_assign(const_iterator hint, K&&, M&&); |
| 115 | // pair<iterator, bool> emplace(Args&&...); |
| 116 | // iterator emplace_hint(const_iterator, Args&&...); |
| 117 | // pair<iterator, bool> try_emplace(K&&, Args&&...); |
| 118 | // iterator try_emplace(const_iterator hint, K&&, Args&&...); |
| 119 |
| 120 | // Underlying type functions: |
| 121 | // underlying_type extract() &&; |
| 122 | // void replace(underlying_type&&); |
| 123 | // |
| 124 | // Erase functions: |
| 125 | // iterator erase(iterator); |
| 126 | // iterator erase(const_iterator); |
| 127 | // iterator erase(const_iterator first, const_iterator& last); |
| 128 | // template <class K> size_t erase(const K& key); |
| 129 | // |
| 130 | // Comparators (see std::map documentation). |
| 131 | // key_compare key_comp() const; |
| 132 | // value_compare value_comp() const; |
| 133 | // |
| 134 | // Search functions: |
| 135 | // template <typename K> size_t count(const K&) const; |
| 136 | // template <typename K> iterator find(const K&); |
| 137 | // template <typename K> const_iterator find(const K&) const; |
| 138 | // template <typename K> bool contains(const K&) const; |
| 139 | // template <typename K> pair<iterator, iterator> equal_range(const K&); |
| 140 | // template <typename K> iterator lower_bound(const K&); |
| 141 | // template <typename K> const_iterator lower_bound(const K&) const; |
| 142 | // template <typename K> iterator upper_bound(const K&); |
| 143 | // template <typename K> const_iterator upper_bound(const K&) const; |
| 144 | // |
| 145 | // General functions: |
| 146 | // void swap(flat_map&&); |
| 147 | // |
| 148 | // Non-member operators: |
| 149 | // bool operator==(const flat_map&, const flat_map); |
| 150 | // bool operator!=(const flat_map&, const flat_map); |
| 151 | // bool operator<(const flat_map&, const flat_map); |
| 152 | // bool operator>(const flat_map&, const flat_map); |
| 153 | // bool operator>=(const flat_map&, const flat_map); |
| 154 | // bool operator<=(const flat_map&, const flat_map); |
| 155 | // |
| 156 | template <class Key, class Mapped, class Compare = std::less<>> |
| 157 | class flat_map : public ::base::internal::flat_tree< |
| 158 | Key, |
| 159 | std::pair<Key, Mapped>, |
| 160 | ::base::internal::GetKeyFromValuePairFirst<Key, Mapped>, |
| 161 | Compare> { |
| 162 | private: |
| 163 | using tree = typename ::base::internal::flat_tree< |
| 164 | Key, |
| 165 | std::pair<Key, Mapped>, |
| 166 | ::base::internal::GetKeyFromValuePairFirst<Key, Mapped>, |
| 167 | Compare>; |
| 168 | using underlying_type = typename tree::underlying_type; |
| 169 |
| 170 | public: |
| 171 | using key_type = typename tree::key_type; |
| 172 | using mapped_type = Mapped; |
| 173 | using value_type = typename tree::value_type; |
| 174 | using iterator = typename tree::iterator; |
| 175 | using const_iterator = typename tree::const_iterator; |
| 176 |
| 177 | // -------------------------------------------------------------------------- |
| 178 | // Lifetime and assignments. |
| 179 | // |
| 180 | // Note: we could do away with these constructors, destructor and assignment |
| 181 | // operator overloads by inheriting |tree|'s, but this breaks the GCC build |
| 182 | // due to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84782 (see |
| 183 | // https://crbug.com/837221). |
| 184 |
| 185 | flat_map() = default; |
| 186 | explicit flat_map(const Compare& comp); |
| 187 |
| 188 | template <class InputIterator> |
| 189 | flat_map(InputIterator first, |
| 190 | InputIterator last, |
| 191 | const Compare& comp = Compare()); |
| 192 |
| 193 | flat_map(const flat_map&) = default; |
| 194 | flat_map(flat_map&&) noexcept = default; |
| 195 |
| 196 | flat_map(const underlying_type& items, const Compare& comp = Compare()); |
| 197 | flat_map(underlying_type&& items, const Compare& comp = Compare()); |
| 198 |
| 199 | flat_map(std::initializer_list<value_type> ilist, |
| 200 | const Compare& comp = Compare()); |
| 201 |
| 202 | ~flat_map() = default; |
| 203 |
| 204 | flat_map& operator=(const flat_map&) = default; |
| 205 | flat_map& operator=(flat_map&&) = default; |
| 206 | // Takes the first if there are duplicates in the initializer list. |
| 207 | flat_map& operator=(std::initializer_list<value_type> ilist); |
| 208 |
| 209 | // Out-of-bound calls to at() will CHECK. |
| 210 | template <class K> |
| 211 | mapped_type& at(const K& key); |
| 212 | template <class K> |
| 213 | const mapped_type& at(const K& key) const; |
| 214 |
| 215 | // -------------------------------------------------------------------------- |
| 216 | // Map-specific insert operations. |
| 217 | // |
| 218 | // Normal insert() functions are inherited from flat_tree. |
| 219 | // |
| 220 | // Assume that every operation invalidates iterators and references. |
| 221 | // Insertion of one element can take O(size). |
| 222 |
| 223 | mapped_type& operator[](const key_type& key); |
| 224 | mapped_type& operator[](key_type&& key); |
| 225 |
| 226 | template <class K, class M> |
| 227 | std::pair<iterator, bool> insert_or_assign(K&& key, M&& obj); |
| 228 | template <class K, class M> |
| 229 | iterator insert_or_assign(const_iterator hint, K&& key, M&& obj); |
| 230 |
| 231 | template <class K, class... Args> |
| 232 | std::enable_if_t<std::is_constructible<key_type, K&&>::value, |
| 233 | std::pair<iterator, bool>> |
| 234 | try_emplace(K&& key, Args&&... args); |
| 235 |
| 236 | template <class K, class... Args> |
| 237 | std::enable_if_t<std::is_constructible<key_type, K&&>::value, iterator> |
| 238 | try_emplace(const_iterator hint, K&& key, Args&&... args); |
| 239 |
| 240 | // -------------------------------------------------------------------------- |
| 241 | // General operations. |
| 242 | // |
| 243 | // Assume that swap invalidates iterators and references. |
| 244 |
| 245 | void swap(flat_map& other) noexcept; |
| 246 |
| 247 | friend void swap(flat_map& lhs, flat_map& rhs) noexcept { lhs.swap(rhs); } |
| 248 | }; |
| 249 |
| 250 | // ---------------------------------------------------------------------------- |
| 251 | // Lifetime. |
| 252 |
| 253 | template <class Key, class Mapped, class Compare> |
| 254 | flat_map<Key, Mapped, Compare>::flat_map(const Compare& comp) : tree(comp) {} |
| 255 |
| 256 | template <class Key, class Mapped, class Compare> |
| 257 | template <class InputIterator> |
| 258 | flat_map<Key, Mapped, Compare>::flat_map(InputIterator first, |
| 259 | InputIterator last, |
| 260 | const Compare& comp) |
| 261 | : tree(first, last, comp) {} |
| 262 |
| 263 | template <class Key, class Mapped, class Compare> |
| 264 | flat_map<Key, Mapped, Compare>::flat_map(const underlying_type& items, |
| 265 | const Compare& comp) |
| 266 | : tree(items, comp) {} |
| 267 |
| 268 | template <class Key, class Mapped, class Compare> |
| 269 | flat_map<Key, Mapped, Compare>::flat_map(underlying_type&& items, |
| 270 | const Compare& comp) |
| 271 | : tree(std::move(items), comp) {} |
| 272 |
| 273 | template <class Key, class Mapped, class Compare> |
| 274 | flat_map<Key, Mapped, Compare>::flat_map( |
| 275 | std::initializer_list<value_type> ilist, |
| 276 | const Compare& comp) |
| 277 | : flat_map(std::begin(ilist), std::end(ilist), comp) {} |
| 278 |
| 279 | // ---------------------------------------------------------------------------- |
| 280 | // Assignments. |
| 281 |
| 282 | template <class Key, class Mapped, class Compare> |
| 283 | auto flat_map<Key, Mapped, Compare>::operator=( |
| 284 | std::initializer_list<value_type> ilist) -> flat_map& { |
| 285 | // When https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84782 gets fixed, we |
| 286 | // need to remember to inherit tree::operator= to prevent |
| 287 | // flat_map<...> x; |
| 288 | // x = {...}; |
| 289 | // from first creating a flat_map and then move assigning it. This most |
| 290 | // likely would be optimized away but still affects our debug builds. |
| 291 | tree::operator=(ilist); |
| 292 | return *this; |
| 293 | } |
| 294 |
| 295 | // ---------------------------------------------------------------------------- |
| 296 | // Lookups. |
| 297 |
| 298 | template <class Key, class Mapped, class Compare> |
| 299 | template <class K> |
| 300 | auto flat_map<Key, Mapped, Compare>::at(const K& key) -> mapped_type& { |
| 301 | iterator found = tree::find(key); |
| 302 | CHECK(found != tree::end()); |
| 303 | return found->second; |
| 304 | } |
| 305 |
| 306 | template <class Key, class Mapped, class Compare> |
| 307 | template <class K> |
| 308 | auto flat_map<Key, Mapped, Compare>::at(const K& key) const |
| 309 | -> const mapped_type& { |
| 310 | const_iterator found = tree::find(key); |
| 311 | CHECK(found != tree::cend()); |
| 312 | return found->second; |
| 313 | } |
| 314 |
| 315 | // ---------------------------------------------------------------------------- |
| 316 | // Insert operations. |
| 317 |
| 318 | template <class Key, class Mapped, class Compare> |
| 319 | auto flat_map<Key, Mapped, Compare>::operator[](const key_type& key) |
| 320 | -> mapped_type& { |
| 321 | iterator found = tree::lower_bound(key); |
| 322 | if (found == tree::end() || tree::key_comp()(key, found->first)) |
| 323 | found = tree::unsafe_emplace(found, key, mapped_type()); |
| 324 | return found->second; |
| 325 | } |
| 326 |
| 327 | template <class Key, class Mapped, class Compare> |
| 328 | auto flat_map<Key, Mapped, Compare>::operator[](key_type&& key) |
| 329 | -> mapped_type& { |
| 330 | iterator found = tree::lower_bound(key); |
| 331 | if (found == tree::end() || tree::key_comp()(key, found->first)) |
| 332 | found = tree::unsafe_emplace(found, std::move(key), mapped_type()); |
| 333 | return found->second; |
| 334 | } |
| 335 |
| 336 | template <class Key, class Mapped, class Compare> |
| 337 | template <class K, class M> |
| 338 | auto flat_map<Key, Mapped, Compare>::insert_or_assign(K&& key, M&& obj) |
| 339 | -> std::pair<iterator, bool> { |
| 340 | auto result = |
| 341 | tree::emplace_key_args(key, std::forward<K>(key), std::forward<M>(obj)); |
| 342 | if (!result.second) |
| 343 | result.first->second = std::forward<M>(obj); |
| 344 | return result; |
| 345 | } |
| 346 |
| 347 | template <class Key, class Mapped, class Compare> |
| 348 | template <class K, class M> |
| 349 | auto flat_map<Key, Mapped, Compare>::insert_or_assign(const_iterator hint, |
| 350 | K&& key, |
| 351 | M&& obj) -> iterator { |
| 352 | auto result = tree::emplace_hint_key_args(hint, key, std::forward<K>(key), |
| 353 | std::forward<M>(obj)); |
| 354 | if (!result.second) |
| 355 | result.first->second = std::forward<M>(obj); |
| 356 | return result.first; |
| 357 | } |
| 358 |
| 359 | template <class Key, class Mapped, class Compare> |
| 360 | template <class K, class... Args> |
| 361 | auto flat_map<Key, Mapped, Compare>::try_emplace(K&& key, Args&&... args) |
| 362 | -> std::enable_if_t<std::is_constructible<key_type, K&&>::value, |
| 363 | std::pair<iterator, bool>> { |
| 364 | return tree::emplace_key_args( |
| 365 | key, std::piecewise_construct, |
| 366 | std::forward_as_tuple(std::forward<K>(key)), |
| 367 | std::forward_as_tuple(std::forward<Args>(args)...)); |
| 368 | } |
| 369 |
| 370 | template <class Key, class Mapped, class Compare> |
| 371 | template <class K, class... Args> |
| 372 | auto flat_map<Key, Mapped, Compare>::try_emplace(const_iterator hint, |
| 373 | K&& key, |
| 374 | Args&&... args) |
| 375 | -> std::enable_if_t<std::is_constructible<key_type, K&&>::value, iterator> { |
| 376 | return tree::emplace_hint_key_args( |
| 377 | hint, key, std::piecewise_construct, |
| 378 | std::forward_as_tuple(std::forward<K>(key)), |
| 379 | std::forward_as_tuple(std::forward<Args>(args)...)) |
| 380 | .first; |
| 381 | } |
| 382 |
| 383 | // ---------------------------------------------------------------------------- |
| 384 | // General operations. |
| 385 |
| 386 | template <class Key, class Mapped, class Compare> |
| 387 | void flat_map<Key, Mapped, Compare>::swap(flat_map& other) noexcept { |
| 388 | tree::swap(other); |
| 389 | } |
| 390 |
| 391 | } // namespace base |
| 392 |
| 393 | #endif // BASE_CONTAINERS_FLAT_MAP_H_ |
| 394 |