Coverage Report

Created: 2025-07-11 07:01

/src/parallel-hashmap/parallel_hashmap/phmap.h
Line
Count
Source (jump to first uncovered line)
1
#if !defined(phmap_h_guard_)
2
#define phmap_h_guard_
3
4
// ---------------------------------------------------------------------------
5
// Copyright (c) 2019, Gregory Popovitch - greg7mdp@gmail.com
6
//
7
// Licensed under the Apache License, Version 2.0 (the "License");
8
// you may not use this file except in compliance with the License.
9
// You may obtain a copy of the License at
10
//
11
//      https://www.apache.org/licenses/LICENSE-2.0
12
//
13
// Unless required by applicable law or agreed to in writing, software
14
// distributed under the License is distributed on an "AS IS" BASIS,
15
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
// See the License for the specific language governing permissions and
17
// limitations under the License.
18
//
19
// Includes work from abseil-cpp (https://github.com/abseil/abseil-cpp)
20
// with modifications.
21
// 
22
// Copyright 2018 The Abseil Authors.
23
//
24
// Licensed under the Apache License, Version 2.0 (the "License");
25
// you may not use this file except in compliance with the License.
26
// You may obtain a copy of the License at
27
//
28
//      https://www.apache.org/licenses/LICENSE-2.0
29
//
30
// Unless required by applicable law or agreed to in writing, software
31
// distributed under the License is distributed on an "AS IS" BASIS,
32
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33
// See the License for the specific language governing permissions and
34
// limitations under the License.
35
// ---------------------------------------------------------------------------
36
37
// ---------------------------------------------------------------------------
38
// IMPLEMENTATION DETAILS
39
//
40
// The table stores elements inline in a slot array. In addition to the slot
41
// array the table maintains some control state per slot. The extra state is one
42
// byte per slot and stores empty or deleted marks, or alternatively 7 bits from
43
// the hash of an occupied slot. The table is split into logical groups of
44
// slots, like so:
45
//
46
//      Group 1         Group 2        Group 3
47
// +---------------+---------------+---------------+
48
// | | | | | | | | | | | | | | | | | | | | | | | | |
49
// +---------------+---------------+---------------+
50
//
51
// On lookup the hash is split into two parts:
52
// - H2: 7 bits (those stored in the control bytes)
53
// - H1: the rest of the bits
54
// The groups are probed using H1. For each group the slots are matched to H2 in
55
// parallel. Because H2 is 7 bits (128 states) and the number of slots per group
56
// is low (8 or 16) in almost all cases a match in H2 is also a lookup hit.
57
//
58
// On insert, once the right group is found (as in lookup), its slots are
59
// filled in order.
60
//
61
// On erase a slot is cleared. In case the group did not have any empty slots
62
// before the erase, the erased slot is marked as deleted.
63
//
64
// Groups without empty slots (but maybe with deleted slots) extend the probe
65
// sequence. The probing algorithm is quadratic. Given N the number of groups,
66
// the probing function for the i'th probe is:
67
//
68
//   P(0) = H1 % N
69
//
70
//   P(i) = (P(i - 1) + i) % N
71
//
72
// This probing function guarantees that after N probes, all the groups of the
73
// table will be probed exactly once.
74
//
75
// The control state and slot array are stored contiguously in a shared heap
76
// allocation. The layout of this allocation is: `capacity()` control bytes,
77
// one sentinel control byte, `Group::kWidth - 1` cloned control bytes,
78
// <possible padding>, `capacity()` slots. The sentinel control byte is used in
79
// iteration so we know when we reach the end of the table. The cloned control
80
// bytes at the end of the table are cloned from the beginning of the table so
81
// groups that begin near the end of the table can see a full group. In cases in
82
// which there are more than `capacity()` cloned control bytes, the extra bytes
83
// are `kEmpty`, and these ensure that we always see at least one empty slot and
84
// can stop an unsuccessful search.
85
// ---------------------------------------------------------------------------
86
87
88
89
#ifdef _MSC_VER
90
    #pragma warning(push)  
91
92
    #pragma warning(disable : 4127) // conditional expression is constant
93
    #pragma warning(disable : 4324) // structure was padded due to alignment specifier
94
    #pragma warning(disable : 4514) // unreferenced inline function has been removed
95
    #pragma warning(disable : 4623) // default constructor was implicitly defined as deleted
96
    #pragma warning(disable : 4625) // copy constructor was implicitly defined as deleted
97
    #pragma warning(disable : 4626) // assignment operator was implicitly defined as deleted
98
    #pragma warning(disable : 4710) // function not inlined
99
    #pragma warning(disable : 4711) // selected for automatic inline expansion
100
    #pragma warning(disable : 4820) // '6' bytes padding added after data member
101
    #pragma warning(disable : 4868) // compiler may not enforce left-to-right evaluation order in braced initializer list
102
    #pragma warning(disable : 5027) // move assignment operator was implicitly defined as deleted
103
    #pragma warning(disable : 5045) // Compiler will insert Spectre mitigation for memory load if /Qspectre switch specified
104
#endif
105
106
#include <algorithm>
107
#include <cmath>
108
#include <cstring>
109
#include <iterator>
110
#include <limits>
111
#include <memory>
112
#include <tuple>
113
#include <type_traits>
114
#include <utility>
115
#include <array>
116
#include <cassert>
117
#include <atomic>
118
119
#include "phmap_fwd_decl.h"
120
#include "phmap_utils.h"
121
#include "phmap_base.h"
122
123
#if PHMAP_HAVE_STD_STRING_VIEW
124
    #include <string_view>
125
#endif
126
127
namespace phmap {
128
129
namespace priv {
130
131
// --------------------------------------------------------------------------
132
template <typename AllocType>
133
void SwapAlloc(AllocType& lhs, AllocType& rhs,
134
               std::true_type /* propagate_on_container_swap */) {
135
  using std::swap;
136
  swap(lhs, rhs);
137
}
138
139
template <typename AllocType>
140
void SwapAlloc(AllocType& /*lhs*/, AllocType& /*rhs*/,
141
483
               std::false_type /* propagate_on_container_swap */) {}
142
143
// --------------------------------------------------------------------------
144
template <size_t Width>
145
class probe_seq 
146
{
147
public:
148
10.8M
    probe_seq(size_t hashval, size_t mask) {
149
10.8M
        assert(((mask + 1) & mask) == 0 && "not a mask");
150
10.8M
        mask_ = mask;
151
10.8M
        offset_ = hashval & mask_;
152
10.8M
    }
153
11.9M
    size_t offset() const { return offset_; }
154
20.3M
    size_t offset(size_t i) const { return (offset_ + i) & mask_; }
155
156
1.09M
    void next() {
157
1.09M
        index_ += Width;
158
1.09M
        offset_ += index_;
159
1.09M
        offset_ &= mask_;
160
1.09M
    }
161
    // 0-based probe index. The i-th probe in the probe sequence.
162
1.79M
    size_t getindex() const { return index_; }
163
164
private:
165
    size_t mask_;
166
    size_t offset_;
167
    size_t index_ = 0;
168
};
169
170
// --------------------------------------------------------------------------
171
template <class ContainerKey, class Hash, class Eq>
172
struct RequireUsableKey 
173
{
174
    template <class PassedKey, class... Args>
175
    std::pair<
176
        decltype(std::declval<const Hash&>()(std::declval<const PassedKey&>())),
177
        decltype(std::declval<const Eq&>()(std::declval<const ContainerKey&>(),
178
                                           std::declval<const PassedKey&>()))>*
179
    operator()(const PassedKey&, const Args&...) const;
180
};
181
182
// --------------------------------------------------------------------------
183
template <class E, class Policy, class Hash, class Eq, class... Ts>
184
struct IsDecomposable : std::false_type {};
185
186
template <class Policy, class Hash, class Eq, class... Ts>
187
struct IsDecomposable<
188
    phmap::void_t<decltype(
189
        Policy::apply(RequireUsableKey<typename Policy::key_type, Hash, Eq>(),
190
                      std::declval<Ts>()...))>,
191
    Policy, Hash, Eq, Ts...> : std::true_type {};
192
193
// TODO(alkis): Switch to std::is_nothrow_swappable when gcc/clang supports it.
194
// --------------------------------------------------------------------------
195
template <class T>
196
0
constexpr bool IsNoThrowSwappable(std::true_type = {} /* is_swappable */) {
197
0
    using std::swap;
198
0
    return noexcept(swap(std::declval<T&>(), std::declval<T&>()));
199
0
}
Unexecuted instantiation: bool phmap::priv::IsNoThrowSwappable<phmap::Hash<unsigned int> >(std::__1::integral_constant<bool, true>)
Unexecuted instantiation: bool phmap::priv::IsNoThrowSwappable<phmap::EqualTo<unsigned int> >(std::__1::integral_constant<bool, true>)
200
201
template <class T>
202
0
constexpr bool IsNoThrowSwappable(std::false_type /* is_swappable */) {
203
0
  return false;
204
0
}
205
206
// --------------------------------------------------------------------------
207
template <typename T>
208
12.6M
uint32_t TrailingZeros(T x) {
209
12.6M
    uint32_t res;
210
12.6M
    PHMAP_IF_CONSTEXPR(sizeof(T) == 8)
211
        res = base_internal::CountTrailingZerosNonZero64(static_cast<uint64_t>(x));
212
    else
213
12.6M
        res = base_internal::CountTrailingZerosNonZero32(static_cast<uint32_t>(x));
214
12.6M
    return res;
215
12.6M
}
unsigned int phmap::priv::TrailingZeros<unsigned int>(unsigned int)
Line
Count
Source
208
12.6M
uint32_t TrailingZeros(T x) {
209
12.6M
    uint32_t res;
210
12.6M
    PHMAP_IF_CONSTEXPR(sizeof(T) == 8)
211
        res = base_internal::CountTrailingZerosNonZero64(static_cast<uint64_t>(x));
212
    else
213
12.6M
        res = base_internal::CountTrailingZerosNonZero32(static_cast<uint32_t>(x));
214
12.6M
    return res;
215
12.6M
}
Unexecuted instantiation: unsigned int phmap::priv::TrailingZeros<unsigned long>(unsigned long)
216
217
// --------------------------------------------------------------------------
218
template <typename T>
219
0
uint32_t LeadingZeros(T x) {
220
0
    uint32_t res;
221
0
    PHMAP_IF_CONSTEXPR(sizeof(T) == 8)
222
0
        res = base_internal::CountLeadingZeros64(static_cast<uint64_t>(x));
223
0
    else
224
0
        res = base_internal::CountLeadingZeros32(static_cast<uint32_t>(x));
225
0
    return res;
226
0
}
227
228
// --------------------------------------------------------------------------
229
// An abstraction over a bitmask. It provides an easy way to iterate through the
230
// indexes of the set bits of a bitmask.  When Shift=0 (platforms with SSE),
231
// this is a true bitmask.  On non-SSE, platforms the arithematic used to
232
// emulate the SSE behavior works in bytes (Shift=3) and leaves each bytes as
233
// either 0x00 or 0x80.
234
//
235
// For example:
236
//   for (int i : BitMask<uint32_t, 16>(0x5)) -> yields 0, 2
237
//   for (int i : BitMask<uint64_t, 8, 3>(0x0000000080800000)) -> yields 2, 3
238
// --------------------------------------------------------------------------
239
template <class T, int SignificantBits, int Shift = 0>
240
class BitMask 
241
{
242
    static_assert(std::is_unsigned<T>::value, "");
243
    static_assert(Shift == 0 || Shift == 3, "");
244
245
public:
246
    // These are useful for unit tests (gunit).
247
    using value_type = int;
248
    using iterator = BitMask;
249
    using const_iterator = BitMask;
250
251
23.6M
    explicit BitMask(T mask) : mask_(mask) {}
252
253
2.22M
    BitMask& operator++() {    // ++iterator
254
2.22M
        mask_ &= (mask_ - 1);  // clear the least significant bit set
255
2.22M
        return *this;
256
2.22M
    }
257
258
3.40M
    explicit operator bool() const { return mask_ != 0; }
259
10.7M
    uint32_t operator*() const { return LowestBitSet(); }
260
261
12.4M
    uint32_t LowestBitSet() const {
262
12.4M
        return priv::TrailingZeros(mask_) >> Shift;
263
12.4M
    }
264
265
    uint32_t HighestBitSet() const {
266
        return (sizeof(T) * CHAR_BIT - priv::LeadingZeros(mask_) - 1) >> Shift;
267
    }
268
269
10.1M
    BitMask begin() const { return *this; }
270
10.1M
    BitMask end() const { return BitMask(0); }
271
272
    uint32_t TrailingZeros() const {
273
        return priv::TrailingZeros(mask_) >> Shift;
274
    }
275
276
    uint32_t LeadingZeros() const {
277
        constexpr uint32_t total_significant_bits = SignificantBits << Shift;
278
        constexpr uint32_t extra_bits = sizeof(T) * 8 - total_significant_bits;
279
        return priv::LeadingZeros(mask_ << extra_bits) >> Shift;
280
    }
281
282
private:
283
    friend bool operator==(const BitMask& a, const BitMask& b) {
284
        return a.mask_ == b.mask_;
285
    }
286
12.3M
    friend bool operator!=(const BitMask& a, const BitMask& b) {
287
12.3M
        return a.mask_ != b.mask_;
288
12.3M
    }
289
290
    T mask_;
291
};
292
293
// --------------------------------------------------------------------------
294
using ctrl_t = signed char;
295
using h2_t = uint8_t;
296
297
// --------------------------------------------------------------------------
298
// The values here are selected for maximum performance. See the static asserts
299
// below for details.
300
// --------------------------------------------------------------------------
301
enum Ctrl : ctrl_t 
302
{
303
    kEmpty = -128,   // 0b10000000 or 0x80
304
    kDeleted = -2,   // 0b11111110 or 0xfe
305
    kSentinel = -1,  // 0b11111111 or 0xff
306
};
307
308
static_assert(
309
    kEmpty & kDeleted & kSentinel & 0x80,
310
    "Special markers need to have the MSB to make checking for them efficient");
311
static_assert(kEmpty < kSentinel && kDeleted < kSentinel,
312
              "kEmpty and kDeleted must be smaller than kSentinel to make the "
313
              "SIMD test of IsEmptyOrDeleted() efficient");
314
static_assert(kSentinel == -1,
315
              "kSentinel must be -1 to elide loading it from memory into SIMD "
316
              "registers (pcmpeqd xmm, xmm)");
317
static_assert(kEmpty == -128,
318
              "kEmpty must be -128 to make the SIMD check for its "
319
              "existence efficient (psignb xmm, xmm)");
320
static_assert(~kEmpty & ~kDeleted & kSentinel & 0x7F,
321
              "kEmpty and kDeleted must share an unset bit that is not shared "
322
              "by kSentinel to make the scalar test for MatchEmptyOrDeleted() "
323
              "efficient");
324
static_assert(kDeleted == -2,
325
              "kDeleted must be -2 to make the implementation of "
326
              "ConvertSpecialToEmptyAndFullToDeleted efficient");
327
328
// --------------------------------------------------------------------------
329
// A single block of empty control bytes for tables without any slots allocated.
330
// This enables removing a branch in the hot path of find().
331
// --------------------------------------------------------------------------
332
template <class std_alloc_t>
333
2.50k
inline ctrl_t* EmptyGroup() {
334
2.50k
  PHMAP_IF_CONSTEXPR (std_alloc_t::value) {
335
2.50k
      alignas(16) static constexpr ctrl_t empty_group[] = {
336
2.50k
          kSentinel, kEmpty, kEmpty, kEmpty, kEmpty, kEmpty, kEmpty, kEmpty,
337
2.50k
          kEmpty,    kEmpty, kEmpty, kEmpty, kEmpty, kEmpty, kEmpty, kEmpty};
338
339
2.50k
      return const_cast<ctrl_t*>(empty_group);
340
  } else {
341
       return nullptr;
342
  }
343
2.50k
}
signed char* phmap::priv::EmptyGroup<std::__1::is_same<std::__1::allocator<std::__1::pair<unsigned int const, int> >, std::__1::allocator<std::__1::pair<unsigned int const, int> > > >()
Line
Count
Source
333
2.02k
inline ctrl_t* EmptyGroup() {
334
2.02k
  PHMAP_IF_CONSTEXPR (std_alloc_t::value) {
335
2.02k
      alignas(16) static constexpr ctrl_t empty_group[] = {
336
2.02k
          kSentinel, kEmpty, kEmpty, kEmpty, kEmpty, kEmpty, kEmpty, kEmpty,
337
2.02k
          kEmpty,    kEmpty, kEmpty, kEmpty, kEmpty, kEmpty, kEmpty, kEmpty};
338
339
2.02k
      return const_cast<ctrl_t*>(empty_group);
340
  } else {
341
       return nullptr;
342
  }
343
2.02k
}
signed char* phmap::priv::EmptyGroup<std::__1::is_same<std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > > >()
Line
Count
Source
333
483
inline ctrl_t* EmptyGroup() {
334
483
  PHMAP_IF_CONSTEXPR (std_alloc_t::value) {
335
483
      alignas(16) static constexpr ctrl_t empty_group[] = {
336
483
          kSentinel, kEmpty, kEmpty, kEmpty, kEmpty, kEmpty, kEmpty, kEmpty,
337
483
          kEmpty,    kEmpty, kEmpty, kEmpty, kEmpty, kEmpty, kEmpty, kEmpty};
338
339
483
      return const_cast<ctrl_t*>(empty_group);
340
  } else {
341
       return nullptr;
342
  }
343
483
}
344
345
// --------------------------------------------------------------------------
346
0
inline size_t HashSeed(const ctrl_t* ctrl) {
347
0
  // The low bits of the pointer have little or no entropy because of
348
0
  // alignment. We shift the pointer to try to use higher entropy bits. A
349
0
  // good number seems to be 12 bits, because that aligns with page size.
350
0
  return reinterpret_cast<uintptr_t>(ctrl) >> 12;
351
0
}
352
353
#ifdef PHMAP_NON_DETERMINISTIC
354
355
inline size_t H1(size_t hashval, const ctrl_t* ctrl) {
356
    // use ctrl_ pointer to add entropy to ensure
357
    // non-deterministic iteration order.
358
    return (hashval >> 7) ^ HashSeed(ctrl);
359
}
360
361
#else
362
363
10.8M
inline size_t H1(size_t hashval, const ctrl_t* ) {
364
10.8M
    return (hashval >> 7);
365
10.8M
}
366
367
#endif
368
369
370
11.8M
inline ctrl_t H2(size_t hashval)       { return (ctrl_t)(hashval & 0x7F); }
371
372
614k
inline bool IsEmpty(ctrl_t c)          { return c == kEmpty; }
373
2.92M
inline bool IsFull(ctrl_t c)           { return c >= static_cast<ctrl_t>(0); }
374
2.70k
inline bool IsDeleted(ctrl_t c)        { return c == kDeleted; }
375
832k
inline bool IsEmptyOrDeleted(ctrl_t c) { return c < kSentinel; }
376
377
#if PHMAP_HAVE_SSE2
378
379
#ifdef _MSC_VER
380
    #pragma warning(push)  
381
    #pragma warning(disable : 4365) // conversion from 'int' to 'T', signed/unsigned mismatch
382
#endif
383
384
// --------------------------------------------------------------------------
385
// https://github.com/abseil/abseil-cpp/issues/209
386
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87853
387
// _mm_cmpgt_epi8 is broken under GCC with -funsigned-char
388
// Work around this by using the portable implementation of Group
389
// when using -funsigned-char under GCC.
390
// --------------------------------------------------------------------------
391
2.01M
inline __m128i _mm_cmpgt_epi8_fixed(__m128i a, __m128i b) {
392
#if defined(__GNUC__) && !defined(__clang__)
393
  #pragma GCC diagnostic push
394
  #pragma GCC diagnostic ignored "-Woverflow"
395
396
  if (std::is_unsigned<char>::value) {
397
    const __m128i mask = _mm_set1_epi8(static_cast<char>(0x80));
398
    const __m128i diff = _mm_subs_epi8(b, a);
399
    return _mm_cmpeq_epi8(_mm_and_si128(diff, mask), mask);
400
  }
401
402
  #pragma GCC diagnostic pop
403
#endif
404
2.01M
  return _mm_cmpgt_epi8(a, b);
405
2.01M
}
406
407
// --------------------------------------------------------------------------
408
// --------------------------------------------------------------------------
409
struct GroupSse2Impl 
410
{
411
    enum { kWidth = 16 };  // the number of slots per group
412
413
12.1M
    explicit GroupSse2Impl(const ctrl_t* pos) {
414
12.1M
        ctrl = _mm_loadu_si128(reinterpret_cast<const __m128i*>(pos));
415
12.1M
    }
416
417
    // Returns a bitmask representing the positions of slots that match hash.
418
    // ----------------------------------------------------------------------
419
11.7M
    BitMask<uint32_t, kWidth> Match(h2_t hash) const {
420
11.7M
        auto match = _mm_set1_epi8((char)hash);
421
11.7M
        return BitMask<uint32_t, kWidth>(
422
11.7M
            static_cast<uint32_t>(_mm_movemask_epi8(_mm_cmpeq_epi8(match, ctrl))));
423
11.7M
    }
424
425
    // Returns a bitmask representing the positions of empty slots.
426
    // ------------------------------------------------------------
427
1.62M
    BitMask<uint32_t, kWidth> MatchEmpty() const {
428
#if PHMAP_HAVE_SSSE3
429
        // This only works because kEmpty is -128.
430
        return BitMask<uint32_t, kWidth>(
431
            static_cast<uint32_t>(_mm_movemask_epi8(_mm_sign_epi8(ctrl, ctrl))));
432
#else
433
1.62M
        return Match(static_cast<h2_t>(kEmpty));
434
1.62M
#endif
435
1.62M
    }
436
437
    // Returns a bitmask representing the positions of empty or deleted slots.
438
    // -----------------------------------------------------------------------
439
1.78M
    BitMask<uint32_t, kWidth> MatchEmptyOrDeleted() const {
440
1.78M
        auto special = _mm_set1_epi8(static_cast<char>(kSentinel));
441
1.78M
        return BitMask<uint32_t, kWidth>(
442
1.78M
            static_cast<uint32_t>(_mm_movemask_epi8(_mm_cmpgt_epi8_fixed(special, ctrl))));
443
1.78M
    }
444
445
    // Returns the number of trailing empty or deleted elements in the group.
446
    // ----------------------------------------------------------------------
447
230k
    uint32_t CountLeadingEmptyOrDeleted() const {
448
230k
        auto special = _mm_set1_epi8(static_cast<char>(kSentinel));
449
230k
        return TrailingZeros(
450
230k
            static_cast<uint32_t>(_mm_movemask_epi8(_mm_cmpgt_epi8_fixed(special, ctrl)) + 1));
451
230k
    }
452
453
    // ----------------------------------------------------------------------
454
0
    void ConvertSpecialToEmptyAndFullToDeleted(ctrl_t* dst) const {
455
0
        auto msbs = _mm_set1_epi8(static_cast<char>(-128));
456
0
        auto x126 = _mm_set1_epi8(126);
457
#if PHMAP_HAVE_SSSE3
458
        auto res = _mm_or_si128(_mm_shuffle_epi8(x126, ctrl), msbs);
459
#else
460
0
        auto zero = _mm_setzero_si128();
461
0
        auto special_mask = _mm_cmpgt_epi8_fixed(zero, ctrl);
462
0
        auto res = _mm_or_si128(msbs, _mm_andnot_si128(special_mask, x126));
463
0
#endif
464
0
        _mm_storeu_si128(reinterpret_cast<__m128i*>(dst), res);
465
0
    }
466
467
    __m128i ctrl;
468
};
469
470
#ifdef _MSC_VER
471
     #pragma warning(pop)  
472
#endif
473
474
#endif  // PHMAP_HAVE_SSE2
475
476
// --------------------------------------------------------------------------
477
// --------------------------------------------------------------------------
478
struct GroupPortableImpl 
479
{
480
    enum { kWidth = 8 };
481
482
    explicit GroupPortableImpl(const ctrl_t* pos)
483
0
        : ctrl(little_endian::Load64(pos)) {}
484
485
0
    BitMask<uint64_t, kWidth, 3> Match(h2_t hash) const {
486
0
        // For the technique, see:
487
0
        // http://graphics.stanford.edu/~seander/bithacks.html##ValueInWord
488
0
        // (Determine if a word has a byte equal to n).
489
0
        //
490
0
        // Caveat: there are false positives but:
491
0
        // - they only occur if there is a real match
492
0
        // - they never occur on kEmpty, kDeleted, kSentinel
493
0
        // - they will be handled gracefully by subsequent checks in code
494
0
        //
495
0
        // Example:
496
0
        //   v = 0x1716151413121110
497
0
        //   hash = 0x12
498
0
        //   retval = (v - lsbs) & ~v & msbs = 0x0000000080800000
499
0
        constexpr uint64_t msbs = 0x8080808080808080ULL;
500
0
        constexpr uint64_t lsbs = 0x0101010101010101ULL;
501
0
        auto x = ctrl ^ (lsbs * hash);
502
0
        return BitMask<uint64_t, kWidth, 3>((x - lsbs) & ~x & msbs);
503
0
    }
504
505
0
    BitMask<uint64_t, kWidth, 3> MatchEmpty() const {          // bit 1 of each byte is 0 for empty (but not for deleted)
506
0
        constexpr uint64_t msbs = 0x8080808080808080ULL;
507
0
        return BitMask<uint64_t, kWidth, 3>((ctrl & (~ctrl << 6)) & msbs);
508
0
    }
509
510
0
    BitMask<uint64_t, kWidth, 3> MatchEmptyOrDeleted() const { // lsb of each byte is 0 for empty or deleted
511
0
        constexpr uint64_t msbs = 0x8080808080808080ULL;
512
0
        return BitMask<uint64_t, kWidth, 3>((ctrl & (~ctrl << 7)) & msbs);
513
0
    }
514
515
0
    uint32_t CountLeadingEmptyOrDeleted() const {
516
0
        constexpr uint64_t gaps = 0x00FEFEFEFEFEFEFEULL;
517
0
        return (uint32_t)((TrailingZeros(((~ctrl & (ctrl >> 7)) | gaps) + 1) + 7) >> 3);
518
0
    }
519
520
0
    void ConvertSpecialToEmptyAndFullToDeleted(ctrl_t* dst) const {
521
0
        constexpr uint64_t msbs = 0x8080808080808080ULL;
522
0
        constexpr uint64_t lsbs = 0x0101010101010101ULL;
523
0
        auto x = ctrl & msbs;
524
0
        auto res = (~x + (x >> 7)) & ~lsbs;
525
0
        little_endian::Store64(dst, res);
526
0
    }
527
528
    uint64_t ctrl;
529
};
530
531
#if PHMAP_HAVE_SSE2  
532
    using Group = GroupSse2Impl;
533
#else
534
    using Group = GroupPortableImpl;
535
#endif
536
537
// The number of cloned control bytes that we copy from the beginning to the
538
// end of the control bytes array.
539
// -------------------------------------------------------------------------
540
0
constexpr size_t NumClonedBytes() { return Group::kWidth - 1; }
541
542
template <class Policy, class Hash, class Eq, class Alloc>
543
class raw_hash_set;
544
545
13.4k
inline bool IsValidCapacity(size_t n) { return ((n + 1) & n) == 0 && n > 0; }
546
547
// --------------------------------------------------------------------------
548
// PRECONDITION:
549
//   IsValidCapacity(capacity)
550
//   ctrl[capacity] == kSentinel
551
//   ctrl[i] != kSentinel for all i < capacity
552
// Applies mapping for every byte in ctrl:
553
//   DELETED -> EMPTY
554
//   EMPTY -> EMPTY
555
//   FULL -> DELETED
556
// --------------------------------------------------------------------------
557
inline void ConvertDeletedToEmptyAndFullToDeleted(
558
    ctrl_t* PHMAP_RESTRICT ctrl, size_t capacity) 
559
0
{
560
0
    assert(ctrl[capacity] == kSentinel);
561
0
    assert(IsValidCapacity(capacity));
562
0
    for (ctrl_t* pos = ctrl; pos != ctrl + capacity + 1; pos += Group::kWidth) {
563
0
        Group{pos}.ConvertSpecialToEmptyAndFullToDeleted(pos);
564
0
    }
565
    // Copy the cloned ctrl bytes.
566
0
    std::memcpy(ctrl + capacity + 1, ctrl, Group::kWidth);
567
0
    ctrl[capacity] = kSentinel;
568
0
}
569
570
// --------------------------------------------------------------------------
571
// Rounds up the capacity to the next power of 2 minus 1, with a minimum of 1.
572
// --------------------------------------------------------------------------
573
inline size_t NormalizeCapacity(size_t n) 
574
0
{
575
0
    return n ? ~size_t{} >> LeadingZeros(n) : 1;
576
0
}
577
578
// --------------------------------------------------------------------------
579
// We use 7/8th as maximum load factor.
580
// For 16-wide groups, that gives an average of two empty slots per group.
581
// --------------------------------------------------------------------------
582
inline size_t CapacityToGrowth(size_t capacity) 
583
5.07k
{
584
5.07k
    assert(IsValidCapacity(capacity));
585
    // `capacity*7/8`
586
5.07k
    PHMAP_IF_CONSTEXPR (Group::kWidth == 8) {
587
5.07k
        if (capacity == 7) {
588
            // x-x/8 does not work when x==7.
589
            return 6;
590
        }
591
    }
592
5.07k
    return capacity - capacity / 8;
593
5.07k
}
594
595
// --------------------------------------------------------------------------
596
// From desired "growth" to a lowerbound of the necessary capacity.
597
// Might not be a valid one and required NormalizeCapacity().
598
// --------------------------------------------------------------------------
599
inline size_t GrowthToLowerboundCapacity(size_t growth) 
600
0
{
601
0
    // `growth*8/7`
602
0
    PHMAP_IF_CONSTEXPR (Group::kWidth == 8) {
603
0
        if (growth == 7) {
604
0
            // x+(x-1)/7 does not work when x==7.
605
0
            return 8;
606
0
        }
607
0
    }
608
0
    return growth + static_cast<size_t>((static_cast<int64_t>(growth) - 1) / 7);
609
0
}
610
611
namespace hashtable_debug_internal {
612
613
// If it is a map, call get<0>().
614
using std::get;
615
template <typename T, typename = typename T::mapped_type>
616
auto GetKey(const typename T::value_type& pair, int) -> decltype(get<0>(pair)) {
617
    return get<0>(pair);
618
}
619
620
// If it is not a map, return the value directly.
621
template <typename T>
622
const typename T::key_type& GetKey(const typename T::key_type& key, char) {
623
    return key;
624
}
625
626
// --------------------------------------------------------------------------
627
// Containers should specialize this to provide debug information for that
628
// container.
629
// --------------------------------------------------------------------------
630
template <class Container, typename Enabler = void>
631
struct HashtableDebugAccess
632
{
633
    // Returns the number of probes required to find `key` in `c`.  The "number of
634
    // probes" is a concept that can vary by container.  Implementations should
635
    // return 0 when `key` was found in the minimum number of operations and
636
    // should increment the result for each non-trivial operation required to find
637
    // `key`.
638
    //
639
    // The default implementation uses the bucket api from the standard and thus
640
    // works for `std::unordered_*` containers.
641
    // --------------------------------------------------------------------------
642
    static size_t GetNumProbes(const Container& c,
643
                               const typename Container::key_type& key) {
644
        if (!c.bucket_count()) return {};
645
        size_t num_probes = 0;
646
        size_t bucket = c.bucket(key);
647
        for (auto it = c.begin(bucket), e = c.end(bucket);; ++it, ++num_probes) {
648
            if (it == e) return num_probes;
649
            if (c.key_eq()(key, GetKey<Container>(*it, 0))) return num_probes;
650
        }
651
    }
652
};
653
654
}  // namespace hashtable_debug_internal
655
656
// ----------------------------------------------------------------------------
657
//                    I N F O Z   S T U B S
658
// ----------------------------------------------------------------------------
659
struct HashtablezInfo 
660
{
661
0
    void PrepareForSampling() {}
662
};
663
664
0
inline void RecordRehashSlow(HashtablezInfo*, size_t ) {}
665
666
0
static inline void RecordInsertSlow(HashtablezInfo* , size_t, size_t ) {}
667
668
0
static inline void RecordEraseSlow(HashtablezInfo*) {}
669
670
0
static inline HashtablezInfo* SampleSlow(int64_t*) { return nullptr; }
671
0
static inline void UnsampleSlow(HashtablezInfo* ) {}
672
673
class HashtablezInfoHandle 
674
{
675
public:
676
2.82k
    inline void RecordStorageChanged(size_t , size_t ) {}
677
0
    inline void RecordRehash(size_t ) {}
678
614k
    inline void RecordInsert(size_t , size_t ) {}
679
0
    inline void RecordErase() {}
680
    friend inline void swap(HashtablezInfoHandle& ,
681
483
                            HashtablezInfoHandle& ) noexcept {}
682
};
683
684
574
static inline HashtablezInfoHandle Sample() { return HashtablezInfoHandle(); }
685
686
class HashtablezSampler 
687
{
688
public:
689
    // Returns a global Sampler.
690
0
    static HashtablezSampler& Global() {  static HashtablezSampler hzs; return hzs; }
691
0
    HashtablezInfo* Register() {  static HashtablezInfo info; return &info; }
692
0
    void Unregister(HashtablezInfo* ) {}
693
694
    using DisposeCallback = void (*)(const HashtablezInfo&);
695
0
    DisposeCallback SetDisposeCallback(DisposeCallback ) { return nullptr; }
696
0
    int64_t Iterate(const std::function<void(const HashtablezInfo& stack)>& ) { return 0; }
697
};
698
699
0
static inline void SetHashtablezEnabled(bool ) {}
700
0
static inline void SetHashtablezSampleParameter(int32_t ) {}
701
0
static inline void SetHashtablezMaxSamples(int32_t ) {}
702
703
704
namespace memory_internal {
705
706
// Constructs T into uninitialized storage pointed by `ptr` using the args
707
// specified in the tuple.
708
// ----------------------------------------------------------------------------
709
template <class Alloc, class T, class Tuple, size_t... I>
710
void ConstructFromTupleImpl(Alloc* alloc, T* ptr, Tuple&& t,
711
                            phmap::index_sequence<I...>) {
712
    phmap::allocator_traits<Alloc>::construct(
713
        *alloc, ptr, std::get<I>(std::forward<Tuple>(t))...);
714
}
715
716
template <class T, class F>
717
struct WithConstructedImplF {
718
    template <class... Args>
719
    decltype(std::declval<F>()(std::declval<T>())) operator()(
720
        Args&&... args) const {
721
        return std::forward<F>(f)(T(std::forward<Args>(args)...));
722
    }
723
    F&& f;
724
};
725
726
template <class T, class Tuple, size_t... Is, class F>
727
decltype(std::declval<F>()(std::declval<T>())) WithConstructedImpl(
728
    Tuple&& t, phmap::index_sequence<Is...>, F&& f) {
729
    return WithConstructedImplF<T, F>{std::forward<F>(f)}(
730
        std::get<Is>(std::forward<Tuple>(t))...);
731
}
732
733
template <class T, size_t... Is>
734
auto TupleRefImpl(T&& t, phmap::index_sequence<Is...>)
735
    -> decltype(std::forward_as_tuple(std::get<Is>(std::forward<T>(t))...)) {
736
  return std::forward_as_tuple(std::get<Is>(std::forward<T>(t))...);
737
}
738
739
// Returns a tuple of references to the elements of the input tuple. T must be a
740
// tuple.
741
// ----------------------------------------------------------------------------
742
template <class T>
743
auto TupleRef(T&& t) -> decltype(
744
    TupleRefImpl(std::forward<T>(t),
745
                 phmap::make_index_sequence<
746
                     std::tuple_size<typename std::decay<T>::type>::value>())) {
747
  return TupleRefImpl(
748
      std::forward<T>(t),
749
      phmap::make_index_sequence<
750
          std::tuple_size<typename std::decay<T>::type>::value>());
751
}
752
753
template <class F, class K, class V>
754
decltype(std::declval<F>()(std::declval<const K&>(), std::piecewise_construct,
755
                           std::declval<std::tuple<K>>(), std::declval<V>()))
756
20.3M
DecomposePairImpl(F&& f, std::pair<std::tuple<K>, V> p) {
757
20.3M
    const auto& key = std::get<0>(p.first);
758
20.3M
    return std::forward<F>(f)(key, std::piecewise_construct, std::move(p.first),
759
20.3M
                              std::move(p.second));
760
20.3M
}
_ZN5phmap4priv15memory_internal17DecomposePairImplINS0_12raw_hash_setINS0_17FlatHashMapPolicyIjiEENS_4HashIjEENS_7EqualToIjEENSt3__19allocatorINSA_4pairIKjiEEEEE19EmplaceDecomposableEOSD_NSA_5tupleIJOiEEEEEDTclclsr3stdE7declvalIT_EEclsr3stdE7declvalIRKT0_EEL_ZNSA_19piecewise_constructEEclsr3stdE7declvalINSJ_IJSN_EEEEEclsr3stdE7declvalIT1_EEEEOSM_NSC_ISQ_SR_EE
Line
Count
Source
756
8.52M
DecomposePairImpl(F&& f, std::pair<std::tuple<K>, V> p) {
757
8.52M
    const auto& key = std::get<0>(p.first);
758
8.52M
    return std::forward<F>(f)(key, std::piecewise_construct, std::move(p.first),
759
8.52M
                              std::move(p.second));
760
8.52M
}
_ZN5phmap4priv15memory_internal17DecomposePairImplINS0_12raw_hash_setINS0_17FlatHashMapPolicyIjiEENS_4HashIjEENS_7EqualToIjEENSt3__19allocatorINSA_4pairIKjiEEEEE12EqualElementIjEERSD_NSA_5tupleIJRKiEEEEEDTclclsr3stdE7declvalIT_EEclsr3stdE7declvalIRKT0_EEL_ZNSA_19piecewise_constructEEclsr3stdE7declvalINSK_IJSP_EEEEEclsr3stdE7declvalIT1_EEEEOSO_NSC_ISS_ST_EE
Line
Count
Source
756
10.1M
DecomposePairImpl(F&& f, std::pair<std::tuple<K>, V> p) {
757
10.1M
    const auto& key = std::get<0>(p.first);
758
10.1M
    return std::forward<F>(f)(key, std::piecewise_construct, std::move(p.first),
759
10.1M
                              std::move(p.second));
760
10.1M
}
_ZN5phmap4priv15memory_internal17DecomposePairImplINS0_12raw_hash_setINS0_17FlatHashMapPolicyIjiEENS_4HashIjEENS_7EqualToIjEENSt3__19allocatorINSA_4pairIKjiEEEEE11HashElementERSD_NSA_5tupleIJRKiEEEEEDTclclsr3stdE7declvalIT_EEclsr3stdE7declvalIRKT0_EEL_ZNSA_19piecewise_constructEEclsr3stdE7declvalINSJ_IJSO_EEEEEclsr3stdE7declvalIT1_EEEEOSN_NSC_ISR_SS_EE
Line
Count
Source
756
1.67M
DecomposePairImpl(F&& f, std::pair<std::tuple<K>, V> p) {
757
1.67M
    const auto& key = std::get<0>(p.first);
758
1.67M
    return std::forward<F>(f)(key, std::piecewise_construct, std::move(p.first),
759
1.67M
                              std::move(p.second));
760
1.67M
}
761
762
}  // namespace memory_internal
763
764
765
// ----------------------------------------------------------------------------
766
//                     R A W _ H A S H _ S E T
767
// ----------------------------------------------------------------------------
768
// An open-addressing
769
// hashtable with quadratic probing.
770
//
771
// This is a low level hashtable on top of which different interfaces can be
772
// implemented, like flat_hash_set, node_hash_set, string_hash_set, etc.
773
//
774
// The table interface is similar to that of std::unordered_set. Notable
775
// differences are that most member functions support heterogeneous keys when
776
// BOTH the hash and eq functions are marked as transparent. They do so by
777
// providing a typedef called `is_transparent`.
778
//
779
// When heterogeneous lookup is enabled, functions that take key_type act as if
780
// they have an overload set like:
781
//
782
//   iterator find(const key_type& key);
783
//   template <class K>
784
//   iterator find(const K& key);
785
//
786
//   size_type erase(const key_type& key);
787
//   template <class K>
788
//   size_type erase(const K& key);
789
//
790
//   std::pair<iterator, iterator> equal_range(const key_type& key);
791
//   template <class K>
792
//   std::pair<iterator, iterator> equal_range(const K& key);
793
//
794
// When heterogeneous lookup is disabled, only the explicit `key_type` overloads
795
// exist.
796
//
797
// find() also supports passing the hash explicitly:
798
//
799
//   iterator find(const key_type& key, size_t hash);
800
//   template <class U>
801
//   iterator find(const U& key, size_t hash);
802
//
803
// In addition the pointer to element and iterator stability guarantees are
804
// weaker: all iterators and pointers are invalidated after a new element is
805
// inserted.
806
//
807
// IMPLEMENTATION DETAILS
808
//
809
// The table stores elements inline in a slot array. In addition to the slot
810
// array the table maintains some control state per slot. The extra state is one
811
// byte per slot and stores empty or deleted marks, or alternatively 7 bits from
812
// the hash of an occupied slot. The table is split into logical groups of
813
// slots, like so:
814
//
815
//      Group 1         Group 2        Group 3
816
// +---------------+---------------+---------------+
817
// | | | | | | | | | | | | | | | | | | | | | | | | |
818
// +---------------+---------------+---------------+
819
//
820
// On lookup the hash is split into two parts:
821
// - H2: 7 bits (those stored in the control bytes)
822
// - H1: the rest of the bits
823
// The groups are probed using H1. For each group the slots are matched to H2 in
824
// parallel. Because H2 is 7 bits (128 states) and the number of slots per group
825
// is low (8 or 16) in almost all cases a match in H2 is also a lookup hit.
826
//
827
// On insert, once the right group is found (as in lookup), its slots are
828
// filled in order.
829
//
830
// On erase a slot is cleared. In case the group did not have any empty slots
831
// before the erase, the erased slot is marked as deleted.
832
//
833
// Groups without empty slots (but maybe with deleted slots) extend the probe
834
// sequence. The probing algorithm is quadratic. Given N the number of groups,
835
// the probing function for the i'th probe is:
836
//
837
//   P(0) = H1 % N
838
//
839
//   P(i) = (P(i - 1) + i) % N
840
//
841
// This probing function guarantees that after N probes, all the groups of the
842
// table will be probed exactly once.
843
// ----------------------------------------------------------------------------
844
template <class Policy, class Hash, class Eq, class Alloc>
845
class raw_hash_set 
846
{
847
    using PolicyTraits = hash_policy_traits<Policy>;
848
    using KeyArgImpl =
849
        KeyArg<IsTransparent<Eq>::value && IsTransparent<Hash>::value>;
850
851
public:
852
    using init_type = typename PolicyTraits::init_type;
853
    using key_type = typename PolicyTraits::key_type;
854
    // TODO(sbenza): Hide slot_type as it is an implementation detail. Needs user
855
    // code fixes!
856
    using slot_type = typename PolicyTraits::slot_type;
857
    using allocator_type = Alloc;
858
    using size_type = size_t;
859
    using difference_type = ptrdiff_t;
860
    using hasher = Hash;
861
    using key_equal = Eq;
862
    using policy_type = Policy;
863
    using value_type = typename PolicyTraits::value_type;
864
    using reference = value_type&;
865
    using const_reference = const value_type&;
866
    using pointer = typename phmap::allocator_traits<
867
        allocator_type>::template rebind_traits<value_type>::pointer;
868
    using const_pointer = typename phmap::allocator_traits<
869
        allocator_type>::template rebind_traits<value_type>::const_pointer;
870
871
    // Alias used for heterogeneous lookup functions.
872
    // `key_arg<K>` evaluates to `K` when the functors are transparent and to
873
    // `key_type` otherwise. It permits template argument deduction on `K` for the
874
    // transparent case.
875
    template <class K>
876
    using key_arg = typename KeyArgImpl::template type<K, key_type>;
877
878
    using std_alloc_t = std::is_same<typename std::decay<Alloc>::type, phmap::priv::Allocator<value_type>>;
879
880
private:
881
    // Give an early error when key_type is not hashable/eq.
882
    auto KeyTypeCanBeHashed(const Hash& h, const key_type& k) -> decltype(h(k));
883
    auto KeyTypeCanBeEq(const Eq& eq, const key_type& k) -> decltype(eq(k, k));
884
885
    using Layout = phmap::priv::Layout<ctrl_t, slot_type>;
886
887
5.64k
    static Layout MakeLayout(size_t capacity) {
888
5.64k
        assert(IsValidCapacity(capacity));
889
5.64k
        return Layout(capacity + Group::kWidth + 1, capacity);
890
5.64k
    }
phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<unsigned int, int>, phmap::Hash<unsigned int>, phmap::EqualTo<unsigned int>, std::__1::allocator<std::__1::pair<unsigned int const, int> > >::MakeLayout(unsigned long)
Line
Count
Source
887
5.64k
    static Layout MakeLayout(size_t capacity) {
888
5.64k
        assert(IsValidCapacity(capacity));
889
5.64k
        return Layout(capacity + Group::kWidth + 1, capacity);
890
5.64k
    }
Unexecuted instantiation: phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, phmap::priv::StringHashEqT<char>::Hash, phmap::priv::StringHashEqT<char>::Eq, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >::MakeLayout(unsigned long)
891
892
    using AllocTraits = phmap::allocator_traits<allocator_type>;
893
    using SlotAlloc = typename phmap::allocator_traits<
894
        allocator_type>::template rebind_alloc<slot_type>;
895
    using SlotAllocTraits = typename phmap::allocator_traits<
896
        allocator_type>::template rebind_traits<slot_type>;
897
898
    static_assert(std::is_lvalue_reference<reference>::value,
899
                  "Policy::element() must return a reference");
900
901
    template <typename T>
902
    struct SameAsElementReference
903
        : std::is_same<typename std::remove_cv<
904
                           typename std::remove_reference<reference>::type>::type,
905
                       typename std::remove_cv<
906
                           typename std::remove_reference<T>::type>::type> {};
907
908
    // An enabler for insert(T&&): T must be convertible to init_type or be the
909
    // same as [cv] value_type [ref].
910
    // Note: we separate SameAsElementReference into its own type to avoid using
911
    // reference unless we need to. MSVC doesn't seem to like it in some
912
    // cases.
913
    template <class T>
914
    using RequiresInsertable = typename std::enable_if<
915
        phmap::disjunction<std::is_convertible<T, init_type>,
916
                           SameAsElementReference<T>>::value,
917
        int>::type;
918
919
    // RequiresNotInit is a workaround for gcc prior to 7.1.
920
    // See https://godbolt.org/g/Y4xsUh.
921
    template <class T>
922
    using RequiresNotInit =
923
        typename std::enable_if<!std::is_same<T, init_type>::value, int>::type;
924
925
    template <class... Ts>
926
    using IsDecomposable = IsDecomposable<void, PolicyTraits, Hash, Eq, Ts...>;
927
928
public:
929
    class iterator
930
    {
931
        friend class raw_hash_set;
932
933
    public:
934
        using iterator_category = std::forward_iterator_tag;
935
        using value_type = typename raw_hash_set::value_type;
936
        using reference =
937
            phmap::conditional_t<PolicyTraits::constant_iterators::value,
938
                                 const value_type&, value_type&>;
939
        using pointer = phmap::remove_reference_t<reference>*;
940
        using difference_type = typename raw_hash_set::difference_type;
941
942
        iterator() {}
943
944
        // PRECONDITION: not an end() iterator.
945
601k
        reference operator*() const { return PolicyTraits::element(slot_); }
phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<unsigned int, int>, phmap::Hash<unsigned int>, phmap::EqualTo<unsigned int>, std::__1::allocator<std::__1::pair<unsigned int const, int> > >::iterator::operator*() const
Line
Count
Source
945
601k
        reference operator*() const { return PolicyTraits::element(slot_); }
Unexecuted instantiation: phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, phmap::priv::StringHashEqT<char>::Hash, phmap::priv::StringHashEqT<char>::Eq, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >::iterator::operator*() const
946
947
        // PRECONDITION: not an end() iterator.
948
        pointer operator->() const { return &operator*(); }
949
950
        // PRECONDITION: not an end() iterator.
951
601k
        iterator& operator++() {
952
601k
            ++ctrl_;
953
601k
            ++slot_;
954
601k
            skip_empty_or_deleted();
955
601k
            return *this;
956
601k
        }
phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<unsigned int, int>, phmap::Hash<unsigned int>, phmap::EqualTo<unsigned int>, std::__1::allocator<std::__1::pair<unsigned int const, int> > >::iterator::operator++()
Line
Count
Source
951
601k
        iterator& operator++() {
952
601k
            ++ctrl_;
953
601k
            ++slot_;
954
601k
            skip_empty_or_deleted();
955
601k
            return *this;
956
601k
        }
Unexecuted instantiation: phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, phmap::priv::StringHashEqT<char>::Hash, phmap::priv::StringHashEqT<char>::Eq, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >::iterator::operator++()
957
        // PRECONDITION: not an end() iterator.
958
        iterator operator++(int) {
959
            auto tmp = *this;
960
            ++*this;
961
            return tmp;
962
        }
963
964
#if 0 // PHMAP_BIDIRECTIONAL
965
        // PRECONDITION: not a begin() iterator.
966
        iterator& operator--() {
967
            assert(ctrl_);
968
            do {
969
                --ctrl_;
970
                --slot_;
971
            } while (IsEmptyOrDeleted(*ctrl_));
972
            return *this;
973
        }
974
975
        // PRECONDITION: not a begin() iterator.
976
        iterator operator--(int) {
977
            auto tmp = *this;
978
            --*this;
979
            return tmp;
980
        }
981
#endif
982
983
602k
        friend bool operator==(const iterator& a, const iterator& b) {
984
602k
            return a.ctrl_ == b.ctrl_;
985
602k
        }
phmap::priv::operator==(phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<unsigned int, int>, phmap::Hash<unsigned int>, phmap::EqualTo<unsigned int>, std::__1::allocator<std::__1::pair<unsigned int const, int> > >::iterator const&, phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<unsigned int, int>, phmap::Hash<unsigned int>, phmap::EqualTo<unsigned int>, std::__1::allocator<std::__1::pair<unsigned int const, int> > >::iterator const&)
Line
Count
Source
983
601k
        friend bool operator==(const iterator& a, const iterator& b) {
984
601k
            return a.ctrl_ == b.ctrl_;
985
601k
        }
phmap::priv::operator==(phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, phmap::priv::StringHashEqT<char>::Hash, phmap::priv::StringHashEqT<char>::Eq, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >::iterator const&, phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, phmap::priv::StringHashEqT<char>::Hash, phmap::priv::StringHashEqT<char>::Eq, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >::iterator const&)
Line
Count
Source
983
483
        friend bool operator==(const iterator& a, const iterator& b) {
984
483
            return a.ctrl_ == b.ctrl_;
985
483
        }
986
483
        friend bool operator!=(const iterator& a, const iterator& b) {
987
483
            return !(a == b);
988
483
        }
989
990
    private:
991
638
        iterator(ctrl_t* ctrl) : ctrl_(ctrl) {}  // for end()
phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<unsigned int, int>, phmap::Hash<unsigned int>, phmap::EqualTo<unsigned int>, std::__1::allocator<std::__1::pair<unsigned int const, int> > >::iterator::iterator(signed char*)
Line
Count
Source
991
155
        iterator(ctrl_t* ctrl) : ctrl_(ctrl) {}  // for end()
phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, phmap::priv::StringHashEqT<char>::Hash, phmap::priv::StringHashEqT<char>::Eq, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >::iterator::iterator(signed char*)
Line
Count
Source
991
483
        iterator(ctrl_t* ctrl) : ctrl_(ctrl) {}  // for end()
992
8.52M
        iterator(ctrl_t* ctrl, slot_type* slot) : ctrl_(ctrl), slot_(slot) {}
phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<unsigned int, int>, phmap::Hash<unsigned int>, phmap::EqualTo<unsigned int>, std::__1::allocator<std::__1::pair<unsigned int const, int> > >::iterator::iterator(signed char*, phmap::priv::map_slot_type<unsigned int, int>*)
Line
Count
Source
992
8.52M
        iterator(ctrl_t* ctrl, slot_type* slot) : ctrl_(ctrl), slot_(slot) {}
phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, phmap::priv::StringHashEqT<char>::Hash, phmap::priv::StringHashEqT<char>::Eq, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >::iterator::iterator(signed char*, phmap::priv::map_slot_type<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >*)
Line
Count
Source
992
483
        iterator(ctrl_t* ctrl, slot_type* slot) : ctrl_(ctrl), slot_(slot) {}
993
994
602k
        void skip_empty_or_deleted() {
995
602k
            PHMAP_IF_CONSTEXPR (!std_alloc_t::value) {
996
                // ctrl_ could be nullptr
997
                if (!ctrl_)
998
                    return;
999
            }
1000
832k
            while (IsEmptyOrDeleted(*ctrl_)) {
1001
                // ctrl is not necessarily aligned to Group::kWidth. It is also likely
1002
                // to read past the space for ctrl bytes and into slots. This is ok
1003
                // because ctrl has sizeof() == 1 and slot has sizeof() >= 1 so there
1004
                // is no way to read outside the combined slot array.
1005
230k
                uint32_t shift = Group{ctrl_}.CountLeadingEmptyOrDeleted();
1006
230k
                ctrl_ += shift;
1007
230k
                slot_ += shift;
1008
230k
            }
1009
602k
        }
phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<unsigned int, int>, phmap::Hash<unsigned int>, phmap::EqualTo<unsigned int>, std::__1::allocator<std::__1::pair<unsigned int const, int> > >::iterator::skip_empty_or_deleted()
Line
Count
Source
994
601k
        void skip_empty_or_deleted() {
995
601k
            PHMAP_IF_CONSTEXPR (!std_alloc_t::value) {
996
                // ctrl_ could be nullptr
997
                if (!ctrl_)
998
                    return;
999
            }
1000
832k
            while (IsEmptyOrDeleted(*ctrl_)) {
1001
                // ctrl is not necessarily aligned to Group::kWidth. It is also likely
1002
                // to read past the space for ctrl bytes and into slots. This is ok
1003
                // because ctrl has sizeof() == 1 and slot has sizeof() >= 1 so there
1004
                // is no way to read outside the combined slot array.
1005
230k
                uint32_t shift = Group{ctrl_}.CountLeadingEmptyOrDeleted();
1006
230k
                ctrl_ += shift;
1007
230k
                slot_ += shift;
1008
230k
            }
1009
601k
        }
phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, phmap::priv::StringHashEqT<char>::Hash, phmap::priv::StringHashEqT<char>::Eq, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >::iterator::skip_empty_or_deleted()
Line
Count
Source
994
483
        void skip_empty_or_deleted() {
995
483
            PHMAP_IF_CONSTEXPR (!std_alloc_t::value) {
996
                // ctrl_ could be nullptr
997
                if (!ctrl_)
998
                    return;
999
            }
1000
483
            while (IsEmptyOrDeleted(*ctrl_)) {
1001
                // ctrl is not necessarily aligned to Group::kWidth. It is also likely
1002
                // to read past the space for ctrl bytes and into slots. This is ok
1003
                // because ctrl has sizeof() == 1 and slot has sizeof() >= 1 so there
1004
                // is no way to read outside the combined slot array.
1005
0
                uint32_t shift = Group{ctrl_}.CountLeadingEmptyOrDeleted();
1006
0
                ctrl_ += shift;
1007
0
                slot_ += shift;
1008
0
            }
1009
483
        }
1010
1011
        ctrl_t* ctrl_ = nullptr;
1012
        // To avoid uninitialized member warnings, put slot_ in an anonymous union.
1013
        // The member is not initialized on singleton and end iterators.
1014
        union {
1015
            slot_type* slot_;
1016
        };
1017
    };
1018
1019
    class const_iterator 
1020
    {
1021
        friend class raw_hash_set;
1022
1023
    public:
1024
        using iterator_category = typename iterator::iterator_category;
1025
        using value_type = typename raw_hash_set::value_type;
1026
        using reference = typename raw_hash_set::const_reference;
1027
        using pointer = typename raw_hash_set::const_pointer;
1028
        using difference_type = typename raw_hash_set::difference_type;
1029
1030
        const_iterator() {}
1031
        // Implicit construction from iterator.
1032
310
        const_iterator(iterator i) : inner_(std::move(i)) {}
1033
1034
601k
        reference operator*() const { return *inner_; }
1035
        pointer operator->() const { return inner_.operator->(); }
1036
1037
601k
        const_iterator& operator++() {
1038
601k
            ++inner_;
1039
601k
            return *this;
1040
601k
        }
1041
        const_iterator operator++(int) { return inner_++; }
1042
1043
601k
        friend bool operator==(const const_iterator& a, const const_iterator& b) {
1044
601k
            return a.inner_ == b.inner_;
1045
601k
        }
1046
601k
        friend bool operator!=(const const_iterator& a, const const_iterator& b) {
1047
601k
            return !(a == b);
1048
601k
        }
1049
1050
    private:
1051
        const_iterator(const ctrl_t* ctrl, const slot_type* slot)
1052
            : inner_(const_cast<ctrl_t*>(ctrl), const_cast<slot_type*>(slot)) {}
1053
1054
        iterator inner_;
1055
    };
1056
1057
    using node_type = node_handle<Policy, hash_policy_traits<Policy>, Alloc>;
1058
    using insert_return_type = InsertReturnType<iterator, node_type>;
1059
1060
    raw_hash_set() noexcept(
1061
        std::is_nothrow_default_constructible<hasher>::value&&
1062
        std::is_nothrow_default_constructible<key_equal>::value&&
1063
1.93k
        std::is_nothrow_default_constructible<allocator_type>::value) {}
phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<unsigned int, int>, phmap::Hash<unsigned int>, phmap::EqualTo<unsigned int>, std::__1::allocator<std::__1::pair<unsigned int const, int> > >::raw_hash_set()
Line
Count
Source
1063
1.44k
        std::is_nothrow_default_constructible<allocator_type>::value) {}
phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, phmap::priv::StringHashEqT<char>::Hash, phmap::priv::StringHashEqT<char>::Eq, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >::raw_hash_set()
Line
Count
Source
1063
483
        std::is_nothrow_default_constructible<allocator_type>::value) {}
1064
1065
    explicit raw_hash_set(size_t bucket_cnt, const hasher& hashfn = hasher(),
1066
                          const key_equal& eq = key_equal(),
1067
                          const allocator_type& alloc = allocator_type())
1068
        : ctrl_(EmptyGroup<std_alloc_t>()), settings_(0, hashfn, eq, alloc) {
1069
        if (bucket_cnt) {
1070
            size_t new_capacity = NormalizeCapacity(bucket_cnt);
1071
            reset_growth_left(new_capacity);
1072
            initialize_slots(new_capacity);
1073
            capacity_ = new_capacity;
1074
        }
1075
    }
1076
1077
    raw_hash_set(size_t bucket_cnt, const hasher& hashfn,
1078
                 const allocator_type& alloc)
1079
        : raw_hash_set(bucket_cnt, hashfn, key_equal(), alloc) {}
1080
1081
    raw_hash_set(size_t bucket_cnt, const allocator_type& alloc)
1082
        : raw_hash_set(bucket_cnt, hasher(), key_equal(), alloc) {}
1083
1084
    explicit raw_hash_set(const allocator_type& alloc)
1085
        : raw_hash_set(0, hasher(), key_equal(), alloc) {}
1086
1087
    template <class InputIter>
1088
    raw_hash_set(InputIter first, InputIter last, size_t bucket_cnt = 0,
1089
                 const hasher& hashfn = hasher(), const key_equal& eq = key_equal(),
1090
                 const allocator_type& alloc = allocator_type())
1091
        : raw_hash_set(bucket_cnt, hashfn, eq, alloc) {
1092
        insert(first, last);
1093
    }
1094
1095
    template <class InputIter>
1096
    raw_hash_set(InputIter first, InputIter last, size_t bucket_cnt,
1097
                 const hasher& hashfn, const allocator_type& alloc)
1098
        : raw_hash_set(first, last, bucket_cnt, hashfn, key_equal(), alloc) {}
1099
1100
    template <class InputIter>
1101
    raw_hash_set(InputIter first, InputIter last, size_t bucket_cnt,
1102
                 const allocator_type& alloc)
1103
        : raw_hash_set(first, last, bucket_cnt, hasher(), key_equal(), alloc) {}
1104
1105
    template <class InputIter>
1106
    raw_hash_set(InputIter first, InputIter last, const allocator_type& alloc)
1107
        : raw_hash_set(first, last, 0, hasher(), key_equal(), alloc) {}
1108
1109
    // Instead of accepting std::initializer_list<value_type> as the first
1110
    // argument like std::unordered_set<value_type> does, we have two overloads
1111
    // that accept std::initializer_list<T> and std::initializer_list<init_type>.
1112
    // This is advantageous for performance.
1113
    //
1114
    //   // Turns {"abc", "def"} into std::initializer_list<std::string>, then
1115
    //   // copies the strings into the set.
1116
    //   std::unordered_set<std::string> s = {"abc", "def"};
1117
    //
1118
    //   // Turns {"abc", "def"} into std::initializer_list<const char*>, then
1119
    //   // copies the strings into the set.
1120
    //   phmap::flat_hash_set<std::string> s = {"abc", "def"};
1121
    //
1122
    // The same trick is used in insert().
1123
    //
1124
    // The enabler is necessary to prevent this constructor from triggering where
1125
    // the copy constructor is meant to be called.
1126
    //
1127
    //   phmap::flat_hash_set<int> a, b{a};
1128
    //
1129
    // RequiresNotInit<T> is a workaround for gcc prior to 7.1.
1130
    template <class T, RequiresNotInit<T> = 0, RequiresInsertable<T> = 0>
1131
    raw_hash_set(std::initializer_list<T> init, size_t bucket_cnt = 0,
1132
                 const hasher& hashfn = hasher(), const key_equal& eq = key_equal(),
1133
                 const allocator_type& alloc = allocator_type())
1134
        : raw_hash_set(init.begin(), init.end(), bucket_cnt, hashfn, eq, alloc) {}
1135
1136
    raw_hash_set(std::initializer_list<init_type> init, size_t bucket_cnt = 0,
1137
                 const hasher& hashfn = hasher(), const key_equal& eq = key_equal(),
1138
                 const allocator_type& alloc = allocator_type())
1139
        : raw_hash_set(init.begin(), init.end(), bucket_cnt, hashfn, eq, alloc) {}
1140
1141
    template <class T, RequiresNotInit<T> = 0, RequiresInsertable<T> = 0>
1142
    raw_hash_set(std::initializer_list<T> init, size_t bucket_cnt,
1143
                 const hasher& hashfn, const allocator_type& alloc)
1144
        : raw_hash_set(init, bucket_cnt, hashfn, key_equal(), alloc) {}
1145
1146
    raw_hash_set(std::initializer_list<init_type> init, size_t bucket_cnt,
1147
                 const hasher& hashfn, const allocator_type& alloc)
1148
        : raw_hash_set(init, bucket_cnt, hashfn, key_equal(), alloc) {}
1149
1150
    template <class T, RequiresNotInit<T> = 0, RequiresInsertable<T> = 0>
1151
    raw_hash_set(std::initializer_list<T> init, size_t bucket_cnt,
1152
                 const allocator_type& alloc)
1153
        : raw_hash_set(init, bucket_cnt, hasher(), key_equal(), alloc) {}
1154
1155
    raw_hash_set(std::initializer_list<init_type> init, size_t bucket_cnt,
1156
                 const allocator_type& alloc)
1157
        : raw_hash_set(init, bucket_cnt, hasher(), key_equal(), alloc) {}
1158
1159
    template <class T, RequiresNotInit<T> = 0, RequiresInsertable<T> = 0>
1160
    raw_hash_set(std::initializer_list<T> init, const allocator_type& alloc)
1161
        : raw_hash_set(init, 0, hasher(), key_equal(), alloc) {}
1162
1163
    raw_hash_set(std::initializer_list<init_type> init,
1164
                 const allocator_type& alloc)
1165
        : raw_hash_set(init, 0, hasher(), key_equal(), alloc) {}
1166
1167
    raw_hash_set(const raw_hash_set& that)
1168
        : raw_hash_set(that, AllocTraits::select_on_container_copy_construction(
1169
                           that.alloc_ref())) {}
1170
1171
    raw_hash_set(const raw_hash_set& that, const allocator_type& a)
1172
        : raw_hash_set(0, that.hash_ref(), that.eq_ref(), a) {
1173
        rehash(that.capacity());   // operator=() should preserve load_factor
1174
        // Because the table is guaranteed to be empty, we can do something faster
1175
        // than a full `insert`.
1176
        for (const auto& v : that) {
1177
            const size_t hashval = PolicyTraits::apply(HashElement{hash_ref()}, v);
1178
            auto target = find_first_non_full(hashval);
1179
            set_ctrl(target.offset, H2(hashval));
1180
            emplace_at(target.offset, v);
1181
            infoz_.RecordInsert(hashval, target.probe_length);
1182
        }
1183
        size_ = that.size();
1184
        growth_left() -= that.size();
1185
    }
1186
1187
    raw_hash_set(raw_hash_set&& that) noexcept(
1188
        std::is_nothrow_copy_constructible<hasher>::value&&
1189
        std::is_nothrow_copy_constructible<key_equal>::value&&
1190
        std::is_nothrow_copy_constructible<allocator_type>::value)
1191
        : ctrl_(phmap::exchange(that.ctrl_, EmptyGroup<std_alloc_t>())),
1192
        slots_(phmap::exchange(that.slots_, nullptr)),
1193
        size_(phmap::exchange(that.size_, 0)),
1194
        capacity_(phmap::exchange(that.capacity_, 0)),
1195
        infoz_(phmap::exchange(that.infoz_, HashtablezInfoHandle())),
1196
        // Hash, equality and allocator are copied instead of moved because
1197
        // `that` must be left valid. If Hash is std::function<Key>, moving it
1198
        // would create a nullptr functor that cannot be called.
1199
        settings_(std::move(that.settings_)) {
1200
        // growth_left was copied above, reset the one from `that`.
1201
        that.growth_left() = 0;
1202
    }
1203
1204
    raw_hash_set(raw_hash_set&& that, const allocator_type& a)
1205
        : ctrl_(EmptyGroup<std_alloc_t>()),
1206
          slots_(nullptr),
1207
          size_(0),
1208
          capacity_(0),
1209
          settings_(0, that.hash_ref(), that.eq_ref(), a) {
1210
        if (a == that.alloc_ref()) {
1211
            std::swap(ctrl_, that.ctrl_);
1212
            std::swap(slots_, that.slots_);
1213
            std::swap(size_, that.size_);
1214
            std::swap(capacity_, that.capacity_);
1215
            std::swap(growth_left(), that.growth_left());
1216
            std::swap(infoz_, that.infoz_);
1217
        } else {
1218
            reserve(that.size());
1219
            // Note: this will copy elements of dense_set and unordered_set instead of
1220
            // moving them. This can be fixed if it ever becomes an issue.
1221
            for (auto& elem : that) insert(std::move(elem));
1222
        }
1223
    }
1224
1225
    raw_hash_set& operator=(const raw_hash_set& that) {
1226
        raw_hash_set tmp(that,
1227
                         AllocTraits::propagate_on_container_copy_assignment::value
1228
                         ? that.alloc_ref()
1229
                         : alloc_ref());
1230
        swap(tmp);
1231
        return *this;
1232
    }
1233
1234
    raw_hash_set& operator=(raw_hash_set&& that) noexcept(
1235
        phmap::allocator_traits<allocator_type>::is_always_equal::value&&
1236
        std::is_nothrow_move_assignable<hasher>::value&&
1237
        std::is_nothrow_move_assignable<key_equal>::value) {
1238
        // TODO(sbenza): We should only use the operations from the noexcept clause
1239
        // to make sure we actually adhere to that contract.
1240
        return move_assign(
1241
            std::move(that),
1242
            typename AllocTraits::propagate_on_container_move_assignment());
1243
    }
1244
1245
1.93k
    ~raw_hash_set() { destroy_slots(); }
phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<unsigned int, int>, phmap::Hash<unsigned int>, phmap::EqualTo<unsigned int>, std::__1::allocator<std::__1::pair<unsigned int const, int> > >::~raw_hash_set()
Line
Count
Source
1245
1.44k
    ~raw_hash_set() { destroy_slots(); }
phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, phmap::priv::StringHashEqT<char>::Hash, phmap::priv::StringHashEqT<char>::Eq, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >::~raw_hash_set()
Line
Count
Source
1245
483
    ~raw_hash_set() { destroy_slots(); }
1246
1247
638
    iterator begin() {
1248
638
        auto it = iterator_at(0);
1249
638
        it.skip_empty_or_deleted();
1250
638
        return it;
1251
638
    }
phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<unsigned int, int>, phmap::Hash<unsigned int>, phmap::EqualTo<unsigned int>, std::__1::allocator<std::__1::pair<unsigned int const, int> > >::begin()
Line
Count
Source
1247
155
    iterator begin() {
1248
155
        auto it = iterator_at(0);
1249
155
        it.skip_empty_or_deleted();
1250
155
        return it;
1251
155
    }
phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, phmap::priv::StringHashEqT<char>::Hash, phmap::priv::StringHashEqT<char>::Eq, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >::begin()
Line
Count
Source
1247
483
    iterator begin() {
1248
483
        auto it = iterator_at(0);
1249
483
        it.skip_empty_or_deleted();
1250
483
        return it;
1251
483
    }
1252
    iterator end() 
1253
638
    {
1254
#if 0 // PHMAP_BIDIRECTIONAL
1255
        return iterator_at(capacity_); 
1256
#else
1257
638
        return {ctrl_ + capacity_};
1258
638
#endif
1259
638
    }
phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<unsigned int, int>, phmap::Hash<unsigned int>, phmap::EqualTo<unsigned int>, std::__1::allocator<std::__1::pair<unsigned int const, int> > >::end()
Line
Count
Source
1253
155
    {
1254
#if 0 // PHMAP_BIDIRECTIONAL
1255
        return iterator_at(capacity_); 
1256
#else
1257
155
        return {ctrl_ + capacity_};
1258
155
#endif
1259
155
    }
phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, phmap::priv::StringHashEqT<char>::Hash, phmap::priv::StringHashEqT<char>::Eq, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >::end()
Line
Count
Source
1253
483
    {
1254
#if 0 // PHMAP_BIDIRECTIONAL
1255
        return iterator_at(capacity_); 
1256
#else
1257
483
        return {ctrl_ + capacity_};
1258
483
#endif
1259
483
    }
1260
1261
155
    const_iterator begin() const {
1262
155
        return const_cast<raw_hash_set*>(this)->begin();
1263
155
    }
1264
155
    const_iterator end() const { return const_cast<raw_hash_set*>(this)->end(); }
1265
    const_iterator cbegin() const { return begin(); }
1266
    const_iterator cend() const { return end(); }
1267
1268
    bool empty() const { return !size(); }
1269
3.21k
    size_t size() const { return size_; }
1270
2.56k
    size_t capacity() const { return capacity_; }
1271
    size_t max_size() const { return (std::numeric_limits<size_t>::max)(); }
1272
1273
    PHMAP_ATTRIBUTE_REINITIALIZES void clear() {
1274
        if (empty())
1275
            return;
1276
        if (capacity_) {
1277
           PHMAP_IF_CONSTEXPR((!std::is_trivially_destructible<typename PolicyTraits::value_type>::value ||
1278
                               std::is_same<typename Policy::is_flat, std::false_type>::value)) {
1279
                // node map or not trivially destructible... we  need to iterate and destroy values one by one
1280
                for (size_t i = 0; i != capacity_; ++i) {
1281
                    if (IsFull(ctrl_[i])) {
1282
                        PolicyTraits::destroy(&alloc_ref(), slots_ + i);
1283
                    }
1284
                }
1285
            }
1286
            size_ = 0;
1287
            reset_ctrl(capacity_);
1288
            reset_growth_left(capacity_);
1289
        }
1290
        assert(empty());
1291
        infoz_.RecordStorageChanged(0, capacity_);
1292
    }
1293
1294
    // This overload kicks in when the argument is an rvalue of insertable and
1295
    // decomposable type other than init_type.
1296
    //
1297
    //   flat_hash_map<std::string, int> m;
1298
    //   m.insert(std::make_pair("abc", 42));
1299
    template <class T, RequiresInsertable<T> = 0,
1300
              typename std::enable_if<IsDecomposable<T>::value, int>::type = 0,
1301
              T* = nullptr>
1302
8.52M
    std::pair<iterator, bool> insert(T&& value) {
1303
8.52M
        return emplace(std::forward<T>(value));
1304
8.52M
    }
1305
1306
    // This overload kicks in when the argument is a bitfield or an lvalue of
1307
    // insertable and decomposable type.
1308
    //
1309
    //   union { int n : 1; };
1310
    //   flat_hash_set<int> s;
1311
    //   s.insert(n);
1312
    //
1313
    //   flat_hash_set<std::string> s;
1314
    //   const char* p = "hello";
1315
    //   s.insert(p);
1316
    //
1317
    // TODO(romanp): Once we stop supporting gcc 5.1 and below, replace
1318
    // RequiresInsertable<T> with RequiresInsertable<const T&>.
1319
    // We are hitting this bug: https://godbolt.org/g/1Vht4f.
1320
    template <class T, RequiresInsertable<T> = 0,
1321
              typename std::enable_if<IsDecomposable<const T&>::value, int>::type = 0>
1322
    std::pair<iterator, bool> insert(const T& value) {
1323
        return emplace(value);
1324
    }
1325
1326
    // This overload kicks in when the argument is an rvalue of init_type. Its
1327
    // purpose is to handle brace-init-list arguments.
1328
    //
1329
    //   flat_hash_set<std::string, int> s;
1330
    //   s.insert({"abc", 42});
1331
    std::pair<iterator, bool> insert(init_type&& value) {
1332
        return emplace(std::move(value));
1333
    }
1334
1335
    template <class T, RequiresInsertable<T> = 0,
1336
              typename std::enable_if<IsDecomposable<T>::value, int>::type = 0,
1337
              T* = nullptr>
1338
    iterator insert(const_iterator, T&& value) {
1339
        return insert(std::forward<T>(value)).first;
1340
    }
1341
1342
    // TODO(romanp): Once we stop supporting gcc 5.1 and below, replace
1343
    // RequiresInsertable<T> with RequiresInsertable<const T&>.
1344
    // We are hitting this bug: https://godbolt.org/g/1Vht4f.
1345
    template <class T, RequiresInsertable<T> = 0,
1346
              typename std::enable_if<IsDecomposable<const T&>::value, int>::type = 0>
1347
    iterator insert(const_iterator, const T& value) {
1348
        return insert(value).first;
1349
    }
1350
1351
    iterator insert(const_iterator, init_type&& value) {
1352
        return insert(std::move(value)).first;
1353
    }
1354
1355
    template <typename It>
1356
    using IsRandomAccess = std::is_same<typename std::iterator_traits<It>::iterator_category,
1357
                                        std::random_access_iterator_tag>;
1358
1359
1360
    template<typename T>
1361
    struct has_difference_operator
1362
    {
1363
    private:
1364
        using yes = std::true_type;
1365
        using no  = std::false_type;
1366
 
1367
        template<typename U> static auto test(int) -> decltype(std::declval<U>() - std::declval<U>() == 1, yes());
1368
        template<typename>   static no   test(...);
1369
 
1370
    public:
1371
        static constexpr bool value = std::is_same<decltype(test<T>(0)), yes>::value;
1372
    };
1373
1374
    template <class InputIt, typename phmap::enable_if_t<has_difference_operator<InputIt>::value, int> = 0>
1375
    void insert(InputIt first, InputIt last) {
1376
        this->reserve(this->size() + (last - first));
1377
        for (; first != last; ++first) 
1378
            emplace(*first);
1379
    }
1380
1381
    template <class InputIt, typename phmap::enable_if_t<!has_difference_operator<InputIt>::value, int> = 0>
1382
    void insert(InputIt first, InputIt last) {
1383
        for (; first != last; ++first) 
1384
            emplace(*first);
1385
    }
1386
1387
    template <class T, RequiresNotInit<T> = 0, RequiresInsertable<const T&> = 0>
1388
    void insert(std::initializer_list<T> ilist) {
1389
        insert(ilist.begin(), ilist.end());
1390
    }
1391
1392
    void insert(std::initializer_list<init_type> ilist) {
1393
        insert(ilist.begin(), ilist.end());
1394
    }
1395
1396
    insert_return_type insert(node_type&& node) {
1397
        if (!node) return {end(), false, node_type()};
1398
        const auto& elem = PolicyTraits::element(CommonAccess::GetSlot(node));
1399
        auto res = PolicyTraits::apply(
1400
            InsertSlot<false>{*this, std::move(*CommonAccess::GetSlot(node))},
1401
            elem);
1402
        if (res.second) {
1403
            CommonAccess::Reset(&node);
1404
            return {res.first, true, node_type()};
1405
        } else {
1406
            return {res.first, false, std::move(node)};
1407
        }
1408
    }
1409
1410
    insert_return_type insert(node_type&& node, size_t hashval) {
1411
        if (!node) return {end(), false, node_type()};
1412
        const auto& elem = PolicyTraits::element(CommonAccess::GetSlot(node));
1413
        auto res = PolicyTraits::apply(
1414
            InsertSlotWithHash<false>{*this, std::move(*CommonAccess::GetSlot(node)), hashval},
1415
            elem);
1416
        if (res.second) {
1417
            CommonAccess::Reset(&node);
1418
            return {res.first, true, node_type()};
1419
        } else {
1420
            return {res.first, false, std::move(node)};
1421
        }
1422
    }
1423
1424
    iterator insert(const_iterator, node_type&& node) {
1425
        auto res = insert(std::move(node));
1426
        node = std::move(res.node);
1427
        return res.position;
1428
    }
1429
1430
    // This overload kicks in if we can deduce the key from args. This enables us
1431
    // to avoid constructing value_type if an entry with the same key already
1432
    // exists.
1433
    //
1434
    // For example:
1435
    //
1436
    //   flat_hash_map<std::string, std::string> m = {{"abc", "def"}};
1437
    //   // Creates no std::string copies and makes no heap allocations.
1438
    //   m.emplace("abc", "xyz");
1439
    template <class... Args, typename std::enable_if<
1440
                                 IsDecomposable<Args...>::value, int>::type = 0>
1441
8.52M
    std::pair<iterator, bool> emplace(Args&&... args) {
1442
8.52M
        return PolicyTraits::apply(EmplaceDecomposable{*this},
1443
8.52M
                                   std::forward<Args>(args)...);
1444
8.52M
    }
1445
1446
    template <class... Args, typename std::enable_if<IsDecomposable<Args...>::value, int>::type = 0>
1447
    std::pair<iterator, bool> emplace_with_hash(size_t hashval, Args&&... args) {
1448
        return PolicyTraits::apply(EmplaceDecomposableHashval{*this, hashval}, std::forward<Args>(args)...);
1449
    }
1450
1451
    // This overload kicks in if we cannot deduce the key from args. It constructs
1452
    // value_type unconditionally and then either moves it into the table or
1453
    // destroys.
1454
    template <class... Args, typename std::enable_if<!IsDecomposable<Args...>::value, int>::type = 0>
1455
    std::pair<iterator, bool> emplace(Args&&... args) {
1456
        typename phmap::aligned_storage<sizeof(slot_type), alignof(slot_type)>::type
1457
            raw;
1458
        slot_type* slot = reinterpret_cast<slot_type*>(&raw);
1459
1460
        PolicyTraits::construct(&alloc_ref(), slot, std::forward<Args>(args)...);
1461
        const auto& elem = PolicyTraits::element(slot);
1462
        return PolicyTraits::apply(InsertSlot<true>{*this, std::move(*slot)}, elem);
1463
    }
1464
1465
    template <class... Args, typename std::enable_if<!IsDecomposable<Args...>::value, int>::type = 0>
1466
    std::pair<iterator, bool> emplace_with_hash(size_t hashval, Args&&... args) {
1467
        typename phmap::aligned_storage<sizeof(slot_type), alignof(slot_type)>::type raw;
1468
        slot_type* slot = reinterpret_cast<slot_type*>(&raw);
1469
1470
        PolicyTraits::construct(&alloc_ref(), slot, std::forward<Args>(args)...);
1471
        const auto& elem = PolicyTraits::element(slot);
1472
        return PolicyTraits::apply(InsertSlotWithHash<true>{*this, std::move(*slot), hashval}, elem);
1473
    }
1474
1475
    template <class... Args>
1476
    iterator emplace_hint(const_iterator, Args&&... args) {
1477
        return emplace(std::forward<Args>(args)...).first;
1478
    }
1479
1480
    template <class... Args>
1481
    iterator emplace_hint_with_hash(size_t hashval, const_iterator, Args&&... args) {
1482
        return emplace_with_hash(hashval, std::forward<Args>(args)...).first;
1483
    }
1484
1485
    // Extension API: support for lazy emplace.
1486
    //
1487
    // Looks up key in the table. If found, returns the iterator to the element.
1488
    // Otherwise calls f with one argument of type raw_hash_set::constructor. f
1489
    // MUST call raw_hash_set::constructor with arguments as if a
1490
    // raw_hash_set::value_type is constructed, otherwise the behavior is
1491
    // undefined.
1492
    //
1493
    // For example:
1494
    //
1495
    //   std::unordered_set<ArenaString> s;
1496
    //   // Makes ArenaStr even if "abc" is in the map.
1497
    //   s.insert(ArenaString(&arena, "abc"));
1498
    //
1499
    //   flat_hash_set<ArenaStr> s;
1500
    //   // Makes ArenaStr only if "abc" is not in the map.
1501
    //   s.lazy_emplace("abc", [&](const constructor& ctor) {
1502
    //     ctor(&arena, "abc");
1503
    //   });
1504
    //
1505
    // WARNING: This API is currently experimental. If there is a way to implement
1506
    // the same thing with the rest of the API, prefer that.
1507
    class constructor 
1508
    {
1509
        friend class raw_hash_set;
1510
1511
    public:
1512
        slot_type* slot() const {
1513
            return *slot_;
1514
        }
1515
1516
        template <class... Args>
1517
        void operator()(Args&&... args) const {
1518
            assert(*slot_);
1519
            PolicyTraits::construct(alloc_, *slot_, std::forward<Args>(args)...);
1520
            *slot_ = nullptr;
1521
        }
1522
1523
    private:
1524
        constructor(allocator_type* a, slot_type** slot) : alloc_(a), slot_(slot) {}
1525
1526
        allocator_type* alloc_;
1527
        slot_type** slot_;
1528
    };
1529
1530
    // Extension API: support for lazy emplace.
1531
    // Looks up key in the table. If found, returns the iterator to the element.
1532
    // Otherwise calls f with one argument of type raw_hash_set::constructor. f
1533
    // MUST call raw_hash_set::constructor with arguments as if a
1534
    // raw_hash_set::value_type is constructed, otherwise the behavior is
1535
    // undefined.
1536
    //
1537
    // For example:
1538
    //
1539
    //   std::unordered_set<ArenaString> s;
1540
    //   // Makes ArenaStr even if "abc" is in the map.
1541
    //   s.insert(ArenaString(&arena, "abc"));
1542
    //
1543
    //   flat_hash_set<ArenaStr> s;
1544
    //   // Makes ArenaStr only if "abc" is not in the map.
1545
    //   s.lazy_emplace("abc", [&](const constructor& ctor) {
1546
    //                         ctor(&arena, "abc");
1547
    //   });
1548
    // -----------------------------------------------------
1549
    template <class K = key_type, class F>
1550
    iterator lazy_emplace(const key_arg<K>& key, F&& f) {
1551
        return lazy_emplace_with_hash(key, this->hash(key), std::forward<F>(f));
1552
    }
1553
1554
    template <class K = key_type, class F>
1555
    iterator lazy_emplace_with_hash(const key_arg<K>& key, size_t hashval, F&& f) {
1556
        size_t offset = _find_key(key, hashval);
1557
        if (offset == (size_t)-1) {
1558
            offset = prepare_insert(hashval);
1559
            lazy_emplace_at(offset, std::forward<F>(f));
1560
            this->set_ctrl(offset, H2(hashval));
1561
        }
1562
        return iterator_at(offset);
1563
    }
1564
1565
    template <class K = key_type, class F>
1566
    void lazy_emplace_at(size_t& idx, F&& f) {
1567
        slot_type* slot = slots_ + idx;
1568
        std::forward<F>(f)(constructor(&alloc_ref(), &slot));
1569
        assert(!slot);
1570
    }
1571
1572
    template <class K = key_type, class F>
1573
    void emplace_single_with_hash(const key_arg<K>& key, size_t hashval, F&& f) {
1574
        size_t offset = _find_key(key, hashval);
1575
        if (offset == (size_t)-1) {
1576
            offset = prepare_insert(hashval);
1577
            lazy_emplace_at(offset, std::forward<F>(f));
1578
            this->set_ctrl(offset, H2(hashval));
1579
        } else
1580
            _erase(iterator_at(offset));
1581
    }
1582
1583
1584
    // Extension API: support for heterogeneous keys.
1585
    //
1586
    //   std::unordered_set<std::string> s;
1587
    //   // Turns "abc" into std::string.
1588
    //   s.erase("abc");
1589
    //
1590
    //   flat_hash_set<std::string> s;
1591
    //   // Uses "abc" directly without copying it into std::string.
1592
    //   s.erase("abc");
1593
    template <class K = key_type>
1594
    size_type erase(const key_arg<K>& key) {
1595
        auto it = find(key);
1596
        if (it == end()) return 0;
1597
        _erase(it);
1598
        return 1;
1599
    }
1600
1601
1602
    iterator erase(const_iterator cit) { return erase(cit.inner_); }
1603
    
1604
    // Erases the element pointed to by `it`.  Unlike `std::unordered_set::erase`,
1605
    // this method returns void to reduce algorithmic complexity to O(1).  In
1606
    // order to erase while iterating across a map, use the following idiom (which
1607
    // also works for standard containers):
1608
    //
1609
    // for (auto it = m.begin(), end = m.end(); it != end;) {
1610
    //   if (<pred>) {
1611
    //     m._erase(it++);
1612
    //   } else {
1613
    //     ++it;
1614
    //   }
1615
    // }
1616
    void _erase(iterator it) {
1617
        assert(it != end());
1618
        PolicyTraits::destroy(&alloc_ref(), it.slot_);
1619
        erase_meta_only(it);
1620
    }
1621
    void _erase(const_iterator cit) { _erase(cit.inner_); }
1622
1623
    // This overload is necessary because otherwise erase<K>(const K&) would be
1624
    // a better match if non-const iterator is passed as an argument.
1625
    iterator erase(iterator it) {
1626
        assert(it != end());
1627
        auto res = it;
1628
        ++res;
1629
        _erase(it);
1630
        return res;
1631
    }
1632
1633
    iterator erase(const_iterator first, const_iterator last) {
1634
        while (first != last) {
1635
            _erase(first++);
1636
        }
1637
        return last.inner_;
1638
    }
1639
1640
    // Moves elements from `src` into `this`.
1641
    // If the element already exists in `this`, it is left unmodified in `src`.
1642
    template <typename H, typename E>
1643
    void merge(raw_hash_set<Policy, H, E, Alloc>& src) {  // NOLINT
1644
        assert(this != &src);
1645
        for (auto it = src.begin(), e = src.end(); it != e; ++it) {
1646
            if (PolicyTraits::apply(InsertSlot<false>{*this, std::move(*it.slot_)},
1647
                                    PolicyTraits::element(it.slot_))
1648
                .second) {
1649
                src.erase_meta_only(it);
1650
            }
1651
        }
1652
    }
1653
1654
    template <typename H, typename E>
1655
    void merge(raw_hash_set<Policy, H, E, Alloc>&& src) {
1656
        merge(src);
1657
    }
1658
1659
    node_type extract(const_iterator position) {
1660
        auto node =
1661
            CommonAccess::Make<node_type>(alloc_ref(), position.inner_.slot_);
1662
        erase_meta_only(position);
1663
        return node;
1664
    }
1665
1666
    template <
1667
        class K = key_type,
1668
        typename std::enable_if<!std::is_same<K, iterator>::value, int>::type = 0>
1669
    node_type extract(const key_arg<K>& key) {
1670
        auto it = find(key);
1671
        return it == end() ? node_type() : extract(const_iterator{it});
1672
    }
1673
1674
    void swap(raw_hash_set& that) noexcept(
1675
        IsNoThrowSwappable<hasher>() && IsNoThrowSwappable<key_equal>() &&
1676
        (!AllocTraits::propagate_on_container_swap::value ||
1677
483
         IsNoThrowSwappable<allocator_type>(typename AllocTraits::propagate_on_container_swap{}))) {
1678
483
        using std::swap;
1679
483
        swap(ctrl_, that.ctrl_);
1680
483
        swap(slots_, that.slots_);
1681
483
        swap(size_, that.size_);
1682
483
        swap(capacity_, that.capacity_);
1683
483
        swap(growth_left(), that.growth_left());
1684
483
        swap(hash_ref(), that.hash_ref());
1685
483
        swap(eq_ref(), that.eq_ref());
1686
483
        swap(infoz_, that.infoz_);
1687
483
        SwapAlloc(alloc_ref(), that.alloc_ref(), typename AllocTraits::propagate_on_container_swap{});
1688
483
    }
1689
1690
#if !defined(PHMAP_NON_DETERMINISTIC)
1691
    template<typename OutputArchive>
1692
    bool phmap_dump(OutputArchive&) const;
1693
1694
    template<typename InputArchive>
1695
    bool  phmap_load(InputArchive&);
1696
#endif
1697
1698
    void rehash(size_t n) {
1699
        if (n == 0 && capacity_ == 0) return;
1700
        if (n == 0 && size_ == 0) {
1701
            destroy_slots();
1702
            infoz_.RecordStorageChanged(0, 0);
1703
            return;
1704
        }
1705
        // bitor is a faster way of doing `max` here. We will round up to the next
1706
        // power-of-2-minus-1, so bitor is good enough.
1707
        auto m = NormalizeCapacity((std::max)(n, size()));
1708
        // n == 0 unconditionally rehashes as per the standard.
1709
        if (n == 0 || m > capacity_) {
1710
            resize(m);
1711
        }
1712
    }
1713
1714
    void reserve(size_t n) { rehash(GrowthToLowerboundCapacity(n)); }
1715
1716
    // Extension API: support for heterogeneous keys.
1717
    //
1718
    //   std::unordered_set<std::string> s;
1719
    //   // Turns "abc" into std::string.
1720
    //   s.count("abc");
1721
    //
1722
    //   ch_set<std::string> s;
1723
    //   // Uses "abc" directly without copying it into std::string.
1724
    //   s.count("abc");
1725
    template <class K = key_type>
1726
    size_t count(const key_arg<K>& key) const {
1727
        return find(key) == end() ? size_t(0) : size_t(1);
1728
    }
1729
1730
    // Issues CPU prefetch instructions for the memory needed to find or insert
1731
    // a key.  Like all lookup functions, this support heterogeneous keys.
1732
    //
1733
    // NOTE: This is a very low level operation and should not be used without
1734
    // specific benchmarks indicating its importance.
1735
    void prefetch_hash(size_t hashval) const {
1736
        (void)hashval;
1737
#if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_IX86))
1738
        auto seq = probe(hashval);
1739
        _mm_prefetch((const char *)(ctrl_ + seq.offset()), _MM_HINT_NTA);
1740
        _mm_prefetch((const char *)(slots_ + seq.offset()), _MM_HINT_NTA);
1741
#elif defined(__GNUC__)
1742
        auto seq = probe(hashval);
1743
        __builtin_prefetch(static_cast<const void*>(ctrl_ + seq.offset()));
1744
        __builtin_prefetch(static_cast<const void*>(slots_ + seq.offset()));
1745
#endif  // __GNUC__
1746
    }
1747
1748
    template <class K = key_type>
1749
    void prefetch(const key_arg<K>& key) const {
1750
        PHMAP_IF_CONSTEXPR (std_alloc_t::value)
1751
            prefetch_hash(this->hash(key));
1752
    }
1753
1754
    // The API of find() has two extensions.
1755
    //
1756
    // 1. The hash can be passed by the user. It must be equal to the hash of the
1757
    // key.
1758
    //
1759
    // 2. The type of the key argument doesn't have to be key_type. This is so
1760
    // called heterogeneous key support.
1761
    template <class K = key_type>
1762
    iterator find(const key_arg<K>& key, size_t hashval) {
1763
        size_t offset;
1764
        if (find_impl(key, hashval, offset))
1765
            return iterator_at(offset);
1766
        else
1767
            return end();
1768
    }
1769
1770
    template <class K = key_type>
1771
    pointer find_ptr(const key_arg<K>& key, size_t hashval) {
1772
        size_t offset;
1773
        if (find_impl(key, hashval, offset))
1774
            return &PolicyTraits::element(slots_ + offset);
1775
        else
1776
            return nullptr;
1777
    }
1778
1779
    template <class K = key_type>
1780
    iterator find(const key_arg<K>& key) {
1781
        return find(key, this->hash(key));
1782
    }
1783
1784
    template <class K = key_type>
1785
    const_iterator find(const key_arg<K>& key, size_t hashval) const {
1786
        return const_cast<raw_hash_set*>(this)->find(key, hashval);
1787
    }
1788
    template <class K = key_type>
1789
    const_iterator find(const key_arg<K>& key) const {
1790
        return find(key, this->hash(key));
1791
    }
1792
1793
    template <class K = key_type>
1794
    bool contains(const key_arg<K>& key) const {
1795
        return find(key) != end();
1796
    }
1797
1798
    template <class K = key_type>
1799
    bool contains(const key_arg<K>& key, size_t hashval) const {
1800
        return find(key, hashval) != end();
1801
    }
1802
1803
    template <class K = key_type>
1804
    std::pair<iterator, iterator> equal_range(const key_arg<K>& key) {
1805
        auto it = find(key);
1806
        if (it != end()) return {it, std::next(it)};
1807
        return {it, it};
1808
    }
1809
    template <class K = key_type>
1810
    std::pair<const_iterator, const_iterator> equal_range(
1811
        const key_arg<K>& key) const {
1812
        auto it = find(key);
1813
        if (it != end()) return {it, std::next(it)};
1814
        return {it, it};
1815
    }
1816
1817
    size_t bucket_count() const { return capacity_; }
1818
    float load_factor() const {
1819
        return capacity_ ? static_cast<float>(static_cast<double>(size()) / capacity_) : 0.0f;
1820
    }
1821
    float max_load_factor() const { return 1.0f; }
1822
    void max_load_factor(float) {
1823
        // Does nothing.
1824
    }
1825
1826
    hasher hash_function() const { return hash_ref(); } // warning: doesn't match internal hash - use hash() member function
1827
    key_equal key_eq() const { return eq_ref(); }
1828
    allocator_type get_allocator() const { return alloc_ref(); }
1829
1830
483
    friend bool operator==(const raw_hash_set& a, const raw_hash_set& b) {
1831
483
        if (a.size() != b.size()) return false;
1832
155
        const raw_hash_set* outer = &a;
1833
155
        const raw_hash_set* inner = &b;
1834
155
        if (outer->capacity() > inner->capacity()) 
1835
0
            std::swap(outer, inner);
1836
155
        for (const value_type& elem : *outer)
1837
601k
            if (!inner->has_element(elem)) return false;
1838
38
        return true;
1839
155
    }
1840
1841
    friend bool operator!=(const raw_hash_set& a, const raw_hash_set& b) {
1842
        return !(a == b);
1843
    }
1844
1845
    friend void swap(raw_hash_set& a,
1846
                     raw_hash_set& b) noexcept(noexcept(a.swap(b))) {
1847
        a.swap(b);
1848
    }
1849
1850
    template <class K>
1851
8.52M
    size_t hash(const K& key) const {
1852
8.52M
        return HashElement{hash_ref()}(key);
1853
8.52M
    }
1854
1855
private:
1856
    template <class Container, typename Enabler>
1857
    friend struct phmap::priv::hashtable_debug_internal::HashtableDebugAccess;
1858
1859
    template <class K = key_type>
1860
    bool find_impl(const key_arg<K>& PHMAP_RESTRICT key, size_t hashval, size_t& PHMAP_RESTRICT offset) {
1861
        PHMAP_IF_CONSTEXPR (!std_alloc_t::value) {
1862
            // ctrl_ could be nullptr
1863
            if (!ctrl_)
1864
                return false;
1865
        }
1866
        auto seq = probe(hashval);
1867
        while (true) {
1868
            Group g{ ctrl_ + seq.offset() };
1869
            for (uint32_t i : g.Match((h2_t)H2(hashval))) {
1870
                offset = seq.offset((size_t)i);
1871
                if (PHMAP_PREDICT_TRUE(PolicyTraits::apply(
1872
                    EqualElement<K>{key, eq_ref()},
1873
                    PolicyTraits::element(slots_ + offset))))
1874
                    return true;
1875
            }
1876
            if (PHMAP_PREDICT_TRUE(g.MatchEmpty()))
1877
                return false;
1878
            seq.next();
1879
        }
1880
    }
1881
1882
    struct FindElement 
1883
    {
1884
        template <class K, class... Args>
1885
        const_iterator operator()(const K& key, Args&&...) const {
1886
            return s.find(key);
1887
        }
1888
        const raw_hash_set& s;
1889
    };
1890
1891
    struct HashElement 
1892
    {
1893
        template <class K, class... Args>
1894
10.2M
        size_t operator()(const K& key, Args&&...) const {
1895
#if PHMAP_DISABLE_MIX
1896
            return h(key);
1897
#else
1898
10.2M
            return phmap_mix<sizeof(size_t)>()(h(key));
1899
10.2M
#endif
1900
10.2M
        }
unsigned long phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<unsigned int, int>, phmap::Hash<unsigned int>, phmap::EqualTo<unsigned int>, std::__1::allocator<std::__1::pair<unsigned int const, int> > >::HashElement::operator()<unsigned int, std::__1::piecewise_construct_t const&, std::__1::tuple<unsigned int const&>, std::__1::tuple<int const&> >(unsigned int const&, std::__1::piecewise_construct_t const&, std::__1::tuple<unsigned int const&>&&, std::__1::tuple<int const&>&&) const
Line
Count
Source
1894
1.67M
        size_t operator()(const K& key, Args&&...) const {
1895
#if PHMAP_DISABLE_MIX
1896
            return h(key);
1897
#else
1898
1.67M
            return phmap_mix<sizeof(size_t)>()(h(key));
1899
1.67M
#endif
1900
1.67M
        }
unsigned long phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<unsigned int, int>, phmap::Hash<unsigned int>, phmap::EqualTo<unsigned int>, std::__1::allocator<std::__1::pair<unsigned int const, int> > >::HashElement::operator()<unsigned int>(unsigned int const&) const
Line
Count
Source
1894
8.52M
        size_t operator()(const K& key, Args&&...) const {
1895
#if PHMAP_DISABLE_MIX
1896
            return h(key);
1897
#else
1898
8.52M
            return phmap_mix<sizeof(size_t)>()(h(key));
1899
8.52M
#endif
1900
8.52M
        }
1901
        const hasher& h;
1902
    };
1903
1904
    template <class K1>
1905
    struct EqualElement 
1906
    {
1907
        template <class K2, class... Args>
1908
10.1M
        bool operator()(const K2& lhs, Args&&...) const {
1909
10.1M
            return eq(lhs, rhs);
1910
10.1M
        }
1911
        const K1& rhs;
1912
        const key_equal& eq;
1913
    };
1914
1915
    template <class K, class... Args>
1916
    std::pair<iterator, bool> emplace_decomposable(const K& key, size_t hashval, 
1917
                                                   Args&&... args)
1918
8.52M
    {
1919
8.52M
        size_t offset = _find_key(key, hashval);
1920
8.52M
        if (offset == (size_t)-1) {
1921
614k
            offset = prepare_insert(hashval);
1922
614k
            emplace_at(offset, std::forward<Args>(args)...);
1923
614k
            this->set_ctrl(offset, H2(hashval));
1924
614k
            return {iterator_at(offset), true};
1925
614k
        }
1926
7.91M
        return {iterator_at(offset), false};
1927
8.52M
    }
1928
1929
    struct EmplaceDecomposable 
1930
    {
1931
        template <class K, class... Args>
1932
8.52M
        std::pair<iterator, bool> operator()(const K& key, Args&&... args) const {
1933
8.52M
            return s.emplace_decomposable(key, s.hash(key), std::forward<Args>(args)...);
1934
8.52M
        }
1935
        raw_hash_set& s;
1936
    };
1937
1938
    struct EmplaceDecomposableHashval {
1939
        template <class K, class... Args>
1940
        std::pair<iterator, bool> operator()(const K& key, Args&&... args) const {
1941
            return s.emplace_decomposable(key, hashval, std::forward<Args>(args)...);
1942
        }
1943
        raw_hash_set& s;
1944
        size_t hashval;
1945
    };
1946
1947
    template <bool do_destroy>
1948
    struct InsertSlot 
1949
    {
1950
        template <class K, class... Args>
1951
        std::pair<iterator, bool> operator()(const K& key, Args&&...) && {
1952
            size_t hashval = s.hash(key);
1953
            auto res = s.find_or_prepare_insert(key, hashval);
1954
            if (res.second) {
1955
                PolicyTraits::transfer(&s.alloc_ref(), s.slots_ + res.first, &slot);
1956
                s.set_ctrl(res.first, H2(hashval));
1957
            } else if (do_destroy) {
1958
                PolicyTraits::destroy(&s.alloc_ref(), &slot);
1959
            }
1960
            return {s.iterator_at(res.first), res.second};
1961
        }
1962
        raw_hash_set& s;
1963
        // Constructed slot. Either moved into place or destroyed.
1964
        slot_type&& slot;
1965
    };
1966
1967
    template <bool do_destroy>
1968
    struct InsertSlotWithHash 
1969
    {
1970
        template <class K, class... Args>
1971
        std::pair<iterator, bool> operator()(const K& key, Args&&...) && {
1972
            auto res = s.find_or_prepare_insert(key, hashval);
1973
            if (res.second) {
1974
                PolicyTraits::transfer(&s.alloc_ref(), s.slots_ + res.first, &slot);
1975
                s.set_ctrl(res.first, H2(hashval));
1976
            } else if (do_destroy) {
1977
                PolicyTraits::destroy(&s.alloc_ref(), &slot);
1978
            }
1979
            return {s.iterator_at(res.first), res.second};
1980
        }
1981
        raw_hash_set& s;
1982
        // Constructed slot. Either moved into place or destroyed.
1983
        slot_type&& slot;
1984
        size_t &hashval;
1985
    };
1986
1987
    // "erases" the object from the container, except that it doesn't actually
1988
    // destroy the object. It only updates all the metadata of the class.
1989
    // This can be used in conjunction with Policy::transfer to move the object to
1990
    // another place.
1991
    void erase_meta_only(const_iterator it) {
1992
        assert(IsFull(*it.inner_.ctrl_) && "erasing a dangling iterator");
1993
        --size_;
1994
        const size_t index = (size_t)(it.inner_.ctrl_ - ctrl_);
1995
        const size_t index_before = (index - Group::kWidth) & capacity_;
1996
        const auto empty_after = Group(it.inner_.ctrl_).MatchEmpty();
1997
        const auto empty_before = Group(ctrl_ + index_before).MatchEmpty();
1998
1999
        // We count how many consecutive non empties we have to the right and to the
2000
        // left of `it`. If the sum is >= kWidth then there is at least one probe
2001
        // window that might have seen a full group.
2002
        bool was_never_full =
2003
            empty_before && empty_after &&
2004
            static_cast<size_t>(empty_after.TrailingZeros() +
2005
                                empty_before.LeadingZeros()) < Group::kWidth;
2006
2007
        set_ctrl(index, was_never_full ? kEmpty : kDeleted);
2008
        growth_left() += was_never_full;
2009
        infoz_.RecordErase();
2010
    }
2011
2012
2.82k
    void initialize_slots(size_t new_capacity) {
2013
2.82k
        assert(new_capacity);
2014
2.82k
        if (std::is_same<SlotAlloc, std::allocator<slot_type>>::value && 
2015
2.82k
            slots_ == nullptr) {
2016
574
            infoz_ = Sample();
2017
574
        }
2018
2019
2.82k
        auto layout = MakeLayout(new_capacity);
2020
2.82k
        char* mem = static_cast<char*>(
2021
2.82k
            Allocate<Layout::Alignment()>(&alloc_ref(), layout.AllocSize()));
2022
2.82k
        ctrl_ = reinterpret_cast<ctrl_t*>(layout.template Pointer<0>(mem));
2023
2.82k
        slots_ = layout.template Pointer<1>(mem);
2024
2.82k
        reset_ctrl(new_capacity);
2025
2.82k
        reset_growth_left(new_capacity);
2026
2.82k
        infoz_.RecordStorageChanged(size_, new_capacity);
2027
2.82k
    }
2028
2029
1.93k
    void destroy_slots() {
2030
1.93k
        if (!capacity_)
2031
1.35k
            return;
2032
        
2033
574
        PHMAP_IF_CONSTEXPR((!std::is_trivially_destructible<typename PolicyTraits::value_type>::value ||
2034
574
                            std::is_same<typename Policy::is_flat, std::false_type>::value)) {
2035
            // node map, or not trivially destructible... we  need to iterate and destroy values one by one
2036
            // std::cout << "either this is a node map or " << type_name<typename PolicyTraits::value_type>()  << " is not trivially_destructible\n";
2037
0
            for (size_t i = 0, cnt = capacity_; i != cnt; ++i) {
2038
0
                if (IsFull(ctrl_[i])) {
2039
0
                    PolicyTraits::destroy(&alloc_ref(), slots_ + i);
2040
0
                }
2041
0
            }
2042
0
        } 
2043
574
        auto layout = MakeLayout(capacity_);
2044
        // Unpoison before returning the memory to the allocator.
2045
574
        SanitizerUnpoisonMemoryRegion(slots_, sizeof(slot_type) * capacity_);
2046
574
        Deallocate<Layout::Alignment()>(&alloc_ref(), ctrl_, layout.AllocSize());
2047
574
        ctrl_ = EmptyGroup<std_alloc_t>();
2048
574
        slots_ = nullptr;
2049
574
        size_ = 0;
2050
574
        capacity_ = 0;
2051
574
        growth_left() = 0;
2052
574
    }
phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<unsigned int, int>, phmap::Hash<unsigned int>, phmap::EqualTo<unsigned int>, std::__1::allocator<std::__1::pair<unsigned int const, int> > >::destroy_slots()
Line
Count
Source
2029
1.44k
    void destroy_slots() {
2030
1.44k
        if (!capacity_)
2031
875
            return;
2032
        
2033
574
        PHMAP_IF_CONSTEXPR((!std::is_trivially_destructible<typename PolicyTraits::value_type>::value ||
2034
574
                            std::is_same<typename Policy::is_flat, std::false_type>::value)) {
2035
            // node map, or not trivially destructible... we  need to iterate and destroy values one by one
2036
            // std::cout << "either this is a node map or " << type_name<typename PolicyTraits::value_type>()  << " is not trivially_destructible\n";
2037
            for (size_t i = 0, cnt = capacity_; i != cnt; ++i) {
2038
                if (IsFull(ctrl_[i])) {
2039
                    PolicyTraits::destroy(&alloc_ref(), slots_ + i);
2040
                }
2041
            }
2042
        } 
2043
574
        auto layout = MakeLayout(capacity_);
2044
        // Unpoison before returning the memory to the allocator.
2045
574
        SanitizerUnpoisonMemoryRegion(slots_, sizeof(slot_type) * capacity_);
2046
574
        Deallocate<Layout::Alignment()>(&alloc_ref(), ctrl_, layout.AllocSize());
2047
574
        ctrl_ = EmptyGroup<std_alloc_t>();
2048
574
        slots_ = nullptr;
2049
574
        size_ = 0;
2050
574
        capacity_ = 0;
2051
574
        growth_left() = 0;
2052
574
    }
phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, phmap::priv::StringHashEqT<char>::Hash, phmap::priv::StringHashEqT<char>::Eq, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >::destroy_slots()
Line
Count
Source
2029
483
    void destroy_slots() {
2030
483
        if (!capacity_)
2031
483
            return;
2032
        
2033
0
        PHMAP_IF_CONSTEXPR((!std::is_trivially_destructible<typename PolicyTraits::value_type>::value ||
2034
0
                            std::is_same<typename Policy::is_flat, std::false_type>::value)) {
2035
            // node map, or not trivially destructible... we  need to iterate and destroy values one by one
2036
            // std::cout << "either this is a node map or " << type_name<typename PolicyTraits::value_type>()  << " is not trivially_destructible\n";
2037
0
            for (size_t i = 0, cnt = capacity_; i != cnt; ++i) {
2038
0
                if (IsFull(ctrl_[i])) {
2039
0
                    PolicyTraits::destroy(&alloc_ref(), slots_ + i);
2040
0
                }
2041
0
            }
2042
0
        } 
2043
0
        auto layout = MakeLayout(capacity_);
2044
        // Unpoison before returning the memory to the allocator.
2045
0
        SanitizerUnpoisonMemoryRegion(slots_, sizeof(slot_type) * capacity_);
2046
0
        Deallocate<Layout::Alignment()>(&alloc_ref(), ctrl_, layout.AllocSize());
2047
0
        ctrl_ = EmptyGroup<std_alloc_t>();
2048
0
        slots_ = nullptr;
2049
0
        size_ = 0;
2050
0
        capacity_ = 0;
2051
0
        growth_left() = 0;
2052
0
    }
2053
2054
2.70k
    void resize(size_t new_capacity) {
2055
2.70k
        assert(IsValidCapacity(new_capacity));
2056
2.70k
        auto* old_ctrl = ctrl_;
2057
2.70k
        auto* old_slots = slots_;
2058
2.70k
        const size_t old_capacity = capacity_;
2059
2.70k
        initialize_slots(new_capacity);
2060
2.70k
        capacity_ = new_capacity;
2061
2062
1.23M
        for (size_t i = 0; i != old_capacity; ++i) {
2063
1.23M
            if (IsFull(old_ctrl[i])) {
2064
1.07M
                size_t hashval = PolicyTraits::apply(HashElement{hash_ref()},
2065
1.07M
                                                     PolicyTraits::element(old_slots + i));
2066
1.07M
                auto target = find_first_non_full(hashval);
2067
1.07M
                size_t new_i = target.offset;
2068
1.07M
                set_ctrl(new_i, H2(hashval));
2069
1.07M
                PolicyTraits::transfer(&alloc_ref(), slots_ + new_i, old_slots + i);
2070
1.07M
            }
2071
1.23M
        }
2072
2.70k
        if (old_capacity) {
2073
2.25k
            SanitizerUnpoisonMemoryRegion(old_slots,
2074
2.25k
                                          sizeof(slot_type) * old_capacity);
2075
2.25k
            auto layout = MakeLayout(old_capacity);
2076
2.25k
            Deallocate<Layout::Alignment()>(&alloc_ref(), old_ctrl,
2077
2.25k
                                            layout.AllocSize());
2078
2.25k
        }
2079
2.70k
    }
2080
2081
0
    void drop_deletes_without_resize() PHMAP_ATTRIBUTE_NOINLINE {
2082
0
        assert(IsValidCapacity(capacity_));
2083
0
        assert(!is_small());
2084
        // Algorithm:
2085
        // - mark all DELETED slots as EMPTY
2086
        // - mark all FULL slots as DELETED
2087
        // - for each slot marked as DELETED
2088
        //     hash = Hash(element)
2089
        //     target = find_first_non_full(hash)
2090
        //     if target is in the same group
2091
        //       mark slot as FULL
2092
        //     else if target is EMPTY
2093
        //       transfer element to target
2094
        //       mark slot as EMPTY
2095
        //       mark target as FULL
2096
        //     else if target is DELETED
2097
        //       swap current element with target element
2098
        //       mark target as FULL
2099
        //       repeat procedure for current slot with moved from element (target)
2100
0
        ConvertDeletedToEmptyAndFullToDeleted(ctrl_, capacity_);
2101
0
        typename phmap::aligned_storage<sizeof(slot_type), alignof(slot_type)>::type
2102
0
            raw;
2103
0
        slot_type* slot = reinterpret_cast<slot_type*>(&raw);
2104
0
        for (size_t i = 0; i != capacity_; ++i) {
2105
0
            if (!IsDeleted(ctrl_[i])) continue;
2106
0
            size_t hashval = PolicyTraits::apply(HashElement{hash_ref()},
2107
0
                                                 PolicyTraits::element(slots_ + i));
2108
0
            auto target = find_first_non_full(hashval);
2109
0
            size_t new_i = target.offset;
2110
2111
            // Verify if the old and new i fall within the same group wrt the hashval.
2112
            // If they do, we don't need to move the object as it falls already in the
2113
            // best probe we can.
2114
0
            const auto probe_index = [&](size_t pos) {
2115
0
                return ((pos - probe(hashval).offset()) & capacity_) / Group::kWidth;
2116
0
            };
2117
2118
            // Element doesn't move.
2119
0
            if (PHMAP_PREDICT_TRUE(probe_index(new_i) == probe_index(i))) {
2120
0
                set_ctrl(i, H2(hashval));
2121
0
                continue;
2122
0
            }
2123
0
            if (IsEmpty(ctrl_[new_i])) {
2124
                // Transfer element to the empty spot.
2125
                // set_ctrl poisons/unpoisons the slots so we have to call it at the
2126
                // right time.
2127
0
                set_ctrl(new_i, H2(hashval));
2128
0
                PolicyTraits::transfer(&alloc_ref(), slots_ + new_i, slots_ + i);
2129
0
                set_ctrl(i, kEmpty);
2130
0
            } else {
2131
0
                assert(IsDeleted(ctrl_[new_i]));
2132
0
                set_ctrl(new_i, H2(hashval));
2133
                // Until we are done rehashing, DELETED marks previously FULL slots.
2134
                // Swap i and new_i elements.
2135
0
                PolicyTraits::transfer(&alloc_ref(), slot, slots_ + i);
2136
0
                PolicyTraits::transfer(&alloc_ref(), slots_ + i, slots_ + new_i);
2137
0
                PolicyTraits::transfer(&alloc_ref(), slots_ + new_i, slot);
2138
0
                --i;  // repeat
2139
0
            }
2140
0
        }
2141
0
        reset_growth_left(capacity_);
2142
0
    }
2143
2144
2.70k
    void rehash_and_grow_if_necessary() {
2145
2.70k
        if (capacity_ == 0) {
2146
451
            resize(1);
2147
2.25k
        } else if (size() <= CapacityToGrowth(capacity()) / 2) {
2148
            // Squash DELETED without growing if there is enough capacity.
2149
0
            drop_deletes_without_resize();
2150
2.25k
        } else {
2151
            // Otherwise grow the container.
2152
2.25k
            resize(capacity_ * 2 + 1);
2153
2.25k
        }
2154
2.70k
    }
2155
2156
601k
    bool has_element(const value_type& PHMAP_RESTRICT elem, size_t hashval) const {
2157
601k
        PHMAP_IF_CONSTEXPR (!std_alloc_t::value) {
2158
            // ctrl_ could be nullptr
2159
            if (!ctrl_)
2160
                return false;
2161
        }
2162
601k
        auto seq = probe(hashval);
2163
605k
        while (true) {
2164
605k
            Group g{ctrl_ + seq.offset()};
2165
605k
            for (uint32_t i : g.Match((h2_t)H2(hashval))) {
2166
604k
                if (PHMAP_PREDICT_TRUE(PolicyTraits::element(slots_ + seq.offset((size_t)i)) ==
2167
604k
                                      elem))
2168
601k
                    return true;
2169
604k
            }
2170
3.61k
            if (PHMAP_PREDICT_TRUE(g.MatchEmpty())) return false;
2171
3.49k
            seq.next();
2172
3.49k
            assert(seq.getindex() < capacity_ && "full table!");
2173
3.49k
        }
2174
0
        return false;
2175
601k
    }
2176
2177
601k
    bool has_element(const value_type& elem) const {
2178
601k
        size_t hashval = PolicyTraits::apply(HashElement{hash_ref()}, elem);
2179
601k
        return has_element(elem, hashval);
2180
601k
    }
2181
2182
    // Probes the raw_hash_set with the probe sequence for hash and returns the
2183
    // pointer to the first empty or deleted slot.
2184
    // NOTE: this function must work with tables having both kEmpty and kDelete
2185
    // in one group. Such tables appears during drop_deletes_without_resize.
2186
    //
2187
    // This function is very useful when insertions happen and:
2188
    // - the input is already a set
2189
    // - there are enough slots
2190
    // - the element with the hash is not in the table
2191
    struct FindInfo 
2192
    {
2193
        size_t offset;
2194
        size_t probe_length;
2195
    };
2196
1.69M
    FindInfo find_first_non_full(size_t hashval) {
2197
1.69M
        auto seq = probe(hashval);
2198
1.78M
        while (true) {
2199
1.78M
            Group g{ctrl_ + seq.offset()};
2200
1.78M
            auto mask = g.MatchEmptyOrDeleted();
2201
1.78M
            if (mask) {
2202
1.69M
                return {seq.offset((size_t)mask.LowestBitSet()), seq.getindex()};
2203
1.69M
            }
2204
92.2k
            assert(seq.getindex() < capacity_ && "full table!");
2205
92.2k
            seq.next();
2206
92.2k
        }
2207
1.69M
    }
2208
2209
    // TODO(alkis): Optimize this assuming *this and that don't overlap.
2210
    raw_hash_set& move_assign(raw_hash_set&& that, std::true_type) {
2211
        raw_hash_set tmp(std::move(that));
2212
        swap(tmp);
2213
        return *this;
2214
    }
2215
    raw_hash_set& move_assign(raw_hash_set&& that, std::false_type) {
2216
        raw_hash_set tmp(std::move(that), alloc_ref());
2217
        swap(tmp);
2218
        return *this;
2219
    }
2220
2221
protected:
2222
    template <class K>
2223
8.52M
    size_t _find_key(const K& PHMAP_RESTRICT key, size_t hashval) {
2224
8.52M
        PHMAP_IF_CONSTEXPR (!std_alloc_t::value) {
2225
            // ctrl_ could be nullptr
2226
            if (!ctrl_)
2227
                return (size_t)-1;
2228
        }
2229
8.52M
        auto seq = probe(hashval);
2230
9.52M
        while (true) {
2231
9.52M
            Group g{ctrl_ + seq.offset()};
2232
10.1M
            for (uint32_t i : g.Match((h2_t)H2(hashval))) {
2233
10.1M
                if (PHMAP_PREDICT_TRUE(PolicyTraits::apply(
2234
10.1M
                                          EqualElement<K>{key, eq_ref()},
2235
10.1M
                                          PolicyTraits::element(slots_ + seq.offset((size_t)i)))))
2236
7.91M
                    return seq.offset((size_t)i);
2237
10.1M
            }
2238
1.61M
            if (PHMAP_PREDICT_TRUE(g.MatchEmpty())) break;
2239
1.00M
            seq.next();
2240
1.00M
        }
2241
614k
        return (size_t)-1;
2242
8.52M
    }
2243
2244
    template <class K>
2245
    std::pair<size_t, bool> find_or_prepare_insert(const K& key, size_t hashval) {
2246
        size_t offset = _find_key(key, hashval);
2247
        if (offset == (size_t)-1)
2248
            return {prepare_insert(hashval), true};
2249
        return {offset, false};
2250
    }
2251
2252
614k
    size_t prepare_insert(size_t hashval) PHMAP_ATTRIBUTE_NOINLINE {
2253
614k
        PHMAP_IF_CONSTEXPR (!std_alloc_t::value) {
2254
            // ctrl_ could be nullptr
2255
            if (!ctrl_)
2256
                rehash_and_grow_if_necessary();
2257
        }
2258
614k
        FindInfo target = find_first_non_full(hashval);
2259
614k
        if (PHMAP_PREDICT_FALSE(growth_left() == 0 &&
2260
614k
                               !IsDeleted(ctrl_[target.offset]))) {
2261
2.70k
            rehash_and_grow_if_necessary();
2262
2.70k
            target = find_first_non_full(hashval);
2263
2.70k
        }
2264
614k
        ++size_;
2265
614k
        growth_left() -= IsEmpty(ctrl_[target.offset]);
2266
        // set_ctrl(target.offset, H2(hashval));
2267
614k
        infoz_.RecordInsert(hashval, target.probe_length);
2268
614k
        return target.offset;
2269
614k
    }
2270
2271
    // Constructs the value in the space pointed by the iterator. This only works
2272
    // after an unsuccessful find_or_prepare_insert() and before any other
2273
    // modifications happen in the raw_hash_set.
2274
    //
2275
    // PRECONDITION: i is an index returned from find_or_prepare_insert(k), where
2276
    // k is the key decomposed from `forward<Args>(args)...`, and the bool
2277
    // returned by find_or_prepare_insert(k) was true.
2278
    // POSTCONDITION: *m.iterator_at(i) == value_type(forward<Args>(args)...).
2279
    template <class... Args>
2280
614k
    void emplace_at(size_t i, Args&&... args) {
2281
614k
        PolicyTraits::construct(&alloc_ref(), slots_ + i,
2282
614k
                                std::forward<Args>(args)...);
2283
        
2284
#ifdef PHMAP_CHECK_CONSTRUCTED_VALUE
2285
        // this check can be costly, so do it only when requested
2286
        assert(PolicyTraits::apply(FindElement{*this}, *iterator_at(i)) ==
2287
               iterator_at(i) &&
2288
               "constructed value does not match the lookup key");
2289
#endif
2290
614k
    }
2291
2292
8.52M
    iterator iterator_at(size_t i) { return {ctrl_ + i, slots_ + i}; }
phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<unsigned int, int>, phmap::Hash<unsigned int>, phmap::EqualTo<unsigned int>, std::__1::allocator<std::__1::pair<unsigned int const, int> > >::iterator_at(unsigned long)
Line
Count
Source
2292
8.52M
    iterator iterator_at(size_t i) { return {ctrl_ + i, slots_ + i}; }
phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, phmap::priv::StringHashEqT<char>::Hash, phmap::priv::StringHashEqT<char>::Eq, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >::iterator_at(unsigned long)
Line
Count
Source
2292
483
    iterator iterator_at(size_t i) { return {ctrl_ + i, slots_ + i}; }
2293
    const_iterator iterator_at(size_t i) const { return {ctrl_ + i, slots_ + i}; }
2294
2295
protected:
2296
    // Sets the control byte, and if `i < Group::kWidth`, set the cloned byte at
2297
    // the end too.
2298
1.69M
    void set_ctrl(size_t i, ctrl_t h) {
2299
1.69M
        assert(i < capacity_);
2300
2301
1.69M
        if (IsFull(h)) {
2302
1.69M
            SanitizerUnpoisonObject(slots_ + i);
2303
1.69M
        } else {
2304
0
            SanitizerPoisonObject(slots_ + i);
2305
0
        }
2306
2307
1.69M
        ctrl_[i] = h;
2308
1.69M
        ctrl_[((i - Group::kWidth) & capacity_) + 1 +
2309
1.69M
              ((Group::kWidth - 1) & capacity_)] = h;
2310
1.69M
    }
2311
2312
private:
2313
    friend struct RawHashSetTestOnlyAccess;
2314
2315
10.8M
    probe_seq<Group::kWidth> probe(size_t hashval) const {
2316
10.8M
        return probe_seq<Group::kWidth>(H1(hashval, ctrl_), capacity_);
2317
10.8M
    }
2318
2319
    // Reset all ctrl bytes back to kEmpty, except the sentinel.
2320
2.82k
    void reset_ctrl(size_t new_capacity) {
2321
2.82k
        std::memset(ctrl_, kEmpty, new_capacity + Group::kWidth);
2322
2.82k
        ctrl_[new_capacity] = kSentinel;
2323
2.82k
        SanitizerPoisonMemoryRegion(slots_, sizeof(slot_type) * new_capacity);
2324
2.82k
    }
2325
2326
2.82k
    void reset_growth_left(size_t new_capacity) {
2327
2.82k
        growth_left() = CapacityToGrowth(new_capacity) - size_;
2328
2.82k
    }
2329
2330
1.23M
    size_t& growth_left() { return std::get<0>(settings_); }
phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<unsigned int, int>, phmap::Hash<unsigned int>, phmap::EqualTo<unsigned int>, std::__1::allocator<std::__1::pair<unsigned int const, int> > >::growth_left()
Line
Count
Source
2330
1.23M
    size_t& growth_left() { return std::get<0>(settings_); }
Unexecuted instantiation: phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, phmap::priv::StringHashEqT<char>::Hash, phmap::priv::StringHashEqT<char>::Eq, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >::growth_left()
2331
2332
451
    const size_t& growth_left() const { return std::get<0>(settings_); }
2333
2334
    template <size_t N,
2335
              template <class, class, class, class> class RefSet,
2336
              class M, class P, class H, class E, class A>
2337
    friend class parallel_hash_set;
2338
2339
    template <size_t N,
2340
              template <class, class, class, class> class RefSet,
2341
              class M, class P, class H, class E, class A>
2342
    friend class parallel_hash_map;
2343
2344
    // The representation of the object has two modes:
2345
    //  - small: For capacities < kWidth-1
2346
    //  - large: For the rest.
2347
    //
2348
    // Differences:
2349
    //  - In small mode we are able to use the whole capacity. The extra control
2350
    //  bytes give us at least one "empty" control byte to stop the iteration.
2351
    //  This is important to make 1 a valid capacity.
2352
    //
2353
    //  - In small mode only the first `capacity()` control bytes after the
2354
    //  sentinel are valid. The rest contain dummy kEmpty values that do not
2355
    //  represent a real slot. This is important to take into account on
2356
    //  find_first_non_full(), where we never try ShouldInsertBackwards() for
2357
    //  small tables.
2358
0
    bool is_small() const { return capacity_ < Group::kWidth - 1; }
2359
2360
1.07M
    hasher& hash_ref() { return std::get<1>(settings_); }
2361
9.12M
    const hasher& hash_ref() const { return std::get<1>(settings_); }
2362
10.1M
    key_equal& eq_ref() { return std::get<2>(settings_); }
2363
    const key_equal& eq_ref() const { return std::get<2>(settings_); }
2364
1.69M
    allocator_type& alloc_ref() { return std::get<3>(settings_); }
phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<unsigned int, int>, phmap::Hash<unsigned int>, phmap::EqualTo<unsigned int>, std::__1::allocator<std::__1::pair<unsigned int const, int> > >::alloc_ref()
Line
Count
Source
2364
1.69M
    allocator_type& alloc_ref() { return std::get<3>(settings_); }
Unexecuted instantiation: phmap::priv::raw_hash_set<phmap::priv::FlatHashMapPolicy<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, phmap::priv::StringHashEqT<char>::Hash, phmap::priv::StringHashEqT<char>::Eq, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >::alloc_ref()
2365
    const allocator_type& alloc_ref() const {
2366
        return std::get<3>(settings_);
2367
    }
2368
2369
    // TODO(alkis): Investigate removing some of these fields:
2370
    // - ctrl/slots can be derived from each other
2371
    // - size can be moved into the slot array
2372
    ctrl_t* ctrl_ = EmptyGroup<std_alloc_t>();    // [(capacity + 1) * ctrl_t]
2373
    slot_type* slots_ = nullptr;                  // [capacity * slot_type]
2374
    size_t size_ = 0;                             // number of full slots
2375
    size_t capacity_ = 0;                         // total number of slots
2376
    HashtablezInfoHandle infoz_;
2377
    std::tuple<size_t /* growth_left */, hasher, key_equal, allocator_type>
2378
        settings_{0, hasher{}, key_equal{}, allocator_type{}};
2379
};
2380
2381
2382
// --------------------------------------------------------------------------
2383
// --------------------------------------------------------------------------
2384
template <class Policy, class Hash, class Eq, class Alloc>
2385
class raw_hash_map : public raw_hash_set<Policy, Hash, Eq, Alloc> 
2386
{
2387
    // P is Policy. It's passed as a template argument to support maps that have
2388
    // incomplete types as values, as in unordered_map<K, IncompleteType>.
2389
    // MappedReference<> may be a non-reference type.
2390
    template <class P>
2391
    using MappedReference = decltype(P::value(
2392
               std::addressof(std::declval<typename raw_hash_map::reference>())));
2393
2394
    // MappedConstReference<> may be a non-reference type.
2395
    template <class P>
2396
    using MappedConstReference = decltype(P::value(
2397
               std::addressof(std::declval<typename raw_hash_map::const_reference>())));
2398
2399
    using KeyArgImpl =
2400
        KeyArg<IsTransparent<Eq>::value && IsTransparent<Hash>::value>;
2401
2402
    using Base = raw_hash_set<Policy, Hash, Eq, Alloc>;
2403
2404
public:
2405
    using key_type = typename Policy::key_type;
2406
    using mapped_type = typename Policy::mapped_type;
2407
    template <class K>
2408
    using key_arg = typename KeyArgImpl::template type<K, key_type>;
2409
2410
    static_assert(!std::is_reference<key_type>::value, "");
2411
2412
    // TODO(b/187807849): Evaluate whether to support reference mapped_type and
2413
    // remove this assertion if/when it is supported.
2414
     static_assert(!std::is_reference<mapped_type>::value, "");
2415
2416
    using iterator = typename raw_hash_map::raw_hash_set::iterator;
2417
    using const_iterator = typename raw_hash_map::raw_hash_set::const_iterator;
2418
2419
1.44k
    raw_hash_map() {}
phmap::priv::raw_hash_map<phmap::priv::FlatHashMapPolicy<unsigned int, int>, phmap::Hash<unsigned int>, phmap::EqualTo<unsigned int>, std::__1::allocator<std::__1::pair<unsigned int const, int> > >::raw_hash_map()
Line
Count
Source
2419
966
    raw_hash_map() {}
phmap::priv::raw_hash_map<phmap::priv::FlatHashMapPolicy<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, phmap::priv::StringHashEqT<char>::Hash, phmap::priv::StringHashEqT<char>::Eq, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >::raw_hash_map()
Line
Count
Source
2419
483
    raw_hash_map() {}
2420
    using Base::raw_hash_set; // use raw_hash_set constructor  
2421
2422
    // The last two template parameters ensure that both arguments are rvalues
2423
    // (lvalue arguments are handled by the overloads below). This is necessary
2424
    // for supporting bitfield arguments.
2425
    //
2426
    //   union { int n : 1; };
2427
    //   flat_hash_map<int, int> m;
2428
    //   m.insert_or_assign(n, n);
2429
    template <class K = key_type, class V = mapped_type, K* = nullptr,
2430
              V* = nullptr>
2431
    std::pair<iterator, bool> insert_or_assign(key_arg<K>&& k, V&& v) {
2432
        return insert_or_assign_impl(std::forward<K>(k), std::forward<V>(v));
2433
    }
2434
2435
    template <class K = key_type, class V = mapped_type, K* = nullptr>
2436
    std::pair<iterator, bool> insert_or_assign(key_arg<K>&& k, const V& v) {
2437
        return insert_or_assign_impl(std::forward<K>(k), v);
2438
    }
2439
2440
    template <class K = key_type, class V = mapped_type, V* = nullptr>
2441
    std::pair<iterator, bool> insert_or_assign(const key_arg<K>& k, V&& v) {
2442
        return insert_or_assign_impl(k, std::forward<V>(v));
2443
    }
2444
2445
    template <class K = key_type, class V = mapped_type>
2446
    std::pair<iterator, bool> insert_or_assign(const key_arg<K>& k, const V& v) {
2447
        return insert_or_assign_impl(k, v);
2448
    }
2449
2450
    template <class K = key_type, class V = mapped_type, K* = nullptr,
2451
              V* = nullptr>
2452
    iterator insert_or_assign(const_iterator, key_arg<K>&& k, V&& v) {
2453
        return insert_or_assign(std::forward<K>(k), std::forward<V>(v)).first;
2454
    }
2455
2456
    template <class K = key_type, class V = mapped_type, K* = nullptr>
2457
    iterator insert_or_assign(const_iterator, key_arg<K>&& k, const V& v) {
2458
        return insert_or_assign(std::forward<K>(k), v).first;
2459
    }
2460
2461
    template <class K = key_type, class V = mapped_type, V* = nullptr>
2462
    iterator insert_or_assign(const_iterator, const key_arg<K>& k, V&& v) {
2463
        return insert_or_assign(k, std::forward<V>(v)).first;
2464
    }
2465
2466
    template <class K = key_type, class V = mapped_type>
2467
    iterator insert_or_assign(const_iterator, const key_arg<K>& k, const V& v) {
2468
        return insert_or_assign(k, v).first;
2469
    }
2470
2471
    template <class K = key_type, class... Args,
2472
              typename std::enable_if<
2473
                  !std::is_convertible<K, const_iterator>::value, int>::type = 0,
2474
              K* = nullptr>
2475
    std::pair<iterator, bool> try_emplace(key_arg<K>&& k, Args&&... args) {
2476
        return try_emplace_impl(std::forward<K>(k), std::forward<Args>(args)...);
2477
    }
2478
2479
    template <class K = key_type, class... Args,
2480
              typename std::enable_if<
2481
                  !std::is_convertible<K, const_iterator>::value, int>::type = 0>
2482
    std::pair<iterator, bool> try_emplace(const key_arg<K>& k, Args&&... args) {
2483
        return try_emplace_impl(k, std::forward<Args>(args)...);
2484
    }
2485
2486
    template <class K = key_type, class... Args, K* = nullptr>
2487
    iterator try_emplace(const_iterator, key_arg<K>&& k, Args&&... args) {
2488
        return try_emplace(std::forward<K>(k), std::forward<Args>(args)...).first;
2489
    }
2490
2491
    template <class K = key_type, class... Args>
2492
    iterator try_emplace(const_iterator, const key_arg<K>& k, Args&&... args) {
2493
        return try_emplace(k, std::forward<Args>(args)...).first;
2494
    }
2495
2496
    template <class K = key_type, class P = Policy>
2497
    MappedReference<P> at(const key_arg<K>& key) {
2498
        auto it = this->find(key);
2499
        if (it == this->end()) 
2500
            phmap::base_internal::ThrowStdOutOfRange("phmap at(): lookup non-existent key");
2501
        return Policy::value(&*it);
2502
    }
2503
2504
    template <class K = key_type, class P = Policy>
2505
    MappedConstReference<P> at(const key_arg<K>& key) const {
2506
        auto it = this->find(key);
2507
        if (it == this->end())
2508
            phmap::base_internal::ThrowStdOutOfRange("phmap at(): lookup non-existent key");
2509
        return Policy::value(&*it);
2510
    }
2511
2512
    template <class K = key_type, class P = Policy, K* = nullptr>
2513
    MappedReference<P> operator[](key_arg<K>&& key) {
2514
        return Policy::value(&*try_emplace(std::forward<K>(key)).first);
2515
    }
2516
2517
    template <class K = key_type, class P = Policy>
2518
    MappedReference<P> operator[](const key_arg<K>& key) {
2519
        return Policy::value(&*try_emplace(key).first);
2520
    }
2521
2522
private:
2523
    template <class K, class V>
2524
    std::pair<iterator, bool> insert_or_assign_impl(K&& k, V&& v) {
2525
        size_t hashval = this->hash(k);
2526
        size_t offset = this->_find_key(k, hashval);
2527
        if (offset == (size_t)-1) {
2528
            offset = this->prepare_insert(hashval);
2529
            this->emplace_at(offset, std::forward<K>(k), std::forward<V>(v));
2530
            this->set_ctrl(offset, H2(hashval));
2531
            return {this->iterator_at(offset), true};
2532
        } 
2533
        Policy::value(&*this->iterator_at(offset)) = std::forward<V>(v);
2534
        return {this->iterator_at(offset), false};
2535
    }
2536
2537
    template <class K = key_type, class... Args>
2538
    std::pair<iterator, bool> try_emplace_impl(K&& k, Args&&... args) {
2539
        size_t hashval = this->hash(k);
2540
        size_t offset = this->_find_key(k, hashval);
2541
        if (offset == (size_t)-1) {
2542
            offset = this->prepare_insert(hashval);
2543
            this->emplace_at(offset, std::piecewise_construct,
2544
                             std::forward_as_tuple(std::forward<K>(k)),
2545
                             std::forward_as_tuple(std::forward<Args>(args)...));
2546
            this->set_ctrl(offset, H2(hashval));
2547
            return {this->iterator_at(offset), true};
2548
        }
2549
        return {this->iterator_at(offset), false};
2550
    }
2551
};
2552
2553
// ----------------------------------------------------------------------------
2554
// ----------------------------------------------------------------------------
2555
// Returns "random" seed.
2556
inline size_t RandomSeed() 
2557
0
{
2558
0
#if PHMAP_HAVE_THREAD_LOCAL
2559
0
    static thread_local size_t counter = 0;
2560
0
    size_t value = ++counter;
2561
0
#else   // PHMAP_HAVE_THREAD_LOCAL
2562
0
    static std::atomic<size_t> counter(0);
2563
0
    size_t value = counter.fetch_add(1, std::memory_order_relaxed);
2564
0
#endif  // PHMAP_HAVE_THREAD_LOCAL
2565
0
    return value ^ static_cast<size_t>(reinterpret_cast<uintptr_t>(&counter));
2566
0
}
2567
2568
// ----------------------------------------------------------------------------
2569
// ----------------------------------------------------------------------------
2570
template <size_t N,
2571
          template <class, class, class, class> class RefSet,
2572
          class Mtx_,
2573
          class Policy, class Hash, class Eq, class Alloc>
2574
class parallel_hash_set 
2575
{
2576
    using PolicyTraits = hash_policy_traits<Policy>;
2577
    using KeyArgImpl =
2578
        KeyArg<IsTransparent<Eq>::value && IsTransparent<Hash>::value>;
2579
2580
    static_assert(N <= 12, "N = 12 means 4096 hash tables!");
2581
    constexpr static size_t num_tables = 1 << N;
2582
    constexpr static size_t mask = num_tables - 1;
2583
2584
public:
2585
    using EmbeddedSet     = RefSet<Policy, Hash, Eq, Alloc>;
2586
    using EmbeddedIterator= typename EmbeddedSet::iterator;
2587
    using EmbeddedConstIterator= typename EmbeddedSet::const_iterator;
2588
    using constructor     = typename EmbeddedSet::constructor;
2589
    using init_type       = typename PolicyTraits::init_type;
2590
    using key_type        = typename PolicyTraits::key_type;
2591
    using slot_type       = typename PolicyTraits::slot_type;
2592
    using allocator_type  = Alloc;
2593
    using size_type       = size_t;
2594
    using difference_type = ptrdiff_t;
2595
    using hasher          = Hash;
2596
    using key_equal       = Eq;
2597
    using policy_type     = Policy;
2598
    using value_type      = typename PolicyTraits::value_type;
2599
    using reference       = value_type&;
2600
    using const_reference = const value_type&;
2601
    using pointer         = typename phmap::allocator_traits<
2602
        allocator_type>::template rebind_traits<value_type>::pointer;
2603
    using const_pointer   = typename phmap::allocator_traits<
2604
        allocator_type>::template rebind_traits<value_type>::const_pointer;
2605
2606
    // Alias used for heterogeneous lookup functions.
2607
    // `key_arg<K>` evaluates to `K` when the functors are transparent and to
2608
    // `key_type` otherwise. It permits template argument deduction on `K` for the
2609
    // transparent case.
2610
    // --------------------------------------------------------------------
2611
    template <class K>
2612
    using key_arg         = typename KeyArgImpl::template type<K, key_type>;
2613
2614
protected:
2615
    using Lockable      = phmap::LockableImpl<Mtx_>;
2616
    using UniqueLock    = typename Lockable::UniqueLock;
2617
    using SharedLock    = typename Lockable::SharedLock;
2618
    using ReadWriteLock = typename Lockable::ReadWriteLock;
2619
2620
    // --------------------------------------------------------------------
2621
    struct Inner : public Lockable
2622
    {
2623
        struct Params
2624
        {
2625
            size_t bucket_cnt;
2626
            const hasher& hashfn;
2627
            const key_equal& eq;
2628
            const allocator_type& alloc;
2629
        };
2630
2631
        Inner() {}
2632
2633
        Inner(Params const &p) : set_(p.bucket_cnt, p.hashfn, p.eq, p.alloc)
2634
        {}
2635
2636
        bool operator==(const Inner& o) const
2637
        {
2638
            typename Lockable::SharedLocks l(const_cast<Inner &>(*this), const_cast<Inner &>(o));
2639
            return set_ == o.set_;
2640
        }
2641
2642
        EmbeddedSet set_;
2643
    };
2644
2645
private:
2646
    // Give an early error when key_type is not hashable/eq.
2647
    // --------------------------------------------------------------------
2648
    auto KeyTypeCanBeHashed(const Hash& h, const key_type& k) -> decltype(h(k));
2649
    auto KeyTypeCanBeEq(const Eq& eq, const key_type& k)      -> decltype(eq(k, k));
2650
2651
    using AllocTraits     = phmap::allocator_traits<allocator_type>;
2652
2653
    static_assert(std::is_lvalue_reference<reference>::value,
2654
                  "Policy::element() must return a reference");
2655
2656
    template <typename T>
2657
    struct SameAsElementReference : std::is_same<
2658
        typename std::remove_cv<typename std::remove_reference<reference>::type>::type,
2659
        typename std::remove_cv<typename std::remove_reference<T>::type>::type> {};
2660
2661
    // An enabler for insert(T&&): T must be convertible to init_type or be the
2662
    // same as [cv] value_type [ref].
2663
    // Note: we separate SameAsElementReference into its own type to avoid using
2664
    // reference unless we need to. MSVC doesn't seem to like it in some
2665
    // cases.
2666
    // --------------------------------------------------------------------
2667
    template <class T>
2668
    using RequiresInsertable = typename std::enable_if<
2669
        phmap::disjunction<std::is_convertible<T, init_type>, SameAsElementReference<T>>::value, int>::type;
2670
2671
    // RequiresNotInit is a workaround for gcc prior to 7.1.
2672
    // See https://godbolt.org/g/Y4xsUh.
2673
    template <class T>
2674
    using RequiresNotInit =
2675
        typename std::enable_if<!std::is_same<T, init_type>::value, int>::type;
2676
2677
    template <class... Ts>
2678
    using IsDecomposable = IsDecomposable<void, PolicyTraits, Hash, Eq, Ts...>;
2679
2680
public:
2681
    static_assert(std::is_same<pointer, value_type*>::value,
2682
                  "Allocators with custom pointer types are not supported");
2683
    static_assert(std::is_same<const_pointer, const value_type*>::value,
2684
                  "Allocators with custom pointer types are not supported");
2685
2686
    // --------------------- i t e r a t o r ------------------------------
2687
    class iterator 
2688
    {
2689
        friend class parallel_hash_set;
2690
2691
    public:
2692
        using iterator_category = std::forward_iterator_tag;
2693
        using value_type        = typename parallel_hash_set::value_type;
2694
        using reference         =
2695
            phmap::conditional_t<PolicyTraits::constant_iterators::value,
2696
                                const value_type&, value_type&>;
2697
        using pointer           = phmap::remove_reference_t<reference>*;
2698
        using difference_type   = typename parallel_hash_set::difference_type;
2699
        using Inner             = typename parallel_hash_set::Inner;
2700
        using EmbeddedSet       = typename parallel_hash_set::EmbeddedSet;
2701
        using EmbeddedIterator  = typename EmbeddedSet::iterator;
2702
2703
        iterator() {}
2704
2705
        reference operator*()  const { return *it_; }
2706
        pointer   operator->() const { return &operator*(); }
2707
2708
        iterator& operator++() {
2709
            assert(inner_); // null inner means we are already at the end
2710
            ++it_;
2711
            skip_empty();
2712
            return *this;
2713
        }
2714
    
2715
        iterator operator++(int) {
2716
            assert(inner_);  // null inner means we are already at the end
2717
            auto tmp = *this;
2718
            ++*this;
2719
            return tmp;
2720
        }
2721
2722
        friend bool operator==(const iterator& a, const iterator& b) {
2723
            return a.inner_ == b.inner_ && (!a.inner_ || a.it_ == b.it_);
2724
        }
2725
2726
        friend bool operator!=(const iterator& a, const iterator& b) {
2727
            return !(a == b);
2728
        }
2729
2730
    private:
2731
        iterator(Inner *inner, Inner *inner_end, const EmbeddedIterator& it) : 
2732
            inner_(inner), inner_end_(inner_end), it_(it)  {  // for begin() and end()
2733
            if (inner)
2734
                it_end_ = inner->set_.end();
2735
        }
2736
2737
        void skip_empty() {
2738
            while (it_ == it_end_) {
2739
                ++inner_;
2740
                if (inner_ == inner_end_) {
2741
                    inner_ = nullptr; // marks end()
2742
                    break;
2743
                }
2744
                else {
2745
                    it_ = inner_->set_.begin();
2746
                    it_end_ = inner_->set_.end();
2747
                }
2748
            }
2749
        }
2750
2751
        Inner *inner_      = nullptr;
2752
        Inner *inner_end_  = nullptr;
2753
        EmbeddedIterator it_, it_end_;
2754
    };
2755
2756
    // --------------------- c o n s t   i t e r a t o r -----------------
2757
    class const_iterator 
2758
    {
2759
        friend class parallel_hash_set;
2760
2761
    public:
2762
        using iterator_category = typename iterator::iterator_category;
2763
        using value_type        = typename parallel_hash_set::value_type;
2764
        using reference         = typename parallel_hash_set::const_reference;
2765
        using pointer           = typename parallel_hash_set::const_pointer;
2766
        using difference_type   = typename parallel_hash_set::difference_type;
2767
        using Inner             = typename parallel_hash_set::Inner;
2768
2769
        const_iterator() {}
2770
        // Implicit construction from iterator.
2771
        const_iterator(iterator i) : iter_(std::move(i)) {}
2772
2773
        reference operator*()  const { return *(iter_); }
2774
        pointer   operator->() const { return iter_.operator->(); }
2775
2776
        const_iterator& operator++() {
2777
            ++iter_;
2778
            return *this;
2779
        }
2780
        const_iterator operator++(int) { return iter_++; }
2781
2782
        friend bool operator==(const const_iterator& a, const const_iterator& b) {
2783
            return a.iter_ == b.iter_;
2784
        }
2785
        friend bool operator!=(const const_iterator& a, const const_iterator& b) {
2786
            return !(a == b);
2787
        }
2788
2789
    private:
2790
        const_iterator(const Inner *inner, const Inner *inner_end, const EmbeddedIterator& it)
2791
            : iter_(const_cast<Inner**>(inner), 
2792
                    const_cast<Inner**>(inner_end),
2793
                    const_cast<EmbeddedIterator*>(it)) {}
2794
2795
        iterator iter_;
2796
    };
2797
2798
    using node_type = node_handle<Policy, hash_policy_traits<Policy>, Alloc>;
2799
    using insert_return_type = InsertReturnType<iterator, node_type>;
2800
2801
    // ------------------------- c o n s t r u c t o r s ------------------
2802
2803
    parallel_hash_set() noexcept(
2804
        std::is_nothrow_default_constructible<hasher>::value&&
2805
        std::is_nothrow_default_constructible<key_equal>::value&&
2806
        std::is_nothrow_default_constructible<allocator_type>::value) {}
2807
2808
#if  (__cplusplus >= 201703L || _MSVC_LANG >= 201402) && (defined(_MSC_VER) || defined(__clang__) || (defined(__GNUC__) && __GNUC__ > 6))
2809
    explicit parallel_hash_set(size_t bucket_cnt, 
2810
                               const hasher& hash_param    = hasher(),
2811
                               const key_equal& eq         = key_equal(),
2812
                               const allocator_type& alloc = allocator_type()) :
2813
        parallel_hash_set(typename Inner::Params{bucket_cnt, hash_param, eq, alloc}, 
2814
                          phmap::make_index_sequence<num_tables>{})
2815
    {}
2816
2817
    template <std::size_t... i>
2818
    parallel_hash_set(typename Inner::Params const &p,
2819
                      phmap::index_sequence<i...>) : sets_{((void)i, p)...}
2820
    {}
2821
#else
2822
    explicit parallel_hash_set(size_t bucket_cnt, 
2823
                               const hasher& hash_param    = hasher(),
2824
                               const key_equal& eq         = key_equal(),
2825
                               const allocator_type& alloc = allocator_type()) {
2826
        for (auto& inner : sets_)
2827
            inner.set_ = EmbeddedSet(bucket_cnt / N, hash_param, eq, alloc);
2828
    }
2829
#endif
2830
2831
    parallel_hash_set(size_t bucket_cnt, 
2832
                      const hasher& hash_param,
2833
                      const allocator_type& alloc)
2834
        : parallel_hash_set(bucket_cnt, hash_param, key_equal(), alloc) {}
2835
2836
    parallel_hash_set(size_t bucket_cnt, const allocator_type& alloc)
2837
        : parallel_hash_set(bucket_cnt, hasher(), key_equal(), alloc) {}
2838
2839
    explicit parallel_hash_set(const allocator_type& alloc)
2840
        : parallel_hash_set(0, hasher(), key_equal(), alloc) {}
2841
2842
    template <class InputIter>
2843
    parallel_hash_set(InputIter first, InputIter last, size_t bucket_cnt = 0,
2844
                      const hasher& hash_param = hasher(), const key_equal& eq = key_equal(),
2845
                      const allocator_type& alloc = allocator_type())
2846
        : parallel_hash_set(bucket_cnt, hash_param, eq, alloc) {
2847
        insert(first, last);
2848
    }
2849
2850
    template <class InputIter>
2851
    parallel_hash_set(InputIter first, InputIter last, size_t bucket_cnt,
2852
                      const hasher& hash_param, const allocator_type& alloc)
2853
        : parallel_hash_set(first, last, bucket_cnt, hash_param, key_equal(), alloc) {}
2854
2855
    template <class InputIter>
2856
    parallel_hash_set(InputIter first, InputIter last, size_t bucket_cnt,
2857
                      const allocator_type& alloc)
2858
        : parallel_hash_set(first, last, bucket_cnt, hasher(), key_equal(), alloc) {}
2859
2860
    template <class InputIter>
2861
    parallel_hash_set(InputIter first, InputIter last, const allocator_type& alloc)
2862
        : parallel_hash_set(first, last, 0, hasher(), key_equal(), alloc) {}
2863
2864
    // Instead of accepting std::initializer_list<value_type> as the first
2865
    // argument like std::unordered_set<value_type> does, we have two overloads
2866
    // that accept std::initializer_list<T> and std::initializer_list<init_type>.
2867
    // This is advantageous for performance.
2868
    //
2869
    //   // Turns {"abc", "def"} into std::initializer_list<std::string>, then copies
2870
    //   // the strings into the set.
2871
    //   std::unordered_set<std::string> s = {"abc", "def"};
2872
    //
2873
    //   // Turns {"abc", "def"} into std::initializer_list<const char*>, then
2874
    //   // copies the strings into the set.
2875
    //   phmap::flat_hash_set<std::string> s = {"abc", "def"};
2876
    //
2877
    // The same trick is used in insert().
2878
    //
2879
    // The enabler is necessary to prevent this constructor from triggering where
2880
    // the copy constructor is meant to be called.
2881
    //
2882
    //   phmap::flat_hash_set<int> a, b{a};
2883
    //
2884
    // RequiresNotInit<T> is a workaround for gcc prior to 7.1.
2885
    // --------------------------------------------------------------------
2886
    template <class T, RequiresNotInit<T> = 0, RequiresInsertable<T> = 0>
2887
    parallel_hash_set(std::initializer_list<T> init, size_t bucket_cnt = 0,
2888
                      const hasher& hash_param = hasher(), const key_equal& eq = key_equal(),
2889
                      const allocator_type& alloc = allocator_type())
2890
        : parallel_hash_set(init.begin(), init.end(), bucket_cnt, hash_param, eq, alloc) {}
2891
2892
    parallel_hash_set(std::initializer_list<init_type> init, size_t bucket_cnt = 0,
2893
                      const hasher& hash_param = hasher(), const key_equal& eq = key_equal(),
2894
                      const allocator_type& alloc = allocator_type())
2895
        : parallel_hash_set(init.begin(), init.end(), bucket_cnt, hash_param, eq, alloc) {}
2896
2897
    template <class T, RequiresNotInit<T> = 0, RequiresInsertable<T> = 0>
2898
    parallel_hash_set(std::initializer_list<T> init, size_t bucket_cnt,
2899
                      const hasher& hash_param, const allocator_type& alloc)
2900
        : parallel_hash_set(init, bucket_cnt, hash_param, key_equal(), alloc) {}
2901
2902
    parallel_hash_set(std::initializer_list<init_type> init, size_t bucket_cnt,
2903
                      const hasher& hash_param, const allocator_type& alloc)
2904
        : parallel_hash_set(init, bucket_cnt, hash_param, key_equal(), alloc) {}
2905
2906
    template <class T, RequiresNotInit<T> = 0, RequiresInsertable<T> = 0>
2907
    parallel_hash_set(std::initializer_list<T> init, size_t bucket_cnt,
2908
                      const allocator_type& alloc)
2909
        : parallel_hash_set(init, bucket_cnt, hasher(), key_equal(), alloc) {}
2910
2911
    parallel_hash_set(std::initializer_list<init_type> init, size_t bucket_cnt,
2912
                      const allocator_type& alloc)
2913
        : parallel_hash_set(init, bucket_cnt, hasher(), key_equal(), alloc) {}
2914
2915
    template <class T, RequiresNotInit<T> = 0, RequiresInsertable<T> = 0>
2916
    parallel_hash_set(std::initializer_list<T> init, const allocator_type& alloc)
2917
        : parallel_hash_set(init, 0, hasher(), key_equal(), alloc) {}
2918
  
2919
    parallel_hash_set(std::initializer_list<init_type> init,
2920
                      const allocator_type& alloc)
2921
        : parallel_hash_set(init, 0, hasher(), key_equal(), alloc) {}
2922
2923
    parallel_hash_set(const parallel_hash_set& that)
2924
        : parallel_hash_set(that, AllocTraits::select_on_container_copy_construction(
2925
                                that.alloc_ref())) {}
2926
2927
    parallel_hash_set(const parallel_hash_set& that, const allocator_type& a)
2928
        : parallel_hash_set(0, that.hash_ref(), that.eq_ref(), a) {
2929
        for (size_t i=0; i<num_tables; ++i)
2930
            sets_[i].set_ = { that.sets_[i].set_, a };
2931
    }
2932
  
2933
    parallel_hash_set(parallel_hash_set&& that) noexcept(
2934
        std::is_nothrow_copy_constructible<hasher>::value&&
2935
        std::is_nothrow_copy_constructible<key_equal>::value&&
2936
        std::is_nothrow_copy_constructible<allocator_type>::value)
2937
        : parallel_hash_set(std::move(that), that.alloc_ref()) {
2938
    }
2939
2940
    parallel_hash_set(parallel_hash_set&& that, const allocator_type& a)
2941
    {
2942
        for (size_t i=0; i<num_tables; ++i)
2943
            sets_[i].set_ = { std::move(that.sets_[i]).set_, a };
2944
    }
2945
2946
    parallel_hash_set& operator=(const parallel_hash_set& that) {
2947
        for (size_t i=0; i<num_tables; ++i)
2948
            sets_[i].set_ = that.sets_[i].set_;
2949
        return *this;
2950
    }
2951
2952
    parallel_hash_set& operator=(parallel_hash_set&& that) noexcept(
2953
        phmap::allocator_traits<allocator_type>::is_always_equal::value &&
2954
        std::is_nothrow_move_assignable<hasher>::value &&
2955
        std::is_nothrow_move_assignable<key_equal>::value) {
2956
        for (size_t i=0; i<num_tables; ++i)
2957
            sets_[i].set_ = std::move(that.sets_[i].set_);
2958
        return *this;
2959
    }
2960
2961
    ~parallel_hash_set() {}
2962
2963
    iterator begin() {
2964
        auto it = iterator(&sets_[0], &sets_[0] + num_tables, sets_[0].set_.begin());
2965
        it.skip_empty();
2966
        return it;
2967
    }
2968
2969
    iterator       end()          { return iterator(); }
2970
    const_iterator begin()  const { return const_cast<parallel_hash_set *>(this)->begin(); }
2971
    const_iterator end()    const { return const_cast<parallel_hash_set *>(this)->end(); }
2972
    const_iterator cbegin() const { return begin(); }
2973
    const_iterator cend()   const { return end(); }
2974
2975
    bool empty() const { return !size(); }
2976
2977
    size_t size() const { 
2978
        size_t sz = 0;
2979
        for (const auto& inner : sets_)
2980
            sz += inner.set_.size();
2981
        return sz; 
2982
    }
2983
  
2984
    size_t capacity() const { 
2985
        size_t c = 0;
2986
        for (const auto& inner : sets_)
2987
            c += inner.set_.capacity();
2988
        return c; 
2989
    }
2990
2991
    size_t max_size() const { return (std::numeric_limits<size_t>::max)(); }
2992
2993
    PHMAP_ATTRIBUTE_REINITIALIZES void clear() {
2994
        for (auto& inner : sets_)
2995
        {
2996
            UniqueLock m(inner);
2997
            inner.set_.clear();
2998
        }
2999
    }
3000
3001
    // extension - clears only soecified submap
3002
    // ----------------------------------------
3003
    void clear(std::size_t submap_index) {
3004
        Inner& inner = sets_[submap_index];
3005
        UniqueLock m(inner);
3006
        inner.set_.clear();
3007
    }
3008
3009
    // This overload kicks in when the argument is an rvalue of insertable and
3010
    // decomposable type other than init_type.
3011
    //
3012
    //   flat_hash_map<std::string, int> m;
3013
    //   m.insert(std::make_pair("abc", 42));
3014
    // --------------------------------------------------------------------
3015
    template <class T, RequiresInsertable<T> = 0,
3016
              typename std::enable_if<IsDecomposable<T>::value, int>::type = 0,
3017
              T* = nullptr>
3018
    std::pair<iterator, bool> insert(T&& value) {
3019
        return emplace(std::forward<T>(value));
3020
    }
3021
3022
    // This overload kicks in when the argument is a bitfield or an lvalue of
3023
    // insertable and decomposable type.
3024
    //
3025
    //   union { int n : 1; };
3026
    //   flat_hash_set<int> s;
3027
    //   s.insert(n);
3028
    //
3029
    //   flat_hash_set<std::string> s;
3030
    //   const char* p = "hello";
3031
    //   s.insert(p);
3032
    //
3033
    // TODO(romanp): Once we stop supporting gcc 5.1 and below, replace
3034
    // RequiresInsertable<T> with RequiresInsertable<const T&>.
3035
    // We are hitting this bug: https://godbolt.org/g/1Vht4f.
3036
    // --------------------------------------------------------------------
3037
    template <
3038
        class T, RequiresInsertable<T> = 0,
3039
        typename std::enable_if<IsDecomposable<const T&>::value, int>::type = 0>
3040
    std::pair<iterator, bool> insert(const T& value) {
3041
        return emplace(value);
3042
    }
3043
3044
    // This overload kicks in when the argument is an rvalue of init_type. Its
3045
    // purpose is to handle brace-init-list arguments.
3046
    //
3047
    //   flat_hash_set<std::pair<std::string, int>> s;
3048
    //   s.insert({"abc", 42});
3049
    // --------------------------------------------------------------------
3050
    std::pair<iterator, bool> insert(init_type&& value) {
3051
        return emplace(std::move(value));
3052
    }
3053
3054
    template <class T, RequiresInsertable<T> = 0,
3055
              typename std::enable_if<IsDecomposable<T>::value, int>::type = 0,
3056
              T* = nullptr>
3057
    iterator insert(const_iterator, T&& value) {
3058
        return insert(std::forward<T>(value)).first;
3059
    }
3060
3061
    // TODO(romanp): Once we stop supporting gcc 5.1 and below, replace
3062
    // RequiresInsertable<T> with RequiresInsertable<const T&>.
3063
    // We are hitting this bug: https://godbolt.org/g/1Vht4f.
3064
    // --------------------------------------------------------------------
3065
    template <
3066
        class T, RequiresInsertable<T> = 0,
3067
        typename std::enable_if<IsDecomposable<const T&>::value, int>::type = 0>
3068
    iterator insert(const_iterator, const T& value) {
3069
        return insert(value).first;
3070
    }
3071
3072
    iterator insert(const_iterator, init_type&& value) {
3073
        return insert(std::move(value)).first;
3074
    }
3075
3076
    template <class InputIt>
3077
    void insert(InputIt first, InputIt last) {
3078
        for (; first != last; ++first) insert(*first);
3079
    }
3080
3081
    template <class T, RequiresNotInit<T> = 0, RequiresInsertable<const T&> = 0>
3082
    void insert(std::initializer_list<T> ilist) {
3083
        insert(ilist.begin(), ilist.end());
3084
    }
3085
3086
    void insert(std::initializer_list<init_type> ilist) {
3087
        insert(ilist.begin(), ilist.end());
3088
    }
3089
3090
    insert_return_type insert(node_type&& node) {
3091
        if (!node) 
3092
            return {end(), false, node_type()};
3093
        auto& key      = node.key();
3094
        size_t hashval = this->hash(key);
3095
        Inner& inner   = sets_[subidx(hashval)];
3096
        auto&  set     = inner.set_;
3097
3098
        UniqueLock m(inner);
3099
        auto   res  = set.insert(std::move(node), hashval);
3100
        return { make_iterator(&inner, res.position),
3101
                 res.inserted,
3102
                 res.inserted ? node_type() : std::move(res.node) };
3103
    }
3104
3105
    iterator insert(const_iterator, node_type&& node) {
3106
        return insert(std::move(node)).first;
3107
    }
3108
3109
    struct ReturnKey_ 
3110
    {
3111
        template <class Key, class... Args>
3112
        Key operator()(Key&& k, const Args&...) const {
3113
            return std::forward<Key>(k);
3114
        }
3115
    };
3116
3117
    // --------------------------------------------------------------------
3118
    // phmap extension: emplace_with_hash
3119
    // ----------------------------------
3120
    // same as emplace, but hashval is provided
3121
    // --------------------------------------------------------------------
3122
    struct EmplaceDecomposableHashval 
3123
    {
3124
        template <class K, class... Args>
3125
        std::pair<iterator, bool> operator()(const K& key, Args&&... args) const {
3126
            return s.emplace_decomposable_with_hash(key, hashval, std::forward<Args>(args)...);
3127
        }
3128
        parallel_hash_set& s;
3129
        size_t hashval;
3130
    };
3131
3132
    // This overload kicks in if we can deduce the key from args. This enables us
3133
    // to avoid constructing value_type if an entry with the same key already
3134
    // exists.
3135
    //
3136
    // For example:
3137
    //
3138
    //   flat_hash_map<std::string, std::string> m = {{"abc", "def"}};
3139
    //   // Creates no std::string copies and makes no heap allocations.
3140
    //   m.emplace("abc", "xyz");
3141
    // --------------------------------------------------------------------
3142
    template <class... Args, typename std::enable_if<IsDecomposable<Args...>::value, int>::type = 0>
3143
    std::pair<iterator, bool> emplace_with_hash(size_t hashval, Args&&... args) {
3144
        return PolicyTraits::apply(EmplaceDecomposableHashval{*this, hashval},
3145
                                   std::forward<Args>(args)...);
3146
    }
3147
3148
    // This overload kicks in if we cannot deduce the key from args. It constructs
3149
    // value_type unconditionally and then either moves it into the table or
3150
    // destroys.
3151
    // --------------------------------------------------------------------
3152
    template <class... Args, typename std::enable_if<!IsDecomposable<Args...>::value, int>::type = 0>
3153
    std::pair<iterator, bool> emplace_with_hash(size_t hashval, Args&&... args) {
3154
        typename phmap::aligned_storage<sizeof(slot_type), alignof(slot_type)>::type raw;
3155
        slot_type* slot = reinterpret_cast<slot_type*>(&raw);
3156
3157
        PolicyTraits::construct(&alloc_ref(), slot, std::forward<Args>(args)...);
3158
        const auto& elem = PolicyTraits::element(slot);
3159
        Inner& inner    = sets_[subidx(hashval)];
3160
        auto&  set      = inner.set_;
3161
        UniqueLock m(inner);
3162
        typename EmbeddedSet::template InsertSlotWithHash<true> f { inner, std::move(*slot), hashval };
3163
        return make_rv(PolicyTraits::apply(f, elem));
3164
    }
3165
3166
    template <class... Args>
3167
    iterator emplace_hint_with_hash(size_t hashval, const_iterator, Args&&... args) {
3168
        return emplace_with_hash(hashval, std::forward<Args>(args)...).first;
3169
    }
3170
3171
    // --------------------------------------------------------------------
3172
    // end of phmap expension
3173
    // --------------------------------------------------------------------
3174
3175
    template <class K, class... Args>
3176
    std::pair<iterator, bool> emplace_decomposable_with_hash(const K& key, size_t hashval, Args&&... args)
3177
    {
3178
        Inner& inner   = sets_[subidx(hashval)];
3179
        auto&  set     = inner.set_;
3180
        UniqueLock m(inner);
3181
        
3182
        size_t offset = set._find_key(key, hashval);
3183
        if (offset == (size_t)-1) {
3184
            offset = set.prepare_insert(hashval);
3185
            set.emplace_at(offset, std::forward<Args>(args)...);
3186
            set.set_ctrl(offset, H2(hashval));
3187
            return make_rv(&inner, {set.iterator_at(offset), true});
3188
        }
3189
        return make_rv(&inner, {set.iterator_at(offset), false});
3190
    }
3191
3192
    template <class K, class... Args>
3193
    std::pair<iterator, bool> emplace_decomposable(const K& key, Args&&... args)
3194
    {
3195
        return emplace_decomposable_with_hash(key, this->hash(key), std::forward<Args>(args)...);
3196
    }
3197
3198
    struct EmplaceDecomposable 
3199
    {
3200
        template <class K, class... Args>
3201
        std::pair<iterator, bool> operator()(const K& key, Args&&... args) const {
3202
            return s.emplace_decomposable(key, std::forward<Args>(args)...);
3203
        }
3204
        parallel_hash_set& s;
3205
    };
3206
3207
    // This overload kicks in if we can deduce the key from args. This enables us
3208
    // to avoid constructing value_type if an entry with the same key already
3209
    // exists.
3210
    //
3211
    // For example:
3212
    //
3213
    //   flat_hash_map<std::string, std::string> m = {{"abc", "def"}};
3214
    //   // Creates no std::string copies and makes no heap allocations.
3215
    //   m.emplace("abc", "xyz");
3216
    // --------------------------------------------------------------------
3217
    template <class... Args, typename std::enable_if<IsDecomposable<Args...>::value, int>::type = 0>
3218
    std::pair<iterator, bool> emplace(Args&&... args) {
3219
        return PolicyTraits::apply(EmplaceDecomposable{*this}, std::forward<Args>(args)...);
3220
    }
3221
3222
    // This overload kicks in if we cannot deduce the key from args. It constructs
3223
    // value_type unconditionally and then either moves it into the table or
3224
    // destroys.
3225
    // --------------------------------------------------------------------
3226
    template <class... Args, typename std::enable_if<!IsDecomposable<Args...>::value, int>::type = 0>
3227
    std::pair<iterator, bool> emplace(Args&&... args) {
3228
        typename phmap::aligned_storage<sizeof(slot_type), alignof(slot_type)>::type raw;
3229
        slot_type* slot = reinterpret_cast<slot_type*>(&raw);
3230
        size_t hashval  = this->hash(PolicyTraits::key(slot));
3231
3232
        PolicyTraits::construct(&alloc_ref(), slot, std::forward<Args>(args)...);
3233
        const auto& elem = PolicyTraits::element(slot);
3234
        Inner& inner     = sets_[subidx(hashval)];
3235
        auto&  set       = inner.set_;
3236
        UniqueLock m(inner);
3237
        typename EmbeddedSet::template InsertSlotWithHash<true> f { inner, std::move(*slot), hashval };
3238
        return make_rv(PolicyTraits::apply(f, elem));
3239
    }
3240
3241
    template <class... Args>
3242
    iterator emplace_hint(const_iterator, Args&&... args) {
3243
        return emplace(std::forward<Args>(args)...).first;
3244
    }
3245
3246
    iterator make_iterator(Inner* inner, const EmbeddedIterator it)
3247
    {
3248
        if (it == inner->set_.end())
3249
            return iterator();
3250
        return iterator(inner, &sets_[0] + num_tables, it);
3251
    }
3252
3253
    std::pair<iterator, bool> make_rv(Inner* inner, 
3254
                                      const std::pair<EmbeddedIterator, bool>& res)
3255
    {
3256
        return {iterator(inner, &sets_[0] + num_tables, res.first), res.second};
3257
    }
3258
3259
    // lazy_emplace
3260
    // ------------
3261
    template <class K = key_type, class F>
3262
    iterator lazy_emplace_with_hash(const key_arg<K>& key, size_t hashval, F&& f) {
3263
        Inner& inner = sets_[subidx(hashval)];
3264
        auto&  set   = inner.set_;
3265
        UniqueLock m(inner);
3266
        size_t offset = set._find_key(key, hashval);
3267
        if (offset == (size_t)-1) {
3268
            offset = set.prepare_insert(hashval);
3269
            set.lazy_emplace_at(offset, std::forward<F>(f));
3270
            set.set_ctrl(offset, H2(hashval));
3271
        }
3272
        return make_iterator(&inner, set.iterator_at(offset));
3273
    }
3274
3275
    template <class K = key_type, class F>
3276
    iterator lazy_emplace(const key_arg<K>& key, F&& f) {
3277
        return lazy_emplace_with_hash(key, this->hash(key), std::forward<F>(f));
3278
    }
3279
    
3280
    // emplace_single
3281
    // --------------
3282
    template <class K = key_type, class F>
3283
    void emplace_single_with_hash(const key_arg<K>& key, size_t hashval, F&& f) {
3284
        Inner& inner = sets_[subidx(hashval)];
3285
        auto&  set   = inner.set_;
3286
        UniqueLock m(inner);
3287
        set.emplace_single_with_hash(key, hashval, std::forward<F>(f));
3288
    }
3289
3290
    template <class K = key_type, class F>
3291
    void emplace_single(const key_arg<K>& key, F&& f) {
3292
        emplace_single_with_hash<K, F>(key, this->hash(key), std::forward<F>(f));
3293
    }
3294
3295
    // if set contains key, lambda is called with the value_type (under read lock protection),
3296
    // and if_contains returns true. This is a const API and lambda should not modify the value
3297
    // -----------------------------------------------------------------------------------------
3298
    template <class K = key_type, class F>
3299
    bool if_contains(const key_arg<K>& key, F&& f) const {
3300
        return const_cast<parallel_hash_set*>(this)->template 
3301
            modify_if_impl<K, F, SharedLock>(key, std::forward<F>(f));
3302
    }
3303
3304
    // if set contains key, lambda is called with the value_type  without read lock protection,
3305
    // and if_contains_unsafe returns true. This is a const API and lambda should not modify the value
3306
    // This should be used only if we know that no other thread may be mutating the set at the time.
3307
    // -----------------------------------------------------------------------------------------
3308
    template <class K = key_type, class F>
3309
    bool if_contains_unsafe(const key_arg<K>& key, F&& f) const {
3310
        return const_cast<parallel_hash_set*>(this)->template 
3311
            modify_if_impl<K, F, LockableBaseImpl<phmap::NullMutex>::DoNothing>(key, std::forward<F>(f));
3312
    }
3313
3314
    // if map contains key, lambda is called with the value_type  (under write lock protection),
3315
    // and modify_if returns true. This is a non-const API and lambda is allowed to modify the mapped value
3316
    // ----------------------------------------------------------------------------------------------------
3317
    template <class K = key_type, class F>
3318
    bool modify_if(const key_arg<K>& key, F&& f) {
3319
        return modify_if_impl<K, F, UniqueLock>(key, std::forward<F>(f));
3320
    }
3321
3322
    // -----------------------------------------------------------------------------------------
3323
    template <class K = key_type, class F, class L>
3324
    bool modify_if_impl(const key_arg<K>& key, F&& f) {
3325
#if __cplusplus >= 201703L
3326
        static_assert(std::is_invocable<F, value_type&>::value);
3327
#endif
3328
        L m;
3329
        auto ptr = this->template find_ptr<K, L>(key, this->hash(key), m);
3330
        if (ptr == nullptr)
3331
            return false;
3332
        std::forward<F>(f)(*ptr);
3333
        return true;
3334
    }
3335
3336
    // if map contains key, lambda is called with the mapped value  (under write lock protection).
3337
    // If the lambda returns true, the key is subsequently erased from the map (the write lock
3338
    // is only released after erase).
3339
    // returns true if key was erased, false otherwise.
3340
    // ----------------------------------------------------------------------------------------------------
3341
    template <class K = key_type, class F>
3342
    bool erase_if(const key_arg<K>& key, F&& f) {
3343
        return !!erase_if_impl<K, F, ReadWriteLock>(key, std::forward<F>(f));
3344
    }
3345
3346
    template <class K = key_type, class F, class L>
3347
    size_type erase_if_impl(const key_arg<K>& key, F&& f) {
3348
#if __cplusplus >= 201703L
3349
        static_assert(std::is_invocable<F, value_type&>::value);
3350
#endif
3351
        auto hashval = this->hash(key);
3352
        Inner& inner = sets_[subidx(hashval)];
3353
        auto& set = inner.set_;
3354
        L m(inner);
3355
        auto it = set.find(key, hashval);
3356
        if (it == set.end())
3357
            return 0;
3358
        if (m.switch_to_unique()) {
3359
            // we did an unlock/lock, need to call `find()` again
3360
            it = set.find(key, hashval);
3361
            if (it == set.end())
3362
                return 0;
3363
        }
3364
        if (std::forward<F>(f)(const_cast<value_type &>(*it)))
3365
        {
3366
            set._erase(it);
3367
            return 1;
3368
        }
3369
        return 0;
3370
    }
3371
3372
    // if map already  contains key, the first lambda is called with the mapped value (under 
3373
    // write lock protection) and can update the mapped value.
3374
    // if map does not contains key, the second lambda is called and it should invoke the 
3375
    // passed constructor to construct the value
3376
    // returns true if key was not already present, false otherwise.
3377
    // ---------------------------------------------------------------------------------------
3378
    template <class K = key_type, class FExists, class FEmplace>
3379
    bool lazy_emplace_l(const key_arg<K>& key, FExists&& fExists, FEmplace&& fEmplace) {
3380
        size_t hashval = this->hash(key);
3381
        UniqueLock m;
3382
        auto res = this->find_or_prepare_insert_with_hash(hashval, key, m);
3383
        Inner* inner = std::get<0>(res);
3384
        if (std::get<2>(res)) {
3385
            // key not found. call fEmplace lambda which should invoke passed constructor
3386
            inner->set_.lazy_emplace_at(std::get<1>(res), std::forward<FEmplace>(fEmplace));
3387
            inner->set_.set_ctrl(std::get<1>(res), H2(hashval));
3388
        } else {
3389
            // key found. Call fExists lambda. In case of the set, non "key" part of value_type can be changed
3390
            auto it = this->iterator_at(inner, inner->set_.iterator_at(std::get<1>(res)));
3391
            std::forward<FExists>(fExists)(const_cast<value_type &>(*it)); 
3392
        }
3393
        return std::get<2>(res);
3394
    }
3395
3396
    // Extension API: support iterating over all values
3397
    //
3398
    // flat_hash_set<std::string> s;
3399
    // s.insert(...);
3400
    // s.for_each([](auto const & key) {
3401
    //    // Safely iterates over all the keys
3402
    // });
3403
    template <class F>
3404
    void for_each(F&& fCallback) const {
3405
        for (auto const& inner : sets_) {
3406
            SharedLock m(const_cast<Inner&>(inner));
3407
            std::for_each(inner.set_.begin(), inner.set_.end(), fCallback);
3408
        }
3409
    }
3410
3411
    // this version allows to modify the values
3412
    template <class F>
3413
    void for_each_m(F&& fCallback) {
3414
        for (auto& inner : sets_) {
3415
            UniqueLock m(inner);
3416
            std::for_each(inner.set_.begin(), inner.set_.end(), fCallback);
3417
        }
3418
    }
3419
3420
#if __cplusplus >= 201703L
3421
    template <class ExecutionPolicy, class F>
3422
    void for_each(ExecutionPolicy&& policy, F&& fCallback) const {
3423
        std::for_each(
3424
            std::forward<ExecutionPolicy>(policy), sets_.begin(), sets_.end(),
3425
            [&](auto const& inner) {
3426
                SharedLock m(const_cast<Inner&>(inner));
3427
                std::for_each(inner.set_.begin(), inner.set_.end(), fCallback);
3428
            }
3429
        );
3430
    }
3431
3432
    template <class ExecutionPolicy, class F>
3433
    void for_each_m(ExecutionPolicy&& policy, F&& fCallback) {
3434
        std::for_each(
3435
            std::forward<ExecutionPolicy>(policy), sets_.begin(), sets_.end(),
3436
            [&](auto& inner) {
3437
                UniqueLock m(inner);
3438
                std::for_each(inner.set_.begin(), inner.set_.end(), fCallback);
3439
            }
3440
        );
3441
    }
3442
#endif
3443
3444
    // Extension API: access internal submaps by index
3445
    // under lock protection
3446
    // ex: m.with_submap(i, [&](const Map::EmbeddedSet& set) {
3447
    //        for (auto& p : set) { ...; }});
3448
    // -------------------------------------------------
3449
    template <class F>
3450
    void with_submap(size_t idx, F&& fCallback) const {
3451
        const Inner& inner     = sets_[idx];
3452
        const auto&  set = inner.set_;
3453
        SharedLock m(const_cast<Inner&>(inner));
3454
        fCallback(set);
3455
    }
3456
3457
    template <class F>
3458
    void with_submap_m(size_t idx, F&& fCallback) {
3459
        Inner& inner   = sets_[idx];
3460
        auto&  set     = inner.set_;
3461
        UniqueLock m(inner);
3462
        fCallback(set);
3463
    }
3464
3465
    // unsafe, for internal use only
3466
    Inner& get_inner(size_t idx) {
3467
        return  sets_[idx];
3468
    }
3469
3470
    const Inner& get_inner(size_t idx) const {
3471
        return  sets_[idx];
3472
    }
3473
3474
    // Extension API: support for heterogeneous keys.
3475
    //
3476
    //   std::unordered_set<std::string> s;
3477
    //   // Turns "abc" into std::string.
3478
    //   s.erase("abc");
3479
    //
3480
    //   flat_hash_set<std::string> s;
3481
    //   // Uses "abc" directly without copying it into std::string.
3482
    //   s.erase("abc");
3483
    //
3484
    // --------------------------------------------------------------------
3485
    template <class K = key_type>
3486
    size_type erase(const key_arg<K>& key) {
3487
        auto always_erase =  [](const value_type&){ return true; };
3488
        return erase_if_impl<K, decltype(always_erase), ReadWriteLock>(key, std::move(always_erase));
3489
    }
3490
3491
    // --------------------------------------------------------------------
3492
    iterator erase(const_iterator cit) { return erase(cit.iter_); }
3493
3494
    // Erases the element pointed to by `it`.  Unlike `std::unordered_set::erase`,
3495
    // this method returns void to reduce algorithmic complexity to O(1).  In
3496
    // order to erase while iterating across a map, use the following idiom (which
3497
    // also works for standard containers):
3498
    //
3499
    // for (auto it = m.begin(), end = m.end(); it != end;) {
3500
    //   if (<pred>) {
3501
    //     m._erase(it++);
3502
    //   } else {
3503
    //     ++it;
3504
    //   }
3505
    // }
3506
    //
3507
    // Do not use erase APIs taking iterators when accessing the map concurrently
3508
    // --------------------------------------------------------------------
3509
    void _erase(iterator it) {
3510
        Inner* inner = it.inner_;
3511
        assert(inner != nullptr);
3512
        auto&  set   = inner->set_;
3513
        // UniqueLock m(*inner); // don't lock here 
3514
        
3515
        set._erase(it.it_);
3516
    }
3517
    void _erase(const_iterator cit) { _erase(cit.iter_); }
3518
3519
    // This overload is necessary because otherwise erase<K>(const K&) would be
3520
    // a better match if non-const iterator is passed as an argument.
3521
    // Do not use erase APIs taking iterators when accessing the map concurrently
3522
    // --------------------------------------------------------------------
3523
    iterator erase(iterator it) { _erase(it++); return it; }
3524
3525
    iterator erase(const_iterator first, const_iterator last) {
3526
        while (first != last) {
3527
            _erase(first++);
3528
        }
3529
        return last.iter_;
3530
    }
3531
3532
    // Moves elements from `src` into `this`.
3533
    // If the element already exists in `this`, it is left unmodified in `src`.
3534
    // Do not use erase APIs taking iterators when accessing the map concurrently
3535
    // --------------------------------------------------------------------
3536
    template <typename E = Eq>
3537
    void merge(parallel_hash_set<N, RefSet, Mtx_, Policy, Hash, E, Alloc>& src) {  // NOLINT
3538
        assert(this != &src);
3539
        if (this != &src)
3540
        {
3541
            for (size_t i=0; i<num_tables; ++i)
3542
            {
3543
                typename Lockable::UniqueLocks l(sets_[i], src.sets_[i]);
3544
                sets_[i].set_.merge(src.sets_[i].set_);
3545
            }
3546
        }
3547
    }
3548
3549
    template <typename E = Eq>
3550
    void merge(parallel_hash_set<N, RefSet, Mtx_, Policy, Hash, E, Alloc>&& src) {
3551
        merge(src);
3552
    }
3553
3554
    node_type extract(const_iterator position) {
3555
        return position.iter_.inner_->set_.extract(EmbeddedConstIterator(position.iter_.it_));
3556
    }
3557
3558
    template <
3559
        class K = key_type,
3560
        typename std::enable_if<!std::is_same<K, iterator>::value, int>::type = 0>
3561
    node_type extract(const key_arg<K>& key) {
3562
        auto it = find(key);
3563
        return it == end() ? node_type() : extract(const_iterator{it});
3564
    }
3565
3566
    template<class Mtx2_>
3567
    void swap(parallel_hash_set<N, RefSet, Mtx2_, Policy, Hash, Eq, Alloc>& that)
3568
        noexcept(IsNoThrowSwappable<EmbeddedSet>() &&
3569
                 (!AllocTraits::propagate_on_container_swap::value ||
3570
                  IsNoThrowSwappable<allocator_type>(typename AllocTraits::propagate_on_container_swap{})))
3571
    {
3572
        using std::swap;
3573
        using Lockable2 = phmap::LockableImpl<Mtx2_>;
3574
         
3575
        for (size_t i=0; i<num_tables; ++i)
3576
        {
3577
            typename Lockable::UniqueLock l(sets_[i]);
3578
            typename Lockable2::UniqueLock l2(that.get_inner(i));
3579
            swap(sets_[i].set_, that.get_inner(i).set_);
3580
        }
3581
    }
3582
3583
    void rehash(size_t n) {
3584
        size_t nn = n / num_tables;
3585
        for (auto& inner : sets_)
3586
        {
3587
            UniqueLock m(inner);
3588
            inner.set_.rehash(nn);
3589
        }
3590
    }
3591
3592
    void reserve(size_t n) 
3593
    {
3594
        size_t target = GrowthToLowerboundCapacity(n);
3595
        size_t normalized = num_tables * NormalizeCapacity(n / num_tables);
3596
        rehash(normalized > target ? normalized : target); 
3597
    }
3598
3599
    // Extension API: support for heterogeneous keys.
3600
    //
3601
    //   std::unordered_set<std::string> s;
3602
    //   // Turns "abc" into std::string.
3603
    //   s.count("abc");
3604
    //
3605
    //   ch_set<std::string> s;
3606
    //   // Uses "abc" directly without copying it into std::string.
3607
    //   s.count("abc");
3608
    // --------------------------------------------------------------------
3609
    template <class K = key_type>
3610
    size_t count(const key_arg<K>& key) const {
3611
        return find(key) == end() ? 0 : 1;
3612
    }
3613
3614
    // Issues CPU prefetch instructions for the memory needed to find or insert
3615
    // a key.  Like all lookup functions, this support heterogeneous keys.
3616
    //
3617
    // NOTE: This is a very low level operation and should not be used without
3618
    // specific benchmarks indicating its importance.
3619
    // --------------------------------------------------------------------
3620
    void prefetch_hash(size_t hashval) const {
3621
        const Inner& inner = sets_[subidx(hashval)];
3622
        const auto&  set   = inner.set_;
3623
        SharedLock m(const_cast<Inner&>(inner));
3624
        set.prefetch_hash(hashval);
3625
    }
3626
3627
    template <class K = key_type>
3628
    void prefetch(const key_arg<K>& key) const {
3629
        prefetch_hash(this->hash(key));
3630
    }
3631
3632
    // The API of find() has two extensions.
3633
    //
3634
    // 1. The hash can be passed by the user. It must be equal to the hash of the
3635
    // key.
3636
    //
3637
    // 2. The type of the key argument doesn't have to be key_type. This is so
3638
    // called heterogeneous key support.
3639
    // --------------------------------------------------------------------
3640
    template <class K = key_type>
3641
    iterator find(const key_arg<K>& key, size_t hashval) {
3642
        SharedLock m;
3643
        return find(key, hashval, m);
3644
    }
3645
3646
    template <class K = key_type>
3647
    iterator find(const key_arg<K>& key) {
3648
        return find(key, this->hash(key));
3649
    }
3650
3651
    template <class K = key_type>
3652
    const_iterator find(const key_arg<K>& key, size_t hashval) const {
3653
        return const_cast<parallel_hash_set*>(this)->find(key, hashval);
3654
    }
3655
3656
    template <class K = key_type>
3657
    const_iterator find(const key_arg<K>& key) const {
3658
        return find(key, this->hash(key));
3659
    }
3660
3661
    template <class K = key_type>
3662
    bool contains(const key_arg<K>& key) const {
3663
        return find(key) != end();
3664
    }
3665
3666
    template <class K = key_type>
3667
    bool contains(const key_arg<K>& key, size_t hashval) const {
3668
        return find(key, hashval) != end();
3669
    }
3670
3671
    template <class K = key_type>
3672
    std::pair<iterator, iterator> equal_range(const key_arg<K>& key) {
3673
        auto it = find(key);
3674
        if (it != end()) return {it, std::next(it)};
3675
        return {it, it};
3676
    }
3677
3678
    template <class K = key_type>
3679
    std::pair<const_iterator, const_iterator> equal_range(
3680
        const key_arg<K>& key) const {
3681
        auto it = find(key);
3682
        if (it != end()) return {it, std::next(it)};
3683
        return {it, it};
3684
    }
3685
3686
    size_t bucket_count() const {
3687
        size_t sz = 0;
3688
        for (const auto& inner : sets_)
3689
        {
3690
            SharedLock m(const_cast<Inner&>(inner));
3691
            sz += inner.set_.bucket_count();
3692
        }
3693
        return sz; 
3694
    }
3695
3696
    float load_factor() const {
3697
        size_t _capacity = bucket_count();
3698
        return _capacity ? static_cast<float>(static_cast<double>(size()) / _capacity) : 0;
3699
    }
3700
3701
    float max_load_factor() const { return 1.0f; }
3702
    void max_load_factor(float) {
3703
        // Does nothing.
3704
    }
3705
3706
    hasher hash_function() const { return hash_ref(); }  // warning: doesn't match internal hash - use hash() member function
3707
    key_equal key_eq() const { return eq_ref(); }
3708
    allocator_type get_allocator() const { return alloc_ref(); }
3709
3710
    friend bool operator==(const parallel_hash_set& a, const parallel_hash_set& b) {
3711
        return std::equal(a.sets_.begin(), a.sets_.end(), b.sets_.begin());
3712
    }
3713
3714
    friend bool operator!=(const parallel_hash_set& a, const parallel_hash_set& b) {
3715
        return !(a == b);
3716
    }
3717
3718
    template<class Mtx2_>
3719
    friend void swap(parallel_hash_set& a,
3720
                     parallel_hash_set<N, RefSet, Mtx2_, Policy, Hash, Eq, Alloc>& b)
3721
        noexcept(noexcept(a.swap(b)))
3722
    {
3723
        a.swap(b);
3724
    }
3725
3726
    template <class K>
3727
    size_t hash(const K& key) const {
3728
        return HashElement{hash_ref()}(key);
3729
    }
3730
3731
#if !defined(PHMAP_NON_DETERMINISTIC)
3732
    template<typename OutputArchive>
3733
    bool phmap_dump(OutputArchive& ar) const;
3734
3735
    template<typename InputArchive>
3736
    bool phmap_load(InputArchive& ar);
3737
#endif
3738
3739
private:
3740
    template <class Container, typename Enabler>
3741
    friend struct phmap::priv::hashtable_debug_internal::HashtableDebugAccess;
3742
3743
    struct FindElement 
3744
    {
3745
        template <class K, class... Args>
3746
        const_iterator operator()(const K& key, Args&&...) const {
3747
            return s.find(key);
3748
        }
3749
        const parallel_hash_set& s;
3750
    };
3751
3752
    struct HashElement 
3753
    {
3754
        template <class K, class... Args>
3755
        size_t operator()(const K& key, Args&&...) const {
3756
#if PHMAP_DISABLE_MIX
3757
            return h(key);
3758
#else
3759
            return phmap_mix<sizeof(size_t)>()(h(key));
3760
#endif
3761
        }
3762
        const hasher& h;
3763
    };
3764
3765
    template <class K1>
3766
    struct EqualElement 
3767
    {
3768
        template <class K2, class... Args>
3769
        bool operator()(const K2& lhs, Args&&...) const {
3770
            return eq(lhs, rhs);
3771
        }
3772
        const K1& rhs;
3773
        const key_equal& eq;
3774
    };
3775
3776
    // "erases" the object from the container, except that it doesn't actually
3777
    // destroy the object. It only updates all the metadata of the class.
3778
    // This can be used in conjunction with Policy::transfer to move the object to
3779
    // another place.
3780
    // --------------------------------------------------------------------
3781
    void erase_meta_only(const_iterator cit) {
3782
        auto &it = cit.iter_;
3783
        assert(it.set_ != nullptr);
3784
        it.set_.erase_meta_only(const_iterator(it.it_));
3785
    }
3786
3787
    void drop_deletes_without_resize() PHMAP_ATTRIBUTE_NOINLINE {
3788
        for (auto& inner : sets_)
3789
        {
3790
            UniqueLock m(inner);
3791
            inner.set_.drop_deletes_without_resize();
3792
        }
3793
    }
3794
3795
    bool has_element(const value_type& elem) const {
3796
        size_t hashval = PolicyTraits::apply(HashElement{hash_ref()}, elem);
3797
        Inner& inner   = sets_[subidx(hashval)];
3798
        auto&  set     = inner.set_;
3799
        SharedLock m(const_cast<Inner&>(inner));
3800
        return set.has_element(elem, hashval);
3801
    }
3802
3803
    // TODO(alkis): Optimize this assuming *this and that don't overlap.
3804
    // --------------------------------------------------------------------
3805
    template<class Mtx2_>
3806
    parallel_hash_set& move_assign(parallel_hash_set<N, RefSet, Mtx2_, Policy, Hash, Eq, Alloc>&& that, std::true_type) {
3807
        parallel_hash_set<N, RefSet, Mtx2_, Policy, Hash, Eq, Alloc> tmp(std::move(that));
3808
        swap(tmp);
3809
        return *this;
3810
    }
3811
3812
    template<class Mtx2_>
3813
    parallel_hash_set& move_assign(parallel_hash_set<N, RefSet, Mtx2_, Policy, Hash, Eq, Alloc>&& that, std::false_type) {
3814
        parallel_hash_set<N, RefSet, Mtx2_, Policy, Hash, Eq, Alloc> tmp(std::move(that), alloc_ref());
3815
        swap(tmp);
3816
        return *this;
3817
    }
3818
3819
protected:
3820
    template <class K = key_type, class L = SharedLock>
3821
    pointer find_ptr(const key_arg<K>& key, size_t hashval, L& mutexlock)
3822
    {
3823
        Inner& inner = sets_[subidx(hashval)];
3824
        auto& set = inner.set_;
3825
        mutexlock = std::move(L(inner));
3826
        return set.find_ptr(key, hashval);
3827
    }
3828
3829
    template <class K = key_type, class L = SharedLock>
3830
    iterator find(const key_arg<K>& key, size_t hashval, L& mutexlock) {
3831
        Inner& inner = sets_[subidx(hashval)];
3832
        auto& set = inner.set_;
3833
        mutexlock = std::move(L(inner));
3834
        return make_iterator(&inner, set.find(key, hashval));
3835
    }
3836
3837
    template <class K>
3838
    std::tuple<Inner*, size_t, bool> 
3839
    find_or_prepare_insert_with_hash(size_t hashval, const K& key, UniqueLock &mutexlock) {
3840
        Inner& inner = sets_[subidx(hashval)];
3841
        auto&  set   = inner.set_;
3842
        mutexlock    = std::move(UniqueLock(inner));
3843
        size_t offset = set._find_key(key, hashval);
3844
        if (offset == (size_t)-1) {
3845
            offset = set.prepare_insert(hashval);
3846
            return std::make_tuple(&inner, offset, true);
3847
        }
3848
        return std::make_tuple(&inner, offset, false);
3849
    }
3850
3851
    template <class K>
3852
    std::tuple<Inner*, size_t, bool> 
3853
    find_or_prepare_insert(const K& key, UniqueLock &mutexlock) {
3854
        return find_or_prepare_insert_with_hash<K>(this->hash(key), key, mutexlock);
3855
    }
3856
3857
    iterator iterator_at(Inner *inner, 
3858
                         const EmbeddedIterator& it) { 
3859
        return {inner, &sets_[0] + num_tables, it}; 
3860
    }
3861
    const_iterator iterator_at(Inner *inner, 
3862
                               const EmbeddedIterator& it) const { 
3863
        return {inner, &sets_[0] + num_tables, it}; 
3864
    }
3865
3866
    static size_t subidx(size_t hashval) {
3867
        return ((hashval >> 8) ^ (hashval >> 16) ^ (hashval >> 24)) & mask;
3868
    }
3869
3870
    static size_t subcnt() {
3871
        return num_tables;
3872
    }
3873
3874
private:
3875
    friend struct RawHashSetTestOnlyAccess;
3876
3877
    size_t growth_left() { 
3878
        size_t sz = 0;
3879
        for (const auto& set : sets_)
3880
            sz += set.growth_left();
3881
        return sz; 
3882
    }
3883
3884
    hasher&       hash_ref()        { return sets_[0].set_.hash_ref(); }
3885
    const hasher& hash_ref() const  { return sets_[0].set_.hash_ref(); }
3886
    key_equal&       eq_ref()       { return sets_[0].set_.eq_ref(); }
3887
    const key_equal& eq_ref() const { return sets_[0].set_.eq_ref(); }
3888
    allocator_type&  alloc_ref()    { return sets_[0].set_.alloc_ref(); }
3889
    const allocator_type& alloc_ref() const { 
3890
        return sets_[0].set_.alloc_ref();
3891
    }
3892
3893
protected:       // protected in case users want to derive fromm this
3894
    std::array<Inner, num_tables> sets_;
3895
};
3896
3897
// --------------------------------------------------------------------------
3898
// --------------------------------------------------------------------------
3899
template <size_t N,
3900
          template <class, class, class, class> class RefSet,
3901
          class Mtx_,
3902
          class Policy, class Hash, class Eq, class Alloc>
3903
class parallel_hash_map : public parallel_hash_set<N, RefSet, Mtx_, Policy, Hash, Eq, Alloc> 
3904
{
3905
    // P is Policy. It's passed as a template argument to support maps that have
3906
    // incomplete types as values, as in unordered_map<K, IncompleteType>.
3907
    // MappedReference<> may be a non-reference type.
3908
    template <class P>
3909
    using MappedReference = decltype(P::value(
3910
            std::addressof(std::declval<typename parallel_hash_map::reference>())));
3911
3912
    // MappedConstReference<> may be a non-reference type.
3913
    template <class P>
3914
    using MappedConstReference = decltype(P::value(
3915
            std::addressof(std::declval<typename parallel_hash_map::const_reference>())));
3916
3917
    using KeyArgImpl =
3918
        KeyArg<IsTransparent<Eq>::value && IsTransparent<Hash>::value>;
3919
3920
    using Base = typename parallel_hash_map::parallel_hash_set;
3921
    using Lockable      = phmap::LockableImpl<Mtx_>;
3922
    using UniqueLock    = typename Lockable::UniqueLock;
3923
    using SharedLock    = typename Lockable::SharedLock;
3924
    using ReadWriteLock = typename Lockable::ReadWriteLock;
3925
3926
public:
3927
    using key_type    = typename Policy::key_type;
3928
    using mapped_type = typename Policy::mapped_type;
3929
    using value_type  = typename Base::value_type;
3930
    template <class K>
3931
    using key_arg = typename KeyArgImpl::template type<K, key_type>;
3932
3933
    static_assert(!std::is_reference<key_type>::value, "");
3934
    // TODO(alkis): remove this assertion and verify that reference mapped_type is
3935
    // supported.
3936
    static_assert(!std::is_reference<mapped_type>::value, "");
3937
3938
    using iterator = typename parallel_hash_map::parallel_hash_set::iterator;
3939
    using const_iterator = typename parallel_hash_map::parallel_hash_set::const_iterator;
3940
3941
    parallel_hash_map() {}
3942
3943
#ifdef __INTEL_COMPILER
3944
    using Base::parallel_hash_set;
3945
#else
3946
    using parallel_hash_map::parallel_hash_set::parallel_hash_set;
3947
#endif
3948
3949
    // The last two template parameters ensure that both arguments are rvalues
3950
    // (lvalue arguments are handled by the overloads below). This is necessary
3951
    // for supporting bitfield arguments.
3952
    //
3953
    //   union { int n : 1; };
3954
    //   flat_hash_map<int, int> m;
3955
    //   m.insert_or_assign(n, n);
3956
    template <class K = key_type, class V = mapped_type, K* = nullptr,
3957
              V* = nullptr>
3958
    std::pair<iterator, bool> insert_or_assign(key_arg<K>&& k, V&& v) {
3959
        return insert_or_assign_impl(std::forward<K>(k), std::forward<V>(v));
3960
    }
3961
3962
    template <class K = key_type, class V = mapped_type, K* = nullptr>
3963
    std::pair<iterator, bool> insert_or_assign(key_arg<K>&& k, const V& v) {
3964
        return insert_or_assign_impl(std::forward<K>(k), v);
3965
    }
3966
3967
    template <class K = key_type, class V = mapped_type, V* = nullptr>
3968
    std::pair<iterator, bool> insert_or_assign(const key_arg<K>& k, V&& v) {
3969
        return insert_or_assign_impl(k, std::forward<V>(v));
3970
    }
3971
3972
    template <class K = key_type, class V = mapped_type>
3973
    std::pair<iterator, bool> insert_or_assign(const key_arg<K>& k, const V& v) {
3974
        return insert_or_assign_impl(k, v);
3975
    }
3976
3977
    template <class K = key_type, class V = mapped_type, K* = nullptr,
3978
              V* = nullptr>
3979
    iterator insert_or_assign(const_iterator, key_arg<K>&& k, V&& v) {
3980
        return insert_or_assign(std::forward<K>(k), std::forward<V>(v)).first;
3981
    }
3982
3983
    template <class K = key_type, class V = mapped_type, K* = nullptr>
3984
    iterator insert_or_assign(const_iterator, key_arg<K>&& k, const V& v) {
3985
        return insert_or_assign(std::forward<K>(k), v).first;
3986
    }
3987
3988
    template <class K = key_type, class V = mapped_type, V* = nullptr>
3989
    iterator insert_or_assign(const_iterator, const key_arg<K>& k, V&& v) {
3990
        return insert_or_assign(k, std::forward<V>(v)).first;
3991
    }
3992
3993
    template <class K = key_type, class V = mapped_type>
3994
    iterator insert_or_assign(const_iterator, const key_arg<K>& k, const V& v) {
3995
        return insert_or_assign(k, v).first;
3996
    }
3997
3998
    template <class K = key_type, class... Args,
3999
              typename std::enable_if<
4000
                  !std::is_convertible<K, const_iterator>::value, int>::type = 0,
4001
              K* = nullptr>
4002
    std::pair<iterator, bool> try_emplace(key_arg<K>&& k, Args&&... args) {
4003
        return try_emplace_impl(std::forward<K>(k), std::forward<Args>(args)...);
4004
    }
4005
4006
    template <class K = key_type, class... Args,
4007
              typename std::enable_if<
4008
                  !std::is_convertible<K, const_iterator>::value, int>::type = 0>
4009
    std::pair<iterator, bool> try_emplace(const key_arg<K>& k, Args&&... args) {
4010
        return try_emplace_impl(k, std::forward<Args>(args)...);
4011
    }
4012
4013
    template <class K = key_type, class... Args, K* = nullptr>
4014
    iterator try_emplace(const_iterator, key_arg<K>&& k, Args&&... args) {
4015
        return try_emplace(std::forward<K>(k), std::forward<Args>(args)...).first;
4016
    }
4017
4018
    template <class K = key_type, class... Args>
4019
    iterator try_emplace(const_iterator, const key_arg<K>& k, Args&&... args) {
4020
        return try_emplace(k, std::forward<Args>(args)...).first;
4021
    }
4022
4023
    template <class K = key_type, class P = Policy>
4024
    MappedReference<P> at(const key_arg<K>& key) {
4025
        auto it = this->find(key);
4026
        if (it == this->end()) 
4027
            phmap::base_internal::ThrowStdOutOfRange("phmap at(): lookup non-existent key");
4028
        return Policy::value(&*it);
4029
    }
4030
4031
    template <class K = key_type, class P = Policy>
4032
    MappedConstReference<P> at(const key_arg<K>& key) const {
4033
        auto it = this->find(key);
4034
        if (it == this->end()) 
4035
            phmap::base_internal::ThrowStdOutOfRange("phmap at(): lookup non-existent key");
4036
        return Policy::value(&*it);
4037
    }
4038
4039
    // ----------- phmap extensions --------------------------
4040
4041
    template <class K = key_type, class... Args,
4042
              typename std::enable_if<
4043
                  !std::is_convertible<K, const_iterator>::value, int>::type = 0,
4044
              K* = nullptr>
4045
    std::pair<iterator, bool> try_emplace_with_hash(size_t hashval, key_arg<K>&& k, Args&&... args) {
4046
        return try_emplace_impl_with_hash(hashval, std::forward<K>(k), std::forward<Args>(args)...);
4047
    }
4048
4049
    template <class K = key_type, class... Args,
4050
              typename std::enable_if<
4051
                  !std::is_convertible<K, const_iterator>::value, int>::type = 0>
4052
    std::pair<iterator, bool> try_emplace_with_hash(size_t hashval, const key_arg<K>& k, Args&&... args) {
4053
        return try_emplace_impl_with_hash(hashval, k, std::forward<Args>(args)...);
4054
    }
4055
4056
    template <class K = key_type, class... Args, K* = nullptr>
4057
    iterator try_emplace_with_hash(size_t hashval, const_iterator, key_arg<K>&& k, Args&&... args) {
4058
        return try_emplace_with_hash(hashval, std::forward<K>(k), std::forward<Args>(args)...).first;
4059
    }
4060
4061
    template <class K = key_type, class... Args>
4062
    iterator try_emplace_with_hash(size_t hashval, const_iterator, const key_arg<K>& k, Args&&... args) {
4063
        return try_emplace_with_hash(hashval, k, std::forward<Args>(args)...).first;
4064
    }
4065
4066
    // if map does not contains key, it is inserted and the mapped value is value-constructed 
4067
    // with the provided arguments (if any), as with try_emplace. 
4068
    // if map already  contains key, then the lambda is called with the mapped value (under 
4069
    // write lock protection) and can update the mapped value.
4070
    // returns true if key was not already present, false otherwise.
4071
    // ---------------------------------------------------------------------------------------
4072
    template <class K = key_type, class F, class... Args>
4073
    bool try_emplace_l(K&& k, F&& f, Args&&... args) {
4074
        size_t hashval = this->hash(k);
4075
        UniqueLock m;
4076
        auto res = this->find_or_prepare_insert_with_hash(hashval, k, m);
4077
        typename Base::Inner *inner = std::get<0>(res);
4078
        if (std::get<2>(res)) {
4079
            inner->set_.emplace_at(std::get<1>(res), std::piecewise_construct,
4080
                                   std::forward_as_tuple(std::forward<K>(k)),
4081
                                   std::forward_as_tuple(std::forward<Args>(args)...));
4082
            inner->set_.set_ctrl(std::get<1>(res), H2(hashval));
4083
        } else {
4084
            auto it = this->iterator_at(inner, inner->set_.iterator_at(std::get<1>(res)));
4085
            // call lambda. in case of the set, non "key" part of value_type can be changed
4086
            std::forward<F>(f)(const_cast<value_type &>(*it));
4087
        }
4088
        return std::get<2>(res);
4089
    }
4090
4091
    // returns {pointer, bool} instead of {iterator, bool} per try_emplace.
4092
    // useful for node-based containers, since the pointer is not invalidated by concurrent insert etc.
4093
    template <class K = key_type, class... Args>
4094
    std::pair<typename parallel_hash_map::parallel_hash_set::pointer, bool> try_emplace_p(K&& k, Args&&... args) {
4095
        size_t hashval = this->hash(k);
4096
        UniqueLock m;
4097
        auto res = this->find_or_prepare_insert_with_hash(hashval, k, m);
4098
        typename Base::Inner *inner = std::get<0>(res);
4099
        if (std::get<2>(res)) {
4100
            inner->set_.emplace_at(std::get<1>(res), std::piecewise_construct,
4101
                                   std::forward_as_tuple(std::forward<K>(k)),
4102
                                   std::forward_as_tuple(std::forward<Args>(args)...));
4103
            inner->set_.set_ctrl(std::get<1>(res), H2(hashval));
4104
        }
4105
        auto it = this->iterator_at(inner, inner->set_.iterator_at(std::get<1>(res)));
4106
        return {&*it, std::get<2>(res)};
4107
    }
4108
4109
    // ----------- end of phmap extensions --------------------------
4110
4111
    template <class K = key_type, class P = Policy, K* = nullptr>
4112
    MappedReference<P> operator[](key_arg<K>&& key) {
4113
        return Policy::value(&*try_emplace(std::forward<K>(key)).first);
4114
    }
4115
4116
    template <class K = key_type, class P = Policy>
4117
    MappedReference<P> operator[](const key_arg<K>& key) {
4118
        return Policy::value(&*try_emplace(key).first);
4119
    }
4120
4121
private:
4122
4123
    template <class K, class V>
4124
    std::pair<iterator, bool> insert_or_assign_impl(K&& k, V&& v) {
4125
        size_t hashval = this->hash(k);
4126
        UniqueLock m;
4127
        auto res = this->find_or_prepare_insert_with_hash(hashval, k, m);
4128
        typename Base::Inner *inner = std::get<0>(res);
4129
        if (std::get<2>(res)) {
4130
            inner->set_.emplace_at(std::get<1>(res), std::forward<K>(k), std::forward<V>(v));
4131
            inner->set_.set_ctrl(std::get<1>(res), H2(hashval));
4132
        } else
4133
            Policy::value(&*inner->set_.iterator_at(std::get<1>(res))) = std::forward<V>(v);
4134
        return {this->iterator_at(inner, inner->set_.iterator_at(std::get<1>(res))), 
4135
                std::get<2>(res)};
4136
    }
4137
4138
    template <class K = key_type, class... Args>
4139
    std::pair<iterator, bool> try_emplace_impl(K&& k, Args&&... args) {
4140
        return try_emplace_impl_with_hash(this->hash(k), std::forward<K>(k),
4141
                                          std::forward<Args>(args)...);
4142
    }
4143
4144
    template <class K = key_type, class... Args>
4145
    std::pair<iterator, bool> try_emplace_impl_with_hash(size_t hashval, K&& k, Args&&... args) {
4146
        UniqueLock m;
4147
        auto res = this->find_or_prepare_insert_with_hash(hashval, k, m);
4148
        typename Base::Inner *inner = std::get<0>(res);
4149
        if (std::get<2>(res)) {
4150
            inner->set_.emplace_at(std::get<1>(res), std::piecewise_construct,
4151
                                   std::forward_as_tuple(std::forward<K>(k)),
4152
                                   std::forward_as_tuple(std::forward<Args>(args)...));
4153
            inner->set_.set_ctrl(std::get<1>(res), H2(hashval));
4154
        }
4155
        return {this->iterator_at(inner, inner->set_.iterator_at(std::get<1>(res))), 
4156
                std::get<2>(res)};
4157
    }
4158
4159
    
4160
};
4161
4162
4163
// Constructs T into uninitialized storage pointed by `ptr` using the args
4164
// specified in the tuple.
4165
// ----------------------------------------------------------------------------
4166
template <class Alloc, class T, class Tuple>
4167
void ConstructFromTuple(Alloc* alloc, T* ptr, Tuple&& t) {
4168
    memory_internal::ConstructFromTupleImpl(
4169
        alloc, ptr, std::forward<Tuple>(t),
4170
        phmap::make_index_sequence<
4171
        std::tuple_size<typename std::decay<Tuple>::type>::value>());
4172
}
4173
4174
// Constructs T using the args specified in the tuple and calls F with the
4175
// constructed value.
4176
// ----------------------------------------------------------------------------
4177
template <class T, class Tuple, class F>
4178
decltype(std::declval<F>()(std::declval<T>())) WithConstructed(
4179
    Tuple&& t, F&& f) {
4180
    return memory_internal::WithConstructedImpl<T>(
4181
        std::forward<Tuple>(t),
4182
        phmap::make_index_sequence<
4183
        std::tuple_size<typename std::decay<Tuple>::type>::value>(),
4184
        std::forward<F>(f));
4185
}
4186
4187
// ----------------------------------------------------------------------------
4188
// Given arguments of an std::pair's consructor, PairArgs() returns a pair of
4189
// tuples with references to the passed arguments. The tuples contain
4190
// constructor arguments for the first and the second elements of the pair.
4191
//
4192
// The following two snippets are equivalent.
4193
//
4194
// 1. std::pair<F, S> p(args...);
4195
//
4196
// 2. auto a = PairArgs(args...);
4197
//    std::pair<F, S> p(std::piecewise_construct,
4198
//                      std::move(p.first), std::move(p.second));
4199
// ----------------------------------------------------------------------------
4200
0
inline std::pair<std::tuple<>, std::tuple<>> PairArgs() { return {}; }
4201
4202
template <class F, class S>
4203
20.3M
std::pair<std::tuple<F&&>, std::tuple<S&&>> PairArgs(F&& f, S&& s) {
4204
20.3M
  return {std::piecewise_construct, std::forward_as_tuple(std::forward<F>(f)),
4205
20.3M
          std::forward_as_tuple(std::forward<S>(s))};
4206
20.3M
}
std::__1::pair<std::__1::tuple<unsigned int const&>, std::__1::tuple<int const&> > phmap::priv::PairArgs<unsigned int const&, int const&>(unsigned int const&, int const&)
Line
Count
Source
4203
11.8M
std::pair<std::tuple<F&&>, std::tuple<S&&>> PairArgs(F&& f, S&& s) {
4204
11.8M
  return {std::piecewise_construct, std::forward_as_tuple(std::forward<F>(f)),
4205
11.8M
          std::forward_as_tuple(std::forward<S>(s))};
4206
11.8M
}
std::__1::pair<std::__1::tuple<unsigned int const&&>, std::__1::tuple<int&&> > phmap::priv::PairArgs<unsigned int const, int>(unsigned int const&&, int&&)
Line
Count
Source
4203
8.52M
std::pair<std::tuple<F&&>, std::tuple<S&&>> PairArgs(F&& f, S&& s) {
4204
8.52M
  return {std::piecewise_construct, std::forward_as_tuple(std::forward<F>(f)),
4205
8.52M
          std::forward_as_tuple(std::forward<S>(s))};
4206
8.52M
}
4207
4208
template <class F, class S>
4209
std::pair<std::tuple<const F&>, std::tuple<const S&>> PairArgs(
4210
11.8M
    const std::pair<F, S>& p) {
4211
11.8M
    return PairArgs(p.first, p.second);
4212
11.8M
}
4213
4214
template <class F, class S>
4215
8.52M
std::pair<std::tuple<F&&>, std::tuple<S&&>> PairArgs(std::pair<F, S>&& p) {
4216
8.52M
    return PairArgs(std::forward<F>(p.first), std::forward<S>(p.second));
4217
8.52M
}
4218
4219
template <class F, class S>
4220
auto PairArgs(std::piecewise_construct_t, F&& f, S&& s)
4221
    -> decltype(std::make_pair(memory_internal::TupleRef(std::forward<F>(f)),
4222
                               memory_internal::TupleRef(std::forward<S>(s)))) {
4223
    return std::make_pair(memory_internal::TupleRef(std::forward<F>(f)),
4224
                          memory_internal::TupleRef(std::forward<S>(s)));
4225
}
4226
4227
// A helper function for implementing apply() in map policies.
4228
// ----------------------------------------------------------------------------
4229
template <class F, class... Args>
4230
auto DecomposePair(F&& f, Args&&... args)
4231
    -> decltype(memory_internal::DecomposePairImpl(
4232
20.3M
        std::forward<F>(f), PairArgs(std::forward<Args>(args)...))) {
4233
20.3M
    return memory_internal::DecomposePairImpl(
4234
20.3M
        std::forward<F>(f), PairArgs(std::forward<Args>(args)...));
4235
20.3M
}
_ZN5phmap4priv13DecomposePairINS0_12raw_hash_setINS0_17FlatHashMapPolicyIjiEENS_4HashIjEENS_7EqualToIjEENSt3__19allocatorINS9_4pairIKjiEEEEE19EmplaceDecomposableEJSD_EEEDTclsr15memory_internalE17DecomposePairImplclsr3stdE7forwardIT_Efp_Ecl8PairArgsspclsr3stdE7forwardIT0_Efp0_EEEEOSH_DpOSI_
Line
Count
Source
4232
8.52M
        std::forward<F>(f), PairArgs(std::forward<Args>(args)...))) {
4233
8.52M
    return memory_internal::DecomposePairImpl(
4234
8.52M
        std::forward<F>(f), PairArgs(std::forward<Args>(args)...));
4235
8.52M
}
_ZN5phmap4priv13DecomposePairINS0_12raw_hash_setINS0_17FlatHashMapPolicyIjiEENS_4HashIjEENS_7EqualToIjEENSt3__19allocatorINS9_4pairIKjiEEEEE12EqualElementIjEEJRSD_EEEDTclsr15memory_internalE17DecomposePairImplclsr3stdE7forwardIT_Efp_Ecl8PairArgsspclsr3stdE7forwardIT0_Efp0_EEEEOSJ_DpOSK_
Line
Count
Source
4232
10.1M
        std::forward<F>(f), PairArgs(std::forward<Args>(args)...))) {
4233
10.1M
    return memory_internal::DecomposePairImpl(
4234
10.1M
        std::forward<F>(f), PairArgs(std::forward<Args>(args)...));
4235
10.1M
}
_ZN5phmap4priv13DecomposePairINS0_12raw_hash_setINS0_17FlatHashMapPolicyIjiEENS_4HashIjEENS_7EqualToIjEENSt3__19allocatorINS9_4pairIKjiEEEEE11HashElementEJRSD_EEEDTclsr15memory_internalE17DecomposePairImplclsr3stdE7forwardIT_Efp_Ecl8PairArgsspclsr3stdE7forwardIT0_Efp0_EEEEOSI_DpOSJ_
Line
Count
Source
4232
1.07M
        std::forward<F>(f), PairArgs(std::forward<Args>(args)...))) {
4233
1.07M
    return memory_internal::DecomposePairImpl(
4234
1.07M
        std::forward<F>(f), PairArgs(std::forward<Args>(args)...));
4235
1.07M
}
_ZN5phmap4priv13DecomposePairINS0_12raw_hash_setINS0_17FlatHashMapPolicyIjiEENS_4HashIjEENS_7EqualToIjEENSt3__19allocatorINS9_4pairIKjiEEEEE11HashElementEJRKSD_EEEDTclsr15memory_internalE17DecomposePairImplclsr3stdE7forwardIT_Efp_Ecl8PairArgsspclsr3stdE7forwardIT0_Efp0_EEEEOSJ_DpOSK_
Line
Count
Source
4232
601k
        std::forward<F>(f), PairArgs(std::forward<Args>(args)...))) {
4233
601k
    return memory_internal::DecomposePairImpl(
4234
601k
        std::forward<F>(f), PairArgs(std::forward<Args>(args)...));
4235
601k
}
4236
4237
// A helper function for implementing apply() in set policies.
4238
// ----------------------------------------------------------------------------
4239
template <class F, class Arg>
4240
decltype(std::declval<F>()(std::declval<const Arg&>(), std::declval<Arg>()))
4241
DecomposeValue(F&& f, Arg&& arg) {
4242
    const auto& key = arg;
4243
    return std::forward<F>(f)(key, std::forward<Arg>(arg));
4244
}
4245
4246
4247
// --------------------------------------------------------------------------
4248
// Policy: a policy defines how to perform different operations on
4249
// the slots of the hashtable (see hash_policy_traits.h for the full interface
4250
// of policy).
4251
//
4252
// Hash: a (possibly polymorphic) functor that hashes keys of the hashtable. The
4253
// functor should accept a key and return size_t as hash. For best performance
4254
// it is important that the hash function provides high entropy across all bits
4255
// of the hash.
4256
//
4257
// Eq: a (possibly polymorphic) functor that compares two keys for equality. It
4258
// should accept two (of possibly different type) keys and return a bool: true
4259
// if they are equal, false if they are not. If two keys compare equal, then
4260
// their hash values as defined by Hash MUST be equal.
4261
//
4262
// Allocator: an Allocator [https://devdocs.io/cpp/concept/allocator] with which
4263
// the storage of the hashtable will be allocated and the elements will be
4264
// constructed and destroyed.
4265
// --------------------------------------------------------------------------
4266
template <class T>
4267
struct FlatHashSetPolicy 
4268
{
4269
    using slot_type = T;
4270
    using key_type = T;
4271
    using init_type = T;
4272
    using constant_iterators = std::true_type;
4273
    using is_flat = std::true_type;
4274
4275
    template <class Allocator, class... Args>
4276
    static void construct(Allocator* alloc, slot_type* slot, Args&&... args) {
4277
        phmap::allocator_traits<Allocator>::construct(*alloc, slot,
4278
                                                      std::forward<Args>(args)...);
4279
    }
4280
4281
    template <class Allocator>
4282
    static void destroy(Allocator* alloc, slot_type* slot) {
4283
        phmap::allocator_traits<Allocator>::destroy(*alloc, slot);
4284
    }
4285
4286
    template <class Allocator>
4287
    static void transfer(Allocator* alloc, slot_type* new_slot,
4288
                         slot_type* old_slot) {
4289
        construct(alloc, new_slot, std::move(*old_slot));
4290
        destroy(alloc, old_slot);
4291
    }
4292
4293
    static T& element(slot_type* slot) { return *slot; }
4294
4295
    template <class F, class... Args>
4296
    static decltype(phmap::priv::DecomposeValue(
4297
                        std::declval<F>(), std::declval<Args>()...))
4298
    apply(F&& f, Args&&... args) {
4299
        return phmap::priv::DecomposeValue(
4300
            std::forward<F>(f), std::forward<Args>(args)...);
4301
    }
4302
4303
    static size_t space_used(const T*) { return 0; }
4304
};
4305
4306
// --------------------------------------------------------------------------
4307
// --------------------------------------------------------------------------
4308
template <class K, class V>
4309
struct FlatHashMapPolicy 
4310
{
4311
    using slot_policy = priv::map_slot_policy<K, V>;
4312
    using slot_type = typename slot_policy::slot_type;
4313
    using key_type = K;
4314
    using mapped_type = V;
4315
    using init_type = std::pair</*non const*/ key_type, mapped_type>;
4316
    using is_flat = std::true_type;
4317
4318
    template <class Allocator, class... Args>
4319
614k
    static void construct(Allocator* alloc, slot_type* slot, Args&&... args) {
4320
614k
        slot_policy::construct(alloc, slot, std::forward<Args>(args)...);
4321
614k
    }
4322
4323
    template <class Allocator>
4324
0
    static void destroy(Allocator* alloc, slot_type* slot) {
4325
0
        slot_policy::destroy(alloc, slot);
4326
0
    }
4327
4328
    template <class Allocator>
4329
    static void transfer(Allocator* alloc, slot_type* new_slot,
4330
1.07M
                         slot_type* old_slot) {
4331
1.07M
        slot_policy::transfer(alloc, new_slot, old_slot);
4332
1.07M
    }
4333
4334
    template <class F, class... Args>
4335
    static decltype(phmap::priv::DecomposePair(
4336
                        std::declval<F>(), std::declval<Args>()...))
4337
20.3M
    apply(F&& f, Args&&... args) {
4338
20.3M
        return phmap::priv::DecomposePair(std::forward<F>(f),
4339
20.3M
                                                        std::forward<Args>(args)...);
4340
20.3M
    }
_ZN5phmap4priv17FlatHashMapPolicyIjiE5applyINS0_12raw_hash_setIS2_NS_4HashIjEENS_7EqualToIjEENSt3__19allocatorINS9_4pairIKjiEEEEE19EmplaceDecomposableEJSD_EEEDTclsr5phmap4privE13DecomposePairclsr3stdE7declvalIT_EEspclsr3stdE7declvalIT0_EEEEOSH_DpOSI_
Line
Count
Source
4337
8.52M
    apply(F&& f, Args&&... args) {
4338
8.52M
        return phmap::priv::DecomposePair(std::forward<F>(f),
4339
8.52M
                                                        std::forward<Args>(args)...);
4340
8.52M
    }
_ZN5phmap4priv17FlatHashMapPolicyIjiE5applyINS0_12raw_hash_setIS2_NS_4HashIjEENS_7EqualToIjEENSt3__19allocatorINS9_4pairIKjiEEEEE12EqualElementIjEEJRSD_EEEDTclsr5phmap4privE13DecomposePairclsr3stdE7declvalIT_EEspclsr3stdE7declvalIT0_EEEEOSJ_DpOSK_
Line
Count
Source
4337
10.1M
    apply(F&& f, Args&&... args) {
4338
10.1M
        return phmap::priv::DecomposePair(std::forward<F>(f),
4339
10.1M
                                                        std::forward<Args>(args)...);
4340
10.1M
    }
_ZN5phmap4priv17FlatHashMapPolicyIjiE5applyINS0_12raw_hash_setIS2_NS_4HashIjEENS_7EqualToIjEENSt3__19allocatorINS9_4pairIKjiEEEEE11HashElementEJRSD_EEEDTclsr5phmap4privE13DecomposePairclsr3stdE7declvalIT_EEspclsr3stdE7declvalIT0_EEEEOSI_DpOSJ_
Line
Count
Source
4337
1.07M
    apply(F&& f, Args&&... args) {
4338
1.07M
        return phmap::priv::DecomposePair(std::forward<F>(f),
4339
1.07M
                                                        std::forward<Args>(args)...);
4340
1.07M
    }
_ZN5phmap4priv17FlatHashMapPolicyIjiE5applyINS0_12raw_hash_setIS2_NS_4HashIjEENS_7EqualToIjEENSt3__19allocatorINS9_4pairIKjiEEEEE11HashElementEJRKSD_EEEDTclsr5phmap4privE13DecomposePairclsr3stdE7declvalIT_EEspclsr3stdE7declvalIT0_EEEEOSJ_DpOSK_
Line
Count
Source
4337
601k
    apply(F&& f, Args&&... args) {
4338
601k
        return phmap::priv::DecomposePair(std::forward<F>(f),
4339
601k
                                                        std::forward<Args>(args)...);
4340
601k
    }
4341
4342
    static size_t space_used(const slot_type*) { return 0; }
4343
4344
12.4M
    static std::pair<const K, V>& element(slot_type* slot) { return slot->value; }
phmap::priv::FlatHashMapPolicy<unsigned int, int>::element(phmap::priv::map_slot_type<unsigned int, int>*)
Line
Count
Source
4344
12.4M
    static std::pair<const K, V>& element(slot_type* slot) { return slot->value; }
Unexecuted instantiation: phmap::priv::FlatHashMapPolicy<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::element(phmap::priv::map_slot_type<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >*)
4345
4346
    static V& value(std::pair<const K, V>* kv) { return kv->second; }
4347
    static const V& value(const std::pair<const K, V>* kv) { return kv->second; }
4348
};
4349
4350
template <class Reference, class Policy>
4351
struct node_hash_policy {
4352
    static_assert(std::is_lvalue_reference<Reference>::value, "");
4353
4354
    using slot_type = typename std::remove_cv<
4355
        typename std::remove_reference<Reference>::type>::type*;
4356
4357
    template <class Alloc, class... Args>
4358
    static void construct(Alloc* alloc, slot_type* slot, Args&&... args) {
4359
        *slot = Policy::new_element(alloc, std::forward<Args>(args)...);
4360
    }
4361
4362
    template <class Alloc>
4363
    static void destroy(Alloc* alloc, slot_type* slot) {
4364
        Policy::delete_element(alloc, *slot);
4365
    }
4366
4367
    template <class Alloc>
4368
    static void transfer(Alloc*, slot_type* new_slot, slot_type* old_slot) {
4369
        *new_slot = *old_slot;
4370
    }
4371
4372
    static size_t space_used(const slot_type* slot) {
4373
        if (slot == nullptr) return Policy::element_space_used(nullptr);
4374
        return Policy::element_space_used(*slot);
4375
    }
4376
4377
    static Reference element(slot_type* slot) { return **slot; }
4378
4379
    template <class T, class P = Policy>
4380
    static auto value(T* elem) -> decltype(P::value(elem)) {
4381
        return P::value(elem);
4382
    }
4383
4384
    template <class... Ts, class P = Policy>
4385
    static auto apply(Ts&&... ts) -> decltype(P::apply(std::forward<Ts>(ts)...)) {
4386
        return P::apply(std::forward<Ts>(ts)...);
4387
    }
4388
};
4389
4390
// --------------------------------------------------------------------------
4391
// --------------------------------------------------------------------------
4392
template <class T>
4393
struct NodeHashSetPolicy
4394
    : phmap::priv::node_hash_policy<T&, NodeHashSetPolicy<T>> 
4395
{
4396
    using key_type = T;
4397
    using init_type = T;
4398
    using constant_iterators = std::true_type;
4399
    using is_flat = std::false_type;
4400
4401
    template <class Allocator, class... Args>
4402
        static T* new_element(Allocator* alloc, Args&&... args) {
4403
        using ValueAlloc =
4404
            typename phmap::allocator_traits<Allocator>::template rebind_alloc<T>;
4405
        ValueAlloc value_alloc(*alloc);
4406
        T* res = phmap::allocator_traits<ValueAlloc>::allocate(value_alloc, 1);
4407
        phmap::allocator_traits<ValueAlloc>::construct(value_alloc, res,
4408
                                                       std::forward<Args>(args)...);
4409
        return res;
4410
    }
4411
4412
    template <class Allocator>
4413
        static void delete_element(Allocator* alloc, T* elem) {
4414
        using ValueAlloc =
4415
            typename phmap::allocator_traits<Allocator>::template rebind_alloc<T>;
4416
        ValueAlloc value_alloc(*alloc);
4417
        phmap::allocator_traits<ValueAlloc>::destroy(value_alloc, elem);
4418
        phmap::allocator_traits<ValueAlloc>::deallocate(value_alloc, elem, 1);
4419
    }
4420
4421
    template <class F, class... Args>
4422
        static decltype(phmap::priv::DecomposeValue(
4423
                            std::declval<F>(), std::declval<Args>()...))
4424
        apply(F&& f, Args&&... args) {
4425
        return phmap::priv::DecomposeValue(
4426
            std::forward<F>(f), std::forward<Args>(args)...);
4427
    }
4428
4429
    static size_t element_space_used(const T*) { return sizeof(T); }
4430
};
4431
4432
// --------------------------------------------------------------------------
4433
// --------------------------------------------------------------------------
4434
template <class Key, class Value>
4435
class NodeHashMapPolicy
4436
    : public phmap::priv::node_hash_policy<
4437
          std::pair<const Key, Value>&, NodeHashMapPolicy<Key, Value>> 
4438
{
4439
    using value_type = std::pair<const Key, Value>;
4440
4441
public:
4442
    using key_type = Key;
4443
    using mapped_type = Value;
4444
    using init_type = std::pair</*non const*/ key_type, mapped_type>;
4445
    using is_flat = std::false_type;
4446
4447
    template <class Allocator, class... Args>
4448
        static value_type* new_element(Allocator* alloc, Args&&... args) {
4449
        using PairAlloc = typename phmap::allocator_traits<
4450
            Allocator>::template rebind_alloc<value_type>;
4451
        PairAlloc pair_alloc(*alloc);
4452
        value_type* res =
4453
            phmap::allocator_traits<PairAlloc>::allocate(pair_alloc, 1);
4454
        phmap::allocator_traits<PairAlloc>::construct(pair_alloc, res,
4455
                                                      std::forward<Args>(args)...);
4456
        return res;
4457
    }
4458
4459
    template <class Allocator>
4460
        static void delete_element(Allocator* alloc, value_type* pair) {
4461
        using PairAlloc = typename phmap::allocator_traits<
4462
            Allocator>::template rebind_alloc<value_type>;
4463
        PairAlloc pair_alloc(*alloc);
4464
        phmap::allocator_traits<PairAlloc>::destroy(pair_alloc, pair);
4465
        phmap::allocator_traits<PairAlloc>::deallocate(pair_alloc, pair, 1);
4466
    }
4467
4468
    template <class F, class... Args>
4469
        static decltype(phmap::priv::DecomposePair(
4470
                            std::declval<F>(), std::declval<Args>()...))
4471
        apply(F&& f, Args&&... args) {
4472
        return phmap::priv::DecomposePair(std::forward<F>(f),
4473
                                                        std::forward<Args>(args)...);
4474
    }
4475
4476
    static size_t element_space_used(const value_type*) {
4477
        return sizeof(value_type);
4478
    }
4479
4480
    static Value& value(value_type* elem) { return elem->second; }
4481
    static const Value& value(const value_type* elem) { return elem->second; }
4482
};
4483
4484
4485
// --------------------------------------------------------------------------
4486
//  hash_default
4487
// --------------------------------------------------------------------------
4488
4489
#if PHMAP_HAVE_STD_STRING_VIEW
4490
4491
// Supports heterogeneous lookup for basic_string<T>-like elements.
4492
template<class CharT> 
4493
struct StringHashEqT
4494
{
4495
    struct Hash 
4496
    {
4497
        using is_transparent = void;
4498
        
4499
        size_t operator()(std::basic_string_view<CharT> v) const {
4500
            std::string_view bv{
4501
                reinterpret_cast<const char*>(v.data()), v.size() * sizeof(CharT)};
4502
            return std::hash<std::string_view>()(bv);
4503
        }
4504
    };
4505
4506
    struct Eq {
4507
        using is_transparent = void;
4508
4509
        bool operator()(std::basic_string_view<CharT> lhs,
4510
                        std::basic_string_view<CharT> rhs) const {
4511
            return lhs == rhs;
4512
        }
4513
    };
4514
};
4515
4516
template <>
4517
struct HashEq<std::string> : StringHashEqT<char> {};
4518
4519
template <>
4520
struct HashEq<std::string_view> : StringHashEqT<char> {};
4521
4522
// char16_t
4523
template <>
4524
struct HashEq<std::u16string> : StringHashEqT<char16_t> {};
4525
4526
template <>
4527
struct HashEq<std::u16string_view> : StringHashEqT<char16_t> {};
4528
4529
// wchar_t
4530
template <>
4531
struct HashEq<std::wstring> : StringHashEqT<wchar_t> {};
4532
4533
template <>
4534
struct HashEq<std::wstring_view> : StringHashEqT<wchar_t> {};
4535
4536
#endif
4537
4538
// Supports heterogeneous lookup for pointers and smart pointers.
4539
// -------------------------------------------------------------
4540
template <class T>
4541
struct HashEq<T*> 
4542
{
4543
    struct Hash {
4544
        using is_transparent = void;
4545
        template <class U>
4546
        size_t operator()(const U& ptr) const {
4547
            // we want phmap::Hash<T*> and not phmap::Hash<const T*>
4548
            // so "struct std::hash<T*> " override works
4549
            return phmap::Hash<T*>{}((T*)(uintptr_t)HashEq::ToPtr(ptr));
4550
        }
4551
    };
4552
4553
    struct Eq {
4554
        using is_transparent = void;
4555
        template <class A, class B>
4556
        bool operator()(const A& a, const B& b) const {
4557
            return HashEq::ToPtr(a) == HashEq::ToPtr(b);
4558
        }
4559
    };
4560
4561
private:
4562
    static const T* ToPtr(const T* ptr) { return ptr; }
4563
4564
    template <class U, class D>
4565
    static const T* ToPtr(const std::unique_ptr<U, D>& ptr) {
4566
        return ptr.get();
4567
    }
4568
4569
    template <class U>
4570
    static const T* ToPtr(const std::shared_ptr<U>& ptr) {
4571
        return ptr.get();
4572
    }
4573
};
4574
4575
template <class T, class D>
4576
struct HashEq<std::unique_ptr<T, D>> : HashEq<T*> {};
4577
4578
template <class T>
4579
struct HashEq<std::shared_ptr<T>> : HashEq<T*> {};
4580
4581
namespace hashtable_debug_internal {
4582
4583
// --------------------------------------------------------------------------
4584
// --------------------------------------------------------------------------
4585
4586
template<typename, typename = void >
4587
struct has_member_type_raw_hash_set : std::false_type
4588
{};
4589
template<typename T>
4590
struct has_member_type_raw_hash_set<T, phmap::void_t<typename T::raw_hash_set>> : std::true_type
4591
{};
4592
4593
template <typename Set>
4594
struct HashtableDebugAccess<Set, typename std::enable_if<has_member_type_raw_hash_set<Set>::value>::type>
4595
{
4596
    using Traits = typename Set::PolicyTraits;
4597
    using Slot = typename Traits::slot_type;
4598
4599
    static size_t GetNumProbes(const Set& set,
4600
                               const typename Set::key_type& key) {
4601
        if (!set.ctrl_)
4602
            return 0;
4603
        size_t num_probes = 0;
4604
        size_t hashval = set.hash(key); 
4605
        auto seq = set.probe(hashval);
4606
        while (true) {
4607
            priv::Group g{set.ctrl_ + seq.offset()};
4608
            for (uint32_t i : g.Match((h2_t)priv::H2(hashval))) {
4609
                if (Traits::apply(
4610
                        typename Set::template EqualElement<typename Set::key_type>{
4611
                            key, set.eq_ref()},
4612
                        Traits::element(set.slots_ + seq.offset((size_t)i))))
4613
                    return num_probes;
4614
                ++num_probes;
4615
            }
4616
            if (g.MatchEmpty()) return num_probes;
4617
            seq.next();
4618
            ++num_probes;
4619
        }
4620
    }
4621
4622
    static size_t AllocatedByteSize(const Set& c) {
4623
        size_t capacity = c.capacity_;
4624
        if (capacity == 0) return 0;
4625
        auto layout = Set::MakeLayout(capacity);
4626
        size_t m = layout.AllocSize();
4627
4628
        size_t per_slot = Traits::space_used(static_cast<const Slot*>(nullptr));
4629
        if (per_slot != ~size_t{}) {
4630
            m += per_slot * c.size();
4631
        } else {
4632
            for (size_t i = 0; i != capacity; ++i) {
4633
                if (priv::IsFull(c.ctrl_[i])) {
4634
                    m += Traits::space_used(c.slots_ + i);
4635
                }
4636
            }
4637
        }
4638
        return m;
4639
    }
4640
4641
    static size_t LowerBoundAllocatedByteSize(size_t size) {
4642
        size_t capacity = GrowthToLowerboundCapacity(size);
4643
        if (capacity == 0) return 0;
4644
        auto layout = Set::MakeLayout(NormalizeCapacity(capacity));
4645
        size_t m = layout.AllocSize();
4646
        size_t per_slot = Traits::space_used(static_cast<const Slot*>(nullptr));
4647
        if (per_slot != ~size_t{}) {
4648
            m += per_slot * size;
4649
        }
4650
        return m;
4651
    }
4652
};
4653
4654
4655
template<typename, typename = void >
4656
struct has_member_type_EmbeddedSet : std::false_type
4657
{};
4658
template<typename T>
4659
struct has_member_type_EmbeddedSet<T, phmap::void_t<typename T::EmbeddedSet>> : std::true_type
4660
{};
4661
4662
template <typename Set>
4663
struct HashtableDebugAccess<Set, typename std::enable_if<has_member_type_EmbeddedSet<Set>::value>::type> {
4664
    using Traits = typename Set::PolicyTraits;
4665
    using Slot = typename Traits::slot_type;
4666
    using EmbeddedSet = typename Set::EmbeddedSet;
4667
4668
    static size_t GetNumProbes(const Set& set, const typename Set::key_type& key) {
4669
        size_t hashval = set.hash(key);
4670
        auto& inner = set.sets_[set.subidx(hashval)];
4671
        auto& inner_set = inner.set_;
4672
        return HashtableDebugAccess<EmbeddedSet>::GetNumProbes(inner_set, key);
4673
    }
4674
};
4675
4676
}  // namespace hashtable_debug_internal
4677
}  // namespace priv
4678
4679
// -----------------------------------------------------------------------------
4680
// phmap::flat_hash_set
4681
// -----------------------------------------------------------------------------
4682
// An `phmap::flat_hash_set<T>` is an unordered associative container which has
4683
// been optimized for both speed and memory footprint in most common use cases.
4684
// Its interface is similar to that of `std::unordered_set<T>` with the
4685
// following notable differences:
4686
//
4687
// * Supports heterogeneous lookup, through `find()`, `operator[]()` and
4688
//   `insert()`, provided that the set is provided a compatible heterogeneous
4689
//   hashing function and equality operator.
4690
// * Invalidates any references and pointers to elements within the table after
4691
//   `rehash()`.
4692
// * Contains a `capacity()` member function indicating the number of element
4693
//   slots (open, deleted, and empty) within the hash set.
4694
// * Returns `void` from the `_erase(iterator)` overload.
4695
// -----------------------------------------------------------------------------
4696
template <class T, class Hash, class Eq, class Alloc> // default values in phmap_fwd_decl.h
4697
class flat_hash_set
4698
    : public phmap::priv::raw_hash_set<
4699
          phmap::priv::FlatHashSetPolicy<T>, Hash, Eq, Alloc> 
4700
{
4701
    using Base = typename flat_hash_set::raw_hash_set;
4702
4703
public:
4704
    flat_hash_set() {}
4705
#ifdef __INTEL_COMPILER
4706
    using Base::raw_hash_set;
4707
#else
4708
    using Base::Base;
4709
#endif
4710
    using Base::begin;
4711
    using Base::cbegin;
4712
    using Base::cend;
4713
    using Base::end;
4714
    using Base::capacity;
4715
    using Base::empty;
4716
    using Base::max_size;
4717
    using Base::size;
4718
    using Base::clear; // may shrink - To avoid shrinking `erase(begin(), end())`
4719
    using Base::erase;
4720
    using Base::insert;
4721
    using Base::emplace;
4722
    using Base::emplace_hint; 
4723
    using Base::extract;
4724
    using Base::merge;
4725
    using Base::swap;
4726
    using Base::rehash;
4727
    using Base::reserve;
4728
    using Base::contains;
4729
    using Base::count;
4730
    using Base::equal_range;
4731
    using Base::find;
4732
    using Base::bucket_count;
4733
    using Base::load_factor;
4734
    using Base::max_load_factor;
4735
    using Base::get_allocator;
4736
    using Base::hash_function;
4737
    using Base::hash;
4738
    using Base::key_eq;
4739
};
4740
4741
// -----------------------------------------------------------------------------
4742
// phmap::flat_hash_map
4743
// -----------------------------------------------------------------------------
4744
//
4745
// An `phmap::flat_hash_map<K, V>` is an unordered associative container which
4746
// has been optimized for both speed and memory footprint in most common use
4747
// cases. Its interface is similar to that of `std::unordered_map<K, V>` with
4748
// the following notable differences:
4749
//
4750
// * Supports heterogeneous lookup, through `find()`, `operator[]()` and
4751
//   `insert()`, provided that the map is provided a compatible heterogeneous
4752
//   hashing function and equality operator.
4753
// * Invalidates any references and pointers to elements within the table after
4754
//   `rehash()`.
4755
// * Contains a `capacity()` member function indicating the number of element
4756
//   slots (open, deleted, and empty) within the hash map.
4757
// * Returns `void` from the `_erase(iterator)` overload.
4758
// -----------------------------------------------------------------------------
4759
template <class K, class V, class Hash, class Eq, class Alloc> // default values in phmap_fwd_decl.h
4760
class flat_hash_map : public phmap::priv::raw_hash_map<
4761
                          phmap::priv::FlatHashMapPolicy<K, V>,
4762
                          Hash, Eq, Alloc> {
4763
    using Base = typename flat_hash_map::raw_hash_map;
4764
4765
public:
4766
1.44k
    flat_hash_map() {}
phmap::flat_hash_map<unsigned int, int, phmap::Hash<unsigned int>, phmap::EqualTo<unsigned int>, std::__1::allocator<std::__1::pair<unsigned int const, int> > >::flat_hash_map()
Line
Count
Source
4766
966
    flat_hash_map() {}
phmap::flat_hash_map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, phmap::priv::StringHashEqT<char>::Hash, phmap::priv::StringHashEqT<char>::Eq, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > >::flat_hash_map()
Line
Count
Source
4766
483
    flat_hash_map() {}
4767
#ifdef __INTEL_COMPILER
4768
    using Base::raw_hash_map;
4769
#else
4770
    using Base::Base;
4771
#endif
4772
    using Base::begin;
4773
    using Base::cbegin;
4774
    using Base::cend;
4775
    using Base::end;
4776
    using Base::capacity;
4777
    using Base::empty;
4778
    using Base::max_size;
4779
    using Base::size;
4780
    using Base::clear;
4781
    using Base::erase;
4782
    using Base::insert;
4783
    using Base::insert_or_assign;
4784
    using Base::emplace;
4785
    using Base::emplace_hint;
4786
    using Base::try_emplace;
4787
    using Base::extract;
4788
    using Base::merge;
4789
    using Base::swap;
4790
    using Base::rehash;
4791
    using Base::reserve;
4792
    using Base::at;
4793
    using Base::contains;
4794
    using Base::count;
4795
    using Base::equal_range;
4796
    using Base::find;
4797
    using Base::operator[];
4798
    using Base::bucket_count;
4799
    using Base::load_factor;
4800
    using Base::max_load_factor;
4801
    using Base::get_allocator;
4802
    using Base::hash_function;
4803
    using Base::hash;
4804
    using Base::key_eq;
4805
};
4806
4807
// -----------------------------------------------------------------------------
4808
// phmap::node_hash_set
4809
// -----------------------------------------------------------------------------
4810
// An `phmap::node_hash_set<T>` is an unordered associative container which
4811
// has been optimized for both speed and memory footprint in most common use
4812
// cases. Its interface is similar to that of `std::unordered_set<T>` with the
4813
// following notable differences:
4814
//
4815
// * Supports heterogeneous lookup, through `find()`, `operator[]()` and
4816
//   `insert()`, provided that the map is provided a compatible heterogeneous
4817
//   hashing function and equality operator.
4818
// * Contains a `capacity()` member function indicating the number of element
4819
//   slots (open, deleted, and empty) within the hash set.
4820
// * Returns `void` from the `_erase(iterator)` overload.
4821
// -----------------------------------------------------------------------------
4822
template <class T, class Hash, class Eq, class Alloc> // default values in phmap_fwd_decl.h
4823
class node_hash_set
4824
    : public phmap::priv::raw_hash_set<
4825
          phmap::priv::NodeHashSetPolicy<T>, Hash, Eq, Alloc> 
4826
{
4827
    using Base = typename node_hash_set::raw_hash_set;
4828
4829
public:
4830
    node_hash_set() {}
4831
#ifdef __INTEL_COMPILER
4832
    using Base::raw_hash_set;
4833
#else
4834
    using Base::Base;
4835
#endif
4836
    using Base::begin;
4837
    using Base::cbegin;
4838
    using Base::cend;
4839
    using Base::end;
4840
    using Base::capacity;
4841
    using Base::empty;
4842
    using Base::max_size;
4843
    using Base::size;
4844
    using Base::clear;
4845
    using Base::erase;
4846
    using Base::insert;
4847
    using Base::emplace;
4848
    using Base::emplace_hint;
4849
    using Base::emplace_with_hash;
4850
    using Base::emplace_hint_with_hash;
4851
    using Base::extract;
4852
    using Base::merge;
4853
    using Base::swap;
4854
    using Base::rehash;
4855
    using Base::reserve;
4856
    using Base::contains;
4857
    using Base::count;
4858
    using Base::equal_range;
4859
    using Base::find;
4860
    using Base::bucket_count;
4861
    using Base::load_factor;
4862
    using Base::max_load_factor;
4863
    using Base::get_allocator;
4864
    using Base::hash_function;
4865
    using Base::hash;
4866
    using Base::key_eq;
4867
    typename Base::hasher hash_funct() { return this->hash_function(); }
4868
    void resize(typename Base::size_type hint) { this->rehash(hint); }
4869
};
4870
4871
// -----------------------------------------------------------------------------
4872
// phmap::node_hash_map
4873
// -----------------------------------------------------------------------------
4874
//
4875
// An `phmap::node_hash_map<K, V>` is an unordered associative container which
4876
// has been optimized for both speed and memory footprint in most common use
4877
// cases. Its interface is similar to that of `std::unordered_map<K, V>` with
4878
// the following notable differences:
4879
//
4880
// * Supports heterogeneous lookup, through `find()`, `operator[]()` and
4881
//   `insert()`, provided that the map is provided a compatible heterogeneous
4882
//   hashing function and equality operator.
4883
// * Contains a `capacity()` member function indicating the number of element
4884
//   slots (open, deleted, and empty) within the hash map.
4885
// * Returns `void` from the `_erase(iterator)` overload.
4886
// -----------------------------------------------------------------------------
4887
template <class Key, class Value, class Hash, class Eq, class Alloc>  // default values in phmap_fwd_decl.h
4888
class node_hash_map
4889
    : public phmap::priv::raw_hash_map<
4890
          phmap::priv::NodeHashMapPolicy<Key, Value>, Hash, Eq,
4891
          Alloc> 
4892
{
4893
    using Base = typename node_hash_map::raw_hash_map;
4894
4895
public:
4896
    node_hash_map() {}
4897
#ifdef __INTEL_COMPILER
4898
    using Base::raw_hash_map;
4899
#else
4900
    using Base::Base;
4901
#endif
4902
    using Base::begin;
4903
    using Base::cbegin;
4904
    using Base::cend;
4905
    using Base::end;
4906
    using Base::capacity;
4907
    using Base::empty;
4908
    using Base::max_size;
4909
    using Base::size;
4910
    using Base::clear;
4911
    using Base::erase;
4912
    using Base::insert;
4913
    using Base::insert_or_assign;
4914
    using Base::emplace;
4915
    using Base::emplace_hint;
4916
    using Base::try_emplace;
4917
    using Base::extract;
4918
    using Base::merge;
4919
    using Base::swap;
4920
    using Base::rehash;
4921
    using Base::reserve;
4922
    using Base::at;
4923
    using Base::contains;
4924
    using Base::count;
4925
    using Base::equal_range;
4926
    using Base::find;
4927
    using Base::operator[];
4928
    using Base::bucket_count;
4929
    using Base::load_factor;
4930
    using Base::max_load_factor;
4931
    using Base::get_allocator;
4932
    using Base::hash_function;
4933
    using Base::hash;
4934
    using Base::key_eq;
4935
    typename Base::hasher hash_funct() { return this->hash_function(); }
4936
    void resize(typename Base::size_type hint) { this->rehash(hint); }
4937
};
4938
4939
// -----------------------------------------------------------------------------
4940
// phmap::parallel_flat_hash_set
4941
// -----------------------------------------------------------------------------
4942
template <class T, class Hash, class Eq, class Alloc, size_t N, class Mtx_> // default values in phmap_fwd_decl.h
4943
class parallel_flat_hash_set
4944
    : public phmap::priv::parallel_hash_set<
4945
         N, phmap::priv::raw_hash_set, Mtx_,
4946
         phmap::priv::FlatHashSetPolicy<T>, 
4947
         Hash, Eq, Alloc> 
4948
{
4949
    using Base = typename parallel_flat_hash_set::parallel_hash_set;
4950
4951
public:
4952
    parallel_flat_hash_set() {}
4953
#ifdef __INTEL_COMPILER
4954
    using Base::parallel_hash_set;
4955
#else
4956
    using Base::Base;
4957
#endif
4958
    using Base::hash;
4959
    using Base::subidx;
4960
    using Base::subcnt;
4961
    using Base::begin;
4962
    using Base::cbegin;
4963
    using Base::cend;
4964
    using Base::end;
4965
    using Base::capacity;
4966
    using Base::empty;
4967
    using Base::max_size;
4968
    using Base::size;
4969
    using Base::clear;
4970
    using Base::erase;
4971
    using Base::insert;
4972
    using Base::emplace;
4973
    using Base::emplace_hint;
4974
    using Base::emplace_with_hash;
4975
    using Base::emplace_hint_with_hash;
4976
    using Base::extract;
4977
    using Base::merge;
4978
    using Base::swap;
4979
    using Base::rehash;
4980
    using Base::reserve;
4981
    using Base::contains;
4982
    using Base::count;
4983
    using Base::equal_range;
4984
    using Base::find;
4985
    using Base::bucket_count;
4986
    using Base::load_factor;
4987
    using Base::max_load_factor;
4988
    using Base::get_allocator;
4989
    using Base::hash_function;
4990
    using Base::key_eq;
4991
};
4992
4993
// -----------------------------------------------------------------------------
4994
// phmap::parallel_flat_hash_map - default values in phmap_fwd_decl.h
4995
// -----------------------------------------------------------------------------
4996
template <class K, class V, class Hash, class Eq, class Alloc, size_t N, class Mtx_>
4997
class parallel_flat_hash_map : public phmap::priv::parallel_hash_map<
4998
                N, phmap::priv::raw_hash_set, Mtx_,
4999
                phmap::priv::FlatHashMapPolicy<K, V>,
5000
                Hash, Eq, Alloc> 
5001
{
5002
    using Base = typename parallel_flat_hash_map::parallel_hash_map;
5003
5004
public:
5005
    parallel_flat_hash_map() {}
5006
#ifdef __INTEL_COMPILER
5007
    using Base::parallel_hash_map;
5008
#else
5009
    using Base::Base;
5010
#endif
5011
    using Base::hash;
5012
    using Base::subidx;
5013
    using Base::subcnt;
5014
    using Base::begin;
5015
    using Base::cbegin;
5016
    using Base::cend;
5017
    using Base::end;
5018
    using Base::capacity;
5019
    using Base::empty;
5020
    using Base::max_size;
5021
    using Base::size;
5022
    using Base::clear;
5023
    using Base::erase;
5024
    using Base::insert;
5025
    using Base::insert_or_assign;
5026
    using Base::emplace;
5027
    using Base::emplace_hint;
5028
    using Base::try_emplace;
5029
    using Base::emplace_with_hash;
5030
    using Base::emplace_hint_with_hash;
5031
    using Base::try_emplace_with_hash;
5032
    using Base::extract;
5033
    using Base::merge;
5034
    using Base::swap;
5035
    using Base::rehash;
5036
    using Base::reserve;
5037
    using Base::at;
5038
    using Base::contains;
5039
    using Base::count;
5040
    using Base::equal_range;
5041
    using Base::find;
5042
    using Base::operator[];
5043
    using Base::bucket_count;
5044
    using Base::load_factor;
5045
    using Base::max_load_factor;
5046
    using Base::get_allocator;
5047
    using Base::hash_function;
5048
    using Base::key_eq;
5049
};
5050
5051
// -----------------------------------------------------------------------------
5052
// phmap::parallel_node_hash_set
5053
// -----------------------------------------------------------------------------
5054
template <class T, class Hash, class Eq, class Alloc, size_t N, class Mtx_>
5055
class parallel_node_hash_set
5056
    : public phmap::priv::parallel_hash_set<
5057
             N, phmap::priv::raw_hash_set, Mtx_,
5058
             phmap::priv::NodeHashSetPolicy<T>, Hash, Eq, Alloc> 
5059
{
5060
    using Base = typename parallel_node_hash_set::parallel_hash_set;
5061
5062
public:
5063
    parallel_node_hash_set() {}
5064
#ifdef __INTEL_COMPILER
5065
    using Base::parallel_hash_set;
5066
#else
5067
    using Base::Base;
5068
#endif
5069
    using Base::hash;
5070
    using Base::subidx;
5071
    using Base::subcnt;
5072
    using Base::begin;
5073
    using Base::cbegin;
5074
    using Base::cend;
5075
    using Base::end;
5076
    using Base::capacity;
5077
    using Base::empty;
5078
    using Base::max_size;
5079
    using Base::size;
5080
    using Base::clear;
5081
    using Base::erase;
5082
    using Base::insert;
5083
    using Base::emplace;
5084
    using Base::emplace_hint;
5085
    using Base::emplace_with_hash;
5086
    using Base::emplace_hint_with_hash;
5087
    using Base::extract;
5088
    using Base::merge;
5089
    using Base::swap;
5090
    using Base::rehash;
5091
    using Base::reserve;
5092
    using Base::contains;
5093
    using Base::count;
5094
    using Base::equal_range;
5095
    using Base::find;
5096
    using Base::bucket_count;
5097
    using Base::load_factor;
5098
    using Base::max_load_factor;
5099
    using Base::get_allocator;
5100
    using Base::hash_function;
5101
    using Base::key_eq;
5102
    typename Base::hasher hash_funct() { return this->hash_function(); }
5103
    void resize(typename Base::size_type hint) { this->rehash(hint); }
5104
};
5105
5106
// -----------------------------------------------------------------------------
5107
// phmap::parallel_node_hash_map
5108
// -----------------------------------------------------------------------------
5109
template <class Key, class Value, class Hash, class Eq, class Alloc, size_t N, class Mtx_>
5110
class parallel_node_hash_map
5111
    : public phmap::priv::parallel_hash_map<
5112
          N, phmap::priv::raw_hash_set, Mtx_,
5113
          phmap::priv::NodeHashMapPolicy<Key, Value>, Hash, Eq,
5114
          Alloc> 
5115
{
5116
    using Base = typename parallel_node_hash_map::parallel_hash_map;
5117
5118
public:
5119
    parallel_node_hash_map() {}
5120
#ifdef __INTEL_COMPILER
5121
    using Base::parallel_hash_map;
5122
#else
5123
    using Base::Base;
5124
#endif
5125
    using Base::hash;
5126
    using Base::subidx;
5127
    using Base::subcnt;
5128
    using Base::begin;
5129
    using Base::cbegin;
5130
    using Base::cend;
5131
    using Base::end;
5132
    using Base::capacity;
5133
    using Base::empty;
5134
    using Base::max_size;
5135
    using Base::size;
5136
    using Base::clear;
5137
    using Base::erase;
5138
    using Base::insert;
5139
    using Base::insert_or_assign;
5140
    using Base::emplace;
5141
    using Base::emplace_hint;
5142
    using Base::try_emplace;
5143
    using Base::emplace_with_hash;
5144
    using Base::emplace_hint_with_hash;
5145
    using Base::try_emplace_with_hash;
5146
    using Base::extract;
5147
    using Base::merge;
5148
    using Base::swap;
5149
    using Base::rehash;
5150
    using Base::reserve;
5151
    using Base::at;
5152
    using Base::contains;
5153
    using Base::count;
5154
    using Base::equal_range;
5155
    using Base::find;
5156
    using Base::operator[];
5157
    using Base::bucket_count;
5158
    using Base::load_factor;
5159
    using Base::max_load_factor;
5160
    using Base::get_allocator;
5161
    using Base::hash_function;
5162
    using Base::key_eq;
5163
    typename Base::hasher hash_funct() { return this->hash_function(); }
5164
    void resize(typename Base::size_type hint) { this->rehash(hint); }
5165
};
5166
5167
}  // namespace phmap
5168
5169
5170
namespace phmap {
5171
    namespace priv {
5172
        template <class C, class Pred> 
5173
        std::size_t erase_if(C &c, Pred pred) {
5174
            auto old_size = c.size();
5175
            for (auto i = c.begin(), last = c.end(); i != last; ) {
5176
                if (pred(*i)) {
5177
                    i = c.erase(i);
5178
                } else {
5179
                    ++i;
5180
                }
5181
            }
5182
            return old_size - c.size();
5183
        }
5184
    } // priv
5185
5186
    // ======== erase_if for phmap set containers ==================================
5187
    template <class T, class Hash, class Eq, class Alloc, class Pred> 
5188
    std::size_t erase_if(phmap::flat_hash_set<T, Hash, Eq, Alloc>& c, Pred pred) {
5189
        return phmap::priv::erase_if(c, std::move(pred));
5190
    }
5191
5192
    template <class T, class Hash, class Eq, class Alloc, class Pred> 
5193
    std::size_t erase_if(phmap::node_hash_set<T, Hash, Eq, Alloc>& c, Pred pred) {
5194
        return phmap::priv::erase_if(c, std::move(pred));
5195
    }
5196
5197
    template <class T, class Hash, class Eq, class Alloc, size_t N, class Mtx_, class Pred> 
5198
    std::size_t erase_if(phmap::parallel_flat_hash_set<T, Hash, Eq, Alloc, N, Mtx_>& c, Pred pred) {
5199
        return phmap::priv::erase_if(c, std::move(pred));
5200
    }
5201
5202
    template <class T, class Hash, class Eq, class Alloc, size_t N, class Mtx_, class Pred> 
5203
    std::size_t erase_if(phmap::parallel_node_hash_set<T, Hash, Eq, Alloc, N, Mtx_>& c, Pred pred) {
5204
        return phmap::priv::erase_if(c, std::move(pred));
5205
    }
5206
5207
    // ======== erase_if for phmap map containers ==================================
5208
    template <class K, class V, class Hash, class Eq, class Alloc, class Pred> 
5209
    std::size_t erase_if(phmap::flat_hash_map<K, V, Hash, Eq, Alloc>& c, Pred pred) {
5210
        return phmap::priv::erase_if(c, std::move(pred));
5211
    }
5212
5213
    template <class K, class V, class Hash, class Eq, class Alloc, class Pred> 
5214
    std::size_t erase_if(phmap::node_hash_map<K, V, Hash, Eq, Alloc>& c, Pred pred) {
5215
        return phmap::priv::erase_if(c, std::move(pred));
5216
    }
5217
5218
    template <class K, class V, class Hash, class Eq, class Alloc, size_t N, class Mtx_, class Pred> 
5219
    std::size_t erase_if(phmap::parallel_flat_hash_map<K, V, Hash, Eq, Alloc, N, Mtx_>& c, Pred pred) {
5220
        return phmap::priv::erase_if(c, std::move(pred));
5221
    }
5222
5223
    template <class K, class V, class Hash, class Eq, class Alloc, size_t N, class Mtx_, class Pred> 
5224
    std::size_t erase_if(phmap::parallel_node_hash_map<K, V, Hash, Eq, Alloc, N, Mtx_>& c, Pred pred) {
5225
        return phmap::priv::erase_if(c, std::move(pred));
5226
    }
5227
5228
} // phmap
5229
5230
#ifdef _MSC_VER
5231
     #pragma warning(pop)  
5232
#endif
5233
5234
5235
#endif // phmap_h_guard_