Add llvm::triple constructor from arch, vendor, os strings, and recognize
[oota-llvm.git] / include / llvm / ADT / StringMap.h
index 407cb1f12382b07c3ac54b6df5f768d5f7bad2ff..a15d24eeae2511fa491b6c7badaea91d19dc48e7 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "llvm/Support/Allocator.h"
 #include <cstring>
+#include <string>
 
 namespace llvm {
   template<typename ValueT>
@@ -33,6 +34,7 @@ class StringMapEntryInitializer {
 public:
   template <typename InitTy>
   static void Initialize(StringMapEntry<ValueTy> &T, InitTy InitVal) {
+    T.second = InitVal;
   }
 };
 
@@ -126,36 +128,40 @@ public:
 /// and data.
 template<typename ValueTy>
 class StringMapEntry : public StringMapEntryBase {
-  ValueTy Val;
 public:
-  explicit StringMapEntry(unsigned StrLen)
-    : StringMapEntryBase(StrLen), Val() {}
-  StringMapEntry(unsigned StrLen, const ValueTy &V)
-    : StringMapEntryBase(StrLen), Val(V) {}
+  ValueTy second;
 
-  const ValueTy &getValue() const { return Val; }
-  ValueTy &getValue() { return Val; }
+  explicit StringMapEntry(unsigned strLen)
+    : StringMapEntryBase(strLen), second() {}
+  StringMapEntry(unsigned strLen, const ValueTy &V)
+    : StringMapEntryBase(strLen), second(V) {}
 
-  void setValue(const ValueTy &V) { Val = V; }
+  const ValueTy &getValue() const { return second; }
+  ValueTy &getValue() { return second; }
+
+  void setValue(const ValueTy &V) { second = V; }
 
   /// getKeyData - Return the start of the string data that is the key for this
   /// value.  The string data is always stored immediately after the
   /// StringMapEntry object.
   const char *getKeyData() const {return reinterpret_cast<const char*>(this+1);}
 
+  const char *first() const { return getKeyData(); }
+
   /// Create - Create a StringMapEntry for the specified key and default
   /// construct the value.
   template<typename AllocatorTy, typename InitType>
   static StringMapEntry *Create(const char *KeyStart, const char *KeyEnd,
                                 AllocatorTy &Allocator,
                                 InitType InitVal) {
-    unsigned KeyLength = KeyEnd-KeyStart;
+    unsigned KeyLength = static_cast<unsigned>(KeyEnd-KeyStart);
 
     // Okay, the item doesn't already exist, and 'Bucket' is the bucket to fill
     // in.  Allocate a new item with space for the string at the end and a null
     // terminator.
 
-    unsigned AllocSize = sizeof(StringMapEntry)+KeyLength+1;
+    unsigned AllocSize = static_cast<unsigned>(sizeof(StringMapEntry))+
+      KeyLength+1;
     unsigned Alignment = alignof<StringMapEntry>();
 
     StringMapEntry *NewItem =
@@ -177,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);
   }
 
 
@@ -190,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
@@ -198,7 +204,7 @@ public:
   static StringMapEntry &GetStringMapEntryFromValue(ValueTy &V) {
     StringMapEntry *EPtr = 0;
     char *Ptr = reinterpret_cast<char*>(&V) -
-                  (reinterpret_cast<char*>(&EPtr->Val) -
+                  (reinterpret_cast<char*>(&EPtr->second) -
                    reinterpret_cast<char*>(EPtr));
     return *reinterpret_cast<StringMapEntry*>(Ptr);
   }
@@ -232,13 +238,29 @@ class StringMap : public StringMapImpl {
   AllocatorTy Allocator;
   typedef StringMapEntry<ValueTy> MapEntryTy;
 public:
-  StringMap() : StringMapImpl(sizeof(MapEntryTy)) {}
+  StringMap() : StringMapImpl(static_cast<unsigned>(sizeof(MapEntryTy))) {}
   explicit StringMap(unsigned InitialSize)
-    : StringMapImpl(InitialSize, sizeof(MapEntryTy)) {}
+    : 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; }
 
+  typedef const char* key_type;
+  typedef ValueTy mapped_type;
+  typedef StringMapEntry<ValueTy> value_type;
+  typedef size_t size_type;
+
   typedef StringMapConstIterator<ValueTy> const_iterator;
   typedef StringMapIterator<ValueTy> iterator;
 
@@ -260,12 +282,62 @@ public:
     if (Bucket == -1) return end();
     return iterator(TheTable+Bucket);
   }
+  iterator find(const char *Key) {
+    return find(Key, Key + strlen(Key));
+  }
+  iterator find(const std::string &Key) {
+    return find(Key.data(), Key.data() + Key.size());
+  }
 
   const_iterator find(const char *KeyStart, const char *KeyEnd) const {
     int Bucket = FindKey(KeyStart, KeyEnd);
     if (Bucket == -1) return end();
     return const_iterator(TheTable+Bucket);
   }
+  const_iterator find(const char *Key) const {
+    return find(Key, Key + strlen(Key));
+  }
+  const_iterator find(const std::string &Key) const {
+    return find(Key.data(), Key.data() + 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) {
+    return GetOrCreateValue(Key, Key + strlen(Key)).getValue();
+  }
+  ValueTy& operator[](const std::string &Key) {
+    return GetOrCreateValue(Key.data(), Key.data() + Key.size()).getValue();
+  }
+
+  size_type count(const char *KeyStart, const char *KeyEnd) const {
+    return find(KeyStart, KeyEnd) == end() ? 0 : 1;
+  }
+  size_type count(const char *Key) const {
+    return count(Key, Key + strlen(Key));
+  }
+  size_type count(const std::string &Key) const {
+    return count(Key.data(), Key.data() + Key.size());
+  }
 
   /// insert - Insert the specified key/value pair into the map.  If the key
   /// already exists in the map, return false and ignore the request, otherwise
@@ -288,6 +360,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.
@@ -317,7 +405,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
@@ -332,16 +420,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.
 };
 
 
@@ -350,16 +446,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);
   }