]> granicus.if.org Git - clang/commitdiff
Add an iterator to OnDiskChainedHashTable to allow iterating over all the key/data...
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Fri, 20 Aug 2010 23:31:11 +0000 (23:31 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Fri, 20 Aug 2010 23:31:11 +0000 (23:31 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@111697 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/OnDiskHashTable.h

index aa3f344a6a491496561dd935f74b5134f61a186f..8909e47146a7f418357a657ef23bd61e587839ae 100644 (file)
@@ -334,6 +334,71 @@ public:
 
   iterator end() const { return iterator(); }
 
+  /// \brief Iterates over all the entries in the table, returning
+  /// a key/data pair.
+  class item_iterator {
+    const unsigned char* Ptr;
+    unsigned NumItemsInBucketLeft;
+    unsigned NumEntriesLeft;
+    Info *InfoObj;
+  public:
+    typedef std::pair<external_key_type, data_type> value_type;
+
+    item_iterator(const unsigned char* const Ptr, unsigned NumEntries,
+                  Info *InfoObj)
+      : Ptr(Ptr), NumItemsInBucketLeft(0), NumEntriesLeft(NumEntries),
+        InfoObj(InfoObj) { }
+    item_iterator()
+      : Ptr(0), NumItemsInBucketLeft(0), NumEntriesLeft(0), InfoObj(0) { }
+
+    bool operator==(const item_iterator& X) const {
+      return X.NumEntriesLeft == NumEntriesLeft;
+    }
+    bool operator!=(const item_iterator& X) const {
+      return X.NumEntriesLeft != NumEntriesLeft;
+    }
+    
+    item_iterator& operator++() {  // Preincrement
+      if (!NumItemsInBucketLeft) {
+        // 'Items' starts with a 16-bit unsigned integer representing the
+        // number of items in this bucket.
+        NumItemsInBucketLeft = io::ReadUnalignedLE16(Ptr);
+      }
+      Ptr += 4; // Skip the hash.
+      // Determine the length of the key and the data.
+      const std::pair<unsigned, unsigned>& L = Info::ReadKeyDataLength(Ptr);
+      Ptr += L.first + L.second;
+      assert(NumItemsInBucketLeft);
+      --NumItemsInBucketLeft;
+      assert(NumEntriesLeft);
+      --NumEntriesLeft;
+      return *this;
+    }
+    item_iterator operator++(int) {  // Postincrement
+      item_iterator tmp = *this; ++*this; return tmp;
+    }
+
+    value_type operator*() const {
+      const unsigned char* LocalPtr = Ptr;
+      if (!NumItemsInBucketLeft)
+        LocalPtr += 2; // number of items in bucket
+      LocalPtr += 4; // Skip the hash.
+
+      // Determine the length of the key and the data.
+      const std::pair<unsigned, unsigned>& L = Info::ReadKeyDataLength(LocalPtr);
+
+      // Read the key.
+      const internal_key_type& Key =
+        InfoObj->ReadKey(LocalPtr, L.first);
+      return std::make_pair(InfoObj->GetExternalKey(Key),
+                          InfoObj->ReadData(Key, LocalPtr + L.first, L.second));
+    }
+  };
+  
+  item_iterator item_begin() {
+    return item_iterator(Base + 4, getNumEntries(), &InfoObj);
+  }
+  item_iterator item_end() { return item_iterator(); }
 
   static OnDiskChainedHashTable* Create(const unsigned char* buckets,
                                         const unsigned char* const base,