llvm.org GIT mirror llvm / 0b93085
implement SmallPtrSet::erase git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33581 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 13 years ago
2 changed file(s) with 37 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
6666 delete[] CurArray;
6767 }
6868
69 bool isSmall() const { return CurArray == &SmallArray[0]; }
70
7169 static void *getTombstoneMarker() { return reinterpret_cast(-2); }
7270 static void *getEmptyMarker() {
7371 // Note that -1 is chosen to make clear() efficiently implementable with
8482 /// insert - This returns true if the pointer was new to the set, false if it
8583 /// was already in the set.
8684 bool insert(void *Ptr);
85
86 /// erase - If the set contains the specified pointer, remove it and return
87 /// true, otherwise return false.
88 bool erase(void *Ptr);
8789
8890 bool count(void *Ptr) const {
8991 if (isSmall()) {
100102 }
101103
102104 private:
105 bool isSmall() const { return CurArray == &SmallArray[0]; }
106
103107 unsigned Hash(void *Ptr) const {
104108 return ((uintptr_t)Ptr >> 4) & (CurArraySize-1);
105109 }
187191 };
188192
189193
190 /// SmallPtrSet - This class implements
194 /// SmallPtrSet - This class implements a set which is optimizer for holding
195 /// SmallSize or less elements. This internally rounds up SmallSize to the next
196 /// power of two if it is not already a power of two. See the comments above
197 /// SmallPtrSetImpl for details of the algorithm.
191198 template
192199 class SmallPtrSet : public SmallPtrSetImpl {
193200 // Make sure that SmallSize is a power of two, round up if not.
4141 // Otherwise, insert it!
4242 *Bucket = Ptr;
4343 ++NumElements; // Track density.
44 return true;
45 }
46
47 bool SmallPtrSetImpl::erase(void *Ptr) {
48 if (isSmall()) {
49 // Check to see if it is in the set.
50 for (void **APtr = SmallArray, **E = SmallArray+NumElements;
51 APtr != E; ++APtr)
52 if (*APtr == Ptr) {
53 // If it is in the set, move everything over, replacing this element.
54 memmove(APtr, APtr+1, sizeof(void*)*(E-APtr-1));
55 // Clear the end element.
56 E[-1] = getEmptyMarker();
57 --NumElements;
58 return false;
59 }
60
61 return false;
62 }
63
64 // Okay, we know we have space. Find a hash bucket.
65 void **Bucket = const_cast(FindBucketFor(Ptr));
66 if (*Bucket != Ptr) return false; // Not in the set?
67
68 // Set this as a tombstone.
69 *Bucket = getTombstoneMarker();
70 --NumElements;
4471 return true;
4572 }
4673