OnDiskHashTable.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- OnDiskHashTable.h - On-Disk Hash Table Implementation --*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// Defines facilities for reading and writing on-disk hash tables.
  16. ///
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_SUPPORT_ONDISKHASHTABLE_H
  19. #define LLVM_SUPPORT_ONDISKHASHTABLE_H
  20. #include "llvm/Support/Alignment.h"
  21. #include "llvm/Support/Allocator.h"
  22. #include "llvm/Support/DataTypes.h"
  23. #include "llvm/Support/EndianStream.h"
  24. #include "llvm/Support/MathExtras.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. #include <cassert>
  27. #include <cstdlib>
  28. namespace llvm {
  29. /// Generates an on disk hash table.
  30. ///
  31. /// This needs an \c Info that handles storing values into the hash table's
  32. /// payload and computes the hash for a given key. This should provide the
  33. /// following interface:
  34. ///
  35. /// \code
  36. /// class ExampleInfo {
  37. /// public:
  38. /// typedef ExampleKey key_type; // Must be copy constructible
  39. /// typedef ExampleKey &key_type_ref;
  40. /// typedef ExampleData data_type; // Must be copy constructible
  41. /// typedef ExampleData &data_type_ref;
  42. /// typedef uint32_t hash_value_type; // The type the hash function returns.
  43. /// typedef uint32_t offset_type; // The type for offsets into the table.
  44. ///
  45. /// /// Calculate the hash for Key
  46. /// static hash_value_type ComputeHash(key_type_ref Key);
  47. /// /// Return the lengths, in bytes, of the given Key/Data pair.
  48. /// static std::pair<offset_type, offset_type>
  49. /// EmitKeyDataLength(raw_ostream &Out, key_type_ref Key, data_type_ref Data);
  50. /// /// Write Key to Out. KeyLen is the length from EmitKeyDataLength.
  51. /// static void EmitKey(raw_ostream &Out, key_type_ref Key,
  52. /// offset_type KeyLen);
  53. /// /// Write Data to Out. DataLen is the length from EmitKeyDataLength.
  54. /// static void EmitData(raw_ostream &Out, key_type_ref Key,
  55. /// data_type_ref Data, offset_type DataLen);
  56. /// /// Determine if two keys are equal. Optional, only needed by contains.
  57. /// static bool EqualKey(key_type_ref Key1, key_type_ref Key2);
  58. /// };
  59. /// \endcode
  60. template <typename Info> class OnDiskChainedHashTableGenerator {
  61. /// A single item in the hash table.
  62. class Item {
  63. public:
  64. typename Info::key_type Key;
  65. typename Info::data_type Data;
  66. Item *Next;
  67. const typename Info::hash_value_type Hash;
  68. Item(typename Info::key_type_ref Key, typename Info::data_type_ref Data,
  69. Info &InfoObj)
  70. : Key(Key), Data(Data), Next(nullptr), Hash(InfoObj.ComputeHash(Key)) {}
  71. };
  72. typedef typename Info::offset_type offset_type;
  73. offset_type NumBuckets;
  74. offset_type NumEntries;
  75. llvm::SpecificBumpPtrAllocator<Item> BA;
  76. /// A linked list of values in a particular hash bucket.
  77. struct Bucket {
  78. offset_type Off;
  79. unsigned Length;
  80. Item *Head;
  81. };
  82. Bucket *Buckets;
  83. private:
  84. /// Insert an item into the appropriate hash bucket.
  85. void insert(Bucket *Buckets, size_t Size, Item *E) {
  86. Bucket &B = Buckets[E->Hash & (Size - 1)];
  87. E->Next = B.Head;
  88. ++B.Length;
  89. B.Head = E;
  90. }
  91. /// Resize the hash table, moving the old entries into the new buckets.
  92. void resize(size_t NewSize) {
  93. Bucket *NewBuckets = static_cast<Bucket *>(
  94. safe_calloc(NewSize, sizeof(Bucket)));
  95. // Populate NewBuckets with the old entries.
  96. for (size_t I = 0; I < NumBuckets; ++I)
  97. for (Item *E = Buckets[I].Head; E;) {
  98. Item *N = E->Next;
  99. E->Next = nullptr;
  100. insert(NewBuckets, NewSize, E);
  101. E = N;
  102. }
  103. free(Buckets);
  104. NumBuckets = NewSize;
  105. Buckets = NewBuckets;
  106. }
  107. public:
  108. /// Insert an entry into the table.
  109. void insert(typename Info::key_type_ref Key,
  110. typename Info::data_type_ref Data) {
  111. Info InfoObj;
  112. insert(Key, Data, InfoObj);
  113. }
  114. /// Insert an entry into the table.
  115. ///
  116. /// Uses the provided Info instead of a stack allocated one.
  117. void insert(typename Info::key_type_ref Key,
  118. typename Info::data_type_ref Data, Info &InfoObj) {
  119. ++NumEntries;
  120. if (4 * NumEntries >= 3 * NumBuckets)
  121. resize(NumBuckets * 2);
  122. insert(Buckets, NumBuckets, new (BA.Allocate()) Item(Key, Data, InfoObj));
  123. }
  124. /// Determine whether an entry has been inserted.
  125. bool contains(typename Info::key_type_ref Key, Info &InfoObj) {
  126. unsigned Hash = InfoObj.ComputeHash(Key);
  127. for (Item *I = Buckets[Hash & (NumBuckets - 1)].Head; I; I = I->Next)
  128. if (I->Hash == Hash && InfoObj.EqualKey(I->Key, Key))
  129. return true;
  130. return false;
  131. }
  132. /// Emit the table to Out, which must not be at offset 0.
  133. offset_type Emit(raw_ostream &Out) {
  134. Info InfoObj;
  135. return Emit(Out, InfoObj);
  136. }
  137. /// Emit the table to Out, which must not be at offset 0.
  138. ///
  139. /// Uses the provided Info instead of a stack allocated one.
  140. offset_type Emit(raw_ostream &Out, Info &InfoObj) {
  141. using namespace llvm::support;
  142. endian::Writer LE(Out, little);
  143. // Now we're done adding entries, resize the bucket list if it's
  144. // significantly too large. (This only happens if the number of
  145. // entries is small and we're within our initial allocation of
  146. // 64 buckets.) We aim for an occupancy ratio in [3/8, 3/4).
  147. //
  148. // As a special case, if there are two or fewer entries, just
  149. // form a single bucket. A linear scan is fine in that case, and
  150. // this is very common in C++ class lookup tables. This also
  151. // guarantees we produce at least one bucket for an empty table.
  152. //
  153. // FIXME: Try computing a perfect hash function at this point.
  154. unsigned TargetNumBuckets =
  155. NumEntries <= 2 ? 1 : NextPowerOf2(NumEntries * 4 / 3);
  156. if (TargetNumBuckets != NumBuckets)
  157. resize(TargetNumBuckets);
  158. // Emit the payload of the table.
  159. for (offset_type I = 0; I < NumBuckets; ++I) {
  160. Bucket &B = Buckets[I];
  161. if (!B.Head)
  162. continue;
  163. // Store the offset for the data of this bucket.
  164. B.Off = Out.tell();
  165. assert(B.Off && "Cannot write a bucket at offset 0. Please add padding.");
  166. // Write out the number of items in the bucket.
  167. LE.write<uint16_t>(B.Length);
  168. assert(B.Length != 0 && "Bucket has a head but zero length?");
  169. // Write out the entries in the bucket.
  170. for (Item *I = B.Head; I; I = I->Next) {
  171. LE.write<typename Info::hash_value_type>(I->Hash);
  172. const std::pair<offset_type, offset_type> &Len =
  173. InfoObj.EmitKeyDataLength(Out, I->Key, I->Data);
  174. #ifdef NDEBUG
  175. InfoObj.EmitKey(Out, I->Key, Len.first);
  176. InfoObj.EmitData(Out, I->Key, I->Data, Len.second);
  177. #else
  178. // In asserts mode, check that the users length matches the data they
  179. // wrote.
  180. uint64_t KeyStart = Out.tell();
  181. InfoObj.EmitKey(Out, I->Key, Len.first);
  182. uint64_t DataStart = Out.tell();
  183. InfoObj.EmitData(Out, I->Key, I->Data, Len.second);
  184. uint64_t End = Out.tell();
  185. assert(offset_type(DataStart - KeyStart) == Len.first &&
  186. "key length does not match bytes written");
  187. assert(offset_type(End - DataStart) == Len.second &&
  188. "data length does not match bytes written");
  189. #endif
  190. }
  191. }
  192. // Pad with zeros so that we can start the hashtable at an aligned address.
  193. offset_type TableOff = Out.tell();
  194. uint64_t N = offsetToAlignment(TableOff, Align(alignof(offset_type)));
  195. TableOff += N;
  196. while (N--)
  197. LE.write<uint8_t>(0);
  198. // Emit the hashtable itself.
  199. LE.write<offset_type>(NumBuckets);
  200. LE.write<offset_type>(NumEntries);
  201. for (offset_type I = 0; I < NumBuckets; ++I)
  202. LE.write<offset_type>(Buckets[I].Off);
  203. return TableOff;
  204. }
  205. OnDiskChainedHashTableGenerator() {
  206. NumEntries = 0;
  207. NumBuckets = 64;
  208. // Note that we do not need to run the constructors of the individual
  209. // Bucket objects since 'calloc' returns bytes that are all 0.
  210. Buckets = static_cast<Bucket *>(safe_calloc(NumBuckets, sizeof(Bucket)));
  211. }
  212. ~OnDiskChainedHashTableGenerator() { std::free(Buckets); }
  213. };
  214. /// Provides lookup on an on disk hash table.
  215. ///
  216. /// This needs an \c Info that handles reading values from the hash table's
  217. /// payload and computes the hash for a given key. This should provide the
  218. /// following interface:
  219. ///
  220. /// \code
  221. /// class ExampleLookupInfo {
  222. /// public:
  223. /// typedef ExampleData data_type;
  224. /// typedef ExampleInternalKey internal_key_type; // The stored key type.
  225. /// typedef ExampleKey external_key_type; // The type to pass to find().
  226. /// typedef uint32_t hash_value_type; // The type the hash function returns.
  227. /// typedef uint32_t offset_type; // The type for offsets into the table.
  228. ///
  229. /// /// Compare two keys for equality.
  230. /// static bool EqualKey(internal_key_type &Key1, internal_key_type &Key2);
  231. /// /// Calculate the hash for the given key.
  232. /// static hash_value_type ComputeHash(internal_key_type &IKey);
  233. /// /// Translate from the semantic type of a key in the hash table to the
  234. /// /// type that is actually stored and used for hashing and comparisons.
  235. /// /// The internal and external types are often the same, in which case this
  236. /// /// can simply return the passed in value.
  237. /// static const internal_key_type &GetInternalKey(external_key_type &EKey);
  238. /// /// Read the key and data length from Buffer, leaving it pointing at the
  239. /// /// following byte.
  240. /// static std::pair<offset_type, offset_type>
  241. /// ReadKeyDataLength(const unsigned char *&Buffer);
  242. /// /// Read the key from Buffer, given the KeyLen as reported from
  243. /// /// ReadKeyDataLength.
  244. /// const internal_key_type &ReadKey(const unsigned char *Buffer,
  245. /// offset_type KeyLen);
  246. /// /// Read the data for Key from Buffer, given the DataLen as reported from
  247. /// /// ReadKeyDataLength.
  248. /// data_type ReadData(StringRef Key, const unsigned char *Buffer,
  249. /// offset_type DataLen);
  250. /// };
  251. /// \endcode
  252. template <typename Info> class OnDiskChainedHashTable {
  253. const typename Info::offset_type NumBuckets;
  254. const typename Info::offset_type NumEntries;
  255. const unsigned char *const Buckets;
  256. const unsigned char *const Base;
  257. Info InfoObj;
  258. public:
  259. typedef Info InfoType;
  260. typedef typename Info::internal_key_type internal_key_type;
  261. typedef typename Info::external_key_type external_key_type;
  262. typedef typename Info::data_type data_type;
  263. typedef typename Info::hash_value_type hash_value_type;
  264. typedef typename Info::offset_type offset_type;
  265. OnDiskChainedHashTable(offset_type NumBuckets, offset_type NumEntries,
  266. const unsigned char *Buckets,
  267. const unsigned char *Base,
  268. const Info &InfoObj = Info())
  269. : NumBuckets(NumBuckets), NumEntries(NumEntries), Buckets(Buckets),
  270. Base(Base), InfoObj(InfoObj) {
  271. assert((reinterpret_cast<uintptr_t>(Buckets) & 0x3) == 0 &&
  272. "'buckets' must have a 4-byte alignment");
  273. }
  274. /// Read the number of buckets and the number of entries from a hash table
  275. /// produced by OnDiskHashTableGenerator::Emit, and advance the Buckets
  276. /// pointer past them.
  277. static std::pair<offset_type, offset_type>
  278. readNumBucketsAndEntries(const unsigned char *&Buckets) {
  279. assert((reinterpret_cast<uintptr_t>(Buckets) & 0x3) == 0 &&
  280. "buckets should be 4-byte aligned.");
  281. using namespace llvm::support;
  282. offset_type NumBuckets =
  283. endian::readNext<offset_type, little, aligned>(Buckets);
  284. offset_type NumEntries =
  285. endian::readNext<offset_type, little, aligned>(Buckets);
  286. return std::make_pair(NumBuckets, NumEntries);
  287. }
  288. offset_type getNumBuckets() const { return NumBuckets; }
  289. offset_type getNumEntries() const { return NumEntries; }
  290. const unsigned char *getBase() const { return Base; }
  291. const unsigned char *getBuckets() const { return Buckets; }
  292. bool isEmpty() const { return NumEntries == 0; }
  293. class iterator {
  294. internal_key_type Key;
  295. const unsigned char *const Data;
  296. const offset_type Len;
  297. Info *InfoObj;
  298. public:
  299. iterator() : Key(), Data(nullptr), Len(0), InfoObj(nullptr) {}
  300. iterator(const internal_key_type K, const unsigned char *D, offset_type L,
  301. Info *InfoObj)
  302. : Key(K), Data(D), Len(L), InfoObj(InfoObj) {}
  303. data_type operator*() const { return InfoObj->ReadData(Key, Data, Len); }
  304. const unsigned char *getDataPtr() const { return Data; }
  305. offset_type getDataLen() const { return Len; }
  306. bool operator==(const iterator &X) const { return X.Data == Data; }
  307. bool operator!=(const iterator &X) const { return X.Data != Data; }
  308. };
  309. /// Look up the stored data for a particular key.
  310. iterator find(const external_key_type &EKey, Info *InfoPtr = nullptr) {
  311. const internal_key_type &IKey = InfoObj.GetInternalKey(EKey);
  312. hash_value_type KeyHash = InfoObj.ComputeHash(IKey);
  313. return find_hashed(IKey, KeyHash, InfoPtr);
  314. }
  315. /// Look up the stored data for a particular key with a known hash.
  316. iterator find_hashed(const internal_key_type &IKey, hash_value_type KeyHash,
  317. Info *InfoPtr = nullptr) {
  318. using namespace llvm::support;
  319. if (!InfoPtr)
  320. InfoPtr = &InfoObj;
  321. // Each bucket is just an offset into the hash table file.
  322. offset_type Idx = KeyHash & (NumBuckets - 1);
  323. const unsigned char *Bucket = Buckets + sizeof(offset_type) * Idx;
  324. offset_type Offset = endian::readNext<offset_type, little, aligned>(Bucket);
  325. if (Offset == 0)
  326. return iterator(); // Empty bucket.
  327. const unsigned char *Items = Base + Offset;
  328. // 'Items' starts with a 16-bit unsigned integer representing the
  329. // number of items in this bucket.
  330. unsigned Len = endian::readNext<uint16_t, little, unaligned>(Items);
  331. for (unsigned i = 0; i < Len; ++i) {
  332. // Read the hash.
  333. hash_value_type ItemHash =
  334. endian::readNext<hash_value_type, little, unaligned>(Items);
  335. // Determine the length of the key and the data.
  336. const std::pair<offset_type, offset_type> &L =
  337. Info::ReadKeyDataLength(Items);
  338. offset_type ItemLen = L.first + L.second;
  339. // Compare the hashes. If they are not the same, skip the entry entirely.
  340. if (ItemHash != KeyHash) {
  341. Items += ItemLen;
  342. continue;
  343. }
  344. // Read the key.
  345. const internal_key_type &X =
  346. InfoPtr->ReadKey((const unsigned char *const)Items, L.first);
  347. // If the key doesn't match just skip reading the value.
  348. if (!InfoPtr->EqualKey(X, IKey)) {
  349. Items += ItemLen;
  350. continue;
  351. }
  352. // The key matches!
  353. return iterator(X, Items + L.first, L.second, InfoPtr);
  354. }
  355. return iterator();
  356. }
  357. iterator end() const { return iterator(); }
  358. Info &getInfoObj() { return InfoObj; }
  359. /// Create the hash table.
  360. ///
  361. /// \param Buckets is the beginning of the hash table itself, which follows
  362. /// the payload of entire structure. This is the value returned by
  363. /// OnDiskHashTableGenerator::Emit.
  364. ///
  365. /// \param Base is the point from which all offsets into the structure are
  366. /// based. This is offset 0 in the stream that was used when Emitting the
  367. /// table.
  368. static OnDiskChainedHashTable *Create(const unsigned char *Buckets,
  369. const unsigned char *const Base,
  370. const Info &InfoObj = Info()) {
  371. assert(Buckets > Base);
  372. auto NumBucketsAndEntries = readNumBucketsAndEntries(Buckets);
  373. return new OnDiskChainedHashTable<Info>(NumBucketsAndEntries.first,
  374. NumBucketsAndEntries.second,
  375. Buckets, Base, InfoObj);
  376. }
  377. };
  378. /// Provides lookup and iteration over an on disk hash table.
  379. ///
  380. /// \copydetails llvm::OnDiskChainedHashTable
  381. template <typename Info>
  382. class OnDiskIterableChainedHashTable : public OnDiskChainedHashTable<Info> {
  383. const unsigned char *Payload;
  384. public:
  385. typedef OnDiskChainedHashTable<Info> base_type;
  386. typedef typename base_type::internal_key_type internal_key_type;
  387. typedef typename base_type::external_key_type external_key_type;
  388. typedef typename base_type::data_type data_type;
  389. typedef typename base_type::hash_value_type hash_value_type;
  390. typedef typename base_type::offset_type offset_type;
  391. private:
  392. /// Iterates over all of the keys in the table.
  393. class iterator_base {
  394. const unsigned char *Ptr;
  395. offset_type NumItemsInBucketLeft;
  396. offset_type NumEntriesLeft;
  397. public:
  398. typedef external_key_type value_type;
  399. iterator_base(const unsigned char *const Ptr, offset_type NumEntries)
  400. : Ptr(Ptr), NumItemsInBucketLeft(0), NumEntriesLeft(NumEntries) {}
  401. iterator_base()
  402. : Ptr(nullptr), NumItemsInBucketLeft(0), NumEntriesLeft(0) {}
  403. friend bool operator==(const iterator_base &X, const iterator_base &Y) {
  404. return X.NumEntriesLeft == Y.NumEntriesLeft;
  405. }
  406. friend bool operator!=(const iterator_base &X, const iterator_base &Y) {
  407. return X.NumEntriesLeft != Y.NumEntriesLeft;
  408. }
  409. /// Move to the next item.
  410. void advance() {
  411. using namespace llvm::support;
  412. if (!NumItemsInBucketLeft) {
  413. // 'Items' starts with a 16-bit unsigned integer representing the
  414. // number of items in this bucket.
  415. NumItemsInBucketLeft =
  416. endian::readNext<uint16_t, little, unaligned>(Ptr);
  417. }
  418. Ptr += sizeof(hash_value_type); // Skip the hash.
  419. // Determine the length of the key and the data.
  420. const std::pair<offset_type, offset_type> &L =
  421. Info::ReadKeyDataLength(Ptr);
  422. Ptr += L.first + L.second;
  423. assert(NumItemsInBucketLeft);
  424. --NumItemsInBucketLeft;
  425. assert(NumEntriesLeft);
  426. --NumEntriesLeft;
  427. }
  428. /// Get the start of the item as written by the trait (after the hash and
  429. /// immediately before the key and value length).
  430. const unsigned char *getItem() const {
  431. return Ptr + (NumItemsInBucketLeft ? 0 : 2) + sizeof(hash_value_type);
  432. }
  433. };
  434. public:
  435. OnDiskIterableChainedHashTable(offset_type NumBuckets, offset_type NumEntries,
  436. const unsigned char *Buckets,
  437. const unsigned char *Payload,
  438. const unsigned char *Base,
  439. const Info &InfoObj = Info())
  440. : base_type(NumBuckets, NumEntries, Buckets, Base, InfoObj),
  441. Payload(Payload) {}
  442. /// Iterates over all of the keys in the table.
  443. class key_iterator : public iterator_base {
  444. Info *InfoObj;
  445. public:
  446. typedef external_key_type value_type;
  447. key_iterator(const unsigned char *const Ptr, offset_type NumEntries,
  448. Info *InfoObj)
  449. : iterator_base(Ptr, NumEntries), InfoObj(InfoObj) {}
  450. key_iterator() : iterator_base(), InfoObj() {}
  451. key_iterator &operator++() {
  452. this->advance();
  453. return *this;
  454. }
  455. key_iterator operator++(int) { // Postincrement
  456. key_iterator tmp = *this;
  457. ++*this;
  458. return tmp;
  459. }
  460. internal_key_type getInternalKey() const {
  461. auto *LocalPtr = this->getItem();
  462. // Determine the length of the key and the data.
  463. auto L = Info::ReadKeyDataLength(LocalPtr);
  464. // Read the key.
  465. return InfoObj->ReadKey(LocalPtr, L.first);
  466. }
  467. value_type operator*() const {
  468. return InfoObj->GetExternalKey(getInternalKey());
  469. }
  470. };
  471. key_iterator key_begin() {
  472. return key_iterator(Payload, this->getNumEntries(), &this->getInfoObj());
  473. }
  474. key_iterator key_end() { return key_iterator(); }
  475. iterator_range<key_iterator> keys() {
  476. return make_range(key_begin(), key_end());
  477. }
  478. /// Iterates over all the entries in the table, returning the data.
  479. class data_iterator : public iterator_base {
  480. Info *InfoObj;
  481. public:
  482. typedef data_type value_type;
  483. data_iterator(const unsigned char *const Ptr, offset_type NumEntries,
  484. Info *InfoObj)
  485. : iterator_base(Ptr, NumEntries), InfoObj(InfoObj) {}
  486. data_iterator() : iterator_base(), InfoObj() {}
  487. data_iterator &operator++() { // Preincrement
  488. this->advance();
  489. return *this;
  490. }
  491. data_iterator operator++(int) { // Postincrement
  492. data_iterator tmp = *this;
  493. ++*this;
  494. return tmp;
  495. }
  496. value_type operator*() const {
  497. auto *LocalPtr = this->getItem();
  498. // Determine the length of the key and the data.
  499. auto L = Info::ReadKeyDataLength(LocalPtr);
  500. // Read the key.
  501. const internal_key_type &Key = InfoObj->ReadKey(LocalPtr, L.first);
  502. return InfoObj->ReadData(Key, LocalPtr + L.first, L.second);
  503. }
  504. };
  505. data_iterator data_begin() {
  506. return data_iterator(Payload, this->getNumEntries(), &this->getInfoObj());
  507. }
  508. data_iterator data_end() { return data_iterator(); }
  509. iterator_range<data_iterator> data() {
  510. return make_range(data_begin(), data_end());
  511. }
  512. /// Create the hash table.
  513. ///
  514. /// \param Buckets is the beginning of the hash table itself, which follows
  515. /// the payload of entire structure. This is the value returned by
  516. /// OnDiskHashTableGenerator::Emit.
  517. ///
  518. /// \param Payload is the beginning of the data contained in the table. This
  519. /// is Base plus any padding or header data that was stored, ie, the offset
  520. /// that the stream was at when calling Emit.
  521. ///
  522. /// \param Base is the point from which all offsets into the structure are
  523. /// based. This is offset 0 in the stream that was used when Emitting the
  524. /// table.
  525. static OnDiskIterableChainedHashTable *
  526. Create(const unsigned char *Buckets, const unsigned char *const Payload,
  527. const unsigned char *const Base, const Info &InfoObj = Info()) {
  528. assert(Buckets > Base);
  529. auto NumBucketsAndEntries =
  530. OnDiskIterableChainedHashTable<Info>::readNumBucketsAndEntries(Buckets);
  531. return new OnDiskIterableChainedHashTable<Info>(
  532. NumBucketsAndEntries.first, NumBucketsAndEntries.second,
  533. Buckets, Payload, Base, InfoObj);
  534. }
  535. };
  536. } // end namespace llvm
  537. #endif
  538. #ifdef __GNUC__
  539. #pragma GCC diagnostic pop
  540. #endif