Introduce a new ReplaceCallWith method, which simplifies a lot of code.
[oota-llvm.git] / lib / Support / SmallPtrSet.cpp
index 0e6d3345905b1d1c2c543a81a3f913cc7d238778..98d5bc99adc19b62b52c4181a7c1627fa7bcfc68 100644 (file)
@@ -7,7 +7,8 @@
 //
 //===----------------------------------------------------------------------===//
 //
-// This file implements the SmallPtrSet class.
+// This file implements the SmallPtrSet class.  See SmallPtrSet.h for an
+// overview of the algorithm.
 //
 //===----------------------------------------------------------------------===//
 
@@ -31,7 +32,8 @@ bool SmallPtrSetImpl::insert(void *Ptr) {
   }
   
   // If more than 3/4 of the array is full, grow.
-  if (NumElements*4 >= CurArraySize*3)
+  if (NumElements*4 >= CurArraySize*3 ||
+      CurArraySize-(NumElements+NumTombstones) < CurArraySize/8)
     Grow();
   
   // Okay, we know we have space.  Find a hash bucket.
@@ -39,11 +41,41 @@ bool SmallPtrSetImpl::insert(void *Ptr) {
   if (*Bucket == Ptr) return false; // Already inserted, good.
   
   // Otherwise, insert it!
+  if (*Bucket == getTombstoneMarker())
+    --NumTombstones;
   *Bucket = Ptr;
   ++NumElements;  // Track density.
   return true;
 }
 
+bool SmallPtrSetImpl::erase(void *Ptr) {
+  if (isSmall()) {
+    // Check to see if it is in the set.
+    for (void **APtr = SmallArray, **E = SmallArray+NumElements;
+         APtr != E; ++APtr)
+      if (*APtr == Ptr) {
+        // If it is in the set, move everything over, replacing this element.
+        memmove(APtr, APtr+1, sizeof(void*)*(E-APtr-1));
+        // Clear the end element.
+        E[-1] = getEmptyMarker();
+        --NumElements;
+        return true;
+      }
+    
+    return false;
+  }
+  
+  // Okay, we know we have space.  Find a hash bucket.
+  void **Bucket = const_cast<void**>(FindBucketFor(Ptr));
+  if (*Bucket != Ptr) return false;  // Not in the set?
+
+  // Set this as a tombstone.
+  *Bucket = getTombstoneMarker();
+  --NumElements;
+  ++NumTombstones;
+  return true;
+}
+
 void * const *SmallPtrSetImpl::FindBucketFor(void *Ptr) const {
   unsigned Bucket = Hash(Ptr);
   unsigned ArraySize = CurArraySize;