File indexing completed on 2026-05-10 08:43:10
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef LLVM_ADT_TRIEHASHINDEXGENERATOR_H
0010 #define LLVM_ADT_TRIEHASHINDEXGENERATOR_H
0011
0012 #include "llvm/ADT/ArrayRef.h"
0013 #include <optional>
0014
0015 namespace llvm {
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031 struct TrieHashIndexGenerator {
0032 size_t NumRootBits;
0033 size_t NumSubtrieBits;
0034 ArrayRef<uint8_t> Bytes;
0035 std::optional<size_t> StartBit = std::nullopt;
0036
0037
0038 size_t getNumBits() const {
0039 assert(StartBit);
0040 size_t TotalNumBits = Bytes.size() * 8;
0041 assert(*StartBit <= TotalNumBits);
0042 return std::min(*StartBit ? NumSubtrieBits : NumRootBits,
0043 TotalNumBits - *StartBit);
0044 }
0045
0046
0047 size_t next() {
0048 if (!StartBit) {
0049
0050 StartBit = 0;
0051 return getIndex(Bytes, *StartBit, NumRootBits);
0052 }
0053 if (*StartBit < Bytes.size() * 8) {
0054
0055 *StartBit += *StartBit ? NumSubtrieBits : NumRootBits;
0056 assert((*StartBit - NumRootBits) % NumSubtrieBits == 0);
0057 return getIndex(Bytes, *StartBit, NumSubtrieBits);
0058 }
0059
0060 return end();
0061 }
0062
0063
0064
0065
0066
0067
0068 size_t hint(unsigned Index, unsigned Bit) {
0069 assert(Bit < Bytes.size() * 8);
0070 assert(Bit == 0 || (Bit - NumRootBits) % NumSubtrieBits == 0);
0071 StartBit = Bit;
0072 return Index;
0073 }
0074
0075
0076
0077
0078 size_t getCollidingBits(ArrayRef<uint8_t> CollidingBits) const {
0079 assert(StartBit);
0080 return getIndex(CollidingBits, *StartBit, NumSubtrieBits);
0081 }
0082
0083 size_t end() const { return SIZE_MAX; }
0084
0085
0086
0087 static size_t getIndex(ArrayRef<uint8_t> Bytes, size_t StartBit,
0088 size_t NumBits) {
0089 assert(StartBit < Bytes.size() * 8);
0090
0091 Bytes = Bytes.drop_front(StartBit / 8u);
0092 StartBit %= 8u;
0093 size_t Index = 0;
0094
0095
0096 for (uint8_t Byte : Bytes) {
0097 size_t ByteStart = 0, ByteEnd = 8;
0098 if (StartBit) {
0099 ByteStart = StartBit;
0100 Byte &= (1u << (8 - StartBit)) - 1u;
0101 StartBit = 0;
0102 }
0103 size_t CurrentNumBits = ByteEnd - ByteStart;
0104 if (CurrentNumBits > NumBits) {
0105 Byte >>= CurrentNumBits - NumBits;
0106 CurrentNumBits = NumBits;
0107 }
0108 Index <<= CurrentNumBits;
0109 Index |= Byte & ((1u << CurrentNumBits) - 1u);
0110
0111 assert(NumBits >= CurrentNumBits);
0112 NumBits -= CurrentNumBits;
0113 if (!NumBits)
0114 break;
0115 }
0116 return Index;
0117 }
0118 };
0119
0120 }
0121
0122 #endif