[llvm-commits] CVS: llvm/include/llvm/ADT/StringMap.h

Chris Lattner sabre at nondot.org
Tue Apr 3 17:29:58 PDT 2007



Changes in directory llvm/include/llvm/ADT:

StringMap.h updated: 1.12 -> 1.13
---
Log message:

Extend StringMap to support being initialized as completely empty.  When
initialized this way, they do not do a malloc to allocate their buckets.


---
Diffs of the changes:  (+24 -11)

 StringMap.h |   35 ++++++++++++++++++++++++-----------
 1 files changed, 24 insertions(+), 11 deletions(-)


Index: llvm/include/llvm/ADT/StringMap.h
diff -u llvm/include/llvm/ADT/StringMap.h:1.12 llvm/include/llvm/ADT/StringMap.h:1.13
--- llvm/include/llvm/ADT/StringMap.h:1.12	Sun Feb 11 15:46:36 2007
+++ llvm/include/llvm/ADT/StringMap.h	Tue Apr  3 19:29:37 2007
@@ -55,6 +55,7 @@
   unsigned NumTombstones;
   unsigned ItemSize;
 protected:
+  StringMapImpl(unsigned itemSize) : ItemSize(itemSize) { init(16); }
   StringMapImpl(unsigned InitSize, unsigned ItemSize);
   void RehashTable();
   
@@ -87,7 +88,8 @@
   /// RemoveKey - Remove the StringMapEntry for the specified key from the
   /// table, returning it.  If the key is not in the table, this returns null.
   StringMapEntryBase *RemoveKey(const char *KeyStart, const char *KeyEnd);
-  
+private:
+  void init(unsigned Size);
 public:
   static StringMapEntryBase *getTombstoneVal() {
     return (StringMapEntryBase*)-1;
@@ -185,7 +187,8 @@
   AllocatorTy Allocator;
   typedef StringMapEntry<ValueTy> MapEntryTy;
 public:
-  StringMap(unsigned InitialSize = 0)
+  StringMap() : StringMapImpl(sizeof(MapEntryTy)) {}
+  StringMap(unsigned InitialSize)
     : StringMapImpl(InitialSize, sizeof(MapEntryTy)) {}
   
   AllocatorTy &getAllocator() { return Allocator; }
@@ -194,11 +197,18 @@
   typedef StringMapConstIterator<ValueTy> const_iterator;
   typedef StringMapIterator<ValueTy> iterator;
   
-  iterator begin() { return iterator(TheTable); }
-  iterator end() { return iterator(TheTable+NumBuckets); }
-  const_iterator begin() const { return const_iterator(TheTable); }
-  const_iterator end() const { return const_iterator(TheTable+NumBuckets); }
-  
+  iterator begin() {
+    return iterator(TheTable, NumBuckets == 0);
+  }
+  iterator end() {
+    return iterator(TheTable+NumBuckets, true);
+  }
+  const_iterator begin() const {
+    return const_iterator(TheTable, NumBuckets == 0);
+  }
+  const_iterator end() const {
+    return const_iterator(TheTable+NumBuckets, true);
+  }
   
   iterator find(const char *KeyStart, const char *KeyEnd) {
     int Bucket = FindKey(KeyStart, KeyEnd);
@@ -279,8 +289,10 @@
 protected:
   StringMapImpl::ItemBucket *Ptr;
 public:
-  StringMapConstIterator(StringMapImpl::ItemBucket *Bucket) : Ptr(Bucket) {
-    AdvancePastEmptyBuckets();
+  StringMapConstIterator(StringMapImpl::ItemBucket *Bucket,
+                         bool NoAdvance = false)
+  : Ptr(Bucket) {
+    if (!NoAdvance) AdvancePastEmptyBuckets();
   }
   
   const StringMapEntry<ValueTy> &operator*() const {
@@ -316,8 +328,9 @@
 template<typename ValueTy>
 class StringMapIterator : public StringMapConstIterator<ValueTy> {
 public:  
-  StringMapIterator(StringMapImpl::ItemBucket *Bucket)
-    : StringMapConstIterator<ValueTy>(Bucket) {
+  StringMapIterator(StringMapImpl::ItemBucket *Bucket,
+                    bool NoAdvance = false)
+    : StringMapConstIterator<ValueTy>(Bucket, NoAdvance) {
   }
   StringMapEntry<ValueTy> &operator*() const {
     return *static_cast<StringMapEntry<ValueTy>*>(this->Ptr->Item);






More information about the llvm-commits mailing list