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