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