Add a note about a potential PIC optimization.
[oota-llvm.git] / include / llvm / ADT / StringMap.h
index 81a55456df228277fd691af6e540f6e37d8f588e..0a4a5d631d9c55df99e10d12b5f51eeff4348fc3 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "llvm/Support/Allocator.h"
 #include <cstring>
+#include <string>
 
 namespace llvm {
   template<typename ValueT>
@@ -152,13 +153,14 @@ public:
   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 =
@@ -235,9 +237,9 @@ 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))) {}
 
   AllocatorTy &getAllocator() { return Allocator; }
   const AllocatorTy &getAllocator() const { return Allocator; }
@@ -268,24 +270,36 @@ 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) {
+    const char* key_start = (Key.empty() ? NULL : &Key[0]);
+    return find(key_start, key_start + 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);
   }
-
-  iterator find(const char *Key) {
-    return find(Key, Key + strlen(Key));
-  }
   const_iterator find(const char *Key) const {
     return find(Key, Key + strlen(Key));
   }
+  const_iterator find(const std::string &Key) const {
+    const char* key_start = (Key.empty() ? NULL : &Key[0]);
+    return find(key_start, key_start + Key.size());
+  }
 
   ValueTy& operator[](const char *Key) {
     value_type& entry = GetOrCreateValue(Key, Key + strlen(Key));
     return entry.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();
+  }
 
   size_type count(const char *KeyStart, const char *KeyEnd) const {
     return find(KeyStart, KeyEnd) == end() ? 0 : 1;
@@ -293,6 +307,10 @@ public:
   size_type count(const char *Key) const {
     return count(Key, Key + strlen(Key));
   }
+  size_type count(const std::string &Key) const {
+    const char* key_start = (Key.empty() ? NULL : &Key[0]);
+    return count(key_start, key_start + 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
@@ -359,6 +377,20 @@ public:
     V.Destroy(Allocator);
   }
 
+  bool erase(const char *Key) {
+    iterator I = find(Key);
+    if (I == end()) return false;
+    erase(I);
+    return true;
+  }
+
+  bool erase(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())