Coverage Report

Created: 2026-04-01 06:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/container/internal/raw_hash_set.h
Line
Count
Source
1
// Copyright 2018 The Abseil Authors.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
//
15
// An open-addressing
16
// hashtable with quadratic probing.
17
//
18
// This is a low level hashtable on top of which different interfaces can be
19
// implemented, like flat_hash_set, node_hash_set, string_hash_set, etc.
20
//
21
// The table interface is similar to that of std::unordered_set. Notable
22
// differences are that most member functions support heterogeneous keys when
23
// BOTH the hash and eq functions are marked as transparent. They do so by
24
// providing a typedef called `is_transparent`.
25
//
26
// When heterogeneous lookup is enabled, functions that take key_type act as if
27
// they have an overload set like:
28
//
29
//   iterator find(const key_type& key);
30
//   template <class K>
31
//   iterator find(const K& key);
32
//
33
//   size_type erase(const key_type& key);
34
//   template <class K>
35
//   size_type erase(const K& key);
36
//
37
//   std::pair<iterator, iterator> equal_range(const key_type& key);
38
//   template <class K>
39
//   std::pair<iterator, iterator> equal_range(const K& key);
40
//
41
// When heterogeneous lookup is disabled, only the explicit `key_type` overloads
42
// exist.
43
//
44
// In addition the pointer to element and iterator stability guarantees are
45
// weaker: all iterators and pointers are invalidated after a new element is
46
// inserted.
47
//
48
// IMPLEMENTATION DETAILS
49
//
50
// # Table Layout
51
//
52
// A raw_hash_set's backing array consists of control bytes followed by slots
53
// that may or may not contain objects.
54
//
55
// The layout of the backing array, for `capacity` slots, is thus, as a
56
// pseudo-struct:
57
//
58
//   struct BackingArray {
59
//     // Sampling handler. This field isn't present when the sampling is
60
//     // disabled or this allocation hasn't been selected for sampling.
61
//     HashtablezInfoHandle infoz_;
62
//     // The number of elements we can insert before growing the capacity.
63
//     size_t growth_left;
64
//     // Control bytes for the "real" slots.
65
//     ctrl_t ctrl[capacity];
66
//     // Always `ctrl_t::kSentinel`. This is used by iterators to find when to
67
//     // stop and serves no other purpose.
68
//     ctrl_t sentinel;
69
//     // A copy of the first `kWidth - 1` elements of `ctrl`. This is used so
70
//     // that if a probe sequence picks a value near the end of `ctrl`,
71
//     // `Group` will have valid control bytes to look at.
72
//     ctrl_t clones[kWidth - 1];
73
//     // The actual slot data.
74
//     slot_type slots[capacity];
75
//   };
76
//
77
// The length of this array is computed by `RawHashSetLayout::alloc_size` below.
78
//
79
// Control bytes (`ctrl_t`) are bytes (collected into groups of a
80
// platform-specific size) that define the state of the corresponding slot in
81
// the slot array. Group manipulation is tightly optimized to be as efficient
82
// as possible: SSE and friends on x86, clever bit operations on other arches.
83
//
84
//      Group 1         Group 2        Group 3
85
// +---------------+---------------+---------------+
86
// | | | | | | | | | | | | | | | | | | | | | | | | |
87
// +---------------+---------------+---------------+
88
//
89
// Each control byte is either a special value for empty slots, deleted slots
90
// (sometimes called *tombstones*), and a special end-of-table marker used by
91
// iterators, or, if occupied, seven bits (H2) from the hash of the value in the
92
// corresponding slot.
93
//
94
// Storing control bytes in a separate array also has beneficial cache effects,
95
// since more logical slots will fit into a cache line.
96
//
97
// # Small Object Optimization (SOO)
98
//
99
// When the size/alignment of the value_type and the capacity of the table are
100
// small, we enable small object optimization and store the values inline in
101
// the raw_hash_set object. This optimization allows us to avoid
102
// allocation/deallocation as well as cache/dTLB misses.
103
//
104
// # Hashing
105
//
106
// We compute two separate hashes, `H1` and `H2`, from the hash of an object.
107
// `H1(hash(x))` is an index into `slots`, and essentially the starting point
108
// for the probe sequence. `H2(hash(x))` is a 7-bit value used to filter out
109
// objects that cannot possibly be the one we are looking for.
110
//
111
// # Table operations.
112
//
113
// The key operations are `insert`, `find`, and `erase`.
114
//
115
// Since `insert` and `erase` are implemented in terms of `find`, we describe
116
// `find` first. To `find` a value `x`, we compute `hash(x)`. From
117
// `H1(hash(x))` and the capacity, we construct a `probe_seq` that visits every
118
// group of slots in some interesting order.
119
//
120
// We now walk through these indices. At each index, we select the entire group
121
// starting with that index and extract potential candidates: occupied slots
122
// with a control byte equal to `H2(hash(x))`. If we find an empty slot in the
123
// group, we stop and return an error. Each candidate slot `y` is compared with
124
// `x`; if `x == y`, we are done and return `&y`; otherwise we continue to the
125
// next probe index. Tombstones effectively behave like full slots that never
126
// match the value we're looking for.
127
//
128
// The `H2` bits ensure when we compare a slot to an object with `==`, we are
129
// likely to have actually found the object.  That is, the chance is low that
130
// `==` is called and returns `false`.  Thus, when we search for an object, we
131
// are unlikely to call `==` many times.  This likelyhood can be analyzed as
132
// follows (assuming that H2 is a random enough hash function).
133
//
134
// Let's assume that there are `k` "wrong" objects that must be examined in a
135
// probe sequence.  For example, when doing a `find` on an object that is in the
136
// table, `k` is the number of objects between the start of the probe sequence
137
// and the final found object (not including the final found object).  The
138
// expected number of objects with an H2 match is then `k/128`.  Measurements
139
// and analysis indicate that even at high load factors, `k` is less than 32,
140
// meaning that the number of "false positive" comparisons we must perform is
141
// less than 1/8 per `find`.
142
143
// `insert` is implemented in terms of `unchecked_insert`, which inserts a
144
// value presumed to not be in the table (violating this requirement will cause
145
// the table to behave erratically). Given `x` and its hash `hash(x)`, to insert
146
// it, we construct a `probe_seq` once again, and use it to find the first
147
// group with an unoccupied (empty *or* deleted) slot. We place `x` into the
148
// first such slot in the group and mark it as full with `x`'s H2.
149
//
150
// To `insert`, we compose `unchecked_insert` with `find`. We compute `h(x)` and
151
// perform a `find` to see if it's already present; if it is, we're done. If
152
// it's not, we may decide the table is getting overcrowded (i.e. the load
153
// factor is greater than 7/8 for big tables; tables smaller than one probing
154
// group use a max load factor of 1); in this case, we allocate a bigger array,
155
// `unchecked_insert` each element of the table into the new array (we know that
156
// no insertion here will insert an already-present value), and discard the old
157
// backing array. At this point, we may `unchecked_insert` the value `x`.
158
//
159
// Below, `unchecked_insert` is partly implemented by `prepare_insert`, which
160
// presents a viable, initialized slot pointee to the caller.
161
//
162
// `erase` is implemented in terms of `erase_at`, which takes an index to a
163
// slot. Given an offset, we simply create a tombstone and destroy its contents.
164
// If we can prove that the slot would not appear in a probe sequence, we can
165
// make the slot as empty, instead. We can prove this by observing that if a
166
// group has any empty slots, it has never been full (assuming we never create
167
// an empty slot in a group with no empties, which this heuristic guarantees we
168
// never do) and find would stop at this group anyways (since it does not probe
169
// beyond groups with empties).
170
//
171
// `erase` is `erase_at` composed with `find`: if we
172
// have a value `x`, we can perform a `find`, and then `erase_at` the resulting
173
// slot.
174
//
175
// To iterate, we simply traverse the array, skipping empty and deleted slots
176
// and stopping when we hit a `kSentinel`.
177
178
#ifndef ABSL_CONTAINER_INTERNAL_RAW_HASH_SET_H_
179
#define ABSL_CONTAINER_INTERNAL_RAW_HASH_SET_H_
180
181
#include <algorithm>
182
#include <cassert>
183
#include <cmath>
184
#include <cstddef>
185
#include <cstdint>
186
#include <cstring>
187
#include <functional>
188
#include <initializer_list>
189
#include <iterator>
190
#include <limits>
191
#include <memory>
192
#include <tuple>
193
#include <type_traits>
194
#include <utility>
195
196
#include "absl/base/attributes.h"
197
#include "absl/base/casts.h"
198
#include "absl/base/config.h"
199
#include "absl/base/internal/endian.h"
200
#include "absl/base/internal/iterator_traits.h"
201
#include "absl/base/internal/raw_logging.h"
202
#include "absl/base/macros.h"
203
#include "absl/base/optimization.h"
204
#include "absl/base/options.h"
205
#include "absl/base/port.h"
206
#include "absl/base/prefetch.h"
207
#include "absl/container/internal/common.h"  // IWYU pragma: export // for node_handle
208
#include "absl/container/internal/common_policy_traits.h"
209
#include "absl/container/internal/compressed_tuple.h"
210
#include "absl/container/internal/container_memory.h"
211
#include "absl/container/internal/hash_function_defaults.h"
212
#include "absl/container/internal/hash_policy_traits.h"
213
#include "absl/container/internal/hashtable_control_bytes.h"
214
#include "absl/container/internal/hashtable_debug_hooks.h"
215
#include "absl/container/internal/hashtablez_sampler.h"
216
#include "absl/functional/function_ref.h"
217
#include "absl/hash/hash.h"
218
#include "absl/hash/internal/weakly_mixed_integer.h"
219
#include "absl/memory/memory.h"
220
#include "absl/meta/type_traits.h"
221
#include "absl/numeric/bits.h"
222
#include "absl/utility/utility.h"
223
224
#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
225
#include <ranges>  // NOLINT(build/c++20)
226
#endif
227
228
namespace absl {
229
ABSL_NAMESPACE_BEGIN
230
namespace container_internal {
231
232
#ifdef ABSL_SWISSTABLE_ENABLE_GENERATIONS
233
#error ABSL_SWISSTABLE_ENABLE_GENERATIONS cannot be directly set
234
#elif (defined(ABSL_HAVE_ADDRESS_SANITIZER) ||   \
235
       defined(ABSL_HAVE_HWADDRESS_SANITIZER) || \
236
       defined(ABSL_HAVE_MEMORY_SANITIZER)) &&   \
237
    !defined(NDEBUG_SANITIZER)  // If defined, performance is important.
238
// When compiled in sanitizer mode, we add generation integers to the backing
239
// array and iterators. In the backing array, we store the generation between
240
// the control bytes and the slots. When iterators are dereferenced, we assert
241
// that the container has not been mutated in a way that could cause iterator
242
// invalidation since the iterator was initialized.
243
#define ABSL_SWISSTABLE_ENABLE_GENERATIONS
244
#endif
245
246
#ifdef ABSL_SWISSTABLE_ASSERT
247
#error ABSL_SWISSTABLE_ASSERT cannot be directly set
248
#else
249
// We use this macro for assertions that users may see when the table is in an
250
// invalid state that sanitizers may help diagnose.
251
#define ABSL_SWISSTABLE_ASSERT(CONDITION) \
252
78.1M
  assert((CONDITION) && "Try enabling sanitizers.")
253
#endif
254
255
// We use uint8_t so we don't need to worry about padding.
256
using GenerationType = uint8_t;
257
258
// A sentinel value for empty generations. Using 0 makes it easy to constexpr
259
// initialize an array of this value.
260
201k
constexpr GenerationType SentinelEmptyGeneration() { return 0; }
261
262
201k
constexpr GenerationType NextGeneration(GenerationType generation) {
263
201k
  return ++generation == SentinelEmptyGeneration() ? ++generation : generation;
264
201k
}
265
266
#ifdef ABSL_SWISSTABLE_ENABLE_GENERATIONS
267
constexpr bool SwisstableGenerationsEnabled() { return true; }
268
constexpr size_t NumGenerationBytes() { return sizeof(GenerationType); }
269
#else
270
0
constexpr bool SwisstableGenerationsEnabled() { return false; }
271
402k
constexpr size_t NumGenerationBytes() { return 0; }
272
#endif
273
274
// Returns true if we should assert that the table is not accessed after it has
275
// been destroyed or during the destruction of the table.
276
0
constexpr bool SwisstableAssertAccessToDestroyedTable() {
277
0
#ifndef NDEBUG
278
0
  return true;
279
0
#endif
280
0
  return SwisstableGenerationsEnabled();
281
0
}
282
283
template <typename AllocType>
284
void SwapAlloc(AllocType& lhs, AllocType& rhs,
285
               std::true_type /* propagate_on_container_swap */) {
286
  using std::swap;
287
  swap(lhs, rhs);
288
}
289
template <typename AllocType>
290
void SwapAlloc([[maybe_unused]] AllocType& lhs, [[maybe_unused]] AllocType& rhs,
291
               std::false_type /* propagate_on_container_swap */) {
292
  assert(lhs == rhs &&
293
         "It's UB to call swap with unequal non-propagating allocators.");
294
}
295
296
template <typename AllocType>
297
void CopyAlloc(AllocType& lhs, AllocType& rhs,
298
               std::true_type /* propagate_alloc */) {
299
  lhs = rhs;
300
}
301
template <typename AllocType>
302
void CopyAlloc(AllocType&, AllocType&, std::false_type /* propagate_alloc */) {}
303
304
// The state for a probe sequence.
305
//
306
// Currently, the sequence is a triangular progression of the form
307
//
308
//   p(i) := Width * (i^2 + i)/2 + hash (mod mask + 1)
309
//
310
// The use of `Width` ensures that each probe step does not overlap groups;
311
// the sequence effectively outputs the addresses of *groups* (although not
312
// necessarily aligned to any boundary). The `Group` machinery allows us
313
// to check an entire group with minimal branching.
314
//
315
// Wrapping around at `mask + 1` is important, but not for the obvious reason.
316
// As described above, the first few entries of the control byte array
317
// are mirrored at the end of the array, which `Group` will find and use
318
// for selecting candidates. However, when those candidates' slots are
319
// actually inspected, there are no corresponding slots for the cloned bytes,
320
// so we need to make sure we've treated those offsets as "wrapping around".
321
//
322
// It turns out that this probe sequence visits every group exactly once if the
323
// number of groups is a power of two, since (i^2+i)/2 is a bijection in
324
// Z/(2^m). See https://en.wikipedia.org/wiki/Quadratic_probing
325
template <size_t Width>
326
class probe_seq {
327
 public:
328
  // Creates a new probe sequence using `hash` as the initial value of the
329
  // sequence and `mask` (usually the capacity of the table) as the mask to
330
  // apply to each value in the progression.
331
164k
  probe_seq(size_t hash, size_t mask) {
332
164k
    ABSL_SWISSTABLE_ASSERT(((mask + 1) & mask) == 0 && "not a mask");
333
164k
    mask_ = mask;
334
164k
    offset_ = hash & mask_;
335
164k
  }
336
337
  // The offset within the table, i.e., the value `p(i)` above.
338
330k
  size_t offset() const { return offset_; }
339
81.1k
  size_t offset(size_t i) const { return (offset_ + i) & mask_; }
340
341
2.70k
  void next() {
342
2.70k
    index_ += Width;
343
2.70k
    offset_ += index_;
344
2.70k
    offset_ &= mask_;
345
2.70k
  }
346
  // 0-based probe index, a multiple of `Width`.
347
83.8k
  size_t index() const { return index_; }
348
349
 private:
350
  size_t mask_;
351
  size_t offset_;
352
  size_t index_ = 0;
353
};
354
355
template <class ContainerKey, class Hash, class Eq>
356
struct RequireUsableKey {
357
  template <class PassedKey, class... Args>
358
  std::pair<
359
      decltype(std::declval<const Hash&>()(std::declval<const PassedKey&>())),
360
      decltype(std::declval<const Eq&>()(std::declval<const ContainerKey&>(),
361
                                         std::declval<const PassedKey&>()))>*
362
  operator()(const PassedKey&, const Args&...) const;
363
};
364
365
template <class E, class Policy, class Hash, class Eq, class... Ts>
366
struct IsDecomposable : std::false_type {};
367
368
template <class Policy, class Hash, class Eq, class... Ts>
369
struct IsDecomposable<
370
    absl::void_t<decltype(Policy::apply(
371
        RequireUsableKey<typename Policy::key_type, Hash, Eq>(),
372
        std::declval<Ts>()...))>,
373
    Policy, Hash, Eq, Ts...> : std::true_type {};
374
375
ABSL_DLL extern ctrl_t kDefaultIterControl;
376
377
// We use these sentinel capacity values in debug mode to indicate different
378
// classes of bugs.
379
enum InvalidCapacity : size_t {
380
  kAboveMaxValidCapacity = ~size_t{} - 100,
381
  kReentrance,
382
  kDestroyed,
383
384
  // These two must be last because we use `>= kMovedFrom` to mean moved-from.
385
  kMovedFrom,
386
  kSelfMovedFrom,
387
};
388
389
// Returns a pointer to a control byte that can be used by default-constructed
390
// iterators. We don't expect this pointer to be dereferenced.
391
0
inline ctrl_t* DefaultIterControl() { return &kDefaultIterControl; }
392
393
// For use in SOO iterators.
394
// TODO(b/289225379): we could potentially get rid of this by adding an is_soo
395
// bit in iterators. This would add branches but reduce cache misses.
396
ABSL_DLL extern const ctrl_t kSooControl[2];
397
398
// Returns a pointer to a full byte followed by a sentinel byte.
399
0
inline ctrl_t* SooControl() {
400
  // Const must be cast away here; no uses of this function will actually write
401
  // to it because it is only used for SOO iterators.
402
0
  return const_cast<ctrl_t*>(kSooControl);
403
0
}
404
// Whether ctrl is from the SooControl array.
405
0
inline bool IsSooControl(const ctrl_t* ctrl) { return ctrl == SooControl(); }
406
407
// Returns a pointer to a generation to use for an empty hashtable.
408
GenerationType* EmptyGeneration();
409
410
// Returns whether `generation` is a generation for an empty hashtable that
411
// could be returned by EmptyGeneration().
412
0
inline bool IsEmptyGeneration(const GenerationType* generation) {
413
0
  return *generation == SentinelEmptyGeneration();
414
0
}
415
416
// We only allow a maximum of 1 SOO element, which makes the implementation
417
// much simpler. Complications with multiple SOO elements include:
418
// - Satisfying the guarantee that erasing one element doesn't invalidate
419
//   iterators to other elements means we would probably need actual SOO
420
//   control bytes.
421
// - In order to prevent user code from depending on iteration order for small
422
//   tables, we would need to randomize the iteration order somehow.
423
653k
constexpr size_t SooCapacity() { return 1; }
424
// Sentinel type to indicate SOO CommonFields construction.
425
struct soo_tag_t {};
426
// Sentinel type to indicate SOO CommonFields construction with full size.
427
struct full_soo_tag_t {};
428
// Sentinel type to indicate non-SOO CommonFields construction.
429
struct non_soo_tag_t {};
430
// Sentinel value to indicate an uninitialized value explicitly.
431
struct uninitialized_tag_t {};
432
// Sentinel value to indicate creation of an empty table without a seed.
433
struct no_seed_empty_tag_t {};
434
435
// Per table hash salt. This gets mixed into H1 to randomize iteration order
436
// per-table.
437
// The seed is needed to ensure non-determinism of iteration order.
438
class PerTableSeed {
439
 public:
440
  // The number of bits in the seed.
441
  // It is big enough to ensure non-determinism of iteration order.
442
  // We store the seed inside a uint64_t together with size and other metadata.
443
  // Using 16 bits allows us to save one `and` instruction in H1 (we use
444
  // zero-extended move instead of mov+and). When absl::Hash is inlined, it can
445
  // also have lower latency knowing that the high bits of the seed are zero.
446
  static constexpr size_t kBitCount = 16;
447
448
  // Returns the seed for the table.
449
206k
  size_t seed() const { return seed_; }
450
451
 private:
452
  friend class HashtableSize;
453
206k
  explicit PerTableSeed(uint16_t seed) : seed_(seed) {}
454
455
  // The most significant bit of the seed is always 1 when there is a non-zero
456
  // seed. This way, when sign-extended the seed has non-zero high bits.
457
  const uint16_t seed_;
458
};
459
460
// The size and also has additionally
461
// 1) one bit that stores whether we have infoz.
462
// 2) PerTableSeed::kBitCount bits for the seed. (For SOO tables, the lowest
463
//    bit of the seed is repurposed to track if sampling has been tried).
464
class HashtableSize {
465
 public:
466
  static constexpr size_t kSizeBitCount = 64 - PerTableSeed::kBitCount - 1;
467
468
0
  explicit HashtableSize(uninitialized_tag_t) {}
469
14.9k
  explicit HashtableSize(no_seed_empty_tag_t) : data_(0) {}
470
  HashtableSize(full_soo_tag_t, bool has_tried_sampling)
471
      : data_(kSizeOneNoMetadata |
472
0
              (has_tried_sampling ? kSooHasTriedSamplingMask : 0)) {}
473
474
  // Returns actual size of the table.
475
8.86M
  size_t size() const { return static_cast<size_t>(data_ >> kSizeShift); }
476
8.33M
  void increment_size() { data_ += kSizeOneNoMetadata; }
477
0
  void increment_size(size_t size) {
478
0
    data_ += static_cast<uint64_t>(size) << kSizeShift;
479
0
  }
480
0
  void decrement_size() { data_ -= kSizeOneNoMetadata; }
481
  // Returns true if the table is empty.
482
0
  bool empty() const { return data_ < kSizeOneNoMetadata; }
483
484
  // Returns true if an empty SOO table has already queried should_sample_soo().
485
0
  bool soo_has_tried_sampling() const {
486
0
    return (data_ & kSooHasTriedSamplingMask) != 0;
487
0
  }
488
489
  // Records that an empty SOO table has tried sampling.
490
0
  void set_soo_has_tried_sampling() { data_ |= kSooHasTriedSamplingMask; }
491
492
  // Sets the size, but keeps all the metadata bits.
493
220k
  void set_size_keep_metadata(size_t size) {
494
220k
    data_ =
495
220k
        (data_ & kMetadataMask) | (static_cast<uint64_t>(size) << kSizeShift);
496
220k
  }
497
498
206k
  PerTableSeed seed() const {
499
206k
    return PerTableSeed(static_cast<size_t>(data_) & kSeedMask);
500
206k
  }
501
502
49.0k
  void generate_new_seed() { set_seed(NextSeed()); }
503
504
  // We need to use a constant seed when the table is sampled so that sampled
505
  // hashes use the same seed and can e.g. identify stuck bits accurately.
506
0
  void set_sampled_seed() { set_seed((std::numeric_limits<uint16_t>::max)()); }
507
508
0
  bool is_sampled_seed() const {
509
0
    return (data_ & kSeedMask) == (std::numeric_limits<uint16_t>::max)();
510
0
  }
511
512
  // Returns true if the table has infoz.
513
8.61M
  bool has_infoz() const {
514
8.61M
    return ABSL_PREDICT_FALSE((data_ & kHasInfozMask) != 0);
515
8.61M
  }
516
517
  // Sets the has_infoz bit.
518
0
  void set_has_infoz() { data_ |= kHasInfozMask; }
519
520
0
  void set_no_seed_for_testing() { data_ &= ~kSeedMask; }
521
522
  // Returns next per-table seed.
523
  static uint16_t NextSeed();
524
525
 private:
526
49.0k
  void set_seed(uint16_t seed) { data_ = (data_ & ~kSeedMask) | seed; }
527
  // Bit layout of `data_`:
528
  // [63 ... 17] (47 bits) : size
529
  // [16]        (1 bit)   : has_infoz
530
  // [15 ...  0] (16 bits) : seed
531
  static constexpr size_t kSizeShift = 64 - kSizeBitCount;
532
  static constexpr uint64_t kSizeOneNoMetadata = uint64_t{1} << kSizeShift;
533
  static constexpr uint64_t kMetadataMask = kSizeOneNoMetadata - 1;
534
  static constexpr uint64_t kSeedMask =
535
      (uint64_t{1} << PerTableSeed::kBitCount) - 1;
536
  // The next bit after the seed.
537
  static constexpr uint64_t kHasInfozMask = kSeedMask + 1;
538
  // For SOO tables, the seed is unused, and bit 0 is repurposed to track
539
  // whether the table has already queried should_sample_soo().
540
  static constexpr uint64_t kSooHasTriedSamplingMask = 1;
541
  uint64_t data_;
542
};
543
544
// H1 is just the low bits of the hash.
545
92.4k
inline size_t H1(size_t hash) { return hash; }
546
547
// Extracts the H2 portion of a hash: the 7 most significant bits.
548
//
549
// These are used as an occupied control byte.
550
8.38M
inline h2_t H2(size_t hash) { return hash >> (sizeof(size_t) * 8 - 7); }
551
552
// When there is an insertion with no reserved growth, we rehash with
553
// probability `min(1, RehashProbabilityConstant() / capacity())`. Using a
554
// constant divided by capacity ensures that inserting N elements is still O(N)
555
// in the average case. Using the constant 16 means that we expect to rehash ~8
556
// times more often than when generations are disabled. We are adding expected
557
// rehash_probability * #insertions/capacity_growth = 16/capacity * ((7/8 -
558
// 7/16) * capacity)/capacity_growth = ~7 extra rehashes per capacity growth.
559
0
inline size_t RehashProbabilityConstant() { return 16; }
560
561
class CommonFieldsGenerationInfoEnabled {
562
  // A sentinel value for reserved_growth_ indicating that we just ran out of
563
  // reserved growth on the last insertion. When reserve is called and then
564
  // insertions take place, reserved_growth_'s state machine is N, ..., 1,
565
  // kReservedGrowthJustRanOut, 0.
566
  static constexpr size_t kReservedGrowthJustRanOut =
567
      (std::numeric_limits<size_t>::max)();
568
569
 public:
570
  CommonFieldsGenerationInfoEnabled() = default;
571
  CommonFieldsGenerationInfoEnabled(CommonFieldsGenerationInfoEnabled&& that)
572
      : reserved_growth_(that.reserved_growth_),
573
        reservation_size_(that.reservation_size_),
574
0
        generation_(that.generation_) {
575
0
    that.reserved_growth_ = 0;
576
0
    that.reservation_size_ = 0;
577
0
    that.generation_ = EmptyGeneration();
578
0
  }
579
  CommonFieldsGenerationInfoEnabled& operator=(
580
      CommonFieldsGenerationInfoEnabled&&) = default;
581
582
  // Whether we should rehash on insert in order to detect bugs of using invalid
583
  // references. We rehash on the first insertion after reserved_growth_ reaches
584
  // 0 after a call to reserve. We also do a rehash with low probability
585
  // whenever reserved_growth_ is zero.
586
  bool should_rehash_for_bug_detection_on_insert(size_t capacity) const;
587
  // Similar to above, except that we don't depend on reserved_growth_.
588
  bool should_rehash_for_bug_detection_on_move(size_t capacity) const;
589
0
  void maybe_increment_generation_on_insert() {
590
0
    if (reserved_growth_ == kReservedGrowthJustRanOut) reserved_growth_ = 0;
591
0
592
0
    if (reserved_growth_ > 0) {
593
0
      if (--reserved_growth_ == 0) reserved_growth_ = kReservedGrowthJustRanOut;
594
0
    } else {
595
0
      increment_generation();
596
0
    }
597
0
  }
598
0
  void increment_generation() { *generation_ = NextGeneration(*generation_); }
599
0
  void reset_reserved_growth(size_t reservation, size_t size) {
600
0
    reserved_growth_ = reservation - size;
601
0
  }
602
0
  size_t reserved_growth() const { return reserved_growth_; }
603
0
  void set_reserved_growth(size_t r) { reserved_growth_ = r; }
604
0
  size_t reservation_size() const { return reservation_size_; }
605
0
  void set_reservation_size(size_t r) { reservation_size_ = r; }
606
0
  GenerationType generation() const { return *generation_; }
607
0
  void set_generation(GenerationType g) { *generation_ = g; }
608
0
  GenerationType* generation_ptr() const { return generation_; }
609
0
  void set_generation_ptr(GenerationType* g) { generation_ = g; }
610
611
 private:
612
  // The number of insertions remaining that are guaranteed to not rehash due to
613
  // a prior call to reserve. Note: we store reserved growth in addition to
614
  // reservation size because calls to erase() decrease size_ but don't decrease
615
  // reserved growth.
616
  size_t reserved_growth_ = 0;
617
  // The maximum argument to reserve() since the container was cleared. We need
618
  // to keep track of this, in addition to reserved growth, because we reset
619
  // reserved growth to this when erase(begin(), end()) is called.
620
  size_t reservation_size_ = 0;
621
  // Pointer to the generation counter, which is used to validate iterators and
622
  // is stored in the backing array between the control bytes and the slots.
623
  // Note that we can't store the generation inside the container itself and
624
  // keep a pointer to the container in the iterators because iterators must
625
  // remain valid when the container is moved.
626
  // Note: we could derive this pointer from the control pointer, but it makes
627
  // the code more complicated, and there's a benefit in having the sizes of
628
  // raw_hash_set in sanitizer mode and non-sanitizer mode a bit more different,
629
  // which is that tests are less likely to rely on the size remaining the same.
630
  GenerationType* generation_ = EmptyGeneration();
631
};
632
633
class CommonFieldsGenerationInfoDisabled {
634
 public:
635
  CommonFieldsGenerationInfoDisabled() = default;
636
  CommonFieldsGenerationInfoDisabled(CommonFieldsGenerationInfoDisabled&&) =
637
      default;
638
  CommonFieldsGenerationInfoDisabled& operator=(
639
      CommonFieldsGenerationInfoDisabled&&) = default;
640
641
0
  bool should_rehash_for_bug_detection_on_insert(size_t) const { return false; }
642
0
  bool should_rehash_for_bug_detection_on_move(size_t) const { return false; }
643
8.33M
  void maybe_increment_generation_on_insert() {}
644
0
  void increment_generation() {}
645
0
  void reset_reserved_growth(size_t, size_t) {}
646
0
  size_t reserved_growth() const { return 0; }
647
0
  void set_reserved_growth(size_t) {}
648
0
  size_t reservation_size() const { return 0; }
649
0
  void set_reservation_size(size_t) {}
650
201k
  GenerationType generation() const { return 0; }
651
201k
  void set_generation(GenerationType) {}
652
0
  GenerationType* generation_ptr() const { return nullptr; }
653
201k
  void set_generation_ptr(GenerationType*) {}
654
};
655
656
class HashSetIteratorGenerationInfoEnabled {
657
 public:
658
  HashSetIteratorGenerationInfoEnabled() = default;
659
  explicit HashSetIteratorGenerationInfoEnabled(
660
      const GenerationType* generation_ptr)
661
0
      : generation_ptr_(generation_ptr), generation_(*generation_ptr) {}
662
663
0
  GenerationType generation() const { return generation_; }
664
0
  void reset_generation() { generation_ = *generation_ptr_; }
665
0
  const GenerationType* generation_ptr() const { return generation_ptr_; }
666
0
  void set_generation_ptr(const GenerationType* ptr) { generation_ptr_ = ptr; }
667
668
 private:
669
  const GenerationType* generation_ptr_ = EmptyGeneration();
670
  GenerationType generation_ = *generation_ptr_;
671
};
672
673
class HashSetIteratorGenerationInfoDisabled {
674
 public:
675
  HashSetIteratorGenerationInfoDisabled() = default;
676
0
  explicit HashSetIteratorGenerationInfoDisabled(const GenerationType*) {}
677
678
0
  GenerationType generation() const { return 0; }
679
0
  void reset_generation() {}
680
0
  const GenerationType* generation_ptr() const { return nullptr; }
681
0
  void set_generation_ptr(const GenerationType*) {}
682
};
683
684
#ifdef ABSL_SWISSTABLE_ENABLE_GENERATIONS
685
using CommonFieldsGenerationInfo = CommonFieldsGenerationInfoEnabled;
686
using HashSetIteratorGenerationInfo = HashSetIteratorGenerationInfoEnabled;
687
#else
688
using CommonFieldsGenerationInfo = CommonFieldsGenerationInfoDisabled;
689
using HashSetIteratorGenerationInfo = HashSetIteratorGenerationInfoDisabled;
690
#endif
691
692
// Stored the information regarding number of slots we can still fill
693
// without needing to rehash.
694
//
695
// We want to ensure sufficient number of empty slots in the table in order
696
// to keep probe sequences relatively short. Empty slot in the probe group
697
// is required to stop probing.
698
//
699
// Tombstones (kDeleted slots) are not included in the growth capacity,
700
// because we'd like to rehash when the table is filled with tombstones and/or
701
// full slots.
702
//
703
// GrowthInfo also stores a bit that encodes whether table may have any
704
// deleted slots.
705
// Most of the tables (>95%) have no deleted slots, so some functions can
706
// be more efficient with this information.
707
//
708
// Callers can also force a rehash via the standard `rehash(0)`,
709
// which will recompute this value as a side-effect.
710
//
711
// See also `CapacityToGrowth()`.
712
class GrowthInfo {
713
 public:
714
  // Leaves data member uninitialized.
715
  GrowthInfo() = default;
716
717
  // Initializes the GrowthInfo assuming we can grow `growth_left` elements
718
  // and there are no kDeleted slots in the table.
719
421k
  void InitGrowthLeftNoDeleted(size_t growth_left) {
720
421k
    growth_left_info_ = growth_left;
721
421k
  }
722
723
  // Overwrites single full slot with an empty slot.
724
0
  void OverwriteFullAsEmpty() { ++growth_left_info_; }
725
726
  // Overwrites single empty slot with a full slot.
727
8.13M
  void OverwriteEmptyAsFull() {
728
8.13M
    ABSL_SWISSTABLE_ASSERT(GetGrowthLeft() > 0);
729
8.13M
    --growth_left_info_;
730
8.13M
  }
731
732
  // Overwrites several empty slots with full slots.
733
0
  void OverwriteManyEmptyAsFull(size_t count) {
734
0
    ABSL_SWISSTABLE_ASSERT(GetGrowthLeft() >= count);
735
0
    growth_left_info_ -= count;
736
0
  }
737
738
  // Overwrites specified control element with full slot.
739
0
  void OverwriteControlAsFull(ctrl_t ctrl) {
740
0
    ABSL_SWISSTABLE_ASSERT(GetGrowthLeft() >=
741
0
                           static_cast<size_t>(IsEmpty(ctrl)));
742
0
    growth_left_info_ -= static_cast<size_t>(IsEmpty(ctrl));
743
0
  }
744
745
  // Overwrites single full slot with a deleted slot.
746
0
  void OverwriteFullAsDeleted() { growth_left_info_ |= kDeletedBit; }
747
748
  // Returns true if table satisfies two properties:
749
  // 1. Guaranteed to have no kDeleted slots.
750
  // 2. There is a place for at least one element to grow.
751
8.43M
  bool HasNoDeletedAndGrowthLeft() const {
752
8.43M
    return static_cast<std::make_signed_t<size_t>>(growth_left_info_) > 0;
753
8.43M
  }
754
755
  // Returns true if the table satisfies two properties:
756
  // 1. Guaranteed to have no kDeleted slots.
757
  // 2. There is no growth left.
758
152k
  bool HasNoGrowthLeftAndNoDeleted() const { return growth_left_info_ == 0; }
759
760
  // Returns true if GetGrowthLeft() == 0, but must be called only if
761
  // HasNoDeleted() is false. It is slightly more efficient.
762
0
  bool HasNoGrowthLeftAssumingMayHaveDeleted() const {
763
0
    ABSL_SWISSTABLE_ASSERT(!HasNoDeleted());
764
0
    return growth_left_info_ == kDeletedBit;
765
0
  }
766
767
  // Returns true if table guaranteed to have no kDeleted slots.
768
152k
  bool HasNoDeleted() const {
769
152k
    return static_cast<std::make_signed_t<size_t>>(growth_left_info_) >= 0;
770
152k
  }
771
772
  // Returns the number of elements left to grow.
773
8.43M
  size_t GetGrowthLeft() const { return growth_left_info_ & kGrowthLeftMask; }
774
775
 private:
776
  static constexpr size_t kGrowthLeftMask = ((~size_t{}) >> 1);
777
  static constexpr size_t kDeletedBit = ~kGrowthLeftMask;
778
  // Topmost bit signal whenever there are deleted slots.
779
  size_t growth_left_info_;
780
};
781
782
static_assert(sizeof(GrowthInfo) == sizeof(size_t), "");
783
static_assert(alignof(GrowthInfo) == alignof(size_t), "");
784
785
// Returns whether `n` is a valid capacity (i.e., number of slots).
786
//
787
// A valid capacity is a non-zero integer `2^m - 1`.
788
1.12M
constexpr bool IsValidCapacity(size_t n) { return ((n + 1) & n) == 0 && n > 0; }
789
790
// Whether a table is small enough that we don't need to hash any keys.
791
60.0M
constexpr bool IsSmallCapacity(size_t capacity) { return capacity <= 1; }
792
793
// Returns the number of "cloned control bytes".
794
//
795
// This is the number of control bytes that are present both at the beginning
796
// of the control byte array and at the end, such that we can create a
797
// `Group::kWidth`-width probe window starting from any control byte.
798
42.9M
constexpr size_t NumClonedBytes() { return Group::kWidth - 1; }
799
800
// Returns the number of control bytes including cloned.
801
26.3M
constexpr size_t NumControlBytes(size_t capacity) {
802
26.3M
  return IsSmallCapacity(capacity) ? 0 : capacity + 1 + NumClonedBytes();
803
26.3M
}
804
805
// Computes the offset from the start of the backing allocation of control.
806
// infoz and growth_info are stored at the beginning of the backing array.
807
402k
constexpr size_t ControlOffset(bool has_infoz) {
808
402k
  return (has_infoz ? sizeof(HashtablezInfoHandle) : 0) + sizeof(GrowthInfo);
809
402k
}
810
811
// Returns the offset of the next item after `offset` that is aligned to `align`
812
// bytes. `align` must be a power of two.
813
402k
constexpr size_t AlignUpTo(size_t offset, size_t align) {
814
402k
  return (offset + align - 1) & (~align + 1);
815
402k
}
816
817
// Helper class for computing offsets and allocation size of hash set fields.
818
class RawHashSetLayout {
819
 public:
820
  // TODO(b/413062340): maybe don't allocate growth info for capacity 1 tables.
821
  // Doing so may require additional branches/complexity so it might not be
822
  // worth it.
823
  explicit RawHashSetLayout(size_t capacity, size_t slot_size,
824
                            size_t slot_align, bool has_infoz)
825
402k
      : control_offset_(ControlOffset(has_infoz)),
826
402k
        generation_offset_(control_offset_ + NumControlBytes(capacity)),
827
        slot_offset_(
828
402k
            AlignUpTo(generation_offset_ + NumGenerationBytes(), slot_align)),
829
402k
        alloc_size_(slot_offset_ + capacity * slot_size) {
830
402k
    ABSL_SWISSTABLE_ASSERT(IsValidCapacity(capacity));
831
402k
    ABSL_SWISSTABLE_ASSERT(
832
402k
        slot_size <=
833
402k
        ((std::numeric_limits<size_t>::max)() - slot_offset_) / capacity);
834
402k
  }
835
836
  // Returns precomputed offset from the start of the backing allocation of
837
  // control.
838
402k
  size_t control_offset() const { return control_offset_; }
839
840
  // Given the capacity of a table, computes the offset (from the start of the
841
  // backing allocation) of the generation counter (if it exists).
842
201k
  size_t generation_offset() const { return generation_offset_; }
843
844
  // Given the capacity of a table, computes the offset (from the start of the
845
  // backing allocation) at which the slots begin.
846
201k
  size_t slot_offset() const { return slot_offset_; }
847
848
  // Given the capacity of a table, computes the total size of the backing
849
  // array.
850
603k
  size_t alloc_size() const { return alloc_size_; }
851
852
 private:
853
  size_t control_offset_;
854
  size_t generation_offset_;
855
  size_t slot_offset_;
856
  size_t alloc_size_;
857
};
858
859
struct HashtableFreeFunctionsAccess;
860
861
// This allows us to work around an uninitialized memory warning when
862
// constructing begin() iterators in empty hashtables.
863
template <typename T>
864
union MaybeInitializedPtr {
865
34.6M
  T* get() const { ABSL_SWISSTABLE_IGNORE_UNINITIALIZED_RETURN(p); }
absl::container_internal::MaybeInitializedPtr<void>::get() const
Line
Count
Source
865
8.75M
  T* get() const { ABSL_SWISSTABLE_IGNORE_UNINITIALIZED_RETURN(p); }
absl::container_internal::MaybeInitializedPtr<absl::container_internal::ctrl_t>::get() const
Line
Count
Source
865
25.8M
  T* get() const { ABSL_SWISSTABLE_IGNORE_UNINITIALIZED_RETURN(p); }
866
402k
  void set(T* ptr) { p = ptr; }
absl::container_internal::MaybeInitializedPtr<absl::container_internal::ctrl_t>::set(absl::container_internal::ctrl_t*)
Line
Count
Source
866
201k
  void set(T* ptr) { p = ptr; }
absl::container_internal::MaybeInitializedPtr<void>::set(void*)
Line
Count
Source
866
201k
  void set(T* ptr) { p = ptr; }
867
868
  T* p;
869
};
870
871
struct HeapPtrs {
872
  // The control bytes (and, also, a pointer near to the base of the backing
873
  // array).
874
  //
875
  // This contains `capacity + 1 + NumClonedBytes()` entries.
876
  //
877
  // Note that growth_info is stored immediately before this pointer.
878
  // May be uninitialized for small tables.
879
  MaybeInitializedPtr<ctrl_t> control;
880
881
  // The beginning of the slots, located at `SlotOffset()` bytes after
882
  // `control`. May be uninitialized for empty tables.
883
  // Note: we can't use `slots` because Qt defines "slots" as a macro.
884
  MaybeInitializedPtr<void> slot_array;
885
};
886
887
// Returns the maximum size of the SOO slot.
888
0
constexpr size_t MaxSooSlotSize() { return sizeof(HeapPtrs); }
889
890
// Manages the backing array pointers or the SOO slot. When raw_hash_set::is_soo
891
// is true, the SOO slot is stored in `soo_data`. Otherwise, we use `heap`.
892
union HeapOrSoo {
893
201k
  MaybeInitializedPtr<ctrl_t>& control() {
894
201k
    ABSL_SWISSTABLE_IGNORE_UNINITIALIZED_RETURN(heap.control);
895
201k
  }
896
25.8M
  MaybeInitializedPtr<ctrl_t> control() const {
897
25.8M
    ABSL_SWISSTABLE_IGNORE_UNINITIALIZED_RETURN(heap.control);
898
25.8M
  }
899
201k
  MaybeInitializedPtr<void>& slot_array() {
900
201k
    ABSL_SWISSTABLE_IGNORE_UNINITIALIZED_RETURN(heap.slot_array);
901
201k
  }
902
8.75M
  MaybeInitializedPtr<void> slot_array() const {
903
8.75M
    ABSL_SWISSTABLE_IGNORE_UNINITIALIZED_RETURN(heap.slot_array);
904
8.75M
  }
905
98.1k
  void* get_soo_data() {
906
98.1k
    ABSL_SWISSTABLE_IGNORE_UNINITIALIZED_RETURN(soo_data);
907
98.1k
  }
908
0
  const void* get_soo_data() const {
909
0
    ABSL_SWISSTABLE_IGNORE_UNINITIALIZED_RETURN(soo_data);
910
0
  }
911
912
  HeapPtrs heap;
913
  unsigned char soo_data[MaxSooSlotSize()];
914
};
915
916
// Returns a reference to the GrowthInfo object stored immediately before
917
// `control`.
918
17.1M
inline GrowthInfo& GetGrowthInfoFromControl(ctrl_t* control) {
919
17.1M
  auto* gl_ptr = reinterpret_cast<GrowthInfo*>(control) - 1;
920
17.1M
  ABSL_SWISSTABLE_ASSERT(
921
17.1M
      reinterpret_cast<uintptr_t>(gl_ptr) % alignof(GrowthInfo) == 0);
922
17.1M
  return *gl_ptr;
923
17.1M
}
924
925
// CommonFields hold the fields in raw_hash_set that do not depend
926
// on template parameters. This allows us to conveniently pass all
927
// of this state to helper functions as a single argument.
928
class CommonFields : public CommonFieldsGenerationInfo {
929
 public:
930
  explicit CommonFields(soo_tag_t)
931
14.9k
      : capacity_(SooCapacity()), size_(no_seed_empty_tag_t{}) {}
932
  explicit CommonFields(full_soo_tag_t, bool has_tried_sampling)
933
0
      : capacity_(SooCapacity()), size_(full_soo_tag_t{}, has_tried_sampling) {}
934
  explicit CommonFields(non_soo_tag_t)
935
0
      : capacity_(0), size_(no_seed_empty_tag_t{}) {}
936
  // For use in swapping.
937
0
  explicit CommonFields(uninitialized_tag_t) : size_(uninitialized_tag_t{}) {}
938
939
  // Not copyable
940
  CommonFields(const CommonFields&) = delete;
941
  CommonFields& operator=(const CommonFields&) = delete;
942
943
  // Copy with guarantee that it is not SOO.
944
  CommonFields(non_soo_tag_t, const CommonFields& that)
945
      : capacity_(that.capacity_),
946
        size_(that.size_),
947
0
        heap_or_soo_(that.heap_or_soo_) {
948
0
  }
949
950
  // Movable
951
  CommonFields(CommonFields&& that) = default;
952
  CommonFields& operator=(CommonFields&&) = default;
953
954
  template <bool kSooEnabled>
955
  static CommonFields CreateDefault() {
956
    return kSooEnabled ? CommonFields{soo_tag_t{}}
957
                       : CommonFields{non_soo_tag_t{}};
958
  }
959
960
  // The inline data for SOO is written on top of control_/slots_.
961
0
  const void* soo_data() const { return heap_or_soo_.get_soo_data(); }
962
98.1k
  void* soo_data() { return heap_or_soo_.get_soo_data(); }
963
964
25.8M
  ctrl_t* control() const {
965
25.8M
    ABSL_SWISSTABLE_ASSERT(capacity() > 0);
966
    // Assume that the control bytes don't alias `this`.
967
25.8M
    ctrl_t* ctrl = heap_or_soo_.control().get();
968
25.8M
    [[maybe_unused]] size_t num_control_bytes = NumControlBytes(capacity());
969
25.8M
    ABSL_ASSUME(reinterpret_cast<uintptr_t>(ctrl + num_control_bytes) <=
970
25.8M
                    reinterpret_cast<uintptr_t>(this) ||
971
25.8M
                reinterpret_cast<uintptr_t>(this + 1) <=
972
25.8M
                    reinterpret_cast<uintptr_t>(ctrl));
973
25.8M
    ABSL_SWISSTABLE_IGNORE_UNINITIALIZED_RETURN(ctrl);
974
25.8M
  }
975
976
201k
  void set_control(ctrl_t* c) { heap_or_soo_.control().set(c); }
977
978
  // Note: we can't use slots() because Qt defines "slots" as a macro.
979
8.75M
  void* slot_array() const { return heap_or_soo_.slot_array().get(); }
980
0
  MaybeInitializedPtr<void> slots_union() const {
981
0
    return heap_or_soo_.slot_array();
982
0
  }
983
201k
  void set_slots(void* s) { heap_or_soo_.slot_array().set(s); }
984
985
  // The number of filled slots.
986
8.86M
  size_t size() const { return size_.size(); }
987
  // Sets the size to zero, but keeps hashinfoz bit and seed.
988
220k
  void set_size_to_zero() { size_.set_size_keep_metadata(0); }
989
0
  void set_empty_soo() {
990
0
    AssertInSooMode();
991
0
    size_.set_size_keep_metadata(0);
992
0
  }
993
0
  void set_full_soo() {
994
0
    AssertInSooMode();
995
0
    size_.set_size_keep_metadata(1);
996
0
  }
997
8.33M
  void increment_size() {
998
8.33M
    ABSL_SWISSTABLE_ASSERT(size() < capacity());
999
8.33M
    size_.increment_size();
1000
8.33M
  }
1001
0
  void increment_size(size_t n) {
1002
0
    ABSL_SWISSTABLE_ASSERT(size() + n <= capacity());
1003
0
    size_.increment_size(n);
1004
0
  }
1005
0
  void decrement_size() {
1006
0
    ABSL_SWISSTABLE_ASSERT(!empty());
1007
0
    size_.decrement_size();
1008
0
  }
1009
0
  bool empty() const { return size_.empty(); }
1010
0
  void set_soo_has_tried_sampling() { size_.set_soo_has_tried_sampling(); }
1011
0
  bool soo_has_tried_sampling() const { return size_.soo_has_tried_sampling(); }
1012
1013
  // The seed used for the hash function.
1014
206k
  PerTableSeed seed() const { return size_.seed(); }
1015
  // Generates a new seed the hash function.
1016
  // The table will be invalidated if `!empty()` because hash is being changed.
1017
  // In such cases, we will need to rehash the table.
1018
49.0k
  void generate_new_seed(bool has_infoz) {
1019
    // Note: we can't use has_infoz() here because we set has_infoz later than
1020
    // we generate the seed.
1021
49.0k
    if (ABSL_PREDICT_FALSE(has_infoz)) {
1022
0
      size_.set_sampled_seed();
1023
0
      return;
1024
0
    }
1025
49.0k
    size_.generate_new_seed();
1026
49.0k
  }
1027
0
  void set_no_seed_for_testing() { size_.set_no_seed_for_testing(); }
1028
1029
  // The total number of available slots.
1030
94.9M
  size_t capacity() const { return capacity_; }
1031
201k
  void set_capacity(size_t c) {
1032
    // We allow setting above the max valid capacity for debugging purposes.
1033
201k
    ABSL_SWISSTABLE_ASSERT(c == 0 || IsValidCapacity(c) ||
1034
201k
                           c > kAboveMaxValidCapacity);
1035
201k
    capacity_ = c;
1036
201k
  }
1037
33.4M
  bool is_small() const { return IsSmallCapacity(capacity_); }
1038
1039
  // The number of slots we can still fill without needing to rehash.
1040
  // This is stored in the heap allocation before the control bytes.
1041
  // TODO(b/289225379): experiment with moving growth_info back inline to
1042
  // increase room for SOO.
1043
152k
  size_t growth_left() const { return growth_info().GetGrowthLeft(); }
1044
1045
16.9M
  GrowthInfo& growth_info() {
1046
16.9M
    ABSL_SWISSTABLE_ASSERT(!is_small());
1047
16.9M
    return GetGrowthInfoFromControl(control());
1048
16.9M
  }
1049
152k
  GrowthInfo growth_info() const {
1050
152k
    return const_cast<CommonFields*>(this)->growth_info();
1051
152k
  }
1052
1053
8.61M
  bool has_infoz() const { return size_.has_infoz(); }
1054
0
  void set_has_infoz() {
1055
0
    ABSL_SWISSTABLE_ASSERT(size_.is_sampled_seed());
1056
0
    size_.set_has_infoz();
1057
0
  }
1058
1059
0
  HashtablezInfoHandle* infoz_ptr() const {
1060
    // growth_info is stored before control bytes.
1061
0
    ABSL_SWISSTABLE_ASSERT(
1062
0
        reinterpret_cast<uintptr_t>(control()) % alignof(size_t) == 0);
1063
0
    ABSL_SWISSTABLE_ASSERT(has_infoz());
1064
0
    return reinterpret_cast<HashtablezInfoHandle*>(
1065
0
        control() - ControlOffset(/*has_infoz=*/true));
1066
0
  }
1067
1068
8.59M
  HashtablezInfoHandle infoz() {
1069
8.59M
    return has_infoz() ? *infoz_ptr() : HashtablezInfoHandle();
1070
8.59M
  }
1071
0
  void set_infoz(HashtablezInfoHandle infoz) {
1072
0
    ABSL_SWISSTABLE_ASSERT(has_infoz());
1073
0
    *infoz_ptr() = infoz;
1074
0
  }
1075
1076
0
  bool should_rehash_for_bug_detection_on_insert() const {
1077
0
    if constexpr (!SwisstableGenerationsEnabled()) {
1078
0
      return false;
1079
0
    }
1080
0
    // As an optimization, we avoid calling ShouldRehashForBugDetection if we
1081
0
    // will end up rehashing anyways.
1082
0
    if (growth_left() == 0) return false;
1083
0
    return CommonFieldsGenerationInfo::
1084
0
        should_rehash_for_bug_detection_on_insert(capacity());
1085
0
  }
1086
0
  bool should_rehash_for_bug_detection_on_move() const {
1087
0
    return CommonFieldsGenerationInfo::should_rehash_for_bug_detection_on_move(
1088
0
        capacity());
1089
0
  }
1090
0
  void reset_reserved_growth(size_t reservation) {
1091
0
    CommonFieldsGenerationInfo::reset_reserved_growth(reservation, size());
1092
0
  }
1093
1094
  // The size of the backing array allocation.
1095
0
  size_t alloc_size(size_t slot_size, size_t slot_align) const {
1096
0
    return RawHashSetLayout(capacity(), slot_size, slot_align, has_infoz())
1097
0
        .alloc_size();
1098
0
  }
1099
1100
  // Move fields other than heap_or_soo_.
1101
0
  void move_non_heap_or_soo_fields(CommonFields& that) {
1102
0
    static_cast<CommonFieldsGenerationInfo&>(*this) =
1103
0
        std::move(static_cast<CommonFieldsGenerationInfo&>(that));
1104
0
    capacity_ = that.capacity_;
1105
0
    size_ = that.size_;
1106
0
  }
1107
1108
  // Returns the number of control bytes set to kDeleted. For testing only.
1109
0
  size_t TombstonesCount() const {
1110
0
    return static_cast<size_t>(
1111
0
        std::count(control(), control() + capacity(), ctrl_t::kDeleted));
1112
0
  }
1113
1114
  // Helper to enable sanitizer mode validation to protect against reentrant
1115
  // calls during element constructor/destructor.
1116
  template <typename F>
1117
  void RunWithReentrancyGuard(F f) {
1118
#ifdef NDEBUG
1119
    f();
1120
    return;
1121
#endif
1122
    const size_t cap = capacity();
1123
    set_capacity(InvalidCapacity::kReentrance);
1124
    f();
1125
    set_capacity(cap);
1126
  }
1127
1128
 private:
1129
  // We store the has_infoz bit in the lowest bit of size_.
1130
0
  static constexpr size_t HasInfozShift() { return 1; }
1131
0
  static constexpr size_t HasInfozMask() {
1132
0
    return (size_t{1} << HasInfozShift()) - 1;
1133
0
  }
1134
1135
  // We can't assert that SOO is enabled because we don't have SooEnabled(), but
1136
  // we assert what we can.
1137
0
  void AssertInSooMode() const {
1138
0
    ABSL_SWISSTABLE_ASSERT(capacity() == SooCapacity());
1139
0
    ABSL_SWISSTABLE_ASSERT(!has_infoz());
1140
0
  }
1141
1142
  // The number of slots in the backing array. This is always 2^N-1 for an
1143
  // integer N. NOTE: we tried experimenting with compressing the capacity and
1144
  // storing it together with size_: (a) using 6 bits to store the corresponding
1145
  // power (N in 2^N-1), and (b) storing 2^N as the most significant bit of
1146
  // size_ and storing size in the low bits. Both of these experiments were
1147
  // regressions, presumably because we need capacity to do find operations.
1148
  size_t capacity_;
1149
1150
  // TODO(b/289225379): we could put size_ into HeapOrSoo and make capacity_
1151
  // encode the size in SOO case. We would be making size()/capacity() more
1152
  // expensive in order to have more SOO space.
1153
  HashtableSize size_;
1154
1155
  // Either the control/slots pointers or the SOO slot.
1156
  HeapOrSoo heap_or_soo_;
1157
};
1158
1159
template <class Policy, class... Params>
1160
class raw_hash_set;
1161
1162
// Returns the next valid capacity after `n`.
1163
152k
constexpr size_t NextCapacity(size_t n) {
1164
152k
  ABSL_SWISSTABLE_ASSERT(IsValidCapacity(n) || n == 0);
1165
152k
  return n * 2 + 1;
1166
152k
}
1167
1168
// Returns the previous valid capacity before `n`.
1169
0
constexpr size_t PreviousCapacity(size_t n) {
1170
0
  ABSL_SWISSTABLE_ASSERT(IsValidCapacity(n));
1171
0
  return n / 2;
1172
0
}
1173
1174
// Applies the following mapping to every byte in the control array:
1175
//   * kDeleted -> kEmpty
1176
//   * kEmpty -> kEmpty
1177
//   * _ -> kDeleted
1178
// PRECONDITION:
1179
//   IsValidCapacity(capacity)
1180
//   ctrl[capacity] == ctrl_t::kSentinel
1181
//   ctrl[i] != ctrl_t::kSentinel for all i < capacity
1182
void ConvertDeletedToEmptyAndFullToDeleted(ctrl_t* ctrl, size_t capacity);
1183
1184
// Converts `n` into the next valid capacity, per `IsValidCapacity`.
1185
0
constexpr size_t NormalizeCapacity(size_t n) {
1186
0
  return n ? ~size_t{} >> countl_zero(n) : 1;
1187
0
}
1188
1189
// General notes on capacity/growth methods below:
1190
// - We use 7/8th as maximum load factor. For 16-wide groups, that gives an
1191
//   average of two empty slots per group.
1192
// - For (capacity+1) >= Group::kWidth, growth is 7/8*capacity.
1193
// - For (capacity+1) < Group::kWidth, growth == capacity. In this case, we
1194
//   never need to probe (the whole table fits in one group) so we don't need a
1195
//   load factor less than 1.
1196
1197
// Given `capacity`, applies the load factor; i.e., it returns the maximum
1198
// number of values we should put into the table before a resizing rehash.
1199
372k
constexpr size_t CapacityToGrowth(size_t capacity) {
1200
372k
  ABSL_SWISSTABLE_ASSERT(IsValidCapacity(capacity));
1201
  // `capacity*7/8`
1202
372k
  if (Group::kWidth == 8 && capacity == 7) {
1203
    // x-x/8 does not work when x==7.
1204
0
    return 6;
1205
0
  }
1206
372k
  return capacity - capacity / 8;
1207
372k
}
1208
1209
// Given `size`, "unapplies" the load factor to find how large the capacity
1210
// should be to stay within the load factor.
1211
//
1212
// For size == 0, returns 0.
1213
// For other values, returns the same as `NormalizeCapacity(size*8/7)`.
1214
0
constexpr size_t SizeToCapacity(size_t size) {
1215
0
  if (size == 0) {
1216
0
    return 0;
1217
0
  }
1218
  // The minimum possible capacity is NormalizeCapacity(size).
1219
  // Shifting right `~size_t{}` by `leading_zeros` yields
1220
  // NormalizeCapacity(size).
1221
0
  int leading_zeros = absl::countl_zero(size);
1222
0
  constexpr size_t kLast3Bits = size_t{7} << (sizeof(size_t) * 8 - 3);
1223
  // max_size_for_next_capacity = max_load_factor * next_capacity
1224
  //                            = (7/8) * (~size_t{} >> leading_zeros)
1225
  //                            = (7/8*~size_t{}) >> leading_zeros
1226
  //                            = kLast3Bits >> leading_zeros
1227
0
  size_t max_size_for_next_capacity = kLast3Bits >> leading_zeros;
1228
  // Decrease shift if size is too big for the minimum capacity.
1229
0
  leading_zeros -= static_cast<int>(size > max_size_for_next_capacity);
1230
  if constexpr (Group::kWidth == 8) {
1231
    // Formula doesn't work when size==7 for 8-wide groups.
1232
    leading_zeros -= (size == 7);
1233
  }
1234
0
  return (~size_t{}) >> leading_zeros;
1235
0
}
1236
1237
template <class InputIter>
1238
size_t SelectBucketCountForIterRange(InputIter first, InputIter last,
1239
                                     size_t bucket_count) {
1240
  if (bucket_count != 0) {
1241
    return bucket_count;
1242
  }
1243
  if (base_internal::IsAtLeastIterator<std::random_access_iterator_tag,
1244
                                       InputIter>()) {
1245
    return SizeToCapacity(static_cast<size_t>(std::distance(first, last)));
1246
  }
1247
  return 0;
1248
}
1249
1250
0
constexpr bool SwisstableDebugEnabled() {
1251
0
#if defined(ABSL_SWISSTABLE_ENABLE_GENERATIONS) || \
1252
0
    ABSL_OPTION_HARDENED == 1 || !defined(NDEBUG)
1253
0
  return true;
1254
0
#else
1255
0
  return false;
1256
0
#endif
1257
0
}
1258
1259
inline void AssertIsFull(const ctrl_t* ctrl, GenerationType generation,
1260
                         const GenerationType* generation_ptr,
1261
0
                         const char* operation) {
1262
0
  if (!SwisstableDebugEnabled()) return;
1263
0
  // `SwisstableDebugEnabled()` is also true for release builds with hardening
1264
0
  // enabled. To minimize their impact in those builds:
1265
0
  // - use `ABSL_PREDICT_FALSE()` to provide a compiler hint for code layout
1266
0
  // - use `ABSL_RAW_LOG()` with a format string to reduce code size and improve
1267
0
  //   the chances that the hot paths will be inlined.
1268
0
  if (ABSL_PREDICT_FALSE(ctrl == nullptr)) {
1269
0
    ABSL_RAW_LOG(FATAL, "%s called on end() iterator.", operation);
1270
0
  }
1271
0
  if (ABSL_PREDICT_FALSE(ctrl == DefaultIterControl())) {
1272
0
    ABSL_RAW_LOG(FATAL, "%s called on default-constructed iterator.",
1273
0
                 operation);
1274
0
  }
1275
0
  if (SwisstableGenerationsEnabled()) {
1276
0
    if (ABSL_PREDICT_FALSE(generation != *generation_ptr)) {
1277
0
      ABSL_RAW_LOG(FATAL,
1278
0
                   "%s called on invalid iterator. The table could have "
1279
0
                   "rehashed or moved since this iterator was initialized.",
1280
0
                   operation);
1281
0
    }
1282
0
    if (ABSL_PREDICT_FALSE(!IsFull(*ctrl))) {
1283
0
      ABSL_RAW_LOG(
1284
0
          FATAL,
1285
0
          "%s called on invalid iterator. The element was likely erased.",
1286
0
          operation);
1287
0
    }
1288
0
  } else {
1289
0
    if (ABSL_PREDICT_FALSE(!IsFull(*ctrl))) {
1290
0
      ABSL_RAW_LOG(
1291
0
          FATAL,
1292
0
          "%s called on invalid iterator. The element might have been erased "
1293
0
          "or the table might have rehashed. Consider running with "
1294
0
          "--config=asan to diagnose rehashing issues.",
1295
0
          operation);
1296
0
    }
1297
0
  }
1298
0
}
1299
1300
// Note that for comparisons, null/end iterators are valid.
1301
inline void AssertIsValidForComparison(const ctrl_t* ctrl,
1302
                                       GenerationType generation,
1303
0
                                       const GenerationType* generation_ptr) {
1304
0
  if (!SwisstableDebugEnabled()) return;
1305
0
  const bool ctrl_is_valid_for_comparison =
1306
0
      ctrl == nullptr || ctrl == DefaultIterControl() || IsFull(*ctrl);
1307
0
  if (SwisstableGenerationsEnabled()) {
1308
0
    if (ABSL_PREDICT_FALSE(generation != *generation_ptr)) {
1309
0
      ABSL_RAW_LOG(FATAL,
1310
0
                   "Invalid iterator comparison. The table could have rehashed "
1311
0
                   "or moved since this iterator was initialized.");
1312
0
    }
1313
0
    if (ABSL_PREDICT_FALSE(!ctrl_is_valid_for_comparison)) {
1314
0
      ABSL_RAW_LOG(
1315
0
          FATAL, "Invalid iterator comparison. The element was likely erased.");
1316
0
    }
1317
0
  } else {
1318
0
    ABSL_HARDENING_ASSERT_SLOW(
1319
0
        ctrl_is_valid_for_comparison &&
1320
0
        "Invalid iterator comparison. The element might have been erased or "
1321
0
        "the table might have rehashed. Consider running with --config=asan to "
1322
0
        "diagnose rehashing issues.");
1323
0
  }
1324
0
}
1325
1326
// If the two iterators come from the same container, then their pointers will
1327
// interleave such that ctrl_a <= ctrl_b < slot_a <= slot_b or vice/versa.
1328
// Note: we take slots by reference so that it's not UB if they're uninitialized
1329
// as long as we don't read them (when ctrl is null).
1330
inline bool AreItersFromSameContainer(const ctrl_t* ctrl_a,
1331
                                      const ctrl_t* ctrl_b,
1332
                                      const void* const& slot_a,
1333
0
                                      const void* const& slot_b) {
1334
0
  // If either control byte is null, then we can't tell.
1335
0
  if (ctrl_a == nullptr || ctrl_b == nullptr) return true;
1336
0
  const bool a_is_soo = IsSooControl(ctrl_a);
1337
0
  if (a_is_soo != IsSooControl(ctrl_b)) return false;
1338
0
  if (a_is_soo) return slot_a == slot_b;
1339
0
1340
0
  const void* low_slot = slot_a;
1341
0
  const void* hi_slot = slot_b;
1342
0
  if (ctrl_a > ctrl_b) {
1343
0
    std::swap(ctrl_a, ctrl_b);
1344
0
    std::swap(low_slot, hi_slot);
1345
0
  }
1346
0
  return ctrl_b < low_slot && low_slot <= hi_slot;
1347
0
}
1348
1349
// Asserts that two iterators come from the same container.
1350
// Note: we take slots by reference so that it's not UB if they're uninitialized
1351
// as long as we don't read them (when ctrl is null).
1352
inline void AssertSameContainer(const ctrl_t* ctrl_a, const ctrl_t* ctrl_b,
1353
                                const void* const& slot_a,
1354
                                const void* const& slot_b,
1355
                                const GenerationType* generation_ptr_a,
1356
0
                                const GenerationType* generation_ptr_b) {
1357
0
  if (!SwisstableDebugEnabled()) return;
1358
0
  // `SwisstableDebugEnabled()` is also true for release builds with hardening
1359
0
  // enabled. To minimize their impact in those builds:
1360
0
  // - use `ABSL_PREDICT_FALSE()` to provide a compiler hint for code layout
1361
0
  // - use `ABSL_RAW_LOG()` with a format string to reduce code size and improve
1362
0
  //   the chances that the hot paths will be inlined.
1363
0
1364
0
  // fail_if(is_invalid, message) crashes when is_invalid is true and provides
1365
0
  // an error message based on `message`.
1366
0
  const auto fail_if = [](bool is_invalid, const char* message) {
1367
0
    if (ABSL_PREDICT_FALSE(is_invalid)) {
1368
0
      ABSL_RAW_LOG(FATAL, "Invalid iterator comparison. %s", message);
1369
0
    }
1370
0
  };
1371
0
1372
0
  const bool a_is_default = ctrl_a == DefaultIterControl();
1373
0
  const bool b_is_default = ctrl_b == DefaultIterControl();
1374
0
  if (a_is_default && b_is_default) return;
1375
0
  fail_if(a_is_default != b_is_default,
1376
0
          "Comparing default-constructed hashtable iterator with a "
1377
0
          "non-default-constructed hashtable iterator.");
1378
0
1379
0
  if (SwisstableGenerationsEnabled()) {
1380
0
    if (ABSL_PREDICT_TRUE(generation_ptr_a == generation_ptr_b)) return;
1381
0
    const bool a_is_empty = IsEmptyGeneration(generation_ptr_a);
1382
0
    const bool b_is_empty = IsEmptyGeneration(generation_ptr_b);
1383
0
    fail_if(a_is_empty != b_is_empty,
1384
0
            "Comparing an iterator from an empty hashtable with an iterator "
1385
0
            "from a non-empty hashtable.");
1386
0
    fail_if(a_is_empty && b_is_empty,
1387
0
            "Comparing iterators from different empty hashtables.");
1388
0
1389
0
    const bool a_is_end = ctrl_a == nullptr;
1390
0
    const bool b_is_end = ctrl_b == nullptr;
1391
0
    fail_if(a_is_end || b_is_end,
1392
0
            "Comparing iterator with an end() iterator from a different "
1393
0
            "hashtable.");
1394
0
    fail_if(true, "Comparing non-end() iterators from different hashtables.");
1395
0
  } else {
1396
0
    ABSL_HARDENING_ASSERT_SLOW(
1397
0
        AreItersFromSameContainer(ctrl_a, ctrl_b, slot_a, slot_b) &&
1398
0
        "Invalid iterator comparison. The iterators may be from different "
1399
0
        "containers or the container might have rehashed or moved. Consider "
1400
0
        "running with --config=asan to diagnose issues.");
1401
0
  }
1402
0
}
1403
1404
struct FindInfo {
1405
  size_t offset;
1406
  size_t probe_length;
1407
};
1408
1409
// Whether a table fits entirely into a probing group.
1410
// Arbitrary order of elements in such tables is correct.
1411
271k
constexpr bool is_single_group(size_t capacity) {
1412
271k
  return capacity <= Group::kWidth;
1413
271k
}
1414
1415
// Begins a probing operation on `common.control`, using `hash`.
1416
164k
inline probe_seq<Group::kWidth> probe_h1(size_t capacity, size_t h1) {
1417
164k
  return probe_seq<Group::kWidth>(h1, capacity);
1418
164k
}
1419
0
inline probe_seq<Group::kWidth> probe(size_t capacity, size_t hash) {
1420
0
  return probe_h1(capacity, H1(hash));
1421
0
}
1422
0
inline probe_seq<Group::kWidth> probe(const CommonFields& common, size_t hash) {
1423
0
  return probe(common.capacity(), hash);
1424
0
}
1425
1426
constexpr size_t kProbedElementIndexSentinel = ~size_t{};
1427
1428
// Implementation detail of transfer_unprobed_elements_to_next_capacity_fn.
1429
// Tries to find the new index for an element whose hash corresponds to
1430
// `h1` for growth to the next capacity.
1431
// Returns kProbedElementIndexSentinel if full probing is required.
1432
//
1433
// If element is located in the first probing group in the table before growth,
1434
// returns one of two positions: `old_index` or `old_index + old_capacity + 1`.
1435
//
1436
// Otherwise, we will try to insert it into the first probe group of the new
1437
// table. We only attempt to do so if the first probe group is already
1438
// initialized.
1439
template <typename = void>
1440
inline size_t TryFindNewIndexWithoutProbing(size_t h1, size_t old_index,
1441
                                            size_t old_capacity,
1442
                                            ctrl_t* new_ctrl,
1443
0
                                            size_t new_capacity) {
1444
0
  size_t index_diff = old_index - h1;
1445
  // The first probe group starts with h1 & capacity.
1446
  // All following groups start at (h1 + Group::kWidth * K) & capacity.
1447
  // We can find an index within the floating group as index_diff modulo
1448
  // Group::kWidth.
1449
  // Both old and new capacity are larger than Group::kWidth so we can avoid
1450
  // computing `& capacity`.
1451
0
  size_t in_floating_group_index = index_diff & (Group::kWidth - 1);
1452
  // By subtracting we will get the difference between the first probe group
1453
  // and the probe group corresponding to old_index.
1454
0
  index_diff -= in_floating_group_index;
1455
0
  if (ABSL_PREDICT_TRUE((index_diff & old_capacity) == 0)) {
1456
0
    size_t new_index = (h1 + in_floating_group_index) & new_capacity;
1457
0
    ABSL_ASSUME(new_index != kProbedElementIndexSentinel);
1458
0
    return new_index;
1459
0
  }
1460
0
  ABSL_SWISSTABLE_ASSERT(((old_index - h1) & old_capacity) >= Group::kWidth);
1461
  // Try to insert element into the first probe group.
1462
  // new_ctrl is not yet fully initialized so we can't use regular search via
1463
  // find_first_non_full.
1464
1465
  // We can search in the first probe group only if it is located in already
1466
  // initialized part of the table.
1467
0
  if (ABSL_PREDICT_FALSE((h1 & old_capacity) >= old_index)) {
1468
0
    return kProbedElementIndexSentinel;
1469
0
  }
1470
0
  size_t offset = h1 & new_capacity;
1471
0
  Group new_g(new_ctrl + offset);
1472
0
  if (auto mask = new_g.MaskNonFull(); ABSL_PREDICT_TRUE(mask)) {
1473
0
    size_t result = offset + mask.LowestBitSet();
1474
0
    ABSL_ASSUME(result != kProbedElementIndexSentinel);
1475
0
    return result;
1476
0
  }
1477
0
  return kProbedElementIndexSentinel;
1478
0
}
1479
1480
// Extern template for inline function keeps possibility of inlining.
1481
// When compiler decided to not inline, no symbols will be added to the
1482
// corresponding translation unit.
1483
extern template size_t TryFindNewIndexWithoutProbing(size_t h1,
1484
                                                     size_t old_index,
1485
                                                     size_t old_capacity,
1486
                                                     ctrl_t* new_ctrl,
1487
                                                     size_t new_capacity);
1488
1489
// growth_info (which is a size_t) is stored with the backing array.
1490
0
constexpr size_t BackingArrayAlignment(size_t align_of_slot) {
1491
0
  return (std::max)(align_of_slot, alignof(GrowthInfo));
1492
0
}
1493
1494
// Iterates over all full slots and calls `cb(const ctrl_t*, void*)`.
1495
// No insertion to the table is allowed during `cb` call.
1496
// Erasure is allowed only for the element passed to the callback.
1497
// The table must not be in SOO mode.
1498
void IterateOverFullSlots(const CommonFields& c, size_t slot_size,
1499
                          absl::FunctionRef<void(const ctrl_t*, void*)> cb);
1500
1501
template <typename CharAlloc>
1502
constexpr bool ShouldSampleHashtablezInfoForAlloc() {
1503
  // Folks with custom allocators often make unwarranted assumptions about the
1504
  // behavior of their classes vis-a-vis trivial destructability and what
1505
  // calls they will or won't make.  Avoid sampling for people with custom
1506
  // allocators to get us out of this mess.  This is not a hard guarantee but
1507
  // a workaround while we plan the exact guarantee we want to provide.
1508
  return std::is_same_v<CharAlloc, std::allocator<char>>;
1509
}
1510
1511
// Allocates `n` bytes for a backing array.
1512
template <size_t AlignOfBackingArray, typename Alloc>
1513
201k
void* AllocateBackingArray(void* alloc, size_t n) {
1514
201k
  return Allocate<AlignOfBackingArray>(static_cast<Alloc*>(alloc), n);
1515
201k
}
1516
1517
// Note: we mark this function as ABSL_ATTRIBUTE_NOINLINE because we don't want
1518
// it to be inlined into e.g. the destructor to save code size.
1519
template <size_t AlignOfBackingArray, typename Alloc>
1520
ABSL_ATTRIBUTE_NOINLINE void DeallocateBackingArray(
1521
    void* alloc, size_t capacity, ctrl_t* ctrl, size_t slot_size,
1522
201k
    size_t slot_align, bool had_infoz) {
1523
201k
  RawHashSetLayout layout(capacity, slot_size, slot_align, had_infoz);
1524
201k
  void* backing_array = ctrl - layout.control_offset();
1525
  // Unpoison before returning the memory to the allocator.
1526
201k
  SanitizerUnpoisonMemoryRegion(backing_array, layout.alloc_size());
1527
201k
  Deallocate<AlignOfBackingArray>(static_cast<Alloc*>(alloc), backing_array,
1528
201k
                                  layout.alloc_size());
1529
201k
}
1530
1531
// PolicyFunctions bundles together some information for a particular
1532
// raw_hash_set<T, ...> instantiation. This information is passed to
1533
// type-erased functions that want to do small amounts of type-specific
1534
// work.
1535
struct PolicyFunctions {
1536
  uint32_t key_size;
1537
  uint32_t value_size;
1538
  uint32_t slot_size;
1539
  uint16_t slot_align;
1540
  bool soo_enabled;
1541
  bool is_hashtablez_eligible;
1542
1543
  // Returns the pointer to the hash function stored in the set.
1544
  void* (*hash_fn)(CommonFields& common);
1545
1546
  // Returns the hash of the pointed-to slot.
1547
  HashSlotFn hash_slot;
1548
1549
  // Transfers the contents of `count` slots from src_slot to dst_slot.
1550
  // We use ability to transfer several slots in single group table growth.
1551
  void (*transfer_n)(void* set, void* dst_slot, void* src_slot, size_t count);
1552
1553
  // Returns the pointer to the CharAlloc stored in the set.
1554
  void* (*get_char_alloc)(CommonFields& common);
1555
1556
  // Allocates n bytes for the backing store for common.
1557
  void* (*alloc)(void* alloc, size_t n);
1558
1559
  // Deallocates the backing store from common.
1560
  void (*dealloc)(void* alloc, size_t capacity, ctrl_t* ctrl, size_t slot_size,
1561
                  size_t slot_align, bool had_infoz);
1562
1563
  // Implementation detail of GrowToNextCapacity.
1564
  // Iterates over all full slots and transfers unprobed elements.
1565
  // Initializes the new control bytes except mirrored bytes and kSentinel.
1566
  // Caller must finish the initialization.
1567
  // All slots corresponding to the full control bytes are transferred.
1568
  // Probed elements are reported by `encode_probed_element` callback.
1569
  // encode_probed_element may overwrite old_ctrl buffer till source_offset.
1570
  // Different encoding is used depending on the capacity of the table.
1571
  // See ProbedItem*Bytes classes for details.
1572
  void (*transfer_unprobed_elements_to_next_capacity)(
1573
      CommonFields& common, const ctrl_t* old_ctrl, void* old_slots,
1574
      // TODO(b/382423690): Try to use absl::FunctionRef here.
1575
      void* probed_storage,
1576
      void (*encode_probed_element)(void* probed_storage, h2_t h2,
1577
                                    size_t source_offset, size_t h1));
1578
1579
402k
  uint8_t soo_capacity() const {
1580
402k
    return static_cast<uint8_t>(soo_enabled ? SooCapacity() : 0);
1581
402k
  }
1582
};
1583
1584
// The following functions are used for calculating the max valid size of the
1585
// table. This is important for security to avoid overflowing size_t when
1586
// calculating the allocation size of the backing array
1587
// (https://nvd.nist.gov/vuln/detail/CVE-2025-0838). We also limit the max valid
1588
// size based on the size of the key_type, and this is an optimization because
1589
// we ABSL_ASSUME that the size is less than MaxValidSize, which can enable
1590
// other optimizations for tables with small keys.
1591
1592
template <size_t kSizeOfSizeT = sizeof(size_t)>
1593
0
constexpr size_t MaxSizeAtMaxValidCapacity(size_t slot_size) {
1594
0
  using SizeT = std::conditional_t<kSizeOfSizeT == 4, uint32_t, uint64_t>;
1595
  // We shift right by 2 for a safe margin against overflow.
1596
0
  constexpr SizeT kMaxValidCapacity = ~SizeT{} >> 2;
1597
0
  return CapacityToGrowth(kMaxValidCapacity) / slot_size;
1598
0
}
1599
1600
0
constexpr size_t MaxStorableSize() {
1601
0
  return static_cast<size_t>(uint64_t{1} << HashtableSize::kSizeBitCount) - 1;
1602
0
}
1603
1604
// There are no more than 2^sizeof(key_type) unique key_types (and hashtable
1605
// keys must be unique) so we can't have a hashtable with more than
1606
// 2^sizeof(key_type) elements.
1607
template <size_t kSizeOfSizeT = sizeof(size_t)>
1608
0
constexpr size_t MaxValidSizeForKeySize(size_t key_size) {
1609
0
  if (key_size < kSizeOfSizeT) return size_t{1} << 8 * key_size;
1610
0
  return (std::numeric_limits<size_t>::max)();
1611
0
}
1612
1613
template <size_t kSizeOfSizeT = sizeof(size_t)>
1614
0
constexpr size_t MaxValidSizeForSlotSize(size_t slot_size) {
1615
0
  if constexpr (kSizeOfSizeT == 8) {
1616
    // For small slot sizes we are limited by HashtableSize::kSizeBitCount.
1617
0
    if (slot_size < size_t{1} << (64 - HashtableSize::kSizeBitCount)) {
1618
0
      return MaxStorableSize();
1619
0
    }
1620
0
  }
1621
0
  return MaxSizeAtMaxValidCapacity<kSizeOfSizeT>(slot_size);
1622
0
}
1623
1624
// Returns the maximum valid size for a table, given the key size and slot size.
1625
// Template parameter is only used to enable testing.
1626
template <size_t kSizeOfSizeT = sizeof(size_t)>
1627
0
constexpr size_t MaxValidSize(size_t key_size, size_t slot_size) {
1628
0
  return (std::min)(MaxValidSizeForKeySize<kSizeOfSizeT>(key_size),
1629
0
                    MaxValidSizeForSlotSize<kSizeOfSizeT>(slot_size));
1630
0
}
1631
1632
// Returns the index of the SOO slot when growing from SOO to non-SOO in a
1633
// single group. See also InitializeSmallControlBytesAfterSoo(). It's important
1634
// to use index 1 so that when resizing from capacity 1 to 3, we can still have
1635
// random iteration order between the first two inserted elements.
1636
// I.e. it allows inserting the second element at either index 0 or 2.
1637
98.1k
constexpr size_t SooSlotIndex() { return 1; }
1638
1639
// Maximum capacity for the algorithm for small table after SOO.
1640
// Note that typical size after SOO is 3, but we allow up to 7.
1641
// Allowing till 16 would require additional store that can be avoided.
1642
0
constexpr size_t MaxSmallAfterSooCapacity() { return 7; }
1643
1644
// Type erased version of raw_hash_set::reserve. Requires:
1645
//   1. `new_size > policy.soo_capacity`.
1646
//   2. `new_size <= kMaxValidSize`.
1647
void ReserveTableToFitNewSize(CommonFields& common,
1648
                              const PolicyFunctions& policy, size_t new_size);
1649
1650
// Resizes empty non-allocated table to the next valid capacity after
1651
// `bucket_count`. Requires:
1652
//   1. `c.capacity() == policy.soo_capacity`.
1653
//   2. `c.empty()`.
1654
//   3. `new_size > policy.soo_capacity`.
1655
//   4. `bucket_count <= MaxValidCapacity()`.
1656
// The table will be attempted to be sampled.
1657
void ReserveEmptyNonAllocatedTableToFitBucketCount(
1658
    CommonFields& common, const PolicyFunctions& policy, size_t bucket_count);
1659
1660
// Type erased version of raw_hash_set::rehash.
1661
// Requires: `n <= MaxValidCapacity()`.
1662
void Rehash(CommonFields& common, const PolicyFunctions& policy, size_t n);
1663
1664
// Type erased version of copy constructor.
1665
void Copy(CommonFields& common, const PolicyFunctions& policy,
1666
          const CommonFields& other,
1667
          absl::FunctionRef<void(void*, const void*)> copy_fn);
1668
1669
// Returns the optimal size for memcpy when transferring SOO slot.
1670
// Otherwise, returns the optimal size for memcpy SOO slot transfer
1671
// to SooSlotIndex().
1672
// At the destination we are allowed to copy upto twice more bytes,
1673
// because there is at least one more slot after SooSlotIndex().
1674
// The result must not exceed MaxSooSlotSize().
1675
// Some of the cases are merged to minimize the number of function
1676
// instantiations.
1677
constexpr size_t OptimalMemcpySizeForSooSlotTransfer(
1678
0
    size_t slot_size, size_t max_soo_slot_size = MaxSooSlotSize()) {
1679
0
  static_assert(MaxSooSlotSize() >= 8, "unexpectedly small SOO slot size");
1680
0
  if (slot_size == 1) {
1681
0
    return 1;
1682
0
  }
1683
0
  if (slot_size <= 3) {
1684
0
    return 4;
1685
0
  }
1686
0
  // We are merging 4 and 8 into one case because we expect them to be the
1687
0
  // hottest cases. Copying 8 bytes is as fast on common architectures.
1688
0
  if (slot_size <= 8) {
1689
0
    return 8;
1690
0
  }
1691
0
  if (max_soo_slot_size <= 16) {
1692
0
    return max_soo_slot_size;
1693
0
  }
1694
0
  if (slot_size <= 16) {
1695
0
    return 16;
1696
0
  }
1697
0
  if (max_soo_slot_size <= 24) {
1698
0
    return max_soo_slot_size;
1699
0
  }
1700
0
  static_assert(MaxSooSlotSize() <= 24, "unexpectedly large SOO slot size");
1701
0
  return 24;
1702
0
}
1703
1704
// Resizes SOO table to the NextCapacity(SooCapacity()) and prepares insert for
1705
// the given new_hash. Returns the offset of the new element.
1706
// All possible template combinations are defined in cc file to improve
1707
// compilation time.
1708
template <size_t SooSlotMemcpySize, bool TransferUsesMemcpy>
1709
size_t GrowSooTableToNextCapacityAndPrepareInsert(
1710
    CommonFields& common, const PolicyFunctions& policy,
1711
    absl::FunctionRef<size_t(size_t)> get_hash, bool force_sampling);
1712
1713
// PrepareInsert for small tables (is_small()==true).
1714
// Returns the new control and the new slot.
1715
// Hash is only computed if the table is sampled or grew to large size
1716
// (is_small()==false).
1717
std::pair<ctrl_t*, void*> PrepareInsertSmallNonSoo(
1718
    CommonFields& common, const PolicyFunctions& policy,
1719
    absl::FunctionRef<size_t(size_t)> get_hash);
1720
1721
// Resizes table with allocated slots and change the table seed.
1722
// Tables with SOO enabled must have capacity > policy.soo_capacity.
1723
// No sampling will be performed since table is already allocated.
1724
void ResizeAllocatedTableWithSeedChange(CommonFields& common,
1725
                                        const PolicyFunctions& policy,
1726
                                        size_t new_capacity);
1727
1728
// ClearBackingArray clears the backing array, either modifying it in place,
1729
// or creating a new one based on the value of "reuse".
1730
// REQUIRES: c.capacity > 0
1731
void ClearBackingArray(CommonFields& c, const PolicyFunctions& policy,
1732
                       void* alloc, bool reuse, bool soo_enabled);
1733
1734
// Type-erased versions of raw_hash_set::erase_meta_only_{small,large}.
1735
void EraseMetaOnlySmall(CommonFields& c, bool soo_enabled, size_t slot_size);
1736
void EraseMetaOnlyLarge(CommonFields& c, const ctrl_t* ctrl, size_t slot_size);
1737
1738
// For trivially relocatable types we use memcpy directly. This allows us to
1739
// share the same function body for raw_hash_set instantiations that have the
1740
// same slot size as long as they are relocatable.
1741
// Separate function for relocating single slot cause significant binary bloat.
1742
template <size_t SizeOfSlot>
1743
ABSL_ATTRIBUTE_NOINLINE void TransferNRelocatable(void*, void* dst, void* src,
1744
                                                  size_t count) {
1745
  // TODO(b/382423690): Experiment with making specialization for power of 2 and
1746
  // non power of 2. This would require passing the size of the slot.
1747
  memcpy(dst, src, SizeOfSlot * count);
1748
}
1749
1750
// Returns a pointer to `common`. This is used to implement type erased
1751
// raw_hash_set::get_hash_ref_fn and raw_hash_set::get_alloc_ref_fn for the
1752
// empty class cases.
1753
void* GetRefForEmptyClass(CommonFields& common);
1754
1755
// Given the hash of a value not currently in the table and the first group with
1756
// an empty slot in the probe sequence, finds a viable slot index to insert it
1757
// at.
1758
//
1759
// In case there's no space left, the table can be resized or rehashed
1760
// (for tables with deleted slots, see FindInsertPositionWithGrowthOrRehash).
1761
//
1762
// In the case of absence of deleted slots and positive growth_left, the element
1763
// can be inserted in one of the empty slots in the provided `target_group`.
1764
//
1765
// When the table has deleted slots (according to GrowthInfo), the target
1766
// position will be searched one more time using `find_first_non_full`.
1767
//
1768
// REQUIRES: `!common.is_small()`.
1769
// REQUIRES: At least one non-full slot available.
1770
// REQUIRES: `mask_empty` is a mask containing empty slots for the
1771
//           `target_group`.
1772
// REQUIRES: `target_group` is a starting position for the group that has
1773
//            at least one empty slot.
1774
size_t PrepareInsertLarge(CommonFields& common, const PolicyFunctions& policy,
1775
                          size_t hash, Group::NonIterableBitMaskType mask_empty,
1776
                          FindInfo target_group);
1777
1778
// Same as above, but with generations enabled, we may end up changing the seed,
1779
// which means we need to be able to recompute the hash.
1780
size_t PrepareInsertLargeGenerationsEnabled(
1781
    CommonFields& common, const PolicyFunctions& policy, size_t hash,
1782
    Group::NonIterableBitMaskType mask_empty, FindInfo target_group,
1783
    absl::FunctionRef<size_t(size_t)> recompute_hash);
1784
1785
template <typename Policy, typename Hash, typename Eq, typename Alloc>
1786
struct InstantiateRawHashSet {
1787
  using type = typename ApplyWithoutDefaultSuffix<
1788
      raw_hash_set,
1789
      TypeList<void, typename Policy::DefaultHash, typename Policy::DefaultEq,
1790
               typename Policy::DefaultAlloc>,
1791
      TypeList<Policy, Hash, Eq, Alloc>>::type;
1792
};
1793
1794
// A SwissTable.
1795
//
1796
// Policy: a policy defines how to perform different operations on
1797
// the slots of the hashtable (see hash_policy_traits.h for the full interface
1798
// of policy).
1799
//
1800
// Params...: a variadic list of parameters that allows us to omit default
1801
//            types. This reduces the mangled name of the class and the size of
1802
//            debug strings like __PRETTY_FUNCTION__. Default types do not give
1803
//            any new information.
1804
//
1805
// Hash: a (possibly polymorphic) functor that hashes keys of the hashtable. The
1806
// functor should accept a key and return size_t as hash. For best performance
1807
// it is important that the hash function provides high entropy across all bits
1808
// of the hash.
1809
// This is the first element in `Params...` if it exists, or Policy::DefaultHash
1810
// otherwise.
1811
//
1812
// Eq: a (possibly polymorphic) functor that compares two keys for equality. It
1813
// should accept two (of possibly different type) keys and return a bool: true
1814
// if they are equal, false if they are not. If two keys compare equal, then
1815
// their hash values as defined by Hash MUST be equal.
1816
// This is the second element in `Params...` if it exists, or Policy::DefaultEq
1817
// otherwise.
1818
//
1819
// Allocator: an Allocator
1820
// [https://en.cppreference.com/w/cpp/named_req/Allocator] with which
1821
// the storage of the hashtable will be allocated and the elements will be
1822
// constructed and destroyed.
1823
// This is the third element in `Params...` if it exists, or
1824
// Policy::DefaultAlloc otherwise.
1825
template <class Policy, class... Params>
1826
class raw_hash_set {
1827
  using PolicyTraits = hash_policy_traits<Policy>;
1828
  using Hash = GetFromListOr<typename Policy::DefaultHash, 0, Params...>;
1829
  using Eq = GetFromListOr<typename Policy::DefaultEq, 1, Params...>;
1830
  using Alloc = GetFromListOr<typename Policy::DefaultAlloc, 2, Params...>;
1831
  using KeyArgImpl =
1832
      KeyArg<IsTransparent<Eq>::value && IsTransparent<Hash>::value>;
1833
1834
  static_assert(
1835
      std::is_same_v<
1836
          typename InstantiateRawHashSet<Policy, Hash, Eq, Alloc>::type,
1837
          raw_hash_set>,
1838
      "Redundant template parameters were passed. Use InstantiateRawHashSet<> "
1839
      "instead");
1840
1841
 public:
1842
  using init_type = typename PolicyTraits::init_type;
1843
  using key_type = typename PolicyTraits::key_type;
1844
  using allocator_type = Alloc;
1845
  using size_type = size_t;
1846
  using difference_type = ptrdiff_t;
1847
  using hasher = Hash;
1848
  using key_equal = Eq;
1849
  using policy_type = Policy;
1850
  using value_type = typename PolicyTraits::value_type;
1851
  using reference = value_type&;
1852
  using const_reference = const value_type&;
1853
  using pointer = typename absl::allocator_traits<
1854
      allocator_type>::template rebind_traits<value_type>::pointer;
1855
  using const_pointer = typename absl::allocator_traits<
1856
      allocator_type>::template rebind_traits<value_type>::const_pointer;
1857
1858
 private:
1859
  // Alias used for heterogeneous lookup functions.
1860
  // `key_arg<K>` evaluates to `K` when the functors are transparent and to
1861
  // `key_type` otherwise. It permits template argument deduction on `K` for the
1862
  // transparent case.
1863
  template <class K>
1864
  using key_arg = typename KeyArgImpl::template type<K, key_type>;
1865
1866
  using slot_type = typename PolicyTraits::slot_type;
1867
1868
  constexpr static bool kIsDefaultHash =
1869
      std::is_same_v<hasher, absl::Hash<key_type>> ||
1870
      std::is_same_v<hasher, absl::container_internal::StringHash>;
1871
1872
  // TODO(b/289225379): we could add extra SOO space inside raw_hash_set
1873
  // after CommonFields to allow inlining larger slot_types (e.g. std::string),
1874
  // but it's a bit complicated if we want to support incomplete mapped_type in
1875
  // flat_hash_map. We could potentially do this for flat_hash_set and for an
1876
  // allowlist of `mapped_type`s of flat_hash_map that includes e.g. arithmetic
1877
  // types, strings, cords, and pairs/tuples of allowlisted types.
1878
  constexpr static bool SooEnabled() {
1879
    return PolicyTraits::soo_enabled() &&
1880
           sizeof(slot_type) <= sizeof(HeapOrSoo) &&
1881
           alignof(slot_type) <= alignof(HeapOrSoo);
1882
  }
1883
1884
  constexpr static size_t DefaultCapacity() {
1885
    return SooEnabled() ? SooCapacity() : 0;
1886
  }
1887
  constexpr static size_t MaxValidSize() {
1888
    return container_internal::MaxValidSize(sizeof(key_type),
1889
                                            sizeof(slot_type));
1890
  }
1891
  constexpr static size_t MaxValidCapacity() {
1892
    return SizeToCapacity(MaxValidSize());
1893
  }
1894
1895
  // Whether `size` fits in the SOO capacity of this table.
1896
  bool fits_in_soo(size_t size) const {
1897
    return SooEnabled() && size <= SooCapacity();
1898
  }
1899
  // Whether this table is in SOO mode or non-SOO mode.
1900
  bool is_soo() const { return fits_in_soo(maybe_invalid_capacity()); }
1901
  bool is_full_soo() const { return is_soo() && !empty(); }
1902
1903
  bool is_small() const { return common().is_small(); }
1904
1905
  // Give an early error when key_type is not hashable/eq.
1906
  auto KeyTypeCanBeHashed(const Hash& h, const key_type& k) -> decltype(h(k));
1907
  auto KeyTypeCanBeEq(const Eq& eq, const key_type& k) -> decltype(eq(k, k));
1908
1909
  // Try to be helpful when the hasher returns an unreasonable type.
1910
  using key_hash_result =
1911
      absl::remove_cvref_t<decltype(std::declval<const Hash&>()(
1912
          std::declval<const key_type&>()))>;
1913
  static_assert(sizeof(key_hash_result) >= sizeof(size_t),
1914
                "`Hash::operator()` should return a `size_t`");
1915
1916
  using AllocTraits = absl::allocator_traits<allocator_type>;
1917
  using SlotAlloc = typename absl::allocator_traits<
1918
      allocator_type>::template rebind_alloc<slot_type>;
1919
  // People are often sloppy with the exact type of their allocator (sometimes
1920
  // it has an extra const or is missing the pair, but rebinds made it work
1921
  // anyway).
1922
  using CharAlloc =
1923
      typename absl::allocator_traits<Alloc>::template rebind_alloc<char>;
1924
  using SlotAllocTraits = typename absl::allocator_traits<
1925
      allocator_type>::template rebind_traits<slot_type>;
1926
1927
  static_assert(std::is_lvalue_reference<reference>::value,
1928
                "Policy::element() must return a reference");
1929
1930
  // An enabler for insert(T&&): T must be convertible to init_type or be the
1931
  // same as [cv] value_type [ref].
1932
  template <class T>
1933
  using Insertable = absl::disjunction<
1934
      std::is_same<absl::remove_cvref_t<reference>, absl::remove_cvref_t<T>>,
1935
      std::is_convertible<T, init_type>>;
1936
  template <class T>
1937
  using IsNotBitField = std::is_pointer<T*>;
1938
1939
  // RequiresNotInit is a workaround for gcc prior to 7.1.
1940
  // See https://godbolt.org/g/Y4xsUh.
1941
  template <class T>
1942
  using RequiresNotInit =
1943
      typename std::enable_if<!std::is_same<T, init_type>::value, int>::type;
1944
1945
  template <class... Ts>
1946
  using IsDecomposable = IsDecomposable<void, PolicyTraits, Hash, Eq, Ts...>;
1947
1948
  template <class T>
1949
  using IsDecomposableAndInsertable =
1950
      IsDecomposable<std::enable_if_t<Insertable<T>::value, T>>;
1951
1952
  // Evaluates to true if an assignment from the given type would require the
1953
  // source object to remain alive for the life of the element.
1954
  template <class U>
1955
  using IsLifetimeBoundAssignmentFrom = std::conditional_t<
1956
      policy_trait_element_is_owner<Policy>::value, std::false_type,
1957
      type_traits_internal::IsLifetimeBoundAssignment<init_type, U>>;
1958
1959
 public:
1960
  static_assert(std::is_same<pointer, value_type*>::value,
1961
                "Allocators with custom pointer types are not supported");
1962
  static_assert(std::is_same<const_pointer, const value_type*>::value,
1963
                "Allocators with custom pointer types are not supported");
1964
1965
  class iterator : private HashSetIteratorGenerationInfo {
1966
    friend class raw_hash_set;
1967
    friend struct HashtableFreeFunctionsAccess;
1968
1969
   public:
1970
    using iterator_category = std::forward_iterator_tag;
1971
    using value_type = typename raw_hash_set::value_type;
1972
    using reference =
1973
        absl::conditional_t<PolicyTraits::constant_iterators::value,
1974
                            const value_type&, value_type&>;
1975
    using pointer = absl::remove_reference_t<reference>*;
1976
    using difference_type = typename raw_hash_set::difference_type;
1977
1978
    iterator() {}
1979
1980
    // PRECONDITION: not an end() iterator.
1981
    reference operator*() const {
1982
      assert_is_full("operator*()");
1983
      return unchecked_deref();
1984
    }
1985
1986
    // PRECONDITION: not an end() iterator.
1987
    pointer operator->() const {
1988
      assert_is_full("operator->");
1989
      return &operator*();
1990
    }
1991
1992
    // PRECONDITION: not an end() iterator.
1993
    iterator& operator++() {
1994
      assert_is_full("operator++");
1995
      ++ctrl_;
1996
      ++slot_;
1997
      skip_empty_or_deleted();
1998
      if (ABSL_PREDICT_FALSE(*ctrl_ == ctrl_t::kSentinel)) ctrl_ = nullptr;
1999
      return *this;
2000
    }
2001
    // PRECONDITION: not an end() iterator.
2002
    iterator operator++(int) {
2003
      auto tmp = *this;
2004
      ++*this;
2005
      return tmp;
2006
    }
2007
2008
    friend bool operator==(const iterator& a, const iterator& b) {
2009
      AssertIsValidForComparison(a.ctrl_, a.generation(), a.generation_ptr());
2010
      AssertIsValidForComparison(b.ctrl_, b.generation(), b.generation_ptr());
2011
      AssertSameContainer(a.ctrl_, b.ctrl_, a.slot_, b.slot_,
2012
                          a.generation_ptr(), b.generation_ptr());
2013
      return a.ctrl_ == b.ctrl_;
2014
    }
2015
    friend bool operator!=(const iterator& a, const iterator& b) {
2016
      return !(a == b);
2017
    }
2018
2019
   private:
2020
    iterator(ctrl_t* ctrl, slot_type* slot,
2021
             const GenerationType* generation_ptr)
2022
        : HashSetIteratorGenerationInfo(generation_ptr),
2023
          ctrl_(ctrl),
2024
          slot_(slot) {
2025
      // This assumption helps the compiler know that any non-end iterator is
2026
      // not equal to any end iterator.
2027
      ABSL_ASSUME(ctrl != nullptr);
2028
    }
2029
    // This constructor is used in begin() to avoid an MSan
2030
    // use-of-uninitialized-value error. Delegating from this constructor to
2031
    // the previous one doesn't avoid the error.
2032
    iterator(ctrl_t* ctrl, MaybeInitializedPtr<void> slot,
2033
             const GenerationType* generation_ptr)
2034
        : HashSetIteratorGenerationInfo(generation_ptr),
2035
          ctrl_(ctrl),
2036
          slot_(to_slot(slot.get())) {
2037
      // This assumption helps the compiler know that any non-end iterator is
2038
      // not equal to any end iterator.
2039
      ABSL_ASSUME(ctrl != nullptr);
2040
    }
2041
    // For end() iterators.
2042
    explicit iterator(const GenerationType* generation_ptr)
2043
        : HashSetIteratorGenerationInfo(generation_ptr), ctrl_(nullptr) {}
2044
2045
    void assert_is_full(const char* operation) const {
2046
      AssertIsFull(ctrl_, generation(), generation_ptr(), operation);
2047
    }
2048
2049
    // Fixes up `ctrl_` to point to a full or sentinel by advancing `ctrl_` and
2050
    // `slot_` until they reach one.
2051
    void skip_empty_or_deleted() {
2052
      while (IsEmptyOrDeleted(*ctrl_)) {
2053
        ++ctrl_;
2054
        ++slot_;
2055
      }
2056
    }
2057
2058
    // An equality check which skips ABSL Hardening iterator invalidation
2059
    // checks.
2060
    // Should be used when the lifetimes of the iterators are well-enough
2061
    // understood to prove that they cannot be invalid.
2062
    bool unchecked_equals(const iterator& b) const {
2063
      return ctrl_ == b.control();
2064
    }
2065
2066
    // Dereferences the iterator without ABSL Hardening iterator invalidation
2067
    // checks.
2068
    reference unchecked_deref() const { return PolicyTraits::element(slot_); }
2069
2070
    ctrl_t* control() const { return ctrl_; }
2071
    slot_type* slot() const { return slot_; }
2072
2073
    // We use DefaultIterControl() for default-constructed iterators so that
2074
    // they can be distinguished from end iterators, which have nullptr ctrl_.
2075
    ctrl_t* ctrl_ = DefaultIterControl();
2076
    // To avoid uninitialized member warnings, put slot_ in an anonymous union.
2077
    // The member is not initialized on singleton and end iterators.
2078
    union {
2079
      slot_type* slot_;
2080
    };
2081
  };
2082
2083
  class const_iterator {
2084
    friend class raw_hash_set;
2085
    template <class Container, typename Enabler>
2086
    friend struct absl::container_internal::hashtable_debug_internal::
2087
        HashtableDebugAccess;
2088
2089
   public:
2090
    using iterator_category = typename iterator::iterator_category;
2091
    using value_type = typename raw_hash_set::value_type;
2092
    using reference = typename raw_hash_set::const_reference;
2093
    using pointer = typename raw_hash_set::const_pointer;
2094
    using difference_type = typename raw_hash_set::difference_type;
2095
2096
    const_iterator() = default;
2097
    // Implicit construction from iterator.
2098
    const_iterator(iterator i) : inner_(std::move(i)) {}  // NOLINT
2099
2100
    reference operator*() const { return *inner_; }
2101
    pointer operator->() const { return inner_.operator->(); }
2102
2103
    const_iterator& operator++() {
2104
      ++inner_;
2105
      return *this;
2106
    }
2107
    const_iterator operator++(int) { return inner_++; }
2108
2109
    friend bool operator==(const const_iterator& a, const const_iterator& b) {
2110
      return a.inner_ == b.inner_;
2111
    }
2112
    friend bool operator!=(const const_iterator& a, const const_iterator& b) {
2113
      return !(a == b);
2114
    }
2115
2116
   private:
2117
    const_iterator(const ctrl_t* ctrl, const slot_type* slot,
2118
                   const GenerationType* gen)
2119
        : inner_(const_cast<ctrl_t*>(ctrl), const_cast<slot_type*>(slot), gen) {
2120
    }
2121
    bool unchecked_equals(const const_iterator& b) const {
2122
      return inner_.unchecked_equals(b.inner_);
2123
    }
2124
    ctrl_t* control() const { return inner_.control(); }
2125
    slot_type* slot() const { return inner_.slot(); }
2126
2127
    iterator inner_;
2128
  };
2129
2130
  using node_type = node_handle<Policy, hash_policy_traits<Policy>, Alloc>;
2131
  using insert_return_type = InsertReturnType<iterator, node_type>;
2132
2133
  // Note: can't use `= default` due to non-default noexcept (causes
2134
  // problems for some compilers). NOLINTNEXTLINE
2135
  raw_hash_set() noexcept(
2136
      std::is_nothrow_default_constructible<hasher>::value &&
2137
      std::is_nothrow_default_constructible<key_equal>::value &&
2138
      std::is_nothrow_default_constructible<allocator_type>::value) {}
2139
2140
  explicit raw_hash_set(
2141
      size_t bucket_count, const hasher& hash = hasher(),
2142
      const key_equal& eq = key_equal(),
2143
      const allocator_type& alloc = allocator_type())
2144
      : settings_(CommonFields::CreateDefault<SooEnabled()>(), hash, eq,
2145
                  alloc) {
2146
    if (bucket_count > DefaultCapacity()) {
2147
      ReserveEmptyNonAllocatedTableToFitBucketCount(
2148
          common(), GetPolicyFunctions(),
2149
          (std::min)(bucket_count, MaxValidCapacity()));
2150
    }
2151
  }
2152
2153
  raw_hash_set(size_t bucket_count, const hasher& hash,
2154
               const allocator_type& alloc)
2155
      : raw_hash_set(bucket_count, hash, key_equal(), alloc) {}
2156
2157
  raw_hash_set(size_t bucket_count, const allocator_type& alloc)
2158
      : raw_hash_set(bucket_count, hasher(), key_equal(), alloc) {}
2159
2160
  explicit raw_hash_set(const allocator_type& alloc)
2161
      : raw_hash_set(0, hasher(), key_equal(), alloc) {}
2162
2163
  template <class InputIter>
2164
  raw_hash_set(InputIter first, InputIter last, size_t bucket_count = 0,
2165
               const hasher& hash = hasher(), const key_equal& eq = key_equal(),
2166
               const allocator_type& alloc = allocator_type())
2167
      : raw_hash_set(SelectBucketCountForIterRange(first, last, bucket_count),
2168
                     hash, eq, alloc) {
2169
    insert(first, last);
2170
  }
2171
2172
  template <class InputIter>
2173
  raw_hash_set(InputIter first, InputIter last, size_t bucket_count,
2174
               const hasher& hash, const allocator_type& alloc)
2175
      : raw_hash_set(first, last, bucket_count, hash, key_equal(), alloc) {}
2176
2177
  template <class InputIter>
2178
  raw_hash_set(InputIter first, InputIter last, size_t bucket_count,
2179
               const allocator_type& alloc)
2180
      : raw_hash_set(first, last, bucket_count, hasher(), key_equal(), alloc) {}
2181
2182
#if defined(__cpp_lib_containers_ranges) && \
2183
    __cpp_lib_containers_ranges >= 202202L
2184
  template <typename R>
2185
  raw_hash_set(std::from_range_t, R&& rg, size_type bucket_count = 0,
2186
               const hasher& hash = hasher(), const key_equal& eq = key_equal(),
2187
               const allocator_type& alloc = allocator_type())
2188
      : raw_hash_set(std::begin(rg), std::end(rg), bucket_count, hash, eq,
2189
                     alloc) {}
2190
2191
  template <typename R>
2192
  raw_hash_set(std::from_range_t, R&& rg, size_type bucket_count,
2193
               const allocator_type& alloc)
2194
      : raw_hash_set(std::from_range, std::forward<R>(rg), bucket_count,
2195
                     hasher(), key_equal(), alloc) {}
2196
2197
  template <typename R>
2198
  raw_hash_set(std::from_range_t, R&& rg, size_type bucket_count,
2199
               const hasher& hash, const allocator_type& alloc)
2200
      : raw_hash_set(std::from_range, std::forward<R>(rg), bucket_count, hash,
2201
                     key_equal(), alloc) {}
2202
#endif
2203
2204
  template <class InputIter>
2205
  raw_hash_set(InputIter first, InputIter last, const allocator_type& alloc)
2206
      : raw_hash_set(first, last, 0, hasher(), key_equal(), alloc) {}
2207
2208
  // Instead of accepting std::initializer_list<value_type> as the first
2209
  // argument like std::unordered_set<value_type> does, we have two overloads
2210
  // that accept std::initializer_list<T> and std::initializer_list<init_type>.
2211
  // This is advantageous for performance.
2212
  //
2213
  //   // Turns {"abc", "def"} into std::initializer_list<std::string>, then
2214
  //   // copies the strings into the set.
2215
  //   std::unordered_set<std::string> s = {"abc", "def"};
2216
  //
2217
  //   // Turns {"abc", "def"} into std::initializer_list<const char*>, then
2218
  //   // copies the strings into the set.
2219
  //   absl::flat_hash_set<std::string> s = {"abc", "def"};
2220
  //
2221
  // The same trick is used in insert().
2222
  //
2223
  // The enabler is necessary to prevent this constructor from triggering where
2224
  // the copy constructor is meant to be called.
2225
  //
2226
  //   absl::flat_hash_set<int> a, b{a};
2227
  //
2228
  // RequiresNotInit<T> is a workaround for gcc prior to 7.1.
2229
  template <class T, RequiresNotInit<T> = 0,
2230
            std::enable_if_t<Insertable<T>::value, int> = 0>
2231
  raw_hash_set(std::initializer_list<T> init, size_t bucket_count = 0,
2232
               const hasher& hash = hasher(), const key_equal& eq = key_equal(),
2233
               const allocator_type& alloc = allocator_type())
2234
      : raw_hash_set(init.begin(), init.end(), bucket_count, hash, eq, alloc) {}
2235
2236
  raw_hash_set(std::initializer_list<init_type> init, size_t bucket_count = 0,
2237
               const hasher& hash = hasher(), const key_equal& eq = key_equal(),
2238
               const allocator_type& alloc = allocator_type())
2239
      : raw_hash_set(init.begin(), init.end(), bucket_count, hash, eq, alloc) {}
2240
2241
  template <class T, RequiresNotInit<T> = 0,
2242
            std::enable_if_t<Insertable<T>::value, int> = 0>
2243
  raw_hash_set(std::initializer_list<T> init, size_t bucket_count,
2244
               const hasher& hash, const allocator_type& alloc)
2245
      : raw_hash_set(init, bucket_count, hash, key_equal(), alloc) {}
2246
2247
  raw_hash_set(std::initializer_list<init_type> init, size_t bucket_count,
2248
               const hasher& hash, const allocator_type& alloc)
2249
      : raw_hash_set(init, bucket_count, hash, key_equal(), alloc) {}
2250
2251
  template <class T, RequiresNotInit<T> = 0,
2252
            std::enable_if_t<Insertable<T>::value, int> = 0>
2253
  raw_hash_set(std::initializer_list<T> init, size_t bucket_count,
2254
               const allocator_type& alloc)
2255
      : raw_hash_set(init, bucket_count, hasher(), key_equal(), alloc) {}
2256
2257
  raw_hash_set(std::initializer_list<init_type> init, size_t bucket_count,
2258
               const allocator_type& alloc)
2259
      : raw_hash_set(init, bucket_count, hasher(), key_equal(), alloc) {}
2260
2261
  template <class T, RequiresNotInit<T> = 0,
2262
            std::enable_if_t<Insertable<T>::value, int> = 0>
2263
  raw_hash_set(std::initializer_list<T> init, const allocator_type& alloc)
2264
      : raw_hash_set(init, 0, hasher(), key_equal(), alloc) {}
2265
2266
  raw_hash_set(std::initializer_list<init_type> init,
2267
               const allocator_type& alloc)
2268
      : raw_hash_set(init, 0, hasher(), key_equal(), alloc) {}
2269
2270
  raw_hash_set(const raw_hash_set& that)
2271
      : raw_hash_set(that, AllocTraits::select_on_container_copy_construction(
2272
                               allocator_type(that.char_alloc_ref()))) {}
2273
2274
  raw_hash_set(const raw_hash_set& that, const allocator_type& a)
2275
      : raw_hash_set(0, that.hash_ref(), that.eq_ref(), a) {
2276
    that.AssertNotDebugCapacity();
2277
    if (that.empty()) return;
2278
    Copy(common(), GetPolicyFunctions(), that.common(),
2279
         [this](void* dst, const void* src) {
2280
           // TODO(b/413598253): type erase for trivially copyable types via
2281
           // PolicyTraits.
2282
           construct(to_slot(dst),
2283
                     PolicyTraits::element(
2284
                         static_cast<slot_type*>(const_cast<void*>(src))));
2285
         });
2286
  }
2287
2288
  ABSL_ATTRIBUTE_NOINLINE raw_hash_set(raw_hash_set&& that) noexcept(
2289
      std::is_nothrow_copy_constructible<hasher>::value &&
2290
      std::is_nothrow_copy_constructible<key_equal>::value &&
2291
      std::is_nothrow_copy_constructible<allocator_type>::value)
2292
      :  // Hash, equality and allocator are copied instead of moved because
2293
         // `that` must be left valid. If Hash is std::function<Key>, moving it
2294
         // would create a nullptr functor that cannot be called.
2295
         // Note: we avoid using exchange for better generated code.
2296
        settings_(PolicyTraits::transfer_uses_memcpy() || !that.is_full_soo()
2297
                      ? std::move(that.common())
2298
                      : CommonFields{full_soo_tag_t{},
2299
                                     that.common().soo_has_tried_sampling()},
2300
                  that.hash_ref(), that.eq_ref(), that.char_alloc_ref()) {
2301
    if (!PolicyTraits::transfer_uses_memcpy() && that.is_full_soo()) {
2302
      transfer(soo_slot(), that.soo_slot());
2303
    }
2304
    that.common() = CommonFields::CreateDefault<SooEnabled()>();
2305
    annotate_for_bug_detection_on_move(that);
2306
  }
2307
2308
  raw_hash_set(raw_hash_set&& that, const allocator_type& a)
2309
      : settings_(CommonFields::CreateDefault<SooEnabled()>(), that.hash_ref(),
2310
                  that.eq_ref(), a) {
2311
    if (CharAlloc(a) == that.char_alloc_ref()) {
2312
      swap_common(that);
2313
      annotate_for_bug_detection_on_move(that);
2314
    } else {
2315
      move_elements_allocs_unequal(std::move(that));
2316
    }
2317
  }
2318
2319
  raw_hash_set& operator=(const raw_hash_set& that) {
2320
    that.AssertNotDebugCapacity();
2321
    if (ABSL_PREDICT_FALSE(this == &that)) return *this;
2322
    constexpr bool propagate_alloc =
2323
        AllocTraits::propagate_on_container_copy_assignment::value;
2324
    // TODO(ezb): maybe avoid allocating a new backing array if this->capacity()
2325
    // is an exact match for that.size(). If this->capacity() is too big, then
2326
    // it would make iteration very slow to reuse the allocation. Maybe we can
2327
    // do the same heuristic as clear() and reuse if it's small enough.
2328
    allocator_type alloc(propagate_alloc ? that.char_alloc_ref()
2329
                                         : char_alloc_ref());
2330
    raw_hash_set tmp(that, alloc);
2331
    // NOLINTNEXTLINE: not returning *this for performance.
2332
    return assign_impl<propagate_alloc>(std::move(tmp));
2333
  }
2334
2335
  raw_hash_set& operator=(raw_hash_set&& that) noexcept(
2336
      AllocTraits::is_always_equal::value &&
2337
      std::is_nothrow_move_assignable<hasher>::value &&
2338
      std::is_nothrow_move_assignable<key_equal>::value) {
2339
    // TODO(sbenza): We should only use the operations from the noexcept clause
2340
    // to make sure we actually adhere to that contract.
2341
    // NOLINTNEXTLINE: not returning *this for performance.
2342
    return move_assign(
2343
        std::move(that),
2344
        typename AllocTraits::propagate_on_container_move_assignment());
2345
  }
2346
2347
  ~raw_hash_set() {
2348
    destructor_impl();
2349
    if constexpr (SwisstableAssertAccessToDestroyedTable()) {
2350
      common().set_capacity(InvalidCapacity::kDestroyed);
2351
    }
2352
  }
2353
2354
  iterator begin() ABSL_ATTRIBUTE_LIFETIME_BOUND {
2355
    if (ABSL_PREDICT_FALSE(empty())) return end();
2356
    if (is_small()) return single_iterator();
2357
    iterator it = {control(), common().slots_union(),
2358
                   common().generation_ptr()};
2359
    it.skip_empty_or_deleted();
2360
    ABSL_SWISSTABLE_ASSERT(IsFull(*it.control()));
2361
    return it;
2362
  }
2363
  iterator end() ABSL_ATTRIBUTE_LIFETIME_BOUND {
2364
    AssertNotDebugCapacity();
2365
    return iterator(common().generation_ptr());
2366
  }
2367
2368
  const_iterator begin() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
2369
    return const_cast<raw_hash_set*>(this)->begin();
2370
  }
2371
  const_iterator end() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
2372
    return const_cast<raw_hash_set*>(this)->end();
2373
  }
2374
  const_iterator cbegin() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
2375
    return begin();
2376
  }
2377
  const_iterator cend() const ABSL_ATTRIBUTE_LIFETIME_BOUND { return end(); }
2378
2379
  bool empty() const { return !size(); }
2380
  size_t size() const {
2381
    AssertNotDebugCapacity();
2382
    const size_t size = common().size();
2383
    [[maybe_unused]] const size_t kMaxValidSize = MaxValidSize();
2384
    ABSL_ASSUME(size <= kMaxValidSize);
2385
    return size;
2386
  }
2387
  size_t capacity() const {
2388
    const size_t cap = common().capacity();
2389
    // Compiler complains when using functions in ASSUME so use local variables.
2390
    [[maybe_unused]] const bool kIsValid = IsValidCapacity(cap);
2391
    [[maybe_unused]] const size_t kDefaultCapacity = DefaultCapacity();
2392
    [[maybe_unused]] const size_t kMaxValidCapacity = MaxValidCapacity();
2393
    ABSL_ASSUME(kIsValid || cap == 0);
2394
    ABSL_ASSUME(cap >= kDefaultCapacity);
2395
    ABSL_ASSUME(cap <= kMaxValidCapacity);
2396
    return cap;
2397
  }
2398
  size_t max_size() const { return MaxValidSize(); }
2399
2400
  ABSL_ATTRIBUTE_REINITIALIZES void clear() {
2401
    if (SwisstableGenerationsEnabled() &&
2402
        maybe_invalid_capacity() >= InvalidCapacity::kMovedFrom) {
2403
      common().set_capacity(DefaultCapacity());
2404
    }
2405
    AssertNotDebugCapacity();
2406
    // Iterating over this container is O(bucket_count()). When bucket_count()
2407
    // is much greater than size(), iteration becomes prohibitively expensive.
2408
    // For clear() it is more important to reuse the allocated array when the
2409
    // container is small because allocation takes comparatively long time
2410
    // compared to destruction of the elements of the container. So we pick the
2411
    // largest bucket_count() threshold for which iteration is still fast and
2412
    // past that we simply deallocate the array.
2413
    const size_t cap = capacity();
2414
    if (cap == 0) {
2415
      // Already guaranteed to be empty; so nothing to do.
2416
    } else if (is_small()) {
2417
      if (!empty()) {
2418
        destroy(single_slot());
2419
        decrement_small_size();
2420
      }
2421
    } else {
2422
      destroy_slots();
2423
      clear_backing_array(/*reuse=*/cap < 128);
2424
    }
2425
    common().set_reserved_growth(0);
2426
    common().set_reservation_size(0);
2427
  }
2428
2429
  // This overload kicks in when the argument is an rvalue of insertable and
2430
  // decomposable type other than init_type.
2431
  //
2432
  //   flat_hash_map<std::string, int> m;
2433
  //   m.insert(std::make_pair("abc", 42));
2434
  template <class T,
2435
            int = std::enable_if_t<IsDecomposableAndInsertable<T>::value &&
2436
                                       IsNotBitField<T>::value &&
2437
                                       !IsLifetimeBoundAssignmentFrom<T>::value,
2438
                                   int>()>
2439
  std::pair<iterator, bool> insert(T&& value) ABSL_ATTRIBUTE_LIFETIME_BOUND {
2440
    return emplace(std::forward<T>(value));
2441
  }
2442
2443
  template <class T, int&...,
2444
            std::enable_if_t<IsDecomposableAndInsertable<T>::value &&
2445
                                 IsNotBitField<T>::value &&
2446
                                 IsLifetimeBoundAssignmentFrom<T>::value,
2447
                             int> = 0>
2448
  std::pair<iterator, bool> insert(
2449
      T&& value ABSL_INTERNAL_ATTRIBUTE_CAPTURED_BY(this))
2450
      ABSL_ATTRIBUTE_LIFETIME_BOUND {
2451
    return this->template insert<T, 0>(std::forward<T>(value));
2452
  }
2453
2454
  // This overload kicks in when the argument is a bitfield or an lvalue of
2455
  // insertable and decomposable type.
2456
  //
2457
  //   union { int n : 1; };
2458
  //   flat_hash_set<int> s;
2459
  //   s.insert(n);
2460
  //
2461
  //   flat_hash_set<std::string> s;
2462
  //   const char* p = "hello";
2463
  //   s.insert(p);
2464
  //
2465
  template <class T, int = std::enable_if_t<
2466
                         IsDecomposableAndInsertable<const T&>::value &&
2467
                             !IsLifetimeBoundAssignmentFrom<const T&>::value,
2468
                         int>()>
2469
  std::pair<iterator, bool> insert(const T& value)
2470
      ABSL_ATTRIBUTE_LIFETIME_BOUND {
2471
    return emplace(value);
2472
  }
2473
  template <class T, int&...,
2474
            std::enable_if_t<IsDecomposableAndInsertable<const T&>::value &&
2475
                                 IsLifetimeBoundAssignmentFrom<const T&>::value,
2476
                             int> = 0>
2477
  std::pair<iterator, bool> insert(
2478
      const T& value ABSL_INTERNAL_ATTRIBUTE_CAPTURED_BY(this))
2479
      ABSL_ATTRIBUTE_LIFETIME_BOUND {
2480
    return this->template insert<T, 0>(value);
2481
  }
2482
2483
  // This overload kicks in when the argument is an rvalue of init_type. Its
2484
  // purpose is to handle brace-init-list arguments.
2485
  //
2486
  //   flat_hash_map<std::string, int> s;
2487
  //   s.insert({"abc", 42});
2488
  std::pair<iterator, bool> insert(init_type&& value)
2489
      ABSL_ATTRIBUTE_LIFETIME_BOUND
2490
#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
2491
    requires(!IsLifetimeBoundAssignmentFrom<init_type>::value)
2492
#endif
2493
  {
2494
    return emplace(std::move(value));
2495
  }
2496
#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
2497
  std::pair<iterator, bool> insert(
2498
      init_type&& value ABSL_INTERNAL_ATTRIBUTE_CAPTURED_BY(this))
2499
      ABSL_ATTRIBUTE_LIFETIME_BOUND
2500
    requires(IsLifetimeBoundAssignmentFrom<init_type>::value)
2501
  {
2502
    return emplace(std::move(value));
2503
  }
2504
#endif
2505
2506
  template <class T,
2507
            int = std::enable_if_t<IsDecomposableAndInsertable<T>::value &&
2508
                                       IsNotBitField<T>::value &&
2509
                                       !IsLifetimeBoundAssignmentFrom<T>::value,
2510
                                   int>()>
2511
  iterator insert(const_iterator, T&& value) ABSL_ATTRIBUTE_LIFETIME_BOUND {
2512
    return insert(std::forward<T>(value)).first;
2513
  }
2514
  template <class T, int&...,
2515
            std::enable_if_t<IsDecomposableAndInsertable<T>::value &&
2516
                                 IsNotBitField<T>::value &&
2517
                                 IsLifetimeBoundAssignmentFrom<T>::value,
2518
                             int> = 0>
2519
  iterator insert(const_iterator hint,
2520
                  T&& value ABSL_INTERNAL_ATTRIBUTE_CAPTURED_BY(this))
2521
      ABSL_ATTRIBUTE_LIFETIME_BOUND {
2522
    return this->template insert<T, 0>(hint, std::forward<T>(value));
2523
  }
2524
2525
  template <class T, std::enable_if_t<
2526
                         IsDecomposableAndInsertable<const T&>::value, int> = 0>
2527
  iterator insert(const_iterator,
2528
                  const T& value) ABSL_ATTRIBUTE_LIFETIME_BOUND {
2529
    return insert(value).first;
2530
  }
2531
2532
  iterator insert(const_iterator,
2533
                  init_type&& value) ABSL_ATTRIBUTE_LIFETIME_BOUND {
2534
    return insert(std::move(value)).first;
2535
  }
2536
2537
  template <class InputIt>
2538
  void insert(InputIt first, InputIt last) {
2539
    insert_range(first, last);
2540
  }
2541
2542
  template <class T, RequiresNotInit<T> = 0,
2543
            std::enable_if_t<Insertable<const T&>::value, int> = 0>
2544
  void insert(std::initializer_list<T> ilist) {
2545
    insert_range(ilist.begin(), ilist.end());
2546
  }
2547
2548
  void insert(std::initializer_list<init_type> ilist) {
2549
    insert_range(ilist.begin(), ilist.end());
2550
  }
2551
2552
  insert_return_type insert(node_type&& node) ABSL_ATTRIBUTE_LIFETIME_BOUND {
2553
    if (!node) return {end(), false, node_type()};
2554
    const auto& elem = PolicyTraits::element(CommonAccess::GetSlot(node));
2555
    auto res = PolicyTraits::apply(
2556
        InsertSlot<false>{*this, std::move(*CommonAccess::GetSlot(node))},
2557
        elem);
2558
    if (res.second) {
2559
      CommonAccess::Reset(&node);
2560
      return {res.first, true, node_type()};
2561
    } else {
2562
      return {res.first, false, std::move(node)};
2563
    }
2564
  }
2565
2566
  iterator insert(const_iterator,
2567
                  node_type&& node) ABSL_ATTRIBUTE_LIFETIME_BOUND {
2568
    auto res = insert(std::move(node));
2569
    node = std::move(res.node);
2570
    return res.position;
2571
  }
2572
2573
  // This overload kicks in if we can deduce the key from args. This enables us
2574
  // to avoid constructing value_type if an entry with the same key already
2575
  // exists.
2576
  //
2577
  // For example:
2578
  //
2579
  //   flat_hash_map<std::string, std::string> m = {{"abc", "def"}};
2580
  //   // Creates no std::string copies and makes no heap allocations.
2581
  //   m.emplace("abc", "xyz");
2582
  template <class... Args,
2583
            std::enable_if_t<IsDecomposable<Args...>::value, int> = 0>
2584
  std::pair<iterator, bool> emplace(Args&&... args)
2585
      ABSL_ATTRIBUTE_LIFETIME_BOUND {
2586
    return PolicyTraits::apply(EmplaceDecomposable{*this},
2587
                               std::forward<Args>(args)...);
2588
  }
2589
2590
  // This overload kicks in if we cannot deduce the key from args. It constructs
2591
  // value_type unconditionally and then either moves it into the table or
2592
  // destroys.
2593
  template <class... Args,
2594
            std::enable_if_t<!IsDecomposable<Args...>::value, int> = 0>
2595
  std::pair<iterator, bool> emplace(Args&&... args)
2596
      ABSL_ATTRIBUTE_LIFETIME_BOUND {
2597
    alignas(slot_type) unsigned char raw[sizeof(slot_type)];
2598
    slot_type* slot = to_slot(&raw);
2599
2600
    construct(slot, std::forward<Args>(args)...);
2601
    const auto& elem = PolicyTraits::element(slot);
2602
    return PolicyTraits::apply(InsertSlot<true>{*this, std::move(*slot)}, elem);
2603
  }
2604
2605
  template <class... Args>
2606
  iterator emplace_hint(const_iterator,
2607
                        Args&&... args) ABSL_ATTRIBUTE_LIFETIME_BOUND {
2608
    return emplace(std::forward<Args>(args)...).first;
2609
  }
2610
2611
  // Extension API: support for lazy emplace.
2612
  //
2613
  // Looks up key in the table. If found, returns the iterator to the element.
2614
  // Otherwise calls `f` with one argument of type `raw_hash_set::constructor`,
2615
  // and returns an iterator to the new element.
2616
  //
2617
  // `f` must abide by several restrictions:
2618
  //  - it MUST call `raw_hash_set::constructor` with arguments as if a
2619
  //    `raw_hash_set::value_type` is constructed,
2620
  //  - it MUST NOT access the container before the call to
2621
  //    `raw_hash_set::constructor`, and
2622
  //  - it MUST NOT erase the lazily emplaced element.
2623
  // Doing any of these is undefined behavior.
2624
  //
2625
  // For example:
2626
  //
2627
  //   std::unordered_set<ArenaString> s;
2628
  //   // Makes ArenaStr even if "abc" is in the map.
2629
  //   s.insert(ArenaString(&arena, "abc"));
2630
  //
2631
  //   flat_hash_set<ArenaStr> s;
2632
  //   // Makes ArenaStr only if "abc" is not in the map.
2633
  //   s.lazy_emplace("abc", [&](const constructor& ctor) {
2634
  //     ctor(&arena, "abc");
2635
  //   });
2636
  //
2637
  // WARNING: This API is currently experimental. If there is a way to implement
2638
  // the same thing with the rest of the API, prefer that.
2639
  class constructor {
2640
    friend class raw_hash_set;
2641
2642
   public:
2643
    template <class... Args>
2644
    void operator()(Args&&... args) const {
2645
      ABSL_SWISSTABLE_ASSERT(*slot_);
2646
      PolicyTraits::construct(alloc_, *slot_, std::forward<Args>(args)...);
2647
      *slot_ = nullptr;
2648
    }
2649
2650
   private:
2651
    constructor(allocator_type* a, slot_type** slot) : alloc_(a), slot_(slot) {}
2652
2653
    allocator_type* alloc_;
2654
    slot_type** slot_;
2655
  };
2656
2657
  template <class K = key_type, class F>
2658
  iterator lazy_emplace(const key_arg<K>& key,
2659
                        F&& f) ABSL_ATTRIBUTE_LIFETIME_BOUND {
2660
    auto res = find_or_prepare_insert(key);
2661
    if (res.second) {
2662
      slot_type* slot = res.first.slot();
2663
      allocator_type alloc(char_alloc_ref());
2664
      std::forward<F>(f)(constructor(&alloc, &slot));
2665
      ABSL_SWISSTABLE_ASSERT(!slot);
2666
    }
2667
    return res.first;
2668
  }
2669
2670
  // Extension API: support for heterogeneous keys.
2671
  //
2672
  //   std::unordered_set<std::string> s;
2673
  //   // Turns "abc" into std::string.
2674
  //   s.erase("abc");
2675
  //
2676
  //   flat_hash_set<std::string> s;
2677
  //   // Uses "abc" directly without copying it into std::string.
2678
  //   s.erase("abc");
2679
  template <class K = key_type>
2680
  size_type erase(const key_arg<K>& key) {
2681
    auto it = find(key);
2682
    if (it == end()) return 0;
2683
    erase(it);
2684
    return 1;
2685
  }
2686
2687
  // Erases the element pointed to by `it`. Unlike `std::unordered_set::erase`,
2688
  // this method returns void to reduce algorithmic complexity to O(1). The
2689
  // iterator is invalidated so any increment should be done before calling
2690
  // erase (e.g. `erase(it++)`).
2691
  void erase(const_iterator cit) { erase(cit.inner_); }
2692
2693
  // This overload is necessary because otherwise erase<K>(const K&) would be
2694
  // a better match if non-const iterator is passed as an argument.
2695
  void erase(iterator it) {
2696
    ABSL_SWISSTABLE_ASSERT(capacity() > 0);
2697
    AssertNotDebugCapacity();
2698
    it.assert_is_full("erase()");
2699
    destroy(it.slot());
2700
    erase_meta_only(it);
2701
  }
2702
2703
  iterator erase(const_iterator first,
2704
                 const_iterator last) ABSL_ATTRIBUTE_LIFETIME_BOUND {
2705
    AssertNotDebugCapacity();
2706
    // We check for empty first because clear_backing_array requires that
2707
    // capacity() > 0 as a precondition.
2708
    if (empty()) return end();
2709
    if (first == last) return last.inner_;
2710
    if (is_small()) {
2711
      destroy(single_slot());
2712
      erase_meta_only_small();
2713
      return end();
2714
    }
2715
    if (first == begin() && last == end()) {
2716
      // TODO(ezb): we access control bytes in destroy_slots so it could make
2717
      // sense to combine destroy_slots and clear_backing_array to avoid cache
2718
      // misses when the table is large. Note that we also do this in clear().
2719
      destroy_slots();
2720
      clear_backing_array(/*reuse=*/true);
2721
      common().set_reserved_growth(common().reservation_size());
2722
      return end();
2723
    }
2724
    while (first != last) {
2725
      erase(first++);
2726
    }
2727
    return last.inner_;
2728
  }
2729
2730
  // Moves elements from `src` into `this`.
2731
  // If the element already exists in `this`, it is left unmodified in `src`.
2732
  template <
2733
      typename... Params2,
2734
      typename = std::enable_if_t<std::is_same_v<
2735
          Alloc, typename raw_hash_set<Policy, Params2...>::allocator_type>>>
2736
  void merge(raw_hash_set<Policy, Params2...>& src) {  // NOLINT
2737
    AssertNotDebugCapacity();
2738
    src.AssertNotDebugCapacity();
2739
    assert(this != &src);
2740
    // Returns whether insertion took place.
2741
    const auto insert_slot = [this](slot_type* src_slot) {
2742
      return PolicyTraits::apply(InsertSlot<false>{*this, std::move(*src_slot)},
2743
                                 PolicyTraits::element(src_slot))
2744
          .second;
2745
    };
2746
2747
    if (src.is_small()) {
2748
      if (src.empty()) return;
2749
      if (insert_slot(src.single_slot()))
2750
        src.erase_meta_only_small();
2751
      return;
2752
    }
2753
    for (auto it = src.begin(), e = src.end(); it != e;) {
2754
      auto next = std::next(it);
2755
      if (insert_slot(it.slot())) src.erase_meta_only_large(it);
2756
      it = next;
2757
    }
2758
  }
2759
2760
  template <
2761
      typename... Params2,
2762
      typename = std::enable_if_t<std::is_same_v<
2763
          Alloc, typename raw_hash_set<Policy, Params2...>::allocator_type>>>
2764
  void merge(raw_hash_set<Policy, Params2...>&& src) {  // NOLINT
2765
    merge(src);
2766
  }
2767
2768
  node_type extract(const_iterator position) {
2769
    AssertNotDebugCapacity();
2770
    position.inner_.assert_is_full("extract()");
2771
    allocator_type alloc(char_alloc_ref());
2772
    auto node = CommonAccess::Transfer<node_type>(alloc, position.slot());
2773
    erase_meta_only(position);
2774
    return node;
2775
  }
2776
2777
  template <class K = key_type,
2778
            std::enable_if_t<!std::is_same<K, iterator>::value, int> = 0>
2779
  node_type extract(const key_arg<K>& key) {
2780
    auto it = find(key);
2781
    return it == end() ? node_type() : extract(const_iterator{it});
2782
  }
2783
2784
  void swap(raw_hash_set& that) noexcept(
2785
      AllocTraits::is_always_equal::value &&
2786
      std::is_nothrow_swappable<hasher>::value &&
2787
      std::is_nothrow_swappable<key_equal>::value) {
2788
    AssertNotDebugCapacity();
2789
    that.AssertNotDebugCapacity();
2790
    using std::swap;
2791
    swap_common(that);
2792
    swap(hash_ref(), that.hash_ref());
2793
    swap(eq_ref(), that.eq_ref());
2794
    SwapAlloc(char_alloc_ref(), that.char_alloc_ref(),
2795
              typename AllocTraits::propagate_on_container_swap{});
2796
  }
2797
2798
  void rehash(size_t n) {
2799
    Rehash(common(), GetPolicyFunctions(), (std::min)(n, MaxValidCapacity()));
2800
  }
2801
2802
  void reserve(size_t n) {
2803
    if (ABSL_PREDICT_TRUE(n > DefaultCapacity())) {
2804
      ReserveTableToFitNewSize(common(), GetPolicyFunctions(),
2805
                               (std::min)(n, MaxValidSize()));
2806
    }
2807
  }
2808
2809
  // Extension API: support for heterogeneous keys.
2810
  //
2811
  //   std::unordered_set<std::string> s;
2812
  //   // Turns "abc" into std::string.
2813
  //   s.count("abc");
2814
  //
2815
  //   ch_set<std::string> s;
2816
  //   // Uses "abc" directly without copying it into std::string.
2817
  //   s.count("abc");
2818
  template <class K = key_type>
2819
  size_t count(const key_arg<K>& key) const {
2820
    return find(key) == end() ? 0 : 1;
2821
  }
2822
2823
  // Issues CPU prefetch instructions for the memory needed to find or insert
2824
  // a key.  Like all lookup functions, this support heterogeneous keys.
2825
  //
2826
  // NOTE: This is a very low level operation and should not be used without
2827
  // specific benchmarks indicating its importance.
2828
  template <class K = key_type>
2829
  void prefetch([[maybe_unused]] const key_arg<K>& key) const {
2830
    if (capacity() == DefaultCapacity()) return;
2831
    // Avoid probing if we won't be able to prefetch the addresses received.
2832
#ifdef ABSL_HAVE_PREFETCH
2833
    prefetch_heap_block();
2834
    if (is_small()) return;
2835
    auto seq = probe(common(), hash_of(key));
2836
    PrefetchToLocalCache(control() + seq.offset());
2837
    PrefetchToLocalCache(slot_array() + seq.offset());
2838
#endif  // ABSL_HAVE_PREFETCH
2839
  }
2840
2841
  template <class K = key_type>
2842
  ABSL_DEPRECATE_AND_INLINE()
2843
  iterator find(const key_arg<K>& key,
2844
                size_t) ABSL_ATTRIBUTE_LIFETIME_BOUND {
2845
    return find(key);
2846
  }
2847
  // The API of find() has one extension: the type of the key argument doesn't
2848
  // have to be key_type. This is so called heterogeneous key support.
2849
  template <class K = key_type>
2850
  iterator find(const key_arg<K>& key) ABSL_ATTRIBUTE_LIFETIME_BOUND {
2851
    AssertOnFind(key);
2852
    if (is_small()) return find_small(key);
2853
    prefetch_heap_block();
2854
    return find_large(key, hash_of(key));
2855
  }
2856
2857
  template <class K = key_type>
2858
  ABSL_DEPRECATE_AND_INLINE()
2859
  const_iterator find(const key_arg<K>& key,
2860
                      size_t) const ABSL_ATTRIBUTE_LIFETIME_BOUND {
2861
    return find(key);
2862
  }
2863
  template <class K = key_type>
2864
  const_iterator find(const key_arg<K>& key) const
2865
      ABSL_ATTRIBUTE_LIFETIME_BOUND {
2866
    return const_cast<raw_hash_set*>(this)->find(key);
2867
  }
2868
2869
  template <class K = key_type>
2870
  bool contains(const key_arg<K>& key) const {
2871
    // Here neither the iterator returned by `find()` nor `end()` can be invalid
2872
    // outside of potential thread-safety issues.
2873
    // `find()`'s return value is constructed, used, and then destructed
2874
    // all in this context.
2875
    return !find(key).unchecked_equals(end());
2876
  }
2877
2878
  template <class K = key_type>
2879
  std::pair<iterator, iterator> equal_range(const key_arg<K>& key)
2880
      ABSL_ATTRIBUTE_LIFETIME_BOUND {
2881
    auto it = find(key);
2882
    if (it != end()) return {it, std::next(it)};
2883
    return {it, it};
2884
  }
2885
  template <class K = key_type>
2886
  std::pair<const_iterator, const_iterator> equal_range(
2887
      const key_arg<K>& key) const ABSL_ATTRIBUTE_LIFETIME_BOUND {
2888
    auto it = find(key);
2889
    if (it != end()) return {it, std::next(it)};
2890
    return {it, it};
2891
  }
2892
2893
  size_t bucket_count() const { return capacity(); }
2894
  float load_factor() const {
2895
    return capacity() ? static_cast<double>(size()) / capacity() : 0.0;
2896
  }
2897
  float max_load_factor() const { return 1.0f; }
2898
  void max_load_factor(float) {
2899
    // Does nothing.
2900
  }
2901
2902
  hasher hash_function() const { return hash_ref(); }
2903
  key_equal key_eq() const { return eq_ref(); }
2904
  allocator_type get_allocator() const {
2905
    return allocator_type(char_alloc_ref());
2906
  }
2907
2908
  friend bool operator==(const raw_hash_set& a, const raw_hash_set& b) {
2909
    if (a.size() != b.size()) return false;
2910
    const raw_hash_set* outer = &a;
2911
    const raw_hash_set* inner = &b;
2912
    if (outer->capacity() > inner->capacity()) std::swap(outer, inner);
2913
    for (const value_type& elem : *outer) {
2914
      auto it = PolicyTraits::apply(FindElement{*inner}, elem);
2915
      if (it == inner->end()) return false;
2916
      // Note: we used key_equal to check for key equality in FindElement, but
2917
      // we may need to do an additional comparison using
2918
      // value_type::operator==. E.g. the keys could be equal and the
2919
      // mapped_types could be unequal in a map or even in a set, key_equal
2920
      // could ignore some fields that aren't ignored by operator==.
2921
      static constexpr bool kKeyEqIsValueEq =
2922
          std::is_same<key_type, value_type>::value &&
2923
          std::is_same<key_equal, hash_default_eq<key_type>>::value;
2924
      if (!kKeyEqIsValueEq && !(*it == elem)) return false;
2925
    }
2926
    return true;
2927
  }
2928
2929
  friend bool operator!=(const raw_hash_set& a, const raw_hash_set& b) {
2930
    return !(a == b);
2931
  }
2932
2933
  template <typename H>
2934
  friend typename std::enable_if<H::template is_hashable<value_type>::value,
2935
                                 H>::type
2936
  AbslHashValue(H h, const raw_hash_set& s) {
2937
    return H::combine(H::combine_unordered(std::move(h), s.begin(), s.end()),
2938
                      hash_internal::WeaklyMixedInteger{s.size()});
2939
  }
2940
2941
  friend void swap(raw_hash_set& a,
2942
                   raw_hash_set& b) noexcept(noexcept(a.swap(b))) {
2943
    a.swap(b);
2944
  }
2945
2946
 private:
2947
  template <class Container, typename Enabler>
2948
  friend struct absl::container_internal::hashtable_debug_internal::
2949
      HashtableDebugAccess;
2950
2951
  friend struct absl::container_internal::HashtableFreeFunctionsAccess;
2952
2953
  struct FindElement {
2954
    template <class K, class... Args>
2955
    const_iterator operator()(const K& key, Args&&...) const {
2956
      return s.find(key);
2957
    }
2958
    const raw_hash_set& s;
2959
  };
2960
2961
  struct EmplaceDecomposable {
2962
    template <class K, class... Args>
2963
    std::pair<iterator, bool> operator()(const K& key, Args&&... args) const {
2964
      auto res = s.find_or_prepare_insert(key);
2965
      if (res.second) {
2966
        s.emplace_at(res.first, std::forward<Args>(args)...);
2967
      }
2968
      return res;
2969
    }
2970
    raw_hash_set& s;
2971
  };
2972
2973
  template <bool do_destroy>
2974
  struct InsertSlot {
2975
    template <class K, class... Args>
2976
    std::pair<iterator, bool> operator()(const K& key, Args&&...) && {
2977
      auto res = s.find_or_prepare_insert(key);
2978
      if (res.second) {
2979
        s.transfer(res.first.slot(), &slot);
2980
      } else if (do_destroy) {
2981
        s.destroy(&slot);
2982
      }
2983
      return res;
2984
    }
2985
    raw_hash_set& s;
2986
    // Constructed slot. Either moved into place or destroyed.
2987
    slot_type&& slot;
2988
  };
2989
2990
  template <typename... Args>
2991
  inline void construct(slot_type* slot, Args&&... args) {
2992
    common().RunWithReentrancyGuard([&] {
2993
      allocator_type alloc(char_alloc_ref());
2994
      PolicyTraits::construct(&alloc, slot, std::forward<Args>(args)...);
2995
    });
2996
  }
2997
  inline void destroy(slot_type* slot) {
2998
    common().RunWithReentrancyGuard([&] {
2999
      allocator_type alloc(char_alloc_ref());
3000
      PolicyTraits::destroy(&alloc, slot);
3001
    });
3002
  }
3003
  inline void transfer(slot_type* to, slot_type* from) {
3004
    common().RunWithReentrancyGuard([&] {
3005
      allocator_type alloc(char_alloc_ref());
3006
      PolicyTraits::transfer(&alloc, to, from);
3007
    });
3008
  }
3009
3010
  // TODO(b/289225379): consider having a helper class that has the impls for
3011
  // SOO functionality.
3012
  template <class K = key_type>
3013
  iterator find_small(const key_arg<K>& key) {
3014
    ABSL_SWISSTABLE_ASSERT(is_small());
3015
    return empty() || !equal_to(key, single_slot()) ? end() : single_iterator();
3016
  }
3017
3018
  template <class K = key_type>
3019
  iterator find_large(const key_arg<K>& key, size_t hash) {
3020
    ABSL_SWISSTABLE_ASSERT(!is_small());
3021
    auto seq = probe(common(), hash);
3022
    const h2_t h2 = H2(hash);
3023
    const ctrl_t* ctrl = control();
3024
    while (true) {
3025
#ifndef ABSL_HAVE_MEMORY_SANITIZER
3026
      absl::PrefetchToLocalCache(slot_array() + seq.offset());
3027
#endif
3028
      Group g{ctrl + seq.offset()};
3029
      for (uint32_t i : g.Match(h2)) {
3030
        if (ABSL_PREDICT_TRUE(equal_to(key, slot_array() + seq.offset(i))))
3031
          return iterator_at(seq.offset(i));
3032
      }
3033
      if (ABSL_PREDICT_TRUE(g.MaskEmpty())) return end();
3034
      seq.next();
3035
      ABSL_SWISSTABLE_ASSERT(seq.index() <= capacity() && "full table!");
3036
    }
3037
  }
3038
3039
  // Returns true if the table needs to be sampled. This keeps track of whether
3040
  // sampling has already been evaluated and ensures that it can only return
3041
  // true on its first evaluation. All subsequent calls will return false.
3042
  //
3043
  // This should be called on insertion into an empty SOO table and in copy
3044
  // construction when the size can fit in SOO capacity.
3045
  bool should_sample_soo() {
3046
    ABSL_SWISSTABLE_ASSERT(is_soo());
3047
    if (!ShouldSampleHashtablezInfoForAlloc<CharAlloc>()) return false;
3048
    if (common().soo_has_tried_sampling()) {
3049
      // Already evaluated sampling on this SOO table; do not re-evaluate
3050
      // sampling each time it transitions from empty to full SOO state.
3051
      return false;
3052
    }
3053
    // TODO: b/396049910 -- consider managing this flag on the 1->0 size
3054
    // transition of SOO tables rather than the 0->1 transition.
3055
    common().set_soo_has_tried_sampling();
3056
    return ABSL_PREDICT_FALSE(ShouldSampleNextTable());
3057
  }
3058
3059
  void clear_backing_array(bool reuse) {
3060
    ABSL_SWISSTABLE_ASSERT(capacity() > DefaultCapacity());
3061
    ClearBackingArray(common(), GetPolicyFunctions(), &char_alloc_ref(), reuse,
3062
                      SooEnabled());
3063
  }
3064
3065
  void destroy_slots() {
3066
    ABSL_SWISSTABLE_ASSERT(!is_small());
3067
    if (PolicyTraits::template destroy_is_trivial<Alloc>()) return;
3068
    auto destroy_slot = [&](const ctrl_t*, void* slot) {
3069
      this->destroy(static_cast<slot_type*>(slot));
3070
    };
3071
    if constexpr (SwisstableAssertAccessToDestroyedTable()) {
3072
      CommonFields common_copy(non_soo_tag_t{}, this->common());
3073
      common().set_capacity(InvalidCapacity::kDestroyed);
3074
      IterateOverFullSlots(common_copy, sizeof(slot_type), destroy_slot);
3075
      common().set_capacity(common_copy.capacity());
3076
    } else {
3077
      IterateOverFullSlots(common(), sizeof(slot_type), destroy_slot);
3078
    }
3079
  }
3080
3081
  void dealloc() {
3082
    ABSL_SWISSTABLE_ASSERT(capacity() > DefaultCapacity());
3083
    // Unpoison before returning the memory to the allocator.
3084
    SanitizerUnpoisonMemoryRegion(slot_array(), sizeof(slot_type) * capacity());
3085
    infoz().Unregister();
3086
    DeallocateBackingArray<BackingArrayAlignment(alignof(slot_type)),
3087
                           CharAlloc>(&char_alloc_ref(), capacity(), control(),
3088
                                      sizeof(slot_type), alignof(slot_type),
3089
                                      common().has_infoz());
3090
  }
3091
3092
  void destructor_impl() {
3093
    if (SwisstableGenerationsEnabled() &&
3094
        maybe_invalid_capacity() >= InvalidCapacity::kMovedFrom) {
3095
      return;
3096
    }
3097
    if (capacity() == 0) return;
3098
    if (is_small()) {
3099
      if (!empty()) {
3100
        ABSL_SWISSTABLE_IGNORE_UNINITIALIZED(destroy(single_slot()));
3101
      }
3102
      if constexpr (SooEnabled()) return;
3103
    } else {
3104
      destroy_slots();
3105
    }
3106
    dealloc();
3107
  }
3108
3109
  // Erases, but does not destroy, the value pointed to by `it`.
3110
  //
3111
  // This merely updates the pertinent control byte. This can be used in
3112
  // conjunction with Policy::transfer to move the object to another place.
3113
  void erase_meta_only(const_iterator it) {
3114
    if (is_small()) {
3115
      erase_meta_only_small();
3116
      return;
3117
    }
3118
    erase_meta_only_large(it);
3119
  }
3120
  void erase_meta_only_small() {
3121
    EraseMetaOnlySmall(common(), SooEnabled(), sizeof(slot_type));
3122
  }
3123
  void erase_meta_only_large(const_iterator it) {
3124
    EraseMetaOnlyLarge(common(), it.control(), sizeof(slot_type));
3125
  }
3126
3127
  template <class K>
3128
  ABSL_ATTRIBUTE_ALWAYS_INLINE bool equal_to(const K& key,
3129
                                             slot_type* slot) const {
3130
    return PolicyTraits::apply(EqualElement<K, key_equal>{key, eq_ref()},
3131
                               PolicyTraits::element(slot));
3132
  }
3133
  template <class K>
3134
  ABSL_ATTRIBUTE_ALWAYS_INLINE size_t hash_of(const K& key) const {
3135
    return HashElement<hasher, kIsDefaultHash>{hash_ref(),
3136
                                               common().seed().seed()}(key);
3137
  }
3138
  ABSL_ATTRIBUTE_ALWAYS_INLINE size_t hash_of(slot_type* slot) const {
3139
    return PolicyTraits::apply(
3140
        HashElement<hasher, kIsDefaultHash>{hash_ref(), common().seed().seed()},
3141
        PolicyTraits::element(slot));
3142
  }
3143
3144
  // Casting directly from e.g. char* to slot_type* can cause compilation errors
3145
  // on objective-C. This function converts to void* first, avoiding the issue.
3146
  static ABSL_ATTRIBUTE_ALWAYS_INLINE slot_type* to_slot(void* buf) {
3147
    return static_cast<slot_type*>(buf);
3148
  }
3149
3150
  // Requires that lhs does not have a full SOO slot.
3151
  static void move_common(bool rhs_is_full_soo, CharAlloc& rhs_alloc,
3152
                          CommonFields& lhs, CommonFields&& rhs) {
3153
    if (PolicyTraits::transfer_uses_memcpy() || !rhs_is_full_soo) {
3154
      lhs = std::move(rhs);
3155
    } else {
3156
      lhs.move_non_heap_or_soo_fields(rhs);
3157
      rhs.RunWithReentrancyGuard([&] {
3158
        lhs.RunWithReentrancyGuard([&] {
3159
          PolicyTraits::transfer(&rhs_alloc, to_slot(lhs.soo_data()),
3160
                                 to_slot(rhs.soo_data()));
3161
        });
3162
      });
3163
    }
3164
  }
3165
3166
  // Swaps common fields making sure to avoid memcpy'ing a full SOO slot if we
3167
  // aren't allowed to do so.
3168
  void swap_common(raw_hash_set& that) {
3169
    using std::swap;
3170
    if (PolicyTraits::transfer_uses_memcpy()) {
3171
      swap(common(), that.common());
3172
      return;
3173
    }
3174
    CommonFields tmp = CommonFields(uninitialized_tag_t{});
3175
    const bool that_is_full_soo = that.is_full_soo();
3176
    move_common(that_is_full_soo, that.char_alloc_ref(), tmp,
3177
                std::move(that.common()));
3178
    move_common(is_full_soo(), char_alloc_ref(), that.common(),
3179
                std::move(common()));
3180
    move_common(that_is_full_soo, that.char_alloc_ref(), common(),
3181
                std::move(tmp));
3182
  }
3183
3184
  void annotate_for_bug_detection_on_move([[maybe_unused]] raw_hash_set& that) {
3185
    // We only enable moved-from validation when generations are enabled (rather
3186
    // than using NDEBUG) to avoid issues in which NDEBUG is enabled in some
3187
    // translation units but not in others.
3188
    if (SwisstableGenerationsEnabled()) {
3189
      that.common().set_capacity(this == &that ? InvalidCapacity::kSelfMovedFrom
3190
                                               : InvalidCapacity::kMovedFrom);
3191
    }
3192
    if (!SwisstableGenerationsEnabled() ||
3193
        maybe_invalid_capacity() == DefaultCapacity() ||
3194
        maybe_invalid_capacity() > kAboveMaxValidCapacity) {
3195
      return;
3196
    }
3197
    common().increment_generation();
3198
    if (!empty() && common().should_rehash_for_bug_detection_on_move()) {
3199
      ResizeAllocatedTableWithSeedChange(common(), GetPolicyFunctions(),
3200
                                         capacity());
3201
    }
3202
  }
3203
3204
  template <bool propagate_alloc>
3205
  raw_hash_set& assign_impl(raw_hash_set&& that) {
3206
    // We don't bother checking for this/that aliasing. We just need to avoid
3207
    // breaking the invariants in that case.
3208
    destructor_impl();
3209
    move_common(that.is_full_soo(), that.char_alloc_ref(), common(),
3210
                std::move(that.common()));
3211
    hash_ref() = that.hash_ref();
3212
    eq_ref() = that.eq_ref();
3213
    CopyAlloc(char_alloc_ref(), that.char_alloc_ref(),
3214
              std::integral_constant<bool, propagate_alloc>());
3215
    that.common() = CommonFields::CreateDefault<SooEnabled()>();
3216
    annotate_for_bug_detection_on_move(that);
3217
    return *this;
3218
  }
3219
3220
  raw_hash_set& move_elements_allocs_unequal(raw_hash_set&& that) {
3221
    const size_t size = that.size();
3222
    if (size == 0) return *this;
3223
    reserve(size);
3224
    for (iterator it = that.begin(); it != that.end(); ++it) {
3225
      insert(std::move(PolicyTraits::element(it.slot())));
3226
      that.destroy(it.slot());
3227
    }
3228
    if (!that.is_soo()) that.dealloc();
3229
    that.common() = CommonFields::CreateDefault<SooEnabled()>();
3230
    annotate_for_bug_detection_on_move(that);
3231
    return *this;
3232
  }
3233
3234
  raw_hash_set& move_assign(raw_hash_set&& that,
3235
                            std::true_type /*propagate_alloc*/) {
3236
    return assign_impl<true>(std::move(that));
3237
  }
3238
  raw_hash_set& move_assign(raw_hash_set&& that,
3239
                            std::false_type /*propagate_alloc*/) {
3240
    if (char_alloc_ref() == that.char_alloc_ref()) {
3241
      return assign_impl<false>(std::move(that));
3242
    }
3243
    // Aliasing can't happen here because allocs would compare equal above.
3244
    assert(this != &that);
3245
    destructor_impl();
3246
    // We can't take over that's memory so we need to move each element.
3247
    // While moving elements, this should have that's hash/eq so copy hash/eq
3248
    // before moving elements.
3249
    hash_ref() = that.hash_ref();
3250
    eq_ref() = that.eq_ref();
3251
    return move_elements_allocs_unequal(std::move(that));
3252
  }
3253
3254
  template <class K>
3255
  std::pair<iterator, bool> find_or_prepare_insert_soo(const K& key) {
3256
    ABSL_SWISSTABLE_ASSERT(is_soo());
3257
    bool force_sampling;
3258
    if (empty()) {
3259
      if (!should_sample_soo()) {
3260
        common().set_full_soo();
3261
        return {single_iterator(), true};
3262
      }
3263
      force_sampling = true;
3264
    } else if (equal_to(key, single_slot())) {
3265
      return {single_iterator(), false};
3266
    } else {
3267
      force_sampling = false;
3268
    }
3269
    ABSL_SWISSTABLE_ASSERT(capacity() == 1);
3270
    constexpr bool kUseMemcpy =
3271
        PolicyTraits::transfer_uses_memcpy() && SooEnabled();
3272
    size_t index = GrowSooTableToNextCapacityAndPrepareInsert<
3273
        kUseMemcpy ? OptimalMemcpySizeForSooSlotTransfer(sizeof(slot_type)) : 0,
3274
        kUseMemcpy>(common(), GetPolicyFunctions(),
3275
                    HashKey<hasher, K, kIsDefaultHash>{hash_ref(), key},
3276
                    force_sampling);
3277
    return {iterator_at(index), true};
3278
  }
3279
3280
  template <class K>
3281
  std::pair<iterator, bool> find_or_prepare_insert_small(const K& key) {
3282
    ABSL_SWISSTABLE_ASSERT(is_small());
3283
    if constexpr (SooEnabled()) {
3284
      return find_or_prepare_insert_soo(key);
3285
    }
3286
    if (!empty()) {
3287
      if (equal_to(key, single_slot())) {
3288
        return {single_iterator(), false};
3289
      }
3290
    }
3291
    return {iterator_at_ptr(PrepareInsertSmallNonSoo(
3292
                common(), GetPolicyFunctions(),
3293
                HashKey<hasher, K, kIsDefaultHash>{hash_ref(), key})),
3294
            true};
3295
  }
3296
3297
  template <class K>
3298
  std::pair<iterator, bool> find_or_prepare_insert_large(const K& key) {
3299
    ABSL_SWISSTABLE_ASSERT(!is_soo());
3300
    prefetch_heap_block();
3301
    const size_t hash = hash_of(key);
3302
    auto seq = probe(common(), hash);
3303
    const h2_t h2 = H2(hash);
3304
    const ctrl_t* ctrl = control();
3305
    size_t index;
3306
    bool inserted;
3307
    // We use a lambda function to be able to exit from the nested loop without
3308
    // duplicating generated code for the return statement (e.g. iterator_at).
3309
    [&]() ABSL_ATTRIBUTE_ALWAYS_INLINE {
3310
      while (true) {
3311
#ifndef ABSL_HAVE_MEMORY_SANITIZER
3312
        absl::PrefetchToLocalCache(slot_array() + seq.offset());
3313
#endif
3314
        Group g{ctrl + seq.offset()};
3315
        for (uint32_t i : g.Match(h2)) {
3316
          if (ABSL_PREDICT_TRUE(equal_to(key, slot_array() + seq.offset(i)))) {
3317
            index = seq.offset(i);
3318
            inserted = false;
3319
            return;
3320
          }
3321
        }
3322
        auto mask_empty = g.MaskEmpty();
3323
        if (ABSL_PREDICT_TRUE(mask_empty)) {
3324
          size_t target_group_offset = seq.offset();
3325
          index = SwisstableGenerationsEnabled()
3326
                      ? PrepareInsertLargeGenerationsEnabled(
3327
                            common(), GetPolicyFunctions(), hash, mask_empty,
3328
                            FindInfo{target_group_offset, seq.index()},
3329
                            HashKey<hasher, K, kIsDefaultHash>{hash_ref(), key})
3330
                      : PrepareInsertLarge(
3331
                            common(), GetPolicyFunctions(), hash, mask_empty,
3332
                            FindInfo{target_group_offset, seq.index()});
3333
          inserted = true;
3334
          return;
3335
        }
3336
        seq.next();
3337
        ABSL_SWISSTABLE_ASSERT(seq.index() <= capacity() && "full table!");
3338
      }
3339
    }();
3340
    return {iterator_at(index), inserted};
3341
  }
3342
3343
  template <class InputIt>
3344
  void insert_range(InputIt first, InputIt last) {
3345
    for (; first != last; ++first) emplace(*first);
3346
  }
3347
3348
 protected:
3349
  // Asserts for correctness that we run on find/find_or_prepare_insert.
3350
  template <class K>
3351
  void AssertOnFind([[maybe_unused]] const K& key) {
3352
    AssertHashEqConsistent(key);
3353
    AssertNotDebugCapacity();
3354
  }
3355
3356
  // Asserts that the capacity is not a sentinel invalid value.
3357
  void AssertNotDebugCapacity() const {
3358
#ifdef NDEBUG
3359
    if (!SwisstableGenerationsEnabled()) {
3360
      return;
3361
    }
3362
#endif
3363
    const size_t cap = maybe_invalid_capacity();
3364
    if (ABSL_PREDICT_TRUE(cap < InvalidCapacity::kAboveMaxValidCapacity)) {
3365
      return;
3366
    }
3367
    assert(cap != InvalidCapacity::kReentrance &&
3368
           "Reentrant container access during element construction/destruction "
3369
           "is not allowed.");
3370
    if constexpr (SwisstableAssertAccessToDestroyedTable()) {
3371
      if (cap == InvalidCapacity::kDestroyed) {
3372
        ABSL_RAW_LOG(FATAL, "Use of destroyed hash table.");
3373
      }
3374
    }
3375
    if (SwisstableGenerationsEnabled() &&
3376
        ABSL_PREDICT_FALSE(cap >= InvalidCapacity::kMovedFrom)) {
3377
      if (cap == InvalidCapacity::kSelfMovedFrom) {
3378
        // If this log triggers, then a hash table was move-assigned to itself
3379
        // and then used again later without being reinitialized.
3380
        ABSL_RAW_LOG(FATAL, "Use of self-move-assigned hash table.");
3381
      }
3382
      ABSL_RAW_LOG(FATAL, "Use of moved-from hash table.");
3383
    }
3384
  }
3385
3386
  // Asserts that hash and equal functors provided by the user are consistent,
3387
  // meaning that `eq(k1, k2)` implies `hash(k1)==hash(k2)`.
3388
  template <class K>
3389
  void AssertHashEqConsistent(const K& key) {
3390
#ifdef NDEBUG
3391
    return;
3392
#endif
3393
    // If the hash/eq functors are known to be consistent, then skip validation.
3394
    if (std::is_same<hasher, absl::container_internal::StringHash>::value &&
3395
        std::is_same<key_equal, absl::container_internal::StringEq>::value) {
3396
      return;
3397
    }
3398
    if (std::is_scalar<key_type>::value &&
3399
        std::is_same<hasher, absl::Hash<key_type>>::value &&
3400
        std::is_same<key_equal, std::equal_to<key_type>>::value) {
3401
      return;
3402
    }
3403
    if (empty()) return;
3404
3405
    const size_t hash_of_arg = hash_of(key);
3406
    const auto assert_consistent = [&](const ctrl_t*, void* slot) {
3407
      const bool is_key_equal = equal_to(key, to_slot(slot));
3408
      if (!is_key_equal) return;
3409
3410
      [[maybe_unused]] const bool is_hash_equal =
3411
          hash_of_arg == hash_of(to_slot(slot));
3412
      assert((!is_key_equal || is_hash_equal) &&
3413
             "eq(k1, k2) must imply that hash(k1) == hash(k2). "
3414
             "hash/eq functors are inconsistent.");
3415
    };
3416
3417
    if (is_small()) {
3418
      assert_consistent(/*unused*/ nullptr, single_slot());
3419
      return;
3420
    }
3421
    // We only do validation for small tables so that it's constant time.
3422
    if (capacity() > 16) return;
3423
    IterateOverFullSlots(common(), sizeof(slot_type), assert_consistent);
3424
  }
3425
3426
  // Attempts to find `key` in the table; if it isn't found, returns an iterator
3427
  // where the value can be inserted into, with the control byte already set to
3428
  // `key`'s H2. Returns a bool indicating whether an insertion can take place.
3429
  template <class K>
3430
  std::pair<iterator, bool> find_or_prepare_insert(const K& key) {
3431
    AssertOnFind(key);
3432
    if (is_small()) return find_or_prepare_insert_small(key);
3433
    return find_or_prepare_insert_large(key);
3434
  }
3435
3436
  // Constructs the value in the space pointed by the iterator. This only works
3437
  // after an unsuccessful find_or_prepare_insert() and before any other
3438
  // modifications happen in the raw_hash_set.
3439
  //
3440
  // PRECONDITION: iter was returned from find_or_prepare_insert(k), where k is
3441
  // the key decomposed from `forward<Args>(args)...`, and the bool returned by
3442
  // find_or_prepare_insert(k) was true.
3443
  // POSTCONDITION: *m.iterator_at(i) == value_type(forward<Args>(args)...).
3444
  template <class... Args>
3445
  void emplace_at(iterator iter, Args&&... args) {
3446
    construct(iter.slot(), std::forward<Args>(args)...);
3447
3448
    // When is_small, find calls find_small and if size is 0, then it will
3449
    // return an end iterator. This can happen in the raw_hash_set copy ctor.
3450
    assert((is_small() ||
3451
            PolicyTraits::apply(FindElement{*this}, *iter) == iter) &&
3452
           "constructed value does not match the lookup key");
3453
  }
3454
3455
  iterator iterator_at(size_t i) ABSL_ATTRIBUTE_LIFETIME_BOUND {
3456
    return {control() + i, slot_array() + i, common().generation_ptr()};
3457
  }
3458
  const_iterator iterator_at(size_t i) const ABSL_ATTRIBUTE_LIFETIME_BOUND {
3459
    return const_cast<raw_hash_set*>(this)->iterator_at(i);
3460
  }
3461
  iterator iterator_at_ptr(std::pair<ctrl_t*, void*> ptrs)
3462
      ABSL_ATTRIBUTE_LIFETIME_BOUND {
3463
    return {ptrs.first, to_slot(ptrs.second), common().generation_ptr()};
3464
  }
3465
3466
  reference unchecked_deref(iterator it) { return it.unchecked_deref(); }
3467
3468
 private:
3469
  friend struct RawHashSetTestOnlyAccess;
3470
3471
  // The number of slots we can still fill without needing to rehash.
3472
  //
3473
  // This is stored separately due to tombstones: we do not include tombstones
3474
  // in the growth capacity, because we'd like to rehash when the table is
3475
  // otherwise filled with tombstones: otherwise, probe sequences might get
3476
  // unacceptably long without triggering a rehash. Callers can also force a
3477
  // rehash via the standard `rehash(0)`, which will recompute this value as a
3478
  // side-effect.
3479
  //
3480
  // See `CapacityToGrowth()`.
3481
  size_t growth_left() const {
3482
    return common().growth_left();
3483
  }
3484
3485
  GrowthInfo& growth_info() {
3486
    return common().growth_info();
3487
  }
3488
  GrowthInfo growth_info() const {
3489
    return common().growth_info();
3490
  }
3491
3492
  // Prefetch the heap-allocated memory region to resolve potential TLB and
3493
  // cache misses. This is intended to overlap with execution of calculating the
3494
  // hash for a key.
3495
  void prefetch_heap_block() const {
3496
    ABSL_SWISSTABLE_ASSERT(!is_soo());
3497
#if ABSL_HAVE_BUILTIN(__builtin_prefetch) || defined(__GNUC__)
3498
    __builtin_prefetch(control(), 0, 1);
3499
#endif
3500
  }
3501
3502
  CommonFields& common() { return settings_.template get<0>(); }
3503
  const CommonFields& common() const { return settings_.template get<0>(); }
3504
3505
  // For use when the capacity is potentially invalid so we shouldn't assume
3506
  // that the capacity is valid (which is done in `capacity()`).
3507
  size_t maybe_invalid_capacity() const { return common().capacity(); }
3508
  ctrl_t* control() const {
3509
    ABSL_SWISSTABLE_ASSERT(!is_soo());
3510
    return common().control();
3511
  }
3512
  slot_type* slot_array() const {
3513
    ABSL_SWISSTABLE_ASSERT(!is_soo());
3514
    return static_cast<slot_type*>(common().slot_array());
3515
  }
3516
  slot_type* soo_slot() {
3517
    ABSL_SWISSTABLE_ASSERT(is_soo());
3518
    ABSL_SWISSTABLE_IGNORE_UNINITIALIZED_RETURN(
3519
        static_cast<slot_type*>(common().soo_data()));
3520
  }
3521
  const slot_type* soo_slot() const {
3522
    ABSL_SWISSTABLE_IGNORE_UNINITIALIZED_RETURN(
3523
        const_cast<raw_hash_set*>(this)->soo_slot());
3524
  }
3525
  slot_type* single_slot() {
3526
    ABSL_SWISSTABLE_ASSERT(is_small());
3527
    return SooEnabled() ? soo_slot() : slot_array();
3528
  }
3529
  const slot_type* single_slot() const {
3530
    return const_cast<raw_hash_set*>(this)->single_slot();
3531
  }
3532
  void decrement_small_size() {
3533
    ABSL_SWISSTABLE_ASSERT(is_small());
3534
    SooEnabled() ? common().set_empty_soo() : common().decrement_size();
3535
    if (!SooEnabled()) {
3536
      SanitizerPoisonObject(single_slot());
3537
    }
3538
  }
3539
  iterator single_iterator() {
3540
    return {SooControl(), single_slot(), common().generation_ptr()};
3541
  }
3542
  const_iterator single_iterator() const {
3543
    return const_cast<raw_hash_set*>(this)->single_iterator();
3544
  }
3545
  HashtablezInfoHandle infoz() {
3546
    ABSL_SWISSTABLE_ASSERT(!is_soo());
3547
    return common().infoz();
3548
  }
3549
3550
  hasher& hash_ref() { return settings_.template get<1>(); }
3551
  const hasher& hash_ref() const { return settings_.template get<1>(); }
3552
  key_equal& eq_ref() { return settings_.template get<2>(); }
3553
  const key_equal& eq_ref() const { return settings_.template get<2>(); }
3554
  CharAlloc& char_alloc_ref() { return settings_.template get<3>(); }
3555
  const CharAlloc& char_alloc_ref() const {
3556
    return settings_.template get<3>();
3557
  }
3558
3559
  static void* get_char_alloc_ref_fn(CommonFields& common) {
3560
    auto* h = reinterpret_cast<raw_hash_set*>(&common);
3561
    return &h->char_alloc_ref();
3562
  }
3563
  static void* get_hash_ref_fn(CommonFields& common) {
3564
    auto* h = reinterpret_cast<raw_hash_set*>(&common);
3565
    // TODO(b/397453582): Remove support for const hasher.
3566
    return const_cast<std::remove_const_t<hasher>*>(&h->hash_ref());
3567
  }
3568
  static void transfer_n_slots_fn(void* set, void* dst, void* src,
3569
                                  size_t count) {
3570
    auto* src_slot = to_slot(src);
3571
    auto* dst_slot = to_slot(dst);
3572
3573
    auto* h = static_cast<raw_hash_set*>(set);
3574
    for (; count > 0; --count, ++src_slot, ++dst_slot) {
3575
      h->transfer(dst_slot, src_slot);
3576
    }
3577
  }
3578
3579
  // TODO(b/382423690): Try to type erase entire function or at least type erase
3580
  // by GetKey + Hash for memcpyable types.
3581
  // TODO(b/382423690): Try to type erase for big slots: sizeof(slot_type) > 16.
3582
  static void transfer_unprobed_elements_to_next_capacity_fn(
3583
      CommonFields& common, const ctrl_t* old_ctrl, void* old_slots,
3584
      void* probed_storage,
3585
      void (*encode_probed_element)(void* probed_storage, h2_t h2,
3586
                                    size_t source_offset, size_t h1)) {
3587
    const size_t new_capacity = common.capacity();
3588
    const size_t old_capacity = PreviousCapacity(new_capacity);
3589
    ABSL_ASSUME(old_capacity + 1 >= Group::kWidth);
3590
    ABSL_ASSUME((old_capacity + 1) % Group::kWidth == 0);
3591
3592
    auto* set = reinterpret_cast<raw_hash_set*>(&common);
3593
    slot_type* old_slots_ptr = to_slot(old_slots);
3594
    ctrl_t* new_ctrl = common.control();
3595
    slot_type* new_slots = set->slot_array();
3596
3597
    for (size_t group_index = 0; group_index < old_capacity;
3598
         group_index += Group::kWidth) {
3599
      GroupFullEmptyOrDeleted old_g(old_ctrl + group_index);
3600
      std::memset(new_ctrl + group_index, static_cast<int8_t>(ctrl_t::kEmpty),
3601
                  Group::kWidth);
3602
      std::memset(new_ctrl + group_index + old_capacity + 1,
3603
                  static_cast<int8_t>(ctrl_t::kEmpty), Group::kWidth);
3604
      // TODO(b/382423690): try to type erase everything outside of the loop.
3605
      // We will share a lot of code in expense of one function call per group.
3606
      for (auto in_fixed_group_index : old_g.MaskFull()) {
3607
        size_t old_index = group_index + in_fixed_group_index;
3608
        slot_type* old_slot = old_slots_ptr + old_index;
3609
        // TODO(b/382423690): try to avoid entire hash calculation since we need
3610
        // only one new bit of h1.
3611
        size_t hash = set->hash_of(old_slot);
3612
        size_t h1 = H1(hash);
3613
        h2_t h2 = H2(hash);
3614
        size_t new_index = TryFindNewIndexWithoutProbing(
3615
            h1, old_index, old_capacity, new_ctrl, new_capacity);
3616
        // Note that encode_probed_element is allowed to use old_ctrl buffer
3617
        // till and included the old_index.
3618
        if (ABSL_PREDICT_FALSE(new_index == kProbedElementIndexSentinel)) {
3619
          encode_probed_element(probed_storage, h2, old_index, h1);
3620
          continue;
3621
        }
3622
        ABSL_SWISSTABLE_ASSERT((new_index & old_capacity) <= old_index);
3623
        ABSL_SWISSTABLE_ASSERT(IsEmpty(new_ctrl[new_index]));
3624
        new_ctrl[new_index] = static_cast<ctrl_t>(h2);
3625
        auto* new_slot = new_slots + new_index;
3626
        SanitizerUnpoisonMemoryRegion(new_slot, sizeof(slot_type));
3627
        set->transfer(new_slot, old_slot);
3628
        SanitizerPoisonMemoryRegion(old_slot, sizeof(slot_type));
3629
      }
3630
    }
3631
  }
3632
3633
  static const PolicyFunctions& GetPolicyFunctions() {
3634
    static_assert(sizeof(slot_type) <= (std::numeric_limits<uint32_t>::max)(),
3635
                  "Slot size is too large. Use std::unique_ptr for value type "
3636
                  "or use absl::node_hash_{map,set}.");
3637
    static_assert(alignof(slot_type) <=
3638
                  size_t{(std::numeric_limits<uint16_t>::max)()});
3639
    static_assert(sizeof(key_type) <=
3640
                  size_t{(std::numeric_limits<uint32_t>::max)()});
3641
    static_assert(sizeof(value_type) <=
3642
                  size_t{(std::numeric_limits<uint32_t>::max)()});
3643
    static constexpr size_t kBackingArrayAlignment =
3644
        BackingArrayAlignment(alignof(slot_type));
3645
    static constexpr PolicyFunctions value = {
3646
        static_cast<uint32_t>(sizeof(key_type)),
3647
        static_cast<uint32_t>(sizeof(value_type)),
3648
        static_cast<uint32_t>(sizeof(slot_type)),
3649
        static_cast<uint16_t>(alignof(slot_type)), SooEnabled(),
3650
        ShouldSampleHashtablezInfoForAlloc<CharAlloc>(),
3651
        // TODO(b/328722020): try to type erase
3652
        // for standard layout and alignof(Hash) <= alignof(CommonFields).
3653
        std::is_empty_v<hasher> ? &GetRefForEmptyClass
3654
                                : &raw_hash_set::get_hash_ref_fn,
3655
        PolicyTraits::template get_hash_slot_fn<hasher, kIsDefaultHash>(),
3656
        PolicyTraits::transfer_uses_memcpy()
3657
            ? TransferNRelocatable<sizeof(slot_type)>
3658
            : &raw_hash_set::transfer_n_slots_fn,
3659
        std::is_empty_v<Alloc> ? &GetRefForEmptyClass
3660
                               : &raw_hash_set::get_char_alloc_ref_fn,
3661
        &AllocateBackingArray<kBackingArrayAlignment, CharAlloc>,
3662
        &DeallocateBackingArray<kBackingArrayAlignment, CharAlloc>,
3663
        &raw_hash_set::transfer_unprobed_elements_to_next_capacity_fn};
3664
    return value;
3665
  }
3666
3667
  // Bundle together CommonFields plus other objects which might be empty.
3668
  // CompressedTuple will ensure that sizeof is not affected by any of the empty
3669
  // fields that occur after CommonFields.
3670
  absl::container_internal::CompressedTuple<CommonFields, hasher, key_equal,
3671
                                            CharAlloc>
3672
      settings_{CommonFields::CreateDefault<SooEnabled()>(), hasher{},
3673
                key_equal{}, CharAlloc{}};
3674
};
3675
3676
// Friend access for free functions in raw_hash_set.h.
3677
struct HashtableFreeFunctionsAccess {
3678
  template <class Predicate, typename Set>
3679
  static typename Set::size_type EraseIf(Predicate& pred, Set* c) {
3680
    if (c->empty()) {
3681
      return 0;
3682
    }
3683
    if (c->is_small()) {
3684
      auto it = c->single_iterator();
3685
      if (!pred(*it)) {
3686
        ABSL_SWISSTABLE_ASSERT(c->size() == 1 &&
3687
                               "hash table was modified unexpectedly");
3688
        return 0;
3689
      }
3690
      c->destroy(it.slot());
3691
      c->erase_meta_only_small();
3692
      return 1;
3693
    }
3694
    [[maybe_unused]] const size_t original_size_for_assert = c->size();
3695
    size_t num_deleted = 0;
3696
    using SlotType = typename Set::slot_type;
3697
    IterateOverFullSlots(
3698
        c->common(), sizeof(SlotType),
3699
        [&](const ctrl_t* ctrl, void* slot_void) {
3700
          auto* slot = static_cast<SlotType*>(slot_void);
3701
          if (pred(Set::PolicyTraits::element(slot))) {
3702
            c->destroy(slot);
3703
            EraseMetaOnlyLarge(c->common(), ctrl, sizeof(*slot));
3704
            ++num_deleted;
3705
          }
3706
        });
3707
    // NOTE: IterateOverFullSlots allow removal of the current element, so we
3708
    // verify the size additionally here.
3709
    ABSL_SWISSTABLE_ASSERT(original_size_for_assert - num_deleted ==
3710
                               c->size() &&
3711
                           "hash table was modified unexpectedly");
3712
    return num_deleted;
3713
  }
3714
3715
  template <class Callback, typename Set>
3716
  static void ForEach(Callback& cb, Set* c) {
3717
    if (c->empty()) {
3718
      return;
3719
    }
3720
    if (c->is_small()) {
3721
      cb(*c->single_iterator());
3722
      return;
3723
    }
3724
    using SlotType = typename Set::slot_type;
3725
    using ElementTypeWithConstness = decltype(*c->begin());
3726
    IterateOverFullSlots(
3727
        c->common(), sizeof(SlotType), [&cb](const ctrl_t*, void* slot) {
3728
          ElementTypeWithConstness& element =
3729
              Set::PolicyTraits::element(static_cast<SlotType*>(slot));
3730
          cb(element);
3731
        });
3732
  }
3733
};
3734
3735
// Erases all elements that satisfy the predicate `pred` from the container `c`.
3736
template <typename P, typename... Params, typename Predicate>
3737
typename raw_hash_set<P, Params...>::size_type EraseIf(
3738
    Predicate& pred, raw_hash_set<P, Params...>* c) {
3739
  return HashtableFreeFunctionsAccess::EraseIf(pred, c);
3740
}
3741
3742
// Calls `cb` for all elements in the container `c`.
3743
template <typename P, typename... Params, typename Callback>
3744
void ForEach(Callback& cb, raw_hash_set<P, Params...>* c) {
3745
  return HashtableFreeFunctionsAccess::ForEach(cb, c);
3746
}
3747
template <typename P, typename... Params, typename Callback>
3748
void ForEach(Callback& cb, const raw_hash_set<P, Params...>* c) {
3749
  return HashtableFreeFunctionsAccess::ForEach(cb, c);
3750
}
3751
3752
namespace hashtable_debug_internal {
3753
template <typename Set>
3754
struct HashtableDebugAccess<Set, absl::void_t<typename Set::raw_hash_set>> {
3755
  using Traits = typename Set::PolicyTraits;
3756
  using Slot = typename Traits::slot_type;
3757
3758
  constexpr static bool kIsDefaultHash = Set::kIsDefaultHash;
3759
3760
  static size_t GetNumProbes(const Set& set,
3761
                             const typename Set::key_type& key) {
3762
    if (set.is_small()) return 0;
3763
    size_t num_probes = 0;
3764
    const size_t hash = set.hash_of(key);
3765
    auto seq = probe(set.common(), hash);
3766
    const h2_t h2 = H2(hash);
3767
    const ctrl_t* ctrl = set.control();
3768
    while (true) {
3769
      container_internal::Group g{ctrl + seq.offset()};
3770
      for (uint32_t i : g.Match(h2)) {
3771
        if (set.equal_to(key, set.slot_array() + seq.offset(i)))
3772
          return num_probes;
3773
        ++num_probes;
3774
      }
3775
      if (g.MaskEmpty()) return num_probes;
3776
      seq.next();
3777
      ++num_probes;
3778
    }
3779
  }
3780
3781
  static size_t AllocatedByteSize(const Set& c) {
3782
    size_t capacity = c.capacity();
3783
    if (capacity == 0) return 0;
3784
    size_t m =
3785
        c.is_soo() ? 0 : c.common().alloc_size(sizeof(Slot), alignof(Slot));
3786
3787
    size_t per_slot = Traits::space_used(static_cast<const Slot*>(nullptr));
3788
    if (per_slot != ~size_t{}) {
3789
      m += per_slot * c.size();
3790
    } else {
3791
      for (auto it = c.begin(); it != c.end(); ++it) {
3792
        m += Traits::space_used(it.slot());
3793
      }
3794
    }
3795
    return m;
3796
  }
3797
};
3798
3799
}  // namespace hashtable_debug_internal
3800
3801
// Extern template instantiations reduce binary size and linker input size.
3802
// Function definition is in raw_hash_set.cc.
3803
extern template size_t GrowSooTableToNextCapacityAndPrepareInsert<0, false>(
3804
    CommonFields&, const PolicyFunctions&, absl::FunctionRef<size_t(size_t)>,
3805
    bool);
3806
extern template size_t GrowSooTableToNextCapacityAndPrepareInsert<1, true>(
3807
    CommonFields&, const PolicyFunctions&, absl::FunctionRef<size_t(size_t)>,
3808
    bool);
3809
extern template size_t GrowSooTableToNextCapacityAndPrepareInsert<4, true>(
3810
    CommonFields&, const PolicyFunctions&, absl::FunctionRef<size_t(size_t)>,
3811
    bool);
3812
extern template size_t GrowSooTableToNextCapacityAndPrepareInsert<8, true>(
3813
    CommonFields&, const PolicyFunctions&, absl::FunctionRef<size_t(size_t)>,
3814
    bool);
3815
#if UINTPTR_MAX == UINT64_MAX
3816
extern template size_t GrowSooTableToNextCapacityAndPrepareInsert<16, true>(
3817
    CommonFields&, const PolicyFunctions&, absl::FunctionRef<size_t(size_t)>,
3818
    bool);
3819
#endif
3820
3821
extern template void* AllocateBackingArray<
3822
    BackingArrayAlignment(alignof(size_t)), std::allocator<char>>(void* alloc,
3823
                                                                  size_t n);
3824
extern template void DeallocateBackingArray<
3825
    BackingArrayAlignment(alignof(size_t)), std::allocator<char>>(
3826
    void* alloc, size_t capacity, ctrl_t* ctrl, size_t slot_size,
3827
    size_t slot_align, bool had_infoz);
3828
3829
}  // namespace container_internal
3830
ABSL_NAMESPACE_END
3831
}  // namespace absl
3832
3833
#undef ABSL_SWISSTABLE_ENABLE_GENERATIONS
3834
#undef ABSL_SWISSTABLE_IGNORE_UNINITIALIZED
3835
#undef ABSL_SWISSTABLE_IGNORE_UNINITIALIZED_RETURN
3836
#undef ABSL_SWISSTABLE_ASSERT
3837
3838
#endif  // ABSL_CONTAINER_INTERNAL_RAW_HASH_SET_H_