Remove OwningPtr.h and associated tests
[oota-llvm.git] / include / llvm / ADT / StringSet.h
index 3e274f0d5c4c981feb866a5381f2d3a7eb0ae13a..7bea577f34d33f411f592d15b8be050d119d4441 100644 (file)
 
 #include "llvm/ADT/StringMap.h"
 
-#include <cassert>
-
 namespace llvm {
 
-  /// StringSet - A wrapper for StringMap that provides set-like
-  /// functionality.  Only insert() and count() methods are used by my
-  /// code.
+  /// StringSet - A wrapper for StringMap that provides set-like functionality.
   template <class AllocatorTy = llvm::MallocAllocator>
   class StringSet : public llvm::StringMap<char, AllocatorTy> {
     typedef llvm::StringMap<char, AllocatorTy> base;
   public:
-    void insert (const std::string& InLang) {
-      assert(!InLang.empty());
-      const char* KeyStart = &InLang[0];
-      const char* KeyEnd = KeyStart + InLang.size();
-      base::insert(llvm::StringMapEntry<char>::
-                   Create(KeyStart, KeyEnd, base::getAllocator(), '+'));
+
+    /// insert - Insert the specified key into the set.  If the key already
+    /// exists in the set, return false and ignore the request, otherwise insert
+    /// it and return true.
+    bool insert(StringRef Key) {
+      // Get or create the map entry for the key; if it doesn't exist the value
+      // type will be default constructed which we use to detect insert.
+      //
+      // We use '+' as the sentinel value in the map.
+      assert(!Key.empty());
+      StringMapEntry<char> &Entry = this->GetOrCreateValue(Key);
+      if (Entry.getValue() == '+')
+        return false;
+      Entry.setValue('+');
+      return true;
     }
   };
 }