You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

node_hash_map.h 27 kB

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