Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 1 | //===- llvm/ADT/SmallPtrSet.cpp - 'Normally small' pointer set ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | 24757de | 2007-01-27 07:18:32 +0000 | [diff] [blame] | 10 | // This file implements the SmallPtrSet class. See SmallPtrSet.h for an |
| 11 | // overview of the algorithm. |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | 91f0158 | 2007-07-09 16:54:03 +0000 | [diff] [blame] | 16 | #include "llvm/Support/MathExtras.h" |
Reid Spencer | 845b31d | 2007-07-17 02:16:12 +0000 | [diff] [blame] | 17 | #include <cstdlib> |
| 18 | |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | |
Chris Lattner | 42e4bdf | 2007-08-05 07:32:14 +0000 | [diff] [blame] | 21 | void SmallPtrSetImpl::shrink_and_clear() { |
| 22 | assert(!isSmall() && "Can't shrink a small set!"); |
| 23 | free(CurArray); |
| 24 | |
| 25 | // Reduce the number of buckets. |
| 26 | CurArraySize = NumElements > 16 ? 1 << (Log2_32_Ceil(NumElements) + 1) : 32; |
| 27 | NumElements = NumTombstones = 0; |
| 28 | |
| 29 | // Install the new array. Clear all the buckets to empty. |
| 30 | CurArray = (const void**)malloc(sizeof(void*) * (CurArraySize+1)); |
| 31 | assert(CurArray && "Failed to allocate memory?"); |
| 32 | memset(CurArray, -1, CurArraySize*sizeof(void*)); |
| 33 | |
| 34 | // The end pointer, always valid, is set to a valid element to help the |
| 35 | // iterator. |
| 36 | CurArray[CurArraySize] = 0; |
| 37 | } |
| 38 | |
Chris Lattner | 373a733 | 2007-11-06 22:12:43 +0000 | [diff] [blame] | 39 | bool SmallPtrSetImpl::insert_imp(const void * Ptr) { |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 40 | if (isSmall()) { |
| 41 | // Check to see if it is already in the set. |
Owen Anderson | e992a56 | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 42 | for (const void **APtr = SmallArray, **E = SmallArray+NumElements; |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 43 | APtr != E; ++APtr) |
| 44 | if (*APtr == Ptr) |
| 45 | return false; |
| 46 | |
| 47 | // Nope, there isn't. If we stay small, just 'pushback' now. |
| 48 | if (NumElements < CurArraySize-1) { |
| 49 | SmallArray[NumElements++] = Ptr; |
| 50 | return true; |
| 51 | } |
| 52 | // Otherwise, hit the big set case, which will call grow. |
| 53 | } |
| 54 | |
| 55 | // If more than 3/4 of the array is full, grow. |
Chris Lattner | e237cf9 | 2007-02-07 01:11:25 +0000 | [diff] [blame] | 56 | if (NumElements*4 >= CurArraySize*3 || |
| 57 | CurArraySize-(NumElements+NumTombstones) < CurArraySize/8) |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 58 | Grow(); |
| 59 | |
| 60 | // Okay, we know we have space. Find a hash bucket. |
Owen Anderson | e992a56 | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 61 | void **Bucket = const_cast<void**>(FindBucketFor((void*)Ptr)); |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 62 | if (*Bucket == Ptr) return false; // Already inserted, good. |
| 63 | |
| 64 | // Otherwise, insert it! |
Chris Lattner | e237cf9 | 2007-02-07 01:11:25 +0000 | [diff] [blame] | 65 | if (*Bucket == getTombstoneMarker()) |
| 66 | --NumTombstones; |
Owen Anderson | e992a56 | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 67 | *Bucket = (void*)Ptr; |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 68 | ++NumElements; // Track density. |
| 69 | return true; |
| 70 | } |
| 71 | |
Chris Lattner | 373a733 | 2007-11-06 22:12:43 +0000 | [diff] [blame] | 72 | bool SmallPtrSetImpl::erase_imp(const void * Ptr) { |
Chris Lattner | 0b93085 | 2007-01-27 07:59:10 +0000 | [diff] [blame] | 73 | if (isSmall()) { |
| 74 | // Check to see if it is in the set. |
Owen Anderson | e992a56 | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 75 | for (const void **APtr = SmallArray, **E = SmallArray+NumElements; |
Chris Lattner | 0b93085 | 2007-01-27 07:59:10 +0000 | [diff] [blame] | 76 | APtr != E; ++APtr) |
| 77 | if (*APtr == Ptr) { |
Chris Lattner | 61766ca | 2007-06-21 23:23:32 +0000 | [diff] [blame] | 78 | // If it is in the set, replace this element. |
| 79 | *APtr = E[-1]; |
Chris Lattner | 0b93085 | 2007-01-27 07:59:10 +0000 | [diff] [blame] | 80 | E[-1] = getEmptyMarker(); |
| 81 | --NumElements; |
Chris Lattner | 7ef856d | 2007-02-05 23:10:31 +0000 | [diff] [blame] | 82 | return true; |
Chris Lattner | 0b93085 | 2007-01-27 07:59:10 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | // Okay, we know we have space. Find a hash bucket. |
| 89 | void **Bucket = const_cast<void**>(FindBucketFor(Ptr)); |
| 90 | if (*Bucket != Ptr) return false; // Not in the set? |
| 91 | |
| 92 | // Set this as a tombstone. |
| 93 | *Bucket = getTombstoneMarker(); |
| 94 | --NumElements; |
Chris Lattner | e237cf9 | 2007-02-07 01:11:25 +0000 | [diff] [blame] | 95 | ++NumTombstones; |
Chris Lattner | 0b93085 | 2007-01-27 07:59:10 +0000 | [diff] [blame] | 96 | return true; |
| 97 | } |
| 98 | |
Owen Anderson | e992a56 | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 99 | const void * const *SmallPtrSetImpl::FindBucketFor(const void *Ptr) const { |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 100 | unsigned Bucket = Hash(Ptr); |
| 101 | unsigned ArraySize = CurArraySize; |
| 102 | unsigned ProbeAmt = 1; |
Owen Anderson | e992a56 | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 103 | const void *const *Array = CurArray; |
| 104 | const void *const *Tombstone = 0; |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 105 | while (1) { |
| 106 | // Found Ptr's bucket? |
| 107 | if (Array[Bucket] == Ptr) |
| 108 | return Array+Bucket; |
| 109 | |
| 110 | // If we found an empty bucket, the pointer doesn't exist in the set. |
| 111 | // Return a tombstone if we've seen one so far, or the empty bucket if |
| 112 | // not. |
| 113 | if (Array[Bucket] == getEmptyMarker()) |
| 114 | return Tombstone ? Tombstone : Array+Bucket; |
| 115 | |
| 116 | // If this is a tombstone, remember it. If Ptr ends up not in the set, we |
| 117 | // prefer to return it than something that would require more probing. |
| 118 | if (Array[Bucket] == getTombstoneMarker() && !Tombstone) |
| 119 | Tombstone = Array+Bucket; // Remember the first tombstone found. |
| 120 | |
| 121 | // It's a hash collision or a tombstone. Reprobe. |
| 122 | Bucket = (Bucket + ProbeAmt++) & (ArraySize-1); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /// Grow - Allocate a larger backing store for the buckets and move it over. |
| 127 | /// |
| 128 | void SmallPtrSetImpl::Grow() { |
| 129 | // Allocate at twice as many buckets, but at least 128. |
| 130 | unsigned OldSize = CurArraySize; |
| 131 | unsigned NewSize = OldSize < 64 ? 128 : OldSize*2; |
| 132 | |
Owen Anderson | e992a56 | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 133 | const void **OldBuckets = CurArray; |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 134 | bool WasSmall = isSmall(); |
| 135 | |
| 136 | // Install the new array. Clear all the buckets to empty. |
Owen Anderson | e992a56 | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 137 | CurArray = (const void**)malloc(sizeof(void*) * (NewSize+1)); |
Owen Anderson | 1629a1f | 2007-07-16 21:27:44 +0000 | [diff] [blame] | 138 | assert(CurArray && "Failed to allocate memory?"); |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 139 | CurArraySize = NewSize; |
| 140 | memset(CurArray, -1, NewSize*sizeof(void*)); |
| 141 | |
| 142 | // The end pointer, always valid, is set to a valid element to help the |
| 143 | // iterator. |
| 144 | CurArray[NewSize] = 0; |
| 145 | |
| 146 | // Copy over all the elements. |
| 147 | if (WasSmall) { |
| 148 | // Small sets store their elements in order. |
Owen Anderson | e992a56 | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 149 | for (const void **BucketPtr = OldBuckets, **E = OldBuckets+NumElements; |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 150 | BucketPtr != E; ++BucketPtr) { |
Owen Anderson | e992a56 | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 151 | const void *Elt = *BucketPtr; |
| 152 | *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt); |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 153 | } |
| 154 | } else { |
| 155 | // Copy over all valid entries. |
Owen Anderson | e992a56 | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 156 | for (const void **BucketPtr = OldBuckets, **E = OldBuckets+OldSize; |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 157 | BucketPtr != E; ++BucketPtr) { |
| 158 | // Copy over the element if it is valid. |
Owen Anderson | e992a56 | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 159 | const void *Elt = *BucketPtr; |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 160 | if (Elt != getTombstoneMarker() && Elt != getEmptyMarker()) |
Owen Anderson | e992a56 | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 161 | *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt); |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Owen Anderson | 1629a1f | 2007-07-16 21:27:44 +0000 | [diff] [blame] | 164 | free(OldBuckets); |
Jeff Cohen | ac58a16 | 2007-04-14 21:50:21 +0000 | [diff] [blame] | 165 | NumTombstones = 0; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | SmallPtrSetImpl::SmallPtrSetImpl(const SmallPtrSetImpl& that) { |
Owen Anderson | bf31b85 | 2007-07-24 21:31:23 +0000 | [diff] [blame] | 170 | // If we're becoming small, prepare to insert into our stack space |
Jeff Cohen | ac58a16 | 2007-04-14 21:50:21 +0000 | [diff] [blame] | 171 | if (that.isSmall()) { |
Jeff Cohen | ac58a16 | 2007-04-14 21:50:21 +0000 | [diff] [blame] | 172 | CurArray = &SmallArray[0]; |
Owen Anderson | bf31b85 | 2007-07-24 21:31:23 +0000 | [diff] [blame] | 173 | // Otherwise, allocate new heap space (unless we were the same size) |
Jeff Cohen | ac58a16 | 2007-04-14 21:50:21 +0000 | [diff] [blame] | 174 | } else { |
Owen Anderson | e992a56 | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 175 | CurArray = (const void**)malloc(sizeof(void*) * (that.CurArraySize+1)); |
Owen Anderson | 1629a1f | 2007-07-16 21:27:44 +0000 | [diff] [blame] | 176 | assert(CurArray && "Failed to allocate memory?"); |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 177 | } |
Owen Anderson | bf31b85 | 2007-07-24 21:31:23 +0000 | [diff] [blame] | 178 | |
| 179 | // Copy over the new array size |
| 180 | CurArraySize = that.CurArraySize; |
| 181 | |
| 182 | // Copy over the contents from the other set |
| 183 | memcpy(CurArray, that.CurArray, sizeof(void*)*(CurArraySize+1)); |
| 184 | |
| 185 | NumElements = that.NumElements; |
| 186 | NumTombstones = that.NumTombstones; |
Chris Lattner | c95dc98 | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 187 | } |
Chris Lattner | 91f0158 | 2007-07-09 16:54:03 +0000 | [diff] [blame] | 188 | |
| 189 | /// CopyFrom - implement operator= from a smallptrset that has the same pointer |
| 190 | /// type, but may have a different small size. |
| 191 | void SmallPtrSetImpl::CopyFrom(const SmallPtrSetImpl &RHS) { |
Owen Anderson | 69b5d12 | 2007-07-09 22:27:20 +0000 | [diff] [blame] | 192 | if (isSmall() && RHS.isSmall()) |
| 193 | assert(CurArraySize == RHS.CurArraySize && |
| 194 | "Cannot assign sets with different small sizes"); |
Owen Anderson | b54b315 | 2007-07-18 19:54:15 +0000 | [diff] [blame] | 195 | |
Owen Anderson | 69b5d12 | 2007-07-09 22:27:20 +0000 | [diff] [blame] | 196 | // If we're becoming small, prepare to insert into our stack space |
Owen Anderson | 71a1e57 | 2007-07-19 06:45:33 +0000 | [diff] [blame] | 197 | if (RHS.isSmall()) { |
| 198 | if (!isSmall()) |
| 199 | free(CurArray); |
Owen Anderson | 69b5d12 | 2007-07-09 22:27:20 +0000 | [diff] [blame] | 200 | CurArray = &SmallArray[0]; |
| 201 | // Otherwise, allocate new heap space (unless we were the same size) |
Owen Anderson | 71a1e57 | 2007-07-19 06:45:33 +0000 | [diff] [blame] | 202 | } else if (CurArraySize != RHS.CurArraySize) { |
Owen Anderson | b54b315 | 2007-07-18 19:54:15 +0000 | [diff] [blame] | 203 | if (isSmall()) |
Owen Anderson | e992a56 | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 204 | CurArray = (const void**)malloc(sizeof(void*) * (RHS.CurArraySize+1)); |
Owen Anderson | b54b315 | 2007-07-18 19:54:15 +0000 | [diff] [blame] | 205 | else |
Owen Anderson | e992a56 | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 206 | CurArray = (const void**)realloc(CurArray, sizeof(void*)*(RHS.CurArraySize+1)); |
Owen Anderson | 1629a1f | 2007-07-16 21:27:44 +0000 | [diff] [blame] | 207 | assert(CurArray && "Failed to allocate memory?"); |
| 208 | } |
Owen Anderson | 69b5d12 | 2007-07-09 22:27:20 +0000 | [diff] [blame] | 209 | |
| 210 | // Copy over the new array size |
| 211 | CurArraySize = RHS.CurArraySize; |
| 212 | |
| 213 | // Copy over the contents from the other set |
| 214 | memcpy(CurArray, RHS.CurArray, sizeof(void*)*(CurArraySize+1)); |
Owen Anderson | b54b315 | 2007-07-18 19:54:15 +0000 | [diff] [blame] | 215 | |
| 216 | NumElements = RHS.NumElements; |
| 217 | NumTombstones = RHS.NumTombstones; |
Chris Lattner | 91f0158 | 2007-07-09 16:54:03 +0000 | [diff] [blame] | 218 | } |
Reid Spencer | 845b31d | 2007-07-17 02:16:12 +0000 | [diff] [blame] | 219 | |
| 220 | SmallPtrSetImpl::~SmallPtrSetImpl() { |
| 221 | if (!isSmall()) |
| 222 | free(CurArray); |
| 223 | } |