flat_hash_map.h 23 KB

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