xref: /freebsd/contrib/llvm-project/libc/src/__support/HashTable/table.h (revision bb722a7d0f1642bff6487f943ad0427799a6e5bf)
1*bb722a7dSDimitry Andric //===-- Resizable Monotonic HashTable ---------------------------*- C++ -*-===//
2*bb722a7dSDimitry Andric //
3*bb722a7dSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*bb722a7dSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*bb722a7dSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*bb722a7dSDimitry Andric //
7*bb722a7dSDimitry Andric //===----------------------------------------------------------------------===//
8*bb722a7dSDimitry Andric 
9*bb722a7dSDimitry Andric #ifndef LLVM_LIBC_SRC___SUPPORT_HASHTABLE_TABLE_H
10*bb722a7dSDimitry Andric #define LLVM_LIBC_SRC___SUPPORT_HASHTABLE_TABLE_H
11*bb722a7dSDimitry Andric 
12*bb722a7dSDimitry Andric #include "hdr/types/ENTRY.h"
13*bb722a7dSDimitry Andric #include "src/__support/CPP/bit.h" // bit_ceil
14*bb722a7dSDimitry Andric #include "src/__support/CPP/new.h"
15*bb722a7dSDimitry Andric #include "src/__support/HashTable/bitmask.h"
16*bb722a7dSDimitry Andric #include "src/__support/hash.h"
17*bb722a7dSDimitry Andric #include "src/__support/macros/attributes.h"
18*bb722a7dSDimitry Andric #include "src/__support/macros/config.h"
19*bb722a7dSDimitry Andric #include "src/__support/macros/optimization.h"
20*bb722a7dSDimitry Andric #include "src/__support/memory_size.h"
21*bb722a7dSDimitry Andric #include "src/string/memory_utils/inline_strcmp.h"
22*bb722a7dSDimitry Andric #include "src/string/string_utils.h"
23*bb722a7dSDimitry Andric #include <stddef.h>
24*bb722a7dSDimitry Andric #include <stdint.h>
25*bb722a7dSDimitry Andric 
26*bb722a7dSDimitry Andric namespace LIBC_NAMESPACE_DECL {
27*bb722a7dSDimitry Andric namespace internal {
28*bb722a7dSDimitry Andric 
secondary_hash(uint64_t hash)29*bb722a7dSDimitry Andric LIBC_INLINE uint8_t secondary_hash(uint64_t hash) {
30*bb722a7dSDimitry Andric   // top 7 bits of the hash.
31*bb722a7dSDimitry Andric   return static_cast<uint8_t>(hash >> 57);
32*bb722a7dSDimitry Andric }
33*bb722a7dSDimitry Andric 
34*bb722a7dSDimitry Andric // Probe sequence based on triangular numbers, which is guaranteed (since our
35*bb722a7dSDimitry Andric // table size is a power of two) to visit every group of elements exactly once.
36*bb722a7dSDimitry Andric //
37*bb722a7dSDimitry Andric // A triangular probe has us jump by 1 more group every time. So first we
38*bb722a7dSDimitry Andric // jump by 1 group (meaning we just continue our linear scan), then 2 groups
39*bb722a7dSDimitry Andric // (skipping over 1 group), then 3 groups (skipping over 2 groups), and so on.
40*bb722a7dSDimitry Andric //
41*bb722a7dSDimitry Andric // If we set sizeof(Group) to be one unit:
42*bb722a7dSDimitry Andric //               T[k] = sum {1 + 2 + ... + k} = k * (k + 1) / 2
43*bb722a7dSDimitry Andric // It is provable that T[k] mod 2^m generates a permutation of
44*bb722a7dSDimitry Andric //                0, 1, 2, 3, ..., 2^m - 2, 2^m - 1
45*bb722a7dSDimitry Andric // Detailed proof is available at:
46*bb722a7dSDimitry Andric // https://fgiesen.wordpress.com/2015/02/22/triangular-numbers-mod-2n/
47*bb722a7dSDimitry Andric struct ProbeSequence {
48*bb722a7dSDimitry Andric   size_t position;
49*bb722a7dSDimitry Andric   size_t stride;
50*bb722a7dSDimitry Andric   size_t entries_mask;
51*bb722a7dSDimitry Andric 
nextProbeSequence52*bb722a7dSDimitry Andric   LIBC_INLINE size_t next() {
53*bb722a7dSDimitry Andric     position += stride;
54*bb722a7dSDimitry Andric     position &= entries_mask;
55*bb722a7dSDimitry Andric     stride += sizeof(Group);
56*bb722a7dSDimitry Andric     return position;
57*bb722a7dSDimitry Andric   }
58*bb722a7dSDimitry Andric };
59*bb722a7dSDimitry Andric 
60*bb722a7dSDimitry Andric // The number of entries is at least group width: we do not
61*bb722a7dSDimitry Andric // need to do the fixup when we set the control bytes.
62*bb722a7dSDimitry Andric // The number of entries is at least 8: we don't have to worry
63*bb722a7dSDimitry Andric // about special sizes when check the fullness of the table.
capacity_to_entries(size_t cap)64*bb722a7dSDimitry Andric LIBC_INLINE size_t capacity_to_entries(size_t cap) {
65*bb722a7dSDimitry Andric   if (8 >= sizeof(Group) && cap < 8)
66*bb722a7dSDimitry Andric     return 8;
67*bb722a7dSDimitry Andric   if (16 >= sizeof(Group) && cap < 15)
68*bb722a7dSDimitry Andric     return 16;
69*bb722a7dSDimitry Andric   if (cap < sizeof(Group))
70*bb722a7dSDimitry Andric     cap = sizeof(Group);
71*bb722a7dSDimitry Andric   // overflow is always checked in allocate()
72*bb722a7dSDimitry Andric   return cpp::bit_ceil(cap * 8 / 7);
73*bb722a7dSDimitry Andric }
74*bb722a7dSDimitry Andric 
75*bb722a7dSDimitry Andric // The heap memory layout for N buckets HashTable is as follows:
76*bb722a7dSDimitry Andric //
77*bb722a7dSDimitry Andric //             =======================
78*bb722a7dSDimitry Andric //             |   N * Entry         |
79*bb722a7dSDimitry Andric //             ======================= <- align boundary
80*bb722a7dSDimitry Andric //             |   Header            |
81*bb722a7dSDimitry Andric //             ======================= <- align boundary (for fast resize)
82*bb722a7dSDimitry Andric //             |   (N + 1) * Byte    |
83*bb722a7dSDimitry Andric //             =======================
84*bb722a7dSDimitry Andric //
85*bb722a7dSDimitry Andric // The trailing group part is to make sure we can always load
86*bb722a7dSDimitry Andric // a whole group of control bytes.
87*bb722a7dSDimitry Andric 
88*bb722a7dSDimitry Andric struct HashTable {
89*bb722a7dSDimitry Andric   HashState state;
90*bb722a7dSDimitry Andric   size_t entries_mask;    // number of buckets - 1
91*bb722a7dSDimitry Andric   size_t available_slots; // less than capacity
92*bb722a7dSDimitry Andric private:
93*bb722a7dSDimitry Andric   // How many entries are there in the table.
num_of_entriesHashTable94*bb722a7dSDimitry Andric   LIBC_INLINE size_t num_of_entries() const { return entries_mask + 1; }
95*bb722a7dSDimitry Andric 
96*bb722a7dSDimitry Andric   // How many entries can we store in the table before resizing.
full_capacityHashTable97*bb722a7dSDimitry Andric   LIBC_INLINE size_t full_capacity() const { return num_of_entries() / 8 * 7; }
98*bb722a7dSDimitry Andric 
99*bb722a7dSDimitry Andric   // The alignment of the whole memory area is the maximum of the alignment
100*bb722a7dSDimitry Andric   // among the following types:
101*bb722a7dSDimitry Andric   // - HashTable
102*bb722a7dSDimitry Andric   // - ENTRY
103*bb722a7dSDimitry Andric   // - Group
table_alignmentHashTable104*bb722a7dSDimitry Andric   LIBC_INLINE constexpr static size_t table_alignment() {
105*bb722a7dSDimitry Andric     size_t left_align = alignof(HashTable) > alignof(ENTRY) ? alignof(HashTable)
106*bb722a7dSDimitry Andric                                                             : alignof(ENTRY);
107*bb722a7dSDimitry Andric     return left_align > alignof(Group) ? left_align : alignof(Group);
108*bb722a7dSDimitry Andric   }
109*bb722a7dSDimitry Andric 
is_fullHashTable110*bb722a7dSDimitry Andric   LIBC_INLINE bool is_full() const { return available_slots == 0; }
111*bb722a7dSDimitry Andric 
offset_from_entriesHashTable112*bb722a7dSDimitry Andric   LIBC_INLINE size_t offset_from_entries() const {
113*bb722a7dSDimitry Andric     size_t entries_size = num_of_entries() * sizeof(ENTRY);
114*bb722a7dSDimitry Andric     return entries_size +
115*bb722a7dSDimitry Andric            SafeMemSize::offset_to(entries_size, table_alignment());
116*bb722a7dSDimitry Andric   }
117*bb722a7dSDimitry Andric 
offset_to_groupsHashTable118*bb722a7dSDimitry Andric   LIBC_INLINE constexpr static size_t offset_to_groups() {
119*bb722a7dSDimitry Andric     size_t header_size = sizeof(HashTable);
120*bb722a7dSDimitry Andric     return header_size + SafeMemSize::offset_to(header_size, table_alignment());
121*bb722a7dSDimitry Andric   }
122*bb722a7dSDimitry Andric 
entryHashTable123*bb722a7dSDimitry Andric   LIBC_INLINE ENTRY &entry(size_t i) {
124*bb722a7dSDimitry Andric     return reinterpret_cast<ENTRY *>(this)[-i - 1];
125*bb722a7dSDimitry Andric   }
126*bb722a7dSDimitry Andric 
entryHashTable127*bb722a7dSDimitry Andric   LIBC_INLINE const ENTRY &entry(size_t i) const {
128*bb722a7dSDimitry Andric     return reinterpret_cast<const ENTRY *>(this)[-i - 1];
129*bb722a7dSDimitry Andric   }
130*bb722a7dSDimitry Andric 
controlHashTable131*bb722a7dSDimitry Andric   LIBC_INLINE uint8_t &control(size_t i) {
132*bb722a7dSDimitry Andric     uint8_t *ptr = reinterpret_cast<uint8_t *>(this) + offset_to_groups();
133*bb722a7dSDimitry Andric     return ptr[i];
134*bb722a7dSDimitry Andric   }
135*bb722a7dSDimitry Andric 
controlHashTable136*bb722a7dSDimitry Andric   LIBC_INLINE const uint8_t &control(size_t i) const {
137*bb722a7dSDimitry Andric     const uint8_t *ptr =
138*bb722a7dSDimitry Andric         reinterpret_cast<const uint8_t *>(this) + offset_to_groups();
139*bb722a7dSDimitry Andric     return ptr[i];
140*bb722a7dSDimitry Andric   }
141*bb722a7dSDimitry Andric 
142*bb722a7dSDimitry Andric   // We duplicate a group of control bytes to the end. Thus, it is possible that
143*bb722a7dSDimitry Andric   // we need to set two control bytes at the same time.
set_ctrlHashTable144*bb722a7dSDimitry Andric   LIBC_INLINE void set_ctrl(size_t index, uint8_t value) {
145*bb722a7dSDimitry Andric     size_t index2 = ((index - sizeof(Group)) & entries_mask) + sizeof(Group);
146*bb722a7dSDimitry Andric     control(index) = value;
147*bb722a7dSDimitry Andric     control(index2) = value;
148*bb722a7dSDimitry Andric   }
149*bb722a7dSDimitry Andric 
findHashTable150*bb722a7dSDimitry Andric   LIBC_INLINE size_t find(const char *key, uint64_t primary) {
151*bb722a7dSDimitry Andric     uint8_t secondary = secondary_hash(primary);
152*bb722a7dSDimitry Andric     ProbeSequence sequence{static_cast<size_t>(primary), 0, entries_mask};
153*bb722a7dSDimitry Andric     while (true) {
154*bb722a7dSDimitry Andric       size_t pos = sequence.next();
155*bb722a7dSDimitry Andric       Group ctrls = Group::load(&control(pos));
156*bb722a7dSDimitry Andric       IteratableBitMask masks = ctrls.match_byte(secondary);
157*bb722a7dSDimitry Andric       for (size_t i : masks) {
158*bb722a7dSDimitry Andric         size_t index = (pos + i) & entries_mask;
159*bb722a7dSDimitry Andric         ENTRY &entry = this->entry(index);
160*bb722a7dSDimitry Andric         auto comp = [](char l, char r) -> int { return l - r; };
161*bb722a7dSDimitry Andric         if (LIBC_LIKELY(entry.key != nullptr &&
162*bb722a7dSDimitry Andric                         inline_strcmp(entry.key, key, comp) == 0))
163*bb722a7dSDimitry Andric           return index;
164*bb722a7dSDimitry Andric       }
165*bb722a7dSDimitry Andric       BitMask available = ctrls.mask_available();
166*bb722a7dSDimitry Andric       // Since there is no deletion, the first time we find an available slot
167*bb722a7dSDimitry Andric       // it is also ready to be used as an insertion point. Therefore, we also
168*bb722a7dSDimitry Andric       // return the first available slot we find. If such entry is empty, the
169*bb722a7dSDimitry Andric       // key will be nullptr.
170*bb722a7dSDimitry Andric       if (LIBC_LIKELY(available.any_bit_set())) {
171*bb722a7dSDimitry Andric         size_t index =
172*bb722a7dSDimitry Andric             (pos + available.lowest_set_bit_nonzero()) & entries_mask;
173*bb722a7dSDimitry Andric         return index;
174*bb722a7dSDimitry Andric       }
175*bb722a7dSDimitry Andric     }
176*bb722a7dSDimitry Andric   }
177*bb722a7dSDimitry Andric 
oneshot_hashHashTable178*bb722a7dSDimitry Andric   LIBC_INLINE uint64_t oneshot_hash(const char *key) const {
179*bb722a7dSDimitry Andric     LIBC_NAMESPACE::internal::HashState hasher = state;
180*bb722a7dSDimitry Andric     hasher.update(key, internal::string_length(key));
181*bb722a7dSDimitry Andric     return hasher.finish();
182*bb722a7dSDimitry Andric   }
183*bb722a7dSDimitry Andric 
184*bb722a7dSDimitry Andric   // A fast insertion routine without checking if a key already exists.
185*bb722a7dSDimitry Andric   // Nor does the routine check if the table is full.
186*bb722a7dSDimitry Andric   // This is only to be used in grow() where we insert all existing entries
187*bb722a7dSDimitry Andric   // into a new table. Hence, the requirements are naturally satisfied.
unsafe_insertHashTable188*bb722a7dSDimitry Andric   LIBC_INLINE ENTRY *unsafe_insert(ENTRY item) {
189*bb722a7dSDimitry Andric     uint64_t primary = oneshot_hash(item.key);
190*bb722a7dSDimitry Andric     uint8_t secondary = secondary_hash(primary);
191*bb722a7dSDimitry Andric     ProbeSequence sequence{static_cast<size_t>(primary), 0, entries_mask};
192*bb722a7dSDimitry Andric     while (true) {
193*bb722a7dSDimitry Andric       size_t pos = sequence.next();
194*bb722a7dSDimitry Andric       Group ctrls = Group::load(&control(pos));
195*bb722a7dSDimitry Andric       BitMask available = ctrls.mask_available();
196*bb722a7dSDimitry Andric       if (available.any_bit_set()) {
197*bb722a7dSDimitry Andric         size_t index =
198*bb722a7dSDimitry Andric             (pos + available.lowest_set_bit_nonzero()) & entries_mask;
199*bb722a7dSDimitry Andric         set_ctrl(index, secondary);
200*bb722a7dSDimitry Andric         entry(index).key = item.key;
201*bb722a7dSDimitry Andric         entry(index).data = item.data;
202*bb722a7dSDimitry Andric         available_slots--;
203*bb722a7dSDimitry Andric         return &entry(index);
204*bb722a7dSDimitry Andric       }
205*bb722a7dSDimitry Andric     }
206*bb722a7dSDimitry Andric   }
207*bb722a7dSDimitry Andric 
growHashTable208*bb722a7dSDimitry Andric   LIBC_INLINE HashTable *grow() const {
209*bb722a7dSDimitry Andric     size_t hint = full_capacity() + 1;
210*bb722a7dSDimitry Andric     HashState state = this->state;
211*bb722a7dSDimitry Andric     // migrate to a new random state
212*bb722a7dSDimitry Andric     state.update(&hint, sizeof(hint));
213*bb722a7dSDimitry Andric     HashTable *new_table = allocate(hint, state.finish());
214*bb722a7dSDimitry Andric     // It is safe to call unsafe_insert() because we know that:
215*bb722a7dSDimitry Andric     // - the new table has enough capacity to hold all the entries
216*bb722a7dSDimitry Andric     // - there is no duplicate key in the old table
217*bb722a7dSDimitry Andric     if (new_table != nullptr)
218*bb722a7dSDimitry Andric       for (ENTRY e : *this)
219*bb722a7dSDimitry Andric         new_table->unsafe_insert(e);
220*bb722a7dSDimitry Andric     return new_table;
221*bb722a7dSDimitry Andric   }
222*bb722a7dSDimitry Andric 
insertHashTable223*bb722a7dSDimitry Andric   LIBC_INLINE static ENTRY *insert(HashTable *&table, ENTRY item,
224*bb722a7dSDimitry Andric                                    uint64_t primary) {
225*bb722a7dSDimitry Andric     auto index = table->find(item.key, primary);
226*bb722a7dSDimitry Andric     auto slot = &table->entry(index);
227*bb722a7dSDimitry Andric     // SVr4 and POSIX.1-2001 specify that action is significant only for
228*bb722a7dSDimitry Andric     // unsuccessful searches, so that an ENTER should not do anything
229*bb722a7dSDimitry Andric     // for a successful search.
230*bb722a7dSDimitry Andric     if (slot->key != nullptr)
231*bb722a7dSDimitry Andric       return slot;
232*bb722a7dSDimitry Andric 
233*bb722a7dSDimitry Andric     // if table of full, we try to grow the table
234*bb722a7dSDimitry Andric     if (table->is_full()) {
235*bb722a7dSDimitry Andric       HashTable *new_table = table->grow();
236*bb722a7dSDimitry Andric       // allocation failed, return nullptr to indicate failure
237*bb722a7dSDimitry Andric       if (new_table == nullptr)
238*bb722a7dSDimitry Andric         return nullptr;
239*bb722a7dSDimitry Andric       // resized sccuessfully: clean up the old table and use the new one
240*bb722a7dSDimitry Andric       deallocate(table);
241*bb722a7dSDimitry Andric       table = new_table;
242*bb722a7dSDimitry Andric       // it is still valid to use the fastpath insertion.
243*bb722a7dSDimitry Andric       return table->unsafe_insert(item);
244*bb722a7dSDimitry Andric     }
245*bb722a7dSDimitry Andric 
246*bb722a7dSDimitry Andric     table->set_ctrl(index, secondary_hash(primary));
247*bb722a7dSDimitry Andric     slot->key = item.key;
248*bb722a7dSDimitry Andric     slot->data = item.data;
249*bb722a7dSDimitry Andric     table->available_slots--;
250*bb722a7dSDimitry Andric     return slot;
251*bb722a7dSDimitry Andric   }
252*bb722a7dSDimitry Andric 
253*bb722a7dSDimitry Andric public:
deallocateHashTable254*bb722a7dSDimitry Andric   LIBC_INLINE static void deallocate(HashTable *table) {
255*bb722a7dSDimitry Andric     if (table) {
256*bb722a7dSDimitry Andric       void *ptr =
257*bb722a7dSDimitry Andric           reinterpret_cast<uint8_t *>(table) - table->offset_from_entries();
258*bb722a7dSDimitry Andric       operator delete(ptr, std::align_val_t{table_alignment()});
259*bb722a7dSDimitry Andric     }
260*bb722a7dSDimitry Andric   }
261*bb722a7dSDimitry Andric 
allocateHashTable262*bb722a7dSDimitry Andric   LIBC_INLINE static HashTable *allocate(size_t capacity, uint64_t randomness) {
263*bb722a7dSDimitry Andric     // check if capacity_to_entries overflows MAX_MEM_SIZE
264*bb722a7dSDimitry Andric     if (capacity > size_t{1} << (8 * sizeof(size_t) - 1 - 3))
265*bb722a7dSDimitry Andric       return nullptr;
266*bb722a7dSDimitry Andric     SafeMemSize entries{capacity_to_entries(capacity)};
267*bb722a7dSDimitry Andric     SafeMemSize entries_size = entries * SafeMemSize{sizeof(ENTRY)};
268*bb722a7dSDimitry Andric     SafeMemSize align_boundary = entries_size.align_up(table_alignment());
269*bb722a7dSDimitry Andric     SafeMemSize ctrl_sizes = entries + SafeMemSize{sizeof(Group)};
270*bb722a7dSDimitry Andric     SafeMemSize header_size{offset_to_groups()};
271*bb722a7dSDimitry Andric     SafeMemSize total_size =
272*bb722a7dSDimitry Andric         (align_boundary + header_size + ctrl_sizes).align_up(table_alignment());
273*bb722a7dSDimitry Andric     if (!total_size.valid())
274*bb722a7dSDimitry Andric       return nullptr;
275*bb722a7dSDimitry Andric     AllocChecker ac;
276*bb722a7dSDimitry Andric 
277*bb722a7dSDimitry Andric     void *mem = operator new(total_size, std::align_val_t{table_alignment()},
278*bb722a7dSDimitry Andric                              ac);
279*bb722a7dSDimitry Andric 
280*bb722a7dSDimitry Andric     HashTable *table = reinterpret_cast<HashTable *>(
281*bb722a7dSDimitry Andric         static_cast<uint8_t *>(mem) + align_boundary);
282*bb722a7dSDimitry Andric     if (ac) {
283*bb722a7dSDimitry Andric       table->entries_mask = entries - 1u;
284*bb722a7dSDimitry Andric       table->available_slots = entries / 8 * 7;
285*bb722a7dSDimitry Andric       table->state = HashState{randomness};
286*bb722a7dSDimitry Andric       __builtin_memset(&table->control(0), 0x80, ctrl_sizes);
287*bb722a7dSDimitry Andric       __builtin_memset(mem, 0, table->offset_from_entries());
288*bb722a7dSDimitry Andric     }
289*bb722a7dSDimitry Andric     return table;
290*bb722a7dSDimitry Andric   }
291*bb722a7dSDimitry Andric 
292*bb722a7dSDimitry Andric   struct FullTableIterator {
293*bb722a7dSDimitry Andric     size_t current_offset;
294*bb722a7dSDimitry Andric     size_t remaining;
295*bb722a7dSDimitry Andric     IteratableBitMask current_mask;
296*bb722a7dSDimitry Andric     const HashTable &table;
297*bb722a7dSDimitry Andric 
298*bb722a7dSDimitry Andric     // It is fine to use remaining to represent the iterator:
299*bb722a7dSDimitry Andric     // - this comparison only happens with the same table
300*bb722a7dSDimitry Andric     // - hashtable will not be mutated during the iteration
301*bb722a7dSDimitry Andric     LIBC_INLINE bool operator==(const FullTableIterator &other) const {
302*bb722a7dSDimitry Andric       return remaining == other.remaining;
303*bb722a7dSDimitry Andric     }
304*bb722a7dSDimitry Andric     LIBC_INLINE bool operator!=(const FullTableIterator &other) const {
305*bb722a7dSDimitry Andric       return remaining != other.remaining;
306*bb722a7dSDimitry Andric     }
307*bb722a7dSDimitry Andric 
308*bb722a7dSDimitry Andric     LIBC_INLINE FullTableIterator &operator++() {
309*bb722a7dSDimitry Andric       this->ensure_valid_group();
310*bb722a7dSDimitry Andric       current_mask.remove_lowest_bit();
311*bb722a7dSDimitry Andric       remaining--;
312*bb722a7dSDimitry Andric       return *this;
313*bb722a7dSDimitry Andric     }
314*bb722a7dSDimitry Andric     LIBC_INLINE const ENTRY &operator*() {
315*bb722a7dSDimitry Andric       this->ensure_valid_group();
316*bb722a7dSDimitry Andric       return table.entry(
317*bb722a7dSDimitry Andric           (current_offset + current_mask.lowest_set_bit_nonzero()) &
318*bb722a7dSDimitry Andric           table.entries_mask);
319*bb722a7dSDimitry Andric     }
320*bb722a7dSDimitry Andric 
321*bb722a7dSDimitry Andric   private:
ensure_valid_groupHashTable::FullTableIterator322*bb722a7dSDimitry Andric     LIBC_INLINE void ensure_valid_group() {
323*bb722a7dSDimitry Andric       while (!current_mask.any_bit_set()) {
324*bb722a7dSDimitry Andric         current_offset += sizeof(Group);
325*bb722a7dSDimitry Andric         // It is ensured that the load will only happen at aligned boundaries.
326*bb722a7dSDimitry Andric         current_mask =
327*bb722a7dSDimitry Andric             Group::load_aligned(&table.control(current_offset)).occupied();
328*bb722a7dSDimitry Andric       }
329*bb722a7dSDimitry Andric     }
330*bb722a7dSDimitry Andric   };
331*bb722a7dSDimitry Andric 
332*bb722a7dSDimitry Andric   using value_type = ENTRY;
333*bb722a7dSDimitry Andric   using iterator = FullTableIterator;
beginHashTable334*bb722a7dSDimitry Andric   iterator begin() const {
335*bb722a7dSDimitry Andric     return {0, full_capacity() - available_slots,
336*bb722a7dSDimitry Andric             Group::load_aligned(&control(0)).occupied(), *this};
337*bb722a7dSDimitry Andric   }
endHashTable338*bb722a7dSDimitry Andric   iterator end() const { return {0, 0, {BitMask{0}}, *this}; }
339*bb722a7dSDimitry Andric 
findHashTable340*bb722a7dSDimitry Andric   LIBC_INLINE ENTRY *find(const char *key) {
341*bb722a7dSDimitry Andric     uint64_t primary = oneshot_hash(key);
342*bb722a7dSDimitry Andric     ENTRY &entry = this->entry(find(key, primary));
343*bb722a7dSDimitry Andric     if (entry.key == nullptr)
344*bb722a7dSDimitry Andric       return nullptr;
345*bb722a7dSDimitry Andric     return &entry;
346*bb722a7dSDimitry Andric   }
347*bb722a7dSDimitry Andric 
insertHashTable348*bb722a7dSDimitry Andric   LIBC_INLINE static ENTRY *insert(HashTable *&table, ENTRY item) {
349*bb722a7dSDimitry Andric     uint64_t primary = table->oneshot_hash(item.key);
350*bb722a7dSDimitry Andric     return insert(table, item, primary);
351*bb722a7dSDimitry Andric   }
352*bb722a7dSDimitry Andric };
353*bb722a7dSDimitry Andric } // namespace internal
354*bb722a7dSDimitry Andric } // namespace LIBC_NAMESPACE_DECL
355*bb722a7dSDimitry Andric 
356*bb722a7dSDimitry Andric #endif // LLVM_LIBC_SRC___SUPPORT_HASHTABLE_TABLE_H
357