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