[llvm] [ADT] Add TrieRawHashMap (PR #69528)
Justin Bogner via llvm-commits
llvm-commits at lists.llvm.org
Sat Oct 26 17:40:55 PDT 2024
================
@@ -0,0 +1,122 @@
+//===- TrieHashIndexGenerator.h ---------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_SUPPORT_TRIEHASHINDEXGENERATOR_H
+#define LLVM_LIB_SUPPORT_TRIEHASHINDEXGENERATOR_H
+
+#include "llvm/ADT/ArrayRef.h"
+#include <optional>
+
+namespace llvm {
+
+/// The utility class that helps computing the index of the object inside trie
+/// from its hash. The generator can be configured with the number of bits
+/// used for each level of trie structure with \c NumRootsBits and \c
+/// NumSubtrieBits.
+/// For example, try computing indexes for a 16-bit hash 0x1234 with 8-bit root
+/// and 4-bit sub-trie:
+///
+/// IndexGenerator IndexGen{8, 4, Hash};
+/// size_t index1 = IndexGen.next(); // index 18 in root node.
+/// size_t index2 = IndexGen.next(); // index 3 in sub-trie level 1.
+/// size_t index3 = IndexGen.next(); // index 4 in sub-tire level 2.
+///
+/// This is used by different trie implementation to figure out where to
+/// insert/find the object in the data structure.
+struct IndexGenerator {
+ size_t NumRootBits;
+ size_t NumSubtrieBits;
+ ArrayRef<uint8_t> Bytes;
+ std::optional<size_t> StartBit = std::nullopt;
+
+ // Get the number of bits used to generate current index.
+ size_t getNumBits() const {
+ assert(StartBit);
+ size_t TotalNumBits = Bytes.size() * 8;
+ assert(*StartBit <= TotalNumBits);
+ return std::min(*StartBit ? NumSubtrieBits : NumRootBits,
+ TotalNumBits - *StartBit);
+ }
+
+ // Get the index of the object in the next level of trie.
+ size_t next() {
+ if (!StartBit) {
+ // Compute index for root when StartBit is not set.
+ StartBit = 0;
+ return getIndex(Bytes, *StartBit, NumRootBits);
+ }
+ if (*StartBit < Bytes.size() * 8) {
+ // Compute index for sub-trie.
+ *StartBit += *StartBit ? NumSubtrieBits : NumRootBits;
+ assert((*StartBit - NumRootBits) % NumSubtrieBits == 0);
+ return getIndex(Bytes, *StartBit, NumSubtrieBits);
+ }
+ // All the bits are consumed.
+ return end();
+ }
+
+ // Provide a hint to speed up the index generation by providing the
+ // information of the hash in current level. For example, if the object is
+ // known to have \c Index on a level that already consumes first n \c Bits of
+ // the hash, it can start index generation from this level by calling \c hint
+ // function.
+ size_t hint(unsigned Index, unsigned Bit) {
+ assert(Bit < Bytes.size() * 8);
+ assert(Bit == 0 || (Bit - NumRootBits) % NumSubtrieBits == 0);
+ StartBit = Bit;
+ return Index;
+ }
+
+ // Utility funciton for looking up the index in the trie for an object that
----------------
bogner wrote:
```suggestion
// Utility function for looking up the index in the trie for an object that
```
https://github.com/llvm/llvm-project/pull/69528
More information about the llvm-commits
mailing list