flat_hash_map.h 24 KB

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