Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:33

0001 //===--- OnDiskHashTable.h - On-Disk Hash Table Implementation --*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 ///
0009 /// \file
0010 /// Defines facilities for reading and writing on-disk hash tables.
0011 ///
0012 //===----------------------------------------------------------------------===//
0013 #ifndef LLVM_SUPPORT_ONDISKHASHTABLE_H
0014 #define LLVM_SUPPORT_ONDISKHASHTABLE_H
0015 
0016 #include "llvm/Support/Alignment.h"
0017 #include "llvm/Support/Allocator.h"
0018 #include "llvm/Support/DataTypes.h"
0019 #include "llvm/Support/EndianStream.h"
0020 #include "llvm/Support/MathExtras.h"
0021 #include "llvm/Support/raw_ostream.h"
0022 #include <cassert>
0023 #include <cstdlib>
0024 
0025 namespace llvm {
0026 
0027 /// Generates an on disk hash table.
0028 ///
0029 /// This needs an \c Info that handles storing values into the hash table's
0030 /// payload and computes the hash for a given key. This should provide the
0031 /// following interface:
0032 ///
0033 /// \code
0034 /// class ExampleInfo {
0035 /// public:
0036 ///   typedef ExampleKey key_type;   // Must be copy constructible
0037 ///   typedef ExampleKey &key_type_ref;
0038 ///   typedef ExampleData data_type; // Must be copy constructible
0039 ///   typedef ExampleData &data_type_ref;
0040 ///   typedef uint32_t hash_value_type; // The type the hash function returns.
0041 ///   typedef uint32_t offset_type; // The type for offsets into the table.
0042 ///
0043 ///   /// Calculate the hash for Key
0044 ///   static hash_value_type ComputeHash(key_type_ref Key);
0045 ///   /// Return the lengths, in bytes, of the given Key/Data pair.
0046 ///   static std::pair<offset_type, offset_type>
0047 ///   EmitKeyDataLength(raw_ostream &Out, key_type_ref Key, data_type_ref Data);
0048 ///   /// Write Key to Out.  KeyLen is the length from EmitKeyDataLength.
0049 ///   static void EmitKey(raw_ostream &Out, key_type_ref Key,
0050 ///                       offset_type KeyLen);
0051 ///   /// Write Data to Out.  DataLen is the length from EmitKeyDataLength.
0052 ///   static void EmitData(raw_ostream &Out, key_type_ref Key,
0053 ///                        data_type_ref Data, offset_type DataLen);
0054 ///   /// Determine if two keys are equal. Optional, only needed by contains.
0055 ///   static bool EqualKey(key_type_ref Key1, key_type_ref Key2);
0056 /// };
0057 /// \endcode
0058 template <typename Info> class OnDiskChainedHashTableGenerator {
0059   /// A single item in the hash table.
0060   class Item {
0061   public:
0062     typename Info::key_type Key;
0063     typename Info::data_type Data;
0064     Item *Next;
0065     const typename Info::hash_value_type Hash;
0066 
0067     Item(typename Info::key_type_ref Key, typename Info::data_type_ref Data,
0068          Info &InfoObj)
0069         : Key(Key), Data(Data), Next(nullptr), Hash(InfoObj.ComputeHash(Key)) {}
0070   };
0071 
0072   typedef typename Info::offset_type offset_type;
0073   offset_type NumBuckets;
0074   offset_type NumEntries;
0075   llvm::SpecificBumpPtrAllocator<Item> BA;
0076 
0077   /// A linked list of values in a particular hash bucket.
0078   struct Bucket {
0079     offset_type Off;
0080     unsigned Length;
0081     Item *Head;
0082   };
0083 
0084   Bucket *Buckets;
0085 
0086 private:
0087   /// Insert an item into the appropriate hash bucket.
0088   void insert(Bucket *Buckets, size_t Size, Item *E) {
0089     Bucket &B = Buckets[E->Hash & (Size - 1)];
0090     E->Next = B.Head;
0091     ++B.Length;
0092     B.Head = E;
0093   }
0094 
0095   /// Resize the hash table, moving the old entries into the new buckets.
0096   void resize(size_t NewSize) {
0097     Bucket *NewBuckets = static_cast<Bucket *>(
0098         safe_calloc(NewSize, sizeof(Bucket)));
0099     // Populate NewBuckets with the old entries.
0100     for (size_t I = 0; I < NumBuckets; ++I)
0101       for (Item *E = Buckets[I].Head; E;) {
0102         Item *N = E->Next;
0103         E->Next = nullptr;
0104         insert(NewBuckets, NewSize, E);
0105         E = N;
0106       }
0107 
0108     free(Buckets);
0109     NumBuckets = NewSize;
0110     Buckets = NewBuckets;
0111   }
0112 
0113 public:
0114   /// Insert an entry into the table.
0115   void insert(typename Info::key_type_ref Key,
0116               typename Info::data_type_ref Data) {
0117     Info InfoObj;
0118     insert(Key, Data, InfoObj);
0119   }
0120 
0121   /// Insert an entry into the table.
0122   ///
0123   /// Uses the provided Info instead of a stack allocated one.
0124   void insert(typename Info::key_type_ref Key,
0125               typename Info::data_type_ref Data, Info &InfoObj) {
0126     ++NumEntries;
0127     if (4 * NumEntries >= 3 * NumBuckets)
0128       resize(NumBuckets * 2);
0129     insert(Buckets, NumBuckets, new (BA.Allocate()) Item(Key, Data, InfoObj));
0130   }
0131 
0132   /// Determine whether an entry has been inserted.
0133   bool contains(typename Info::key_type_ref Key, Info &InfoObj) {
0134     unsigned Hash = InfoObj.ComputeHash(Key);
0135     for (Item *I = Buckets[Hash & (NumBuckets - 1)].Head; I; I = I->Next)
0136       if (I->Hash == Hash && InfoObj.EqualKey(I->Key, Key))
0137         return true;
0138     return false;
0139   }
0140 
0141   /// Emit the table to Out, which must not be at offset 0.
0142   offset_type Emit(raw_ostream &Out) {
0143     Info InfoObj;
0144     return Emit(Out, InfoObj);
0145   }
0146 
0147   /// Emit the table to Out, which must not be at offset 0.
0148   ///
0149   /// Uses the provided Info instead of a stack allocated one.
0150   offset_type Emit(raw_ostream &Out, Info &InfoObj) {
0151     using namespace llvm::support;
0152     endian::Writer LE(Out, llvm::endianness::little);
0153 
0154     // Now we're done adding entries, resize the bucket list if it's
0155     // significantly too large. (This only happens if the number of
0156     // entries is small and we're within our initial allocation of
0157     // 64 buckets.) We aim for an occupancy ratio in [3/8, 3/4).
0158     //
0159     // As a special case, if there are two or fewer entries, just
0160     // form a single bucket. A linear scan is fine in that case, and
0161     // this is very common in C++ class lookup tables. This also
0162     // guarantees we produce at least one bucket for an empty table.
0163     //
0164     // FIXME: Try computing a perfect hash function at this point.
0165     unsigned TargetNumBuckets =
0166         NumEntries <= 2 ? 1 : llvm::bit_ceil(NumEntries * 4 / 3 + 1);
0167     if (TargetNumBuckets != NumBuckets)
0168       resize(TargetNumBuckets);
0169 
0170     // Emit the payload of the table.
0171     for (offset_type I = 0; I < NumBuckets; ++I) {
0172       Bucket &B = Buckets[I];
0173       if (!B.Head)
0174         continue;
0175 
0176       // Store the offset for the data of this bucket.
0177       B.Off = Out.tell();
0178       assert(B.Off && "Cannot write a bucket at offset 0. Please add padding.");
0179 
0180       // Write out the number of items in the bucket.
0181       LE.write<uint16_t>(B.Length);
0182       assert(B.Length != 0 && "Bucket has a head but zero length?");
0183 
0184       // Write out the entries in the bucket.
0185       for (Item *I = B.Head; I; I = I->Next) {
0186         LE.write<typename Info::hash_value_type>(I->Hash);
0187         const std::pair<offset_type, offset_type> &Len =
0188             InfoObj.EmitKeyDataLength(Out, I->Key, I->Data);
0189 #ifdef NDEBUG
0190         InfoObj.EmitKey(Out, I->Key, Len.first);
0191         InfoObj.EmitData(Out, I->Key, I->Data, Len.second);
0192 #else
0193         // In asserts mode, check that the users length matches the data they
0194         // wrote.
0195         uint64_t KeyStart = Out.tell();
0196         InfoObj.EmitKey(Out, I->Key, Len.first);
0197         uint64_t DataStart = Out.tell();
0198         InfoObj.EmitData(Out, I->Key, I->Data, Len.second);
0199         uint64_t End = Out.tell();
0200         assert(offset_type(DataStart - KeyStart) == Len.first &&
0201                "key length does not match bytes written");
0202         assert(offset_type(End - DataStart) == Len.second &&
0203                "data length does not match bytes written");
0204 #endif
0205       }
0206     }
0207 
0208     // Pad with zeros so that we can start the hashtable at an aligned address.
0209     offset_type TableOff = Out.tell();
0210     uint64_t N = offsetToAlignment(TableOff, Align(alignof(offset_type)));
0211     TableOff += N;
0212     while (N--)
0213       LE.write<uint8_t>(0);
0214 
0215     // Emit the hashtable itself.
0216     LE.write<offset_type>(NumBuckets);
0217     LE.write<offset_type>(NumEntries);
0218     for (offset_type I = 0; I < NumBuckets; ++I)
0219       LE.write<offset_type>(Buckets[I].Off);
0220 
0221     return TableOff;
0222   }
0223 
0224   OnDiskChainedHashTableGenerator() {
0225     NumEntries = 0;
0226     NumBuckets = 64;
0227     // Note that we do not need to run the constructors of the individual
0228     // Bucket objects since 'calloc' returns bytes that are all 0.
0229     Buckets = static_cast<Bucket *>(safe_calloc(NumBuckets, sizeof(Bucket)));
0230   }
0231 
0232   ~OnDiskChainedHashTableGenerator() { std::free(Buckets); }
0233 };
0234 
0235 /// Provides lookup on an on disk hash table.
0236 ///
0237 /// This needs an \c Info that handles reading values from the hash table's
0238 /// payload and computes the hash for a given key. This should provide the
0239 /// following interface:
0240 ///
0241 /// \code
0242 /// class ExampleLookupInfo {
0243 /// public:
0244 ///   typedef ExampleData data_type;
0245 ///   typedef ExampleInternalKey internal_key_type; // The stored key type.
0246 ///   typedef ExampleKey external_key_type; // The type to pass to find().
0247 ///   typedef uint32_t hash_value_type; // The type the hash function returns.
0248 ///   typedef uint32_t offset_type; // The type for offsets into the table.
0249 ///
0250 ///   /// Compare two keys for equality.
0251 ///   static bool EqualKey(internal_key_type &Key1, internal_key_type &Key2);
0252 ///   /// Calculate the hash for the given key.
0253 ///   static hash_value_type ComputeHash(internal_key_type &IKey);
0254 ///   /// Translate from the semantic type of a key in the hash table to the
0255 ///   /// type that is actually stored and used for hashing and comparisons.
0256 ///   /// The internal and external types are often the same, in which case this
0257 ///   /// can simply return the passed in value.
0258 ///   static const internal_key_type &GetInternalKey(external_key_type &EKey);
0259 ///   /// Read the key and data length from Buffer, leaving it pointing at the
0260 ///   /// following byte.
0261 ///   static std::pair<offset_type, offset_type>
0262 ///   ReadKeyDataLength(const unsigned char *&Buffer);
0263 ///   /// Read the key from Buffer, given the KeyLen as reported from
0264 ///   /// ReadKeyDataLength.
0265 ///   const internal_key_type &ReadKey(const unsigned char *Buffer,
0266 ///                                    offset_type KeyLen);
0267 ///   /// Read the data for Key from Buffer, given the DataLen as reported from
0268 ///   /// ReadKeyDataLength.
0269 ///   data_type ReadData(StringRef Key, const unsigned char *Buffer,
0270 ///                      offset_type DataLen);
0271 /// };
0272 /// \endcode
0273 template <typename Info> class OnDiskChainedHashTable {
0274   const typename Info::offset_type NumBuckets;
0275   const typename Info::offset_type NumEntries;
0276   const unsigned char *const Buckets;
0277   const unsigned char *const Base;
0278   Info InfoObj;
0279 
0280 public:
0281   typedef Info InfoType;
0282   typedef typename Info::internal_key_type internal_key_type;
0283   typedef typename Info::external_key_type external_key_type;
0284   typedef typename Info::data_type data_type;
0285   typedef typename Info::hash_value_type hash_value_type;
0286   typedef typename Info::offset_type offset_type;
0287 
0288   OnDiskChainedHashTable(offset_type NumBuckets, offset_type NumEntries,
0289                          const unsigned char *Buckets,
0290                          const unsigned char *Base,
0291                          const Info &InfoObj = Info())
0292       : NumBuckets(NumBuckets), NumEntries(NumEntries), Buckets(Buckets),
0293         Base(Base), InfoObj(InfoObj) {
0294     assert((reinterpret_cast<uintptr_t>(Buckets) & 0x3) == 0 &&
0295            "'buckets' must have a 4-byte alignment");
0296   }
0297 
0298   /// Read the number of buckets and the number of entries from a hash table
0299   /// produced by OnDiskHashTableGenerator::Emit, and advance the Buckets
0300   /// pointer past them.
0301   static std::pair<offset_type, offset_type>
0302   readNumBucketsAndEntries(const unsigned char *&Buckets) {
0303     assert((reinterpret_cast<uintptr_t>(Buckets) & 0x3) == 0 &&
0304            "buckets should be 4-byte aligned.");
0305     using namespace llvm::support;
0306     offset_type NumBuckets =
0307         endian::readNext<offset_type, llvm::endianness::little, aligned>(
0308             Buckets);
0309     offset_type NumEntries =
0310         endian::readNext<offset_type, llvm::endianness::little, aligned>(
0311             Buckets);
0312     return std::make_pair(NumBuckets, NumEntries);
0313   }
0314 
0315   offset_type getNumBuckets() const { return NumBuckets; }
0316   offset_type getNumEntries() const { return NumEntries; }
0317   const unsigned char *getBase() const { return Base; }
0318   const unsigned char *getBuckets() const { return Buckets; }
0319 
0320   bool isEmpty() const { return NumEntries == 0; }
0321 
0322   class iterator {
0323     internal_key_type Key;
0324     const unsigned char *const Data;
0325     const offset_type Len;
0326     Info *InfoObj;
0327 
0328   public:
0329     iterator() : Key(), Data(nullptr), Len(0), InfoObj(nullptr) {}
0330     iterator(const internal_key_type K, const unsigned char *D, offset_type L,
0331              Info *InfoObj)
0332         : Key(K), Data(D), Len(L), InfoObj(InfoObj) {}
0333 
0334     data_type operator*() const { return InfoObj->ReadData(Key, Data, Len); }
0335 
0336     const unsigned char *getDataPtr() const { return Data; }
0337     offset_type getDataLen() const { return Len; }
0338 
0339     bool operator==(const iterator &X) const { return X.Data == Data; }
0340     bool operator!=(const iterator &X) const { return X.Data != Data; }
0341   };
0342 
0343   /// Look up the stored data for a particular key.
0344   iterator find(const external_key_type &EKey, Info *InfoPtr = nullptr) {
0345     const internal_key_type &IKey = InfoObj.GetInternalKey(EKey);
0346     hash_value_type KeyHash = InfoObj.ComputeHash(IKey);
0347     return find_hashed(IKey, KeyHash, InfoPtr);
0348   }
0349 
0350   /// Look up the stored data for a particular key with a known hash.
0351   iterator find_hashed(const internal_key_type &IKey, hash_value_type KeyHash,
0352                        Info *InfoPtr = nullptr) {
0353     using namespace llvm::support;
0354 
0355     if (!InfoPtr)
0356       InfoPtr = &InfoObj;
0357 
0358     // Each bucket is just an offset into the hash table file.
0359     offset_type Idx = KeyHash & (NumBuckets - 1);
0360     const unsigned char *Bucket = Buckets + sizeof(offset_type) * Idx;
0361 
0362     offset_type Offset =
0363         endian::readNext<offset_type, llvm::endianness::little, aligned>(
0364             Bucket);
0365     if (Offset == 0)
0366       return iterator(); // Empty bucket.
0367     const unsigned char *Items = Base + Offset;
0368 
0369     // 'Items' starts with a 16-bit unsigned integer representing the
0370     // number of items in this bucket.
0371     unsigned Len = endian::readNext<uint16_t, llvm::endianness::little>(Items);
0372 
0373     for (unsigned i = 0; i < Len; ++i) {
0374       // Read the hash.
0375       hash_value_type ItemHash =
0376           endian::readNext<hash_value_type, llvm::endianness::little>(Items);
0377 
0378       // Determine the length of the key and the data.
0379       const std::pair<offset_type, offset_type> &L =
0380           Info::ReadKeyDataLength(Items);
0381       offset_type ItemLen = L.first + L.second;
0382 
0383       // Compare the hashes.  If they are not the same, skip the entry entirely.
0384       if (ItemHash != KeyHash) {
0385         Items += ItemLen;
0386         continue;
0387       }
0388 
0389       // Read the key.
0390       const internal_key_type &X =
0391           InfoPtr->ReadKey((const unsigned char *const)Items, L.first);
0392 
0393       // If the key doesn't match just skip reading the value.
0394       if (!InfoPtr->EqualKey(X, IKey)) {
0395         Items += ItemLen;
0396         continue;
0397       }
0398 
0399       // The key matches!
0400       return iterator(X, Items + L.first, L.second, InfoPtr);
0401     }
0402 
0403     return iterator();
0404   }
0405 
0406   iterator end() const { return iterator(); }
0407 
0408   Info &getInfoObj() { return InfoObj; }
0409 
0410   /// Create the hash table.
0411   ///
0412   /// \param Buckets is the beginning of the hash table itself, which follows
0413   /// the payload of entire structure. This is the value returned by
0414   /// OnDiskHashTableGenerator::Emit.
0415   ///
0416   /// \param Base is the point from which all offsets into the structure are
0417   /// based. This is offset 0 in the stream that was used when Emitting the
0418   /// table.
0419   static OnDiskChainedHashTable *Create(const unsigned char *Buckets,
0420                                         const unsigned char *const Base,
0421                                         const Info &InfoObj = Info()) {
0422     assert(Buckets > Base);
0423     auto NumBucketsAndEntries = readNumBucketsAndEntries(Buckets);
0424     return new OnDiskChainedHashTable<Info>(NumBucketsAndEntries.first,
0425                                             NumBucketsAndEntries.second,
0426                                             Buckets, Base, InfoObj);
0427   }
0428 };
0429 
0430 /// Provides lookup and iteration over an on disk hash table.
0431 ///
0432 /// \copydetails llvm::OnDiskChainedHashTable
0433 template <typename Info>
0434 class OnDiskIterableChainedHashTable : public OnDiskChainedHashTable<Info> {
0435   const unsigned char *Payload;
0436 
0437 public:
0438   typedef OnDiskChainedHashTable<Info>          base_type;
0439   typedef typename base_type::internal_key_type internal_key_type;
0440   typedef typename base_type::external_key_type external_key_type;
0441   typedef typename base_type::data_type         data_type;
0442   typedef typename base_type::hash_value_type   hash_value_type;
0443   typedef typename base_type::offset_type       offset_type;
0444 
0445 private:
0446   /// Iterates over all of the keys in the table.
0447   class iterator_base {
0448     const unsigned char *Ptr;
0449     offset_type NumItemsInBucketLeft;
0450     offset_type NumEntriesLeft;
0451 
0452   public:
0453     typedef external_key_type value_type;
0454 
0455     iterator_base(const unsigned char *const Ptr, offset_type NumEntries)
0456         : Ptr(Ptr), NumItemsInBucketLeft(0), NumEntriesLeft(NumEntries) {}
0457     iterator_base()
0458         : Ptr(nullptr), NumItemsInBucketLeft(0), NumEntriesLeft(0) {}
0459 
0460     friend bool operator==(const iterator_base &X, const iterator_base &Y) {
0461       return X.NumEntriesLeft == Y.NumEntriesLeft;
0462     }
0463     friend bool operator!=(const iterator_base &X, const iterator_base &Y) {
0464       return X.NumEntriesLeft != Y.NumEntriesLeft;
0465     }
0466 
0467     /// Move to the next item.
0468     void advance() {
0469       using namespace llvm::support;
0470       if (!NumItemsInBucketLeft) {
0471         // 'Items' starts with a 16-bit unsigned integer representing the
0472         // number of items in this bucket.
0473         NumItemsInBucketLeft =
0474             endian::readNext<uint16_t, llvm::endianness::little>(Ptr);
0475       }
0476       Ptr += sizeof(hash_value_type); // Skip the hash.
0477       // Determine the length of the key and the data.
0478       const std::pair<offset_type, offset_type> &L =
0479           Info::ReadKeyDataLength(Ptr);
0480       Ptr += L.first + L.second;
0481       assert(NumItemsInBucketLeft);
0482       --NumItemsInBucketLeft;
0483       assert(NumEntriesLeft);
0484       --NumEntriesLeft;
0485     }
0486 
0487     /// Get the start of the item as written by the trait (after the hash and
0488     /// immediately before the key and value length).
0489     const unsigned char *getItem() const {
0490       return Ptr + (NumItemsInBucketLeft ? 0 : 2) + sizeof(hash_value_type);
0491     }
0492   };
0493 
0494 public:
0495   OnDiskIterableChainedHashTable(offset_type NumBuckets, offset_type NumEntries,
0496                                  const unsigned char *Buckets,
0497                                  const unsigned char *Payload,
0498                                  const unsigned char *Base,
0499                                  const Info &InfoObj = Info())
0500       : base_type(NumBuckets, NumEntries, Buckets, Base, InfoObj),
0501         Payload(Payload) {}
0502 
0503   /// Iterates over all of the keys in the table.
0504   class key_iterator : public iterator_base {
0505     Info *InfoObj;
0506 
0507   public:
0508     typedef external_key_type value_type;
0509 
0510     key_iterator(const unsigned char *const Ptr, offset_type NumEntries,
0511                  Info *InfoObj)
0512         : iterator_base(Ptr, NumEntries), InfoObj(InfoObj) {}
0513     key_iterator() : iterator_base(), InfoObj() {}
0514 
0515     key_iterator &operator++() {
0516       this->advance();
0517       return *this;
0518     }
0519     key_iterator operator++(int) { // Postincrement
0520       key_iterator tmp = *this;
0521       ++*this;
0522       return tmp;
0523     }
0524 
0525     internal_key_type getInternalKey() const {
0526       auto *LocalPtr = this->getItem();
0527 
0528       // Determine the length of the key and the data.
0529       auto L = Info::ReadKeyDataLength(LocalPtr);
0530 
0531       // Read the key.
0532       return InfoObj->ReadKey(LocalPtr, L.first);
0533     }
0534 
0535     value_type operator*() const {
0536       return InfoObj->GetExternalKey(getInternalKey());
0537     }
0538   };
0539 
0540   key_iterator key_begin() {
0541     return key_iterator(Payload, this->getNumEntries(), &this->getInfoObj());
0542   }
0543   key_iterator key_end() { return key_iterator(); }
0544 
0545   iterator_range<key_iterator> keys() {
0546     return make_range(key_begin(), key_end());
0547   }
0548 
0549   /// Iterates over all the entries in the table, returning the data.
0550   class data_iterator : public iterator_base {
0551     Info *InfoObj;
0552 
0553   public:
0554     typedef data_type value_type;
0555 
0556     data_iterator(const unsigned char *const Ptr, offset_type NumEntries,
0557                   Info *InfoObj)
0558         : iterator_base(Ptr, NumEntries), InfoObj(InfoObj) {}
0559     data_iterator() : iterator_base(), InfoObj() {}
0560 
0561     data_iterator &operator++() { // Preincrement
0562       this->advance();
0563       return *this;
0564     }
0565     data_iterator operator++(int) { // Postincrement
0566       data_iterator tmp = *this;
0567       ++*this;
0568       return tmp;
0569     }
0570 
0571     value_type operator*() const {
0572       auto *LocalPtr = this->getItem();
0573 
0574       // Determine the length of the key and the data.
0575       auto L = Info::ReadKeyDataLength(LocalPtr);
0576 
0577       // Read the key.
0578       const internal_key_type &Key = InfoObj->ReadKey(LocalPtr, L.first);
0579       return InfoObj->ReadData(Key, LocalPtr + L.first, L.second);
0580     }
0581   };
0582 
0583   data_iterator data_begin() {
0584     return data_iterator(Payload, this->getNumEntries(), &this->getInfoObj());
0585   }
0586   data_iterator data_end() { return data_iterator(); }
0587 
0588   iterator_range<data_iterator> data() {
0589     return make_range(data_begin(), data_end());
0590   }
0591 
0592   /// Create the hash table.
0593   ///
0594   /// \param Buckets is the beginning of the hash table itself, which follows
0595   /// the payload of entire structure. This is the value returned by
0596   /// OnDiskHashTableGenerator::Emit.
0597   ///
0598   /// \param Payload is the beginning of the data contained in the table.  This
0599   /// is Base plus any padding or header data that was stored, ie, the offset
0600   /// that the stream was at when calling Emit.
0601   ///
0602   /// \param Base is the point from which all offsets into the structure are
0603   /// based. This is offset 0 in the stream that was used when Emitting the
0604   /// table.
0605   static OnDiskIterableChainedHashTable *
0606   Create(const unsigned char *Buckets, const unsigned char *const Payload,
0607          const unsigned char *const Base, const Info &InfoObj = Info()) {
0608     assert(Buckets > Base);
0609     auto NumBucketsAndEntries =
0610         OnDiskIterableChainedHashTable<Info>::readNumBucketsAndEntries(Buckets);
0611     return new OnDiskIterableChainedHashTable<Info>(
0612         NumBucketsAndEntries.first, NumBucketsAndEntries.second,
0613         Buckets, Payload, Base, InfoObj);
0614   }
0615 };
0616 
0617 } // end namespace llvm
0618 
0619 #endif