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