OnDiskHashTable.h 22 KB

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