Refactor to reduce duplication in OnDiskIterableChainedHashTable's iterators.

llvm-svn: 245995
This commit is contained in:
Richard Smith 2015-08-26 00:22:41 +00:00
parent 30130f2070
commit 0924ceab04
1 changed files with 88 additions and 91 deletions

View File

@ -255,6 +255,21 @@ public:
"'buckets' must have a 4-byte alignment"); "'buckets' must have a 4-byte alignment");
} }
/// Read the number of buckets and the number of entries from a hash table
/// produced by OnDiskHashTableGenerator::Emit, and advance the Buckets
/// pointer past them.
static std::pair<offset_type, offset_type>
readNumBucketsAndEntries(const unsigned char *&Buckets) {
assert((reinterpret_cast<uintptr_t>(Buckets) & 0x3) == 0 &&
"buckets should be 4-byte aligned.");
using namespace llvm::support;
offset_type NumBuckets =
endian::readNext<offset_type, little, aligned>(Buckets);
offset_type NumEntries =
endian::readNext<offset_type, little, aligned>(Buckets);
return std::make_pair(NumBuckets, NumEntries);
}
offset_type getNumBuckets() const { return NumBuckets; } offset_type getNumBuckets() const { return NumBuckets; }
offset_type getNumEntries() const { return NumEntries; } offset_type getNumEntries() const { return NumEntries; }
const unsigned char *getBase() const { return Base; } const unsigned char *getBase() const { return Base; }
@ -356,17 +371,11 @@ public:
static OnDiskChainedHashTable *Create(const unsigned char *Buckets, static OnDiskChainedHashTable *Create(const unsigned char *Buckets,
const unsigned char *const Base, const unsigned char *const Base,
const Info &InfoObj = Info()) { const Info &InfoObj = Info()) {
using namespace llvm::support;
assert(Buckets > Base); assert(Buckets > Base);
assert((reinterpret_cast<uintptr_t>(Buckets) & 0x3) == 0 && auto NumBucketsAndEntries = readNumBucketsAndEntries(Buckets);
"buckets should be 4-byte aligned."); return new OnDiskChainedHashTable<Info>(NumBucketsAndEntries.first,
NumBucketsAndEntries.second,
offset_type NumBuckets = Buckets, Base, InfoObj);
endian::readNext<offset_type, little, aligned>(Buckets);
offset_type NumEntries =
endian::readNext<offset_type, little, aligned>(Buckets);
return new OnDiskChainedHashTable<Info>(NumBuckets, NumEntries, Buckets,
Base, InfoObj);
} }
}; };
@ -385,40 +394,30 @@ public:
typedef typename base_type::hash_value_type hash_value_type; typedef typename base_type::hash_value_type hash_value_type;
typedef typename base_type::offset_type offset_type; typedef typename base_type::offset_type offset_type;
OnDiskIterableChainedHashTable(offset_type NumBuckets, offset_type NumEntries, private:
const unsigned char *Buckets,
const unsigned char *Payload,
const unsigned char *Base,
const Info &InfoObj = Info())
: base_type(NumBuckets, NumEntries, Buckets, Base, InfoObj),
Payload(Payload) {}
/// \brief Iterates over all of the keys in the table. /// \brief Iterates over all of the keys in the table.
class key_iterator { class iterator_base {
const unsigned char *Ptr; const unsigned char *Ptr;
offset_type NumItemsInBucketLeft; offset_type NumItemsInBucketLeft;
offset_type NumEntriesLeft; offset_type NumEntriesLeft;
Info *InfoObj;
public: public:
typedef external_key_type value_type; typedef external_key_type value_type;
key_iterator(const unsigned char *const Ptr, offset_type NumEntries, iterator_base(const unsigned char *const Ptr, offset_type NumEntries)
Info *InfoObj) : Ptr(Ptr), NumItemsInBucketLeft(0), NumEntriesLeft(NumEntries) {}
: Ptr(Ptr), NumItemsInBucketLeft(0), NumEntriesLeft(NumEntries), iterator_base()
InfoObj(InfoObj) {} : Ptr(nullptr), NumItemsInBucketLeft(0), NumEntriesLeft(0) {}
key_iterator()
: Ptr(nullptr), NumItemsInBucketLeft(0), NumEntriesLeft(0),
InfoObj(0) {}
friend bool operator==(const key_iterator &X, const key_iterator &Y) { friend bool operator==(const iterator_base &X, const iterator_base &Y) {
return X.NumEntriesLeft == Y.NumEntriesLeft; return X.NumEntriesLeft == Y.NumEntriesLeft;
} }
friend bool operator!=(const key_iterator &X, const key_iterator &Y) { friend bool operator!=(const iterator_base &X, const iterator_base &Y) {
return X.NumEntriesLeft != Y.NumEntriesLeft; return X.NumEntriesLeft != Y.NumEntriesLeft;
} }
key_iterator &operator++() { // Preincrement /// Move to the next item.
void advance() {
using namespace llvm::support; using namespace llvm::support;
if (!NumItemsInBucketLeft) { if (!NumItemsInBucketLeft) {
// 'Items' starts with a 16-bit unsigned integer representing the // 'Items' starts with a 16-bit unsigned integer representing the
@ -435,25 +434,58 @@ public:
--NumItemsInBucketLeft; --NumItemsInBucketLeft;
assert(NumEntriesLeft); assert(NumEntriesLeft);
--NumEntriesLeft; --NumEntriesLeft;
}
/// Get the start of the item as written by the trait (after the hash and
/// immediately before the key and value length).
const unsigned char *getItem() const {
return Ptr + (NumItemsInBucketLeft ? 0 : 2) + sizeof(hash_value_type);
}
};
public:
OnDiskIterableChainedHashTable(offset_type NumBuckets, offset_type NumEntries,
const unsigned char *Buckets,
const unsigned char *Payload,
const unsigned char *Base,
const Info &InfoObj = Info())
: base_type(NumBuckets, NumEntries, Buckets, Base, InfoObj),
Payload(Payload) {}
/// \brief Iterates over all of the keys in the table.
class key_iterator : public iterator_base {
Info *InfoObj;
public:
typedef external_key_type value_type;
key_iterator(const unsigned char *const Ptr, offset_type NumEntries,
Info *InfoObj)
: iterator_base(Ptr, NumEntries), InfoObj(InfoObj) {}
key_iterator() : iterator_base(), InfoObj() {}
key_iterator &operator++() {
this->advance();
return *this; return *this;
} }
key_iterator operator++(int) { // Postincrement key_iterator operator++(int) { // Postincrement
key_iterator tmp = *this; ++*this; return tmp; key_iterator tmp = *this;
++*this;
return tmp;
}
internal_key_type getInternalKey() const {
auto *LocalPtr = this->getItem();
// Determine the length of the key and the data.
auto L = Info::ReadKeyDataLength(LocalPtr);
// Read the key.
return InfoObj->ReadKey(LocalPtr, L.first);
} }
value_type operator*() const { value_type operator*() const {
const unsigned char *LocalPtr = Ptr; return InfoObj->GetExternalKey(getInternalKey());
if (!NumItemsInBucketLeft)
LocalPtr += 2; // number of items in bucket
LocalPtr += sizeof(hash_value_type); // Skip the hash.
// Determine the length of the key and the data.
const std::pair<offset_type, offset_type> &L =
Info::ReadKeyDataLength(LocalPtr);
// Read the key.
const internal_key_type &Key = InfoObj->ReadKey(LocalPtr, L.first);
return InfoObj->GetExternalKey(Key);
} }
}; };
@ -467,10 +499,7 @@ public:
} }
/// \brief Iterates over all the entries in the table, returning the data. /// \brief Iterates over all the entries in the table, returning the data.
class data_iterator { class data_iterator : public iterator_base {
const unsigned char *Ptr;
offset_type NumItemsInBucketLeft;
offset_type NumEntriesLeft;
Info *InfoObj; Info *InfoObj;
public: public:
@ -478,51 +507,24 @@ public:
data_iterator(const unsigned char *const Ptr, offset_type NumEntries, data_iterator(const unsigned char *const Ptr, offset_type NumEntries,
Info *InfoObj) Info *InfoObj)
: Ptr(Ptr), NumItemsInBucketLeft(0), NumEntriesLeft(NumEntries), : iterator_base(Ptr, NumEntries), InfoObj(InfoObj) {}
InfoObj(InfoObj) {} data_iterator() : iterator_base(), InfoObj() {}
data_iterator()
: Ptr(nullptr), NumItemsInBucketLeft(0), NumEntriesLeft(0),
InfoObj(nullptr) {}
bool operator==(const data_iterator &X) const {
return X.NumEntriesLeft == NumEntriesLeft;
}
bool operator!=(const data_iterator &X) const {
return X.NumEntriesLeft != NumEntriesLeft;
}
data_iterator &operator++() { // Preincrement data_iterator &operator++() { // Preincrement
using namespace llvm::support; this->advance();
if (!NumItemsInBucketLeft) {
// 'Items' starts with a 16-bit unsigned integer representing the
// number of items in this bucket.
NumItemsInBucketLeft =
endian::readNext<uint16_t, little, unaligned>(Ptr);
}
Ptr += sizeof(hash_value_type); // Skip the hash.
// Determine the length of the key and the data.
const std::pair<offset_type, offset_type> &L =
Info::ReadKeyDataLength(Ptr);
Ptr += L.first + L.second;
assert(NumItemsInBucketLeft);
--NumItemsInBucketLeft;
assert(NumEntriesLeft);
--NumEntriesLeft;
return *this; return *this;
} }
data_iterator operator++(int) { // Postincrement data_iterator operator++(int) { // Postincrement
data_iterator tmp = *this; ++*this; return tmp; data_iterator tmp = *this;
++*this;
return tmp;
} }
value_type operator*() const { value_type operator*() const {
const unsigned char *LocalPtr = Ptr; auto *LocalPtr = this->getItem();
if (!NumItemsInBucketLeft)
LocalPtr += 2; // number of items in bucket
LocalPtr += sizeof(hash_value_type); // Skip the hash.
// Determine the length of the key and the data. // Determine the length of the key and the data.
const std::pair<offset_type, offset_type> &L = auto L = Info::ReadKeyDataLength(LocalPtr);
Info::ReadKeyDataLength(LocalPtr);
// Read the key. // Read the key.
const internal_key_type &Key = InfoObj->ReadKey(LocalPtr, L.first); const internal_key_type &Key = InfoObj->ReadKey(LocalPtr, L.first);
@ -555,17 +557,12 @@ public:
static OnDiskIterableChainedHashTable * static OnDiskIterableChainedHashTable *
Create(const unsigned char *Buckets, const unsigned char *const Payload, Create(const unsigned char *Buckets, const unsigned char *const Payload,
const unsigned char *const Base, const Info &InfoObj = Info()) { const unsigned char *const Base, const Info &InfoObj = Info()) {
using namespace llvm::support;
assert(Buckets > Base); assert(Buckets > Base);
assert((reinterpret_cast<uintptr_t>(Buckets) & 0x3) == 0 && auto NumBucketsAndEntries =
"buckets should be 4-byte aligned."); OnDiskIterableChainedHashTable<Info>::readNumBucketsAndEntries(Buckets);
offset_type NumBuckets =
endian::readNext<offset_type, little, aligned>(Buckets);
offset_type NumEntries =
endian::readNext<offset_type, little, aligned>(Buckets);
return new OnDiskIterableChainedHashTable<Info>( return new OnDiskIterableChainedHashTable<Info>(
NumBuckets, NumEntries, Buckets, Payload, Base, InfoObj); NumBucketsAndEntries.first, NumBucketsAndEntries.second,
Buckets, Payload, Base, InfoObj);
} }
}; };