Make isNegative() a const function since it doesn't modify the APInt.
[oota-llvm.git] / include / llvm / ADT / UniqueVector.h
index e888678675eff4a7ae4c2ea3e875046cb4a6f505..f3fd7b1d26fc36e97614f6950fa933a0f49b4a81 100644 (file)
@@ -1,4 +1,3 @@
-
 //===-- llvm/ADT/UniqueVector.h ---------------------------------*- C++ -*-===//
 //
 //                     The LLVM Compiler Infrastructure
@@ -11,6 +10,7 @@
 #ifndef LLVM_ADT_UNIQUEVECTOR_H
 #define LLVM_ADT_UNIQUEVECTOR_H
 
+#include <cassert>
 #include <map>
 #include <vector>
 
@@ -28,38 +28,34 @@ private:
 
   // Vector - ID ordered vector of entries. Entries can be indexed by ID - 1.
   //
-  std::vector<const T *> Vector;
+  std::vector<T> Vector;
   
 public:
   /// insert - Append entry to the vector if it doesn't already exist.  Returns
   /// the entry's index + 1 to be used as a unique ID.
   unsigned insert(const T &Entry) {
     // Check if the entry is already in the map.
-    typename std::map<T, unsigned>::iterator MI = Map.lower_bound(Entry);
+    unsigned &Val = Map[Entry];
     
     // See if entry exists, if so return prior ID.
-    if (MI != Map.end() && MI->first == Entry) return MI->second;
+    if (Val) return Val;
 
     // Compute ID for entry.
-    unsigned ID = Vector.size() + 1;
-    
-    // Insert in map.
-    MI = Map.insert(MI, std::make_pair(Entry, ID));
+    Val = Vector.size() + 1;
     
     // Insert in vector.
-    Vector.push_back(&MI->first);
-
-    return ID;
+    Vector.push_back(Entry);
+    return Val;
   }
   
   /// idFor - return the ID for an existing entry.  Returns 0 if the entry is
   /// not found.
   unsigned idFor(const T &Entry) const {
     // Search for entry in the map.
-    typename std::map<T, unsigned>::iterator MI = Map.lower_bound(Entry);
+    typename std::map<T, unsigned>::const_iterator MI = Map.find(Entry);
     
     // See if entry exists, if so return ID.
-    if (MI != Map.end() && MI->first == Entry) return MI->second;
+    if (MI != Map.end()) return MI->second;
     
     // No luck.
     return 0;
@@ -67,7 +63,10 @@ public:
 
   /// operator[] - Returns a reference to the entry with the specified ID.
   ///
-  const T &operator[](unsigned ID) const { return *Vector[ID - 1]; }
+  const T &operator[](unsigned ID) const {
+    assert(ID-1 < size() && "ID is 0 or out of range!");
+    return Vector[ID - 1];
+  }
   
   /// size - Returns the number of entries in the vector.
   ///