1 // Copyright 2018 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // -----------------------------------------------------------------------------
16 // File: flat_hash_map.h
17 // -----------------------------------------------------------------------------
18 //
19 // An `absl::flat_hash_map<K, V>` is an unordered associative container of
20 // unique keys and associated values designed to be a more efficient replacement
21 // for `std::unordered_map`. Like `unordered_map`, search, insertion, and
22 // deletion of map elements can be done as an `O(1)` operation. However,
23 // `flat_hash_map` (and other unordered associative containers known as the
24 // collection of Abseil "Swiss tables") contain other optimizations that result
25 // in both memory and computation advantages.
26 //
27 // In most cases, your default choice for a hash map should be a map of type
28 // `flat_hash_map`.
29
30 #ifndef ABSL_CONTAINER_FLAT_HASH_MAP_H_
31 #define ABSL_CONTAINER_FLAT_HASH_MAP_H_
32
33 #include <cstddef>
34 #include <new>
35 #include <type_traits>
36 #include <utility>
37
38 #include "absl/algorithm/container.h"
39 #include "absl/base/macros.h"
40 #include "absl/container/internal/container_memory.h"
41 #include "absl/container/internal/hash_function_defaults.h" // IWYU pragma: export
42 #include "absl/container/internal/raw_hash_map.h" // IWYU pragma: export
43 #include "absl/memory/memory.h"
44
45 namespace absl {
46 ABSL_NAMESPACE_BEGIN
47 namespace container_internal {
48 template <class K, class V>
49 struct FlatHashMapPolicy;
50 } // namespace container_internal
51
52 // -----------------------------------------------------------------------------
53 // absl::flat_hash_map
54 // -----------------------------------------------------------------------------
55 //
56 // An `absl::flat_hash_map<K, V>` is an unordered associative container which
57 // has been optimized for both speed and memory footprint in most common use
58 // cases. Its interface is similar to that of `std::unordered_map<K, V>` with
59 // the following notable differences:
60 //
61 // * Requires keys that are CopyConstructible
62 // * Requires values that are MoveConstructible
63 // * Supports heterogeneous lookup, through `find()`, `operator[]()` and
64 // `insert()`, provided that the map is provided a compatible heterogeneous
65 // hashing function and equality operator.
66 // * Invalidates any references and pointers to elements within the table after
67 // `rehash()` and when the table is moved.
68 // * Contains a `capacity()` member function indicating the number of element
69 // slots (open, deleted, and empty) within the hash map.
70 // * Returns `void` from the `erase(iterator)` overload.
71 //
72 // By default, `flat_hash_map` uses the `absl::Hash` hashing framework.
73 // All fundamental and Abseil types that support the `absl::Hash` framework have
74 // a compatible equality operator for comparing insertions into `flat_hash_map`.
75 // If your type is not yet supported by the `absl::Hash` framework, see
76 // absl/hash/hash.h for information on extending Abseil hashing to user-defined
77 // types.
78 //
79 // Using `absl::flat_hash_map` at interface boundaries in dynamically loaded
80 // libraries (e.g. .dll, .so) is unsupported due to way `absl::Hash` values may
81 // be randomized across dynamically loaded libraries.
82 //
83 // NOTE: A `flat_hash_map` stores its value types directly inside its
84 // implementation array to avoid memory indirection. Because a `flat_hash_map`
85 // is designed to move data when rehashed, map values will not retain pointer
86 // stability. If you require pointer stability, or if your values are large,
87 // consider using `absl::flat_hash_map<Key, std::unique_ptr<Value>>` instead.
88 // If your types are not moveable or you require pointer stability for keys,
89 // consider `absl::node_hash_map`.
90 //
91 // Example:
92 //
93 // // Create a flat hash map of three strings (that map to strings)
94 // absl::flat_hash_map<std::string, std::string> ducks =
95 // {{"a", "huey"}, {"b", "dewey"}, {"c", "louie"}};
96 //
97 // // Insert a new element into the flat hash map
98 // ducks.insert({"d", "donald"});
99 //
100 // // Force a rehash of the flat hash map
101 // ducks.rehash(0);
102 //
103 // // Find the element with the key "b"
104 // std::string search_key = "b";
105 // auto result = ducks.find(search_key);
106 // if (result != ducks.end()) {
107 // std::cout << "Result: " << result->second << std::endl;
108 // }
109 template <class K, class V,
110 class Hash = absl::container_internal::hash_default_hash<K>,
111 class Eq = absl::container_internal::hash_default_eq<K>,
112 class Allocator = std::allocator<std::pair<const K, V>>>
113 class flat_hash_map : public absl::container_internal::raw_hash_map<
114 absl::container_internal::FlatHashMapPolicy<K, V>,
115 Hash, Eq, Allocator> {
116 using Base = typename flat_hash_map::raw_hash_map;
117
118 public:
119 // Constructors and Assignment Operators
120 //
121 // A flat_hash_map supports the same overload set as `std::unordered_map`
122 // for construction and assignment:
123 //
124 // * Default constructor
125 //
126 // // No allocation for the table's elements is made.
127 // absl::flat_hash_map<int, std::string> map1;
128 //
129 // * Initializer List constructor
130 //
131 // absl::flat_hash_map<int, std::string> map2 =
132 // {{1, "huey"}, {2, "dewey"}, {3, "louie"},};
133 //
134 // * Copy constructor
135 //
136 // absl::flat_hash_map<int, std::string> map3(map2);
137 //
138 // * Copy assignment operator
139 //
140 // // Hash functor and Comparator are copied as well
141 // absl::flat_hash_map<int, std::string> map4;
142 // map4 = map3;
143 //
144 // * Move constructor
145 //
146 // // Move is guaranteed efficient
147 // absl::flat_hash_map<int, std::string> map5(std::move(map4));
148 //
149 // * Move assignment operator
150 //
151 // // May be efficient if allocators are compatible
152 // absl::flat_hash_map<int, std::string> map6;
153 // map6 = std::move(map5);
154 //
155 // * Range constructor
156 //
157 // std::vector<std::pair<int, std::string>> v = {{1, "a"}, {2, "b"}};
158 // absl::flat_hash_map<int, std::string> map7(v.begin(), v.end());
flat_hash_map()159 flat_hash_map() {}
160 using Base::Base;
161
162 // flat_hash_map::begin()
163 //
164 // Returns an iterator to the beginning of the `flat_hash_map`.
165 using Base::begin;
166
167 // flat_hash_map::cbegin()
168 //
169 // Returns a const iterator to the beginning of the `flat_hash_map`.
170 using Base::cbegin;
171
172 // flat_hash_map::cend()
173 //
174 // Returns a const iterator to the end of the `flat_hash_map`.
175 using Base::cend;
176
177 // flat_hash_map::end()
178 //
179 // Returns an iterator to the end of the `flat_hash_map`.
180 using Base::end;
181
182 // flat_hash_map::capacity()
183 //
184 // Returns the number of element slots (assigned, deleted, and empty)
185 // available within the `flat_hash_map`.
186 //
187 // NOTE: this member function is particular to `absl::flat_hash_map` and is
188 // not provided in the `std::unordered_map` API.
189 using Base::capacity;
190
191 // flat_hash_map::empty()
192 //
193 // Returns whether or not the `flat_hash_map` is empty.
194 using Base::empty;
195
196 // flat_hash_map::max_size()
197 //
198 // Returns the largest theoretical possible number of elements within a
199 // `flat_hash_map` under current memory constraints. This value can be thought
200 // of the largest value of `std::distance(begin(), end())` for a
201 // `flat_hash_map<K, V>`.
202 using Base::max_size;
203
204 // flat_hash_map::size()
205 //
206 // Returns the number of elements currently within the `flat_hash_map`.
207 using Base::size;
208
209 // flat_hash_map::clear()
210 //
211 // Removes all elements from the `flat_hash_map`. Invalidates any references,
212 // pointers, or iterators referring to contained elements.
213 //
214 // NOTE: this operation may shrink the underlying buffer. To avoid shrinking
215 // the underlying buffer call `erase(begin(), end())`.
216 using Base::clear;
217
218 // flat_hash_map::erase()
219 //
220 // Erases elements within the `flat_hash_map`. Erasing does not trigger a
221 // rehash. Overloads are listed below.
222 //
223 // void erase(const_iterator pos):
224 //
225 // Erases the element at `position` of the `flat_hash_map`, returning
226 // `void`.
227 //
228 // NOTE: returning `void` in this case is different than that of STL
229 // containers in general and `std::unordered_map` in particular (which
230 // return an iterator to the element following the erased element). If that
231 // iterator is needed, simply post increment the iterator:
232 //
233 // map.erase(it++);
234 //
235 // iterator erase(const_iterator first, const_iterator last):
236 //
237 // Erases the elements in the open interval [`first`, `last`), returning an
238 // iterator pointing to `last`. The special case of calling
239 // `erase(begin(), end())` resets the reserved growth such that if
240 // `reserve(N)` has previously been called and there has been no intervening
241 // call to `clear()`, then after calling `erase(begin(), end())`, it is safe
242 // to assume that inserting N elements will not cause a rehash.
243 //
244 // size_type erase(const key_type& key):
245 //
246 // Erases the element with the matching key, if it exists, returning the
247 // number of elements erased (0 or 1).
248 using Base::erase;
249
250 // flat_hash_map::insert()
251 //
252 // Inserts an element of the specified value into the `flat_hash_map`,
253 // returning an iterator pointing to the newly inserted element, provided that
254 // an element with the given key does not already exist. If rehashing occurs
255 // due to the insertion, all iterators are invalidated. Overloads are listed
256 // below.
257 //
258 // std::pair<iterator,bool> insert(const init_type& value):
259 //
260 // Inserts a value into the `flat_hash_map`. Returns a pair consisting of an
261 // iterator to the inserted element (or to the element that prevented the
262 // insertion) and a bool denoting whether the insertion took place.
263 //
264 // std::pair<iterator,bool> insert(T&& value):
265 // std::pair<iterator,bool> insert(init_type&& value):
266 //
267 // Inserts a moveable value into the `flat_hash_map`. Returns a pair
268 // consisting of an iterator to the inserted element (or to the element that
269 // prevented the insertion) and a bool denoting whether the insertion took
270 // place.
271 //
272 // iterator insert(const_iterator hint, const init_type& value):
273 // iterator insert(const_iterator hint, T&& value):
274 // iterator insert(const_iterator hint, init_type&& value);
275 //
276 // Inserts a value, using the position of `hint` as a non-binding suggestion
277 // for where to begin the insertion search. Returns an iterator to the
278 // inserted element, or to the existing element that prevented the
279 // insertion.
280 //
281 // void insert(InputIterator first, InputIterator last):
282 //
283 // Inserts a range of values [`first`, `last`).
284 //
285 // NOTE: Although the STL does not specify which element may be inserted if
286 // multiple keys compare equivalently, for `flat_hash_map` we guarantee the
287 // first match is inserted.
288 //
289 // void insert(std::initializer_list<init_type> ilist):
290 //
291 // Inserts the elements within the initializer list `ilist`.
292 //
293 // NOTE: Although the STL does not specify which element may be inserted if
294 // multiple keys compare equivalently within the initializer list, for
295 // `flat_hash_map` we guarantee the first match is inserted.
296 using Base::insert;
297
298 // flat_hash_map::insert_or_assign()
299 //
300 // Inserts an element of the specified value into the `flat_hash_map` provided
301 // that a value with the given key does not already exist, or replaces it with
302 // the element value if a key for that value already exists, returning an
303 // iterator pointing to the newly inserted element. If rehashing occurs due
304 // to the insertion, all existing iterators are invalidated. Overloads are
305 // listed below.
306 //
307 // pair<iterator, bool> insert_or_assign(const init_type& k, T&& obj):
308 // pair<iterator, bool> insert_or_assign(init_type&& k, T&& obj):
309 //
310 // Inserts/Assigns (or moves) the element of the specified key into the
311 // `flat_hash_map`.
312 //
313 // iterator insert_or_assign(const_iterator hint,
314 // const init_type& k, T&& obj):
315 // iterator insert_or_assign(const_iterator hint, init_type&& k, T&& obj):
316 //
317 // Inserts/Assigns (or moves) the element of the specified key into the
318 // `flat_hash_map` using the position of `hint` as a non-binding suggestion
319 // for where to begin the insertion search.
320 using Base::insert_or_assign;
321
322 // flat_hash_map::emplace()
323 //
324 // Inserts an element of the specified value by constructing it in-place
325 // within the `flat_hash_map`, provided that no element with the given key
326 // already exists.
327 //
328 // The element may be constructed even if there already is an element with the
329 // key in the container, in which case the newly constructed element will be
330 // destroyed immediately. Prefer `try_emplace()` unless your key is not
331 // copyable or moveable.
332 //
333 // If rehashing occurs due to the insertion, all iterators are invalidated.
334 using Base::emplace;
335
336 // flat_hash_map::emplace_hint()
337 //
338 // Inserts an element of the specified value by constructing it in-place
339 // within the `flat_hash_map`, using the position of `hint` as a non-binding
340 // suggestion for where to begin the insertion search, and only inserts
341 // provided that no element with the given key already exists.
342 //
343 // The element may be constructed even if there already is an element with the
344 // key in the container, in which case the newly constructed element will be
345 // destroyed immediately. Prefer `try_emplace()` unless your key is not
346 // copyable or moveable.
347 //
348 // If rehashing occurs due to the insertion, all iterators are invalidated.
349 using Base::emplace_hint;
350
351 // flat_hash_map::try_emplace()
352 //
353 // Inserts an element of the specified value by constructing it in-place
354 // within the `flat_hash_map`, provided that no element with the given key
355 // already exists. Unlike `emplace()`, if an element with the given key
356 // already exists, we guarantee that no element is constructed.
357 //
358 // If rehashing occurs due to the insertion, all iterators are invalidated.
359 // Overloads are listed below.
360 //
361 // pair<iterator, bool> try_emplace(const key_type& k, Args&&... args):
362 // pair<iterator, bool> try_emplace(key_type&& k, Args&&... args):
363 //
364 // Inserts (via copy or move) the element of the specified key into the
365 // `flat_hash_map`.
366 //
367 // iterator try_emplace(const_iterator hint,
368 // const key_type& k, Args&&... args):
369 // iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args):
370 //
371 // Inserts (via copy or move) the element of the specified key into the
372 // `flat_hash_map` using the position of `hint` as a non-binding suggestion
373 // for where to begin the insertion search.
374 //
375 // All `try_emplace()` overloads make the same guarantees regarding rvalue
376 // arguments as `std::unordered_map::try_emplace()`, namely that these
377 // functions will not move from rvalue arguments if insertions do not happen.
378 using Base::try_emplace;
379
380 // flat_hash_map::extract()
381 //
382 // Extracts the indicated element, erasing it in the process, and returns it
383 // as a C++17-compatible node handle. Overloads are listed below.
384 //
385 // node_type extract(const_iterator position):
386 //
387 // Extracts the key,value pair of the element at the indicated position and
388 // returns a node handle owning that extracted data.
389 //
390 // node_type extract(const key_type& x):
391 //
392 // Extracts the key,value pair of the element with a key matching the passed
393 // key value and returns a node handle owning that extracted data. If the
394 // `flat_hash_map` does not contain an element with a matching key, this
395 // function returns an empty node handle.
396 //
397 // NOTE: when compiled in an earlier version of C++ than C++17,
398 // `node_type::key()` returns a const reference to the key instead of a
399 // mutable reference. We cannot safely return a mutable reference without
400 // std::launder (which is not available before C++17).
401 using Base::extract;
402
403 // flat_hash_map::merge()
404 //
405 // Extracts elements from a given `source` flat hash map into this
406 // `flat_hash_map`. If the destination `flat_hash_map` already contains an
407 // element with an equivalent key, that element is not extracted.
408 using Base::merge;
409
410 // flat_hash_map::swap(flat_hash_map& other)
411 //
412 // Exchanges the contents of this `flat_hash_map` with those of the `other`
413 // flat hash map, avoiding invocation of any move, copy, or swap operations on
414 // individual elements.
415 //
416 // All iterators and references on the `flat_hash_map` remain valid, excepting
417 // for the past-the-end iterator, which is invalidated.
418 //
419 // `swap()` requires that the flat hash map's hashing and key equivalence
420 // functions be Swappable, and are exchanged using unqualified calls to
421 // non-member `swap()`. If the map's allocator has
422 // `std::allocator_traits<allocator_type>::propagate_on_container_swap::value`
423 // set to `true`, the allocators are also exchanged using an unqualified call
424 // to non-member `swap()`; otherwise, the allocators are not swapped.
425 using Base::swap;
426
427 // flat_hash_map::rehash(count)
428 //
429 // Rehashes the `flat_hash_map`, setting the number of slots to be at least
430 // the passed value. If the new number of slots increases the load factor more
431 // than the current maximum load factor
432 // (`count` < `size()` / `max_load_factor()`), then the new number of slots
433 // will be at least `size()` / `max_load_factor()`.
434 //
435 // To force a rehash, pass rehash(0).
436 //
437 // NOTE: unlike behavior in `std::unordered_map`, references are also
438 // invalidated upon a `rehash()`.
439 using Base::rehash;
440
441 // flat_hash_map::reserve(count)
442 //
443 // Sets the number of slots in the `flat_hash_map` to the number needed to
444 // accommodate at least `count` total elements without exceeding the current
445 // maximum load factor, and may rehash the container if needed.
446 using Base::reserve;
447
448 // flat_hash_map::at()
449 //
450 // Returns a reference to the mapped value of the element with key equivalent
451 // to the passed key.
452 using Base::at;
453
454 // flat_hash_map::contains()
455 //
456 // Determines whether an element with a key comparing equal to the given `key`
457 // exists within the `flat_hash_map`, returning `true` if so or `false`
458 // otherwise.
459 using Base::contains;
460
461 // flat_hash_map::count(const Key& key) const
462 //
463 // Returns the number of elements with a key comparing equal to the given
464 // `key` within the `flat_hash_map`. note that this function will return
465 // either `1` or `0` since duplicate keys are not allowed within a
466 // `flat_hash_map`.
467 using Base::count;
468
469 // flat_hash_map::equal_range()
470 //
471 // Returns a closed range [first, last], defined by a `std::pair` of two
472 // iterators, containing all elements with the passed key in the
473 // `flat_hash_map`.
474 using Base::equal_range;
475
476 // flat_hash_map::find()
477 //
478 // Finds an element with the passed `key` within the `flat_hash_map`.
479 using Base::find;
480
481 // flat_hash_map::operator[]()
482 //
483 // Returns a reference to the value mapped to the passed key within the
484 // `flat_hash_map`, performing an `insert()` if the key does not already
485 // exist.
486 //
487 // If an insertion occurs and results in a rehashing of the container, all
488 // iterators are invalidated. Otherwise iterators are not affected and
489 // references are not invalidated. Overloads are listed below.
490 //
491 // T& operator[](const Key& key):
492 //
493 // Inserts an init_type object constructed in-place if the element with the
494 // given key does not exist.
495 //
496 // T& operator[](Key&& key):
497 //
498 // Inserts an init_type object constructed in-place provided that an element
499 // with the given key does not exist.
500 using Base::operator[];
501
502 // flat_hash_map::bucket_count()
503 //
504 // Returns the number of "buckets" within the `flat_hash_map`. Note that
505 // because a flat hash map contains all elements within its internal storage,
506 // this value simply equals the current capacity of the `flat_hash_map`.
507 using Base::bucket_count;
508
509 // flat_hash_map::load_factor()
510 //
511 // Returns the current load factor of the `flat_hash_map` (the average number
512 // of slots occupied with a value within the hash map).
513 using Base::load_factor;
514
515 // flat_hash_map::max_load_factor()
516 //
517 // Manages the maximum load factor of the `flat_hash_map`. Overloads are
518 // listed below.
519 //
520 // float flat_hash_map::max_load_factor()
521 //
522 // Returns the current maximum load factor of the `flat_hash_map`.
523 //
524 // void flat_hash_map::max_load_factor(float ml)
525 //
526 // Sets the maximum load factor of the `flat_hash_map` to the passed value.
527 //
528 // NOTE: This overload is provided only for API compatibility with the STL;
529 // `flat_hash_map` will ignore any set load factor and manage its rehashing
530 // internally as an implementation detail.
531 using Base::max_load_factor;
532
533 // flat_hash_map::get_allocator()
534 //
535 // Returns the allocator function associated with this `flat_hash_map`.
536 using Base::get_allocator;
537
538 // flat_hash_map::hash_function()
539 //
540 // Returns the hashing function used to hash the keys within this
541 // `flat_hash_map`.
542 using Base::hash_function;
543
544 // flat_hash_map::key_eq()
545 //
546 // Returns the function used for comparing keys equality.
547 using Base::key_eq;
548 };
549
550 // erase_if(flat_hash_map<>, Pred)
551 //
552 // Erases all elements that satisfy the predicate `pred` from the container `c`.
553 // Returns the number of erased elements.
554 template <typename K, typename V, typename H, typename E, typename A,
555 typename Predicate>
erase_if(flat_hash_map<K,V,H,E,A> & c,Predicate pred)556 typename flat_hash_map<K, V, H, E, A>::size_type erase_if(
557 flat_hash_map<K, V, H, E, A>& c, Predicate pred) {
558 return container_internal::EraseIf(pred, &c);
559 }
560
561 namespace container_internal {
562
563 template <class K, class V>
564 struct FlatHashMapPolicy {
565 using slot_policy = container_internal::map_slot_policy<K, V>;
566 using slot_type = typename slot_policy::slot_type;
567 using key_type = K;
568 using mapped_type = V;
569 using init_type = std::pair</*non const*/ key_type, mapped_type>;
570
571 template <class Allocator, class... Args>
constructFlatHashMapPolicy572 static void construct(Allocator* alloc, slot_type* slot, Args&&... args) {
573 slot_policy::construct(alloc, slot, std::forward<Args>(args)...);
574 }
575
576 template <class Allocator>
destroyFlatHashMapPolicy577 static void destroy(Allocator* alloc, slot_type* slot) {
578 slot_policy::destroy(alloc, slot);
579 }
580
581 template <class Allocator>
transferFlatHashMapPolicy582 static auto transfer(Allocator* alloc, slot_type* new_slot,
583 slot_type* old_slot) {
584 return slot_policy::transfer(alloc, new_slot, old_slot);
585 }
586
587 template <class F, class... Args>
decltypeFlatHashMapPolicy588 static decltype(absl::container_internal::DecomposePair(
589 std::declval<F>(), std::declval<Args>()...))
590 apply(F&& f, Args&&... args) {
591 return absl::container_internal::DecomposePair(std::forward<F>(f),
592 std::forward<Args>(args)...);
593 }
594
space_usedFlatHashMapPolicy595 static size_t space_used(const slot_type*) { return 0; }
596
elementFlatHashMapPolicy597 static std::pair<const K, V>& element(slot_type* slot) { return slot->value; }
598
valueFlatHashMapPolicy599 static V& value(std::pair<const K, V>* kv) { return kv->second; }
valueFlatHashMapPolicy600 static const V& value(const std::pair<const K, V>* kv) { return kv->second; }
601 };
602
603 } // namespace container_internal
604
605 namespace container_algorithm_internal {
606
607 // Specialization of trait in absl/algorithm/container.h
608 template <class Key, class T, class Hash, class KeyEqual, class Allocator>
609 struct IsUnorderedContainer<
610 absl::flat_hash_map<Key, T, Hash, KeyEqual, Allocator>> : std::true_type {};
611
612 } // namespace container_algorithm_internal
613
614 ABSL_NAMESPACE_END
615 } // namespace absl
616
617 #endif // ABSL_CONTAINER_FLAT_HASH_MAP_H_
618