Really, really fix PointerUnion3::is
[oota-llvm.git] / include / llvm / ADT / StringMap.h
index 869a87fdced331b0be84ea6b353dfe211cd46f48..f5394750f253fe23cf9d507d748419e4c03d4684 100644 (file)
@@ -34,6 +34,7 @@ class StringMapEntryInitializer {
 public:
   template <typename InitTy>
   static void Initialize(StringMapEntry<ValueTy> &T, InitTy InitVal) {
+    T.second = InitVal;
   }
 };
 
@@ -130,10 +131,10 @@ class StringMapEntry : public StringMapEntryBase {
 public:
   ValueTy second;
 
-  explicit StringMapEntry(unsigned StrLen)
-    : StringMapEntryBase(StrLen), second() {}
-  StringMapEntry(unsigned StrLen, const ValueTy &V)
-    : StringMapEntryBase(StrLen), second(V) {}
+  explicit StringMapEntry(unsigned strLen)
+    : StringMapEntryBase(strLen), second() {}
+  StringMapEntry(unsigned strLen, const ValueTy &V)
+    : StringMapEntryBase(strLen), second(V) {}
 
   const ValueTy &getValue() const { return second; }
   ValueTy &getValue() { return second; }
@@ -182,7 +183,7 @@ public:
   template<typename AllocatorTy>
   static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd,
                                 AllocatorTy &Allocator) {
-    return Create(KeyStart, KeyEnd, Allocator, (void*)0);
+    return Create(KeyStart, KeyEnd, Allocator, 0);
   }
 
 
@@ -195,7 +196,7 @@ public:
   }
 
   static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd) {
-    return Create(KeyStart, KeyEnd, (void*)0);
+    return Create(KeyStart, KeyEnd, ValueTy());
   }
 
   /// GetStringMapEntryFromValue - Given a value that is known to be embedded
@@ -240,6 +241,17 @@ public:
   StringMap() : StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))) {}
   explicit StringMap(unsigned InitialSize)
     : StringMapImpl(InitialSize, static_cast<unsigned>(sizeof(MapEntryTy))) {}
+  explicit StringMap(const StringMap &RHS)
+    : StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))) {
+    assert(RHS.empty() &&
+           "Copy ctor from non-empty stringmap not implemented yet!");
+  }
+  void operator=(const StringMap &RHS) {
+    assert(RHS.empty() &&
+           "assignment from non-empty stringmap not implemented yet!");
+    clear();
+  }
+
 
   AllocatorTy &getAllocator() { return Allocator; }
   const AllocatorTy &getAllocator() const { return Allocator; }
@@ -291,14 +303,33 @@ public:
     return find(key_start, key_start + Key.size());
   }
 
+   /// lookup - Return the entry for the specified key, or a default
+  /// constructed value if no such entry exists.
+  ValueTy lookup(const char *KeyStart, const char *KeyEnd) const {
+    const_iterator it = find(KeyStart, KeyEnd);
+    if (it != end())
+      return it->second;
+    return ValueTy();
+  }
+  ValueTy lookup(const char *Key) const {
+    const_iterator it = find(Key);
+    if (it != end())
+      return it->second;
+    return ValueTy();
+  }
+  ValueTy lookup(const std::string &Key) const {
+    const_iterator it = find(Key);
+    if (it != end())
+      return it->second;
+    return ValueTy();
+  }
+
   ValueTy& operator[](const char *Key) {
-    value_type& entry = GetOrCreateValue(Key, Key + strlen(Key));
-    return entry.getValue();
+    return GetOrCreateValue(Key, Key + strlen(Key)).getValue();
   }
   ValueTy& operator[](const std::string &Key) {
     const char* key_start = (Key.empty() ? NULL : &Key[0]);
-    value_type& entry = GetOrCreateValue(key_start, key_start + Key.size());
-    return entry.getValue();
+    return GetOrCreateValue(key_start, key_start + Key.size()).getValue();
   }
 
   size_type count(const char *KeyStart, const char *KeyEnd) const {
@@ -333,6 +364,22 @@ public:
     return true;
   }
 
+  // clear - Empties out the StringMap
+  void clear() {
+    if (empty()) return;
+
+    // Zap all values, resetting the keys back to non-present (not tombstone),
+    // which is safe because we're removing all elements.
+    for (ItemBucket *I = TheTable, *E = TheTable+NumBuckets; I != E; ++I) {
+      if (I->Item && I->Item != getTombstoneVal()) {
+        static_cast<MapEntryTy*>(I->Item)->Destroy(Allocator);
+        I->Item = 0;
+      }
+    }
+
+    NumItems = 0;
+  }
+
   /// GetOrCreateValue - Look up the specified key in the table.  If a value
   /// exists, return it.  Otherwise, default construct a value, insert it, and
   /// return.
@@ -362,7 +409,7 @@ public:
 
   StringMapEntry<ValueTy> &GetOrCreateValue(const char *KeyStart,
                                             const char *KeyEnd) {
-    return GetOrCreateValue(KeyStart, KeyEnd, (void*)0);
+    return GetOrCreateValue(KeyStart, KeyEnd, ValueTy());
   }
 
   /// remove - Remove the specified key/value pair from the map, but do not
@@ -377,16 +424,24 @@ public:
     V.Destroy(Allocator);
   }
 
+  bool erase(const char *Key) {
+    iterator I = find(Key);
+    if (I == end()) return false;
+    erase(I);
+    return true;
+  }
+
+  bool erase(const std::string &Key) {
+    iterator I = find(Key);
+    if (I == end()) return false;
+    erase(I);
+    return true;
+  }
+
   ~StringMap() {
-    for (ItemBucket *I = TheTable, *E = TheTable+NumBuckets; I != E; ++I) {
-      if (I->Item && I->Item != getTombstoneVal())
-        static_cast<MapEntryTy*>(I->Item)->Destroy(Allocator);
-    }
+    clear();
     free(TheTable);
   }
-private:
-  StringMap(const StringMap &);  // FIXME: Implement.
-  void operator=(const StringMap &);  // FIXME: Implement.
 };
 
 
@@ -395,16 +450,18 @@ class StringMapConstIterator {
 protected:
   StringMapImpl::ItemBucket *Ptr;
 public:
+  typedef StringMapEntry<ValueTy> value_type;
+
   explicit StringMapConstIterator(StringMapImpl::ItemBucket *Bucket,
                                   bool NoAdvance = false)
   : Ptr(Bucket) {
     if (!NoAdvance) AdvancePastEmptyBuckets();
   }
 
-  const StringMapEntry<ValueTy> &operator*() const {
+  const value_type &operator*() const {
     return *static_cast<StringMapEntry<ValueTy>*>(Ptr->Item);
   }
-  const StringMapEntry<ValueTy> *operator->() const {
+  const value_type *operator->() const {
     return static_cast<StringMapEntry<ValueTy>*>(Ptr->Item);
   }