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