1 //===- llvm/ADT/SmallPtrSet.cpp - 'Normally small' pointer set ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the SmallPtrSet class. See SmallPtrSet.h for an
11 // overview of the algorithm.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ADT/SmallPtrSet.h"
16 #include "llvm/Support/MathExtras.h"
22 void SmallPtrSetImpl::shrink_and_clear() {
23 assert(!isSmall() && "Can't shrink a small set!");
26 // Reduce the number of buckets.
27 CurArraySize = NumElements > 16 ? 1 << (Log2_32_Ceil(NumElements) + 1) : 32;
28 NumElements = NumTombstones = 0;
30 // Install the new array. Clear all the buckets to empty.
31 CurArray = (const void**)malloc(sizeof(void*) * (CurArraySize+1));
32 assert(CurArray && "Failed to allocate memory?");
33 memset(CurArray, -1, CurArraySize*sizeof(void*));
35 // The end pointer, always valid, is set to a valid element to help the
37 CurArray[CurArraySize] = 0;
40 bool SmallPtrSetImpl::insert_imp(const void * Ptr) {
42 // Check to see if it is already in the set.
43 for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
48 // Nope, there isn't. If we stay small, just 'pushback' now.
49 if (NumElements < CurArraySize-1) {
50 SmallArray[NumElements++] = Ptr;
53 // Otherwise, hit the big set case, which will call grow.
56 if (NumElements*4 >= CurArraySize*3) {
57 // If more than 3/4 of the array is full, grow.
58 Grow(CurArraySize < 64 ? 128 : CurArraySize*2);
59 } else if (CurArraySize-(NumElements+NumTombstones) < CurArraySize/8) {
60 // If fewer of 1/8 of the array is empty (meaning that many are filled with
61 // tombstones), rehash.
65 // Okay, we know we have space. Find a hash bucket.
66 const void **Bucket = const_cast<const void**>(FindBucketFor(Ptr));
67 if (*Bucket == Ptr) return false; // Already inserted, good.
69 // Otherwise, insert it!
70 if (*Bucket == getTombstoneMarker())
73 ++NumElements; // Track density.
77 bool SmallPtrSetImpl::erase_imp(const void * Ptr) {
79 // Check to see if it is in the set.
80 for (const void **APtr = SmallArray, **E = SmallArray+NumElements;
83 // If it is in the set, replace this element.
85 E[-1] = getEmptyMarker();
93 // Okay, we know we have space. Find a hash bucket.
94 void **Bucket = const_cast<void**>(FindBucketFor(Ptr));
95 if (*Bucket != Ptr) return false; // Not in the set?
97 // Set this as a tombstone.
98 *Bucket = getTombstoneMarker();
104 const void * const *SmallPtrSetImpl::FindBucketFor(const void *Ptr) const {
105 unsigned Bucket = Hash(Ptr);
106 unsigned ArraySize = CurArraySize;
107 unsigned ProbeAmt = 1;
108 const void *const *Array = CurArray;
109 const void *const *Tombstone = 0;
111 // Found Ptr's bucket?
112 if (Array[Bucket] == Ptr)
115 // If we found an empty bucket, the pointer doesn't exist in the set.
116 // Return a tombstone if we've seen one so far, or the empty bucket if
118 if (Array[Bucket] == getEmptyMarker())
119 return Tombstone ? Tombstone : Array+Bucket;
121 // If this is a tombstone, remember it. If Ptr ends up not in the set, we
122 // prefer to return it than something that would require more probing.
123 if (Array[Bucket] == getTombstoneMarker() && !Tombstone)
124 Tombstone = Array+Bucket; // Remember the first tombstone found.
126 // It's a hash collision or a tombstone. Reprobe.
127 Bucket = (Bucket + ProbeAmt++) & (ArraySize-1);
131 /// Grow - Allocate a larger backing store for the buckets and move it over.
133 void SmallPtrSetImpl::Grow(unsigned NewSize) {
134 // Allocate at twice as many buckets, but at least 128.
135 unsigned OldSize = CurArraySize;
137 const void **OldBuckets = CurArray;
138 bool WasSmall = isSmall();
140 // Install the new array. Clear all the buckets to empty.
141 CurArray = (const void**)malloc(sizeof(void*) * (NewSize+1));
142 assert(CurArray && "Failed to allocate memory?");
143 CurArraySize = NewSize;
144 memset(CurArray, -1, NewSize*sizeof(void*));
146 // The end pointer, always valid, is set to a valid element to help the
148 CurArray[NewSize] = 0;
150 // Copy over all the elements.
152 // Small sets store their elements in order.
153 for (const void **BucketPtr = OldBuckets, **E = OldBuckets+NumElements;
154 BucketPtr != E; ++BucketPtr) {
155 const void *Elt = *BucketPtr;
156 *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
159 // Copy over all valid entries.
160 for (const void **BucketPtr = OldBuckets, **E = OldBuckets+OldSize;
161 BucketPtr != E; ++BucketPtr) {
162 // Copy over the element if it is valid.
163 const void *Elt = *BucketPtr;
164 if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
165 *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt);
173 SmallPtrSetImpl::SmallPtrSetImpl(const void **SmallStorage,
174 const SmallPtrSetImpl& that) {
175 SmallArray = SmallStorage;
177 // If we're becoming small, prepare to insert into our stack space
178 if (that.isSmall()) {
179 CurArray = SmallArray;
180 // Otherwise, allocate new heap space (unless we were the same size)
182 CurArray = (const void**)malloc(sizeof(void*) * (that.CurArraySize+1));
183 assert(CurArray && "Failed to allocate memory?");
186 // Copy over the new array size
187 CurArraySize = that.CurArraySize;
189 // Copy over the contents from the other set
190 memcpy(CurArray, that.CurArray, sizeof(void*)*(CurArraySize+1));
192 NumElements = that.NumElements;
193 NumTombstones = that.NumTombstones;
196 /// CopyFrom - implement operator= from a smallptrset that has the same pointer
197 /// type, but may have a different small size.
198 void SmallPtrSetImpl::CopyFrom(const SmallPtrSetImpl &RHS) {
199 if (isSmall() && RHS.isSmall())
200 assert(CurArraySize == RHS.CurArraySize &&
201 "Cannot assign sets with different small sizes");
203 // If we're becoming small, prepare to insert into our stack space
207 CurArray = SmallArray;
208 // Otherwise, allocate new heap space (unless we were the same size)
209 } else if (CurArraySize != RHS.CurArraySize) {
211 CurArray = (const void**)malloc(sizeof(void*) * (RHS.CurArraySize+1));
213 CurArray = (const void**)realloc(CurArray, sizeof(void*)*(RHS.CurArraySize+1));
214 assert(CurArray && "Failed to allocate memory?");
217 // Copy over the new array size
218 CurArraySize = RHS.CurArraySize;
220 // Copy over the contents from the other set
221 memcpy(CurArray, RHS.CurArray, sizeof(void*)*(CurArraySize+1));
223 NumElements = RHS.NumElements;
224 NumTombstones = RHS.NumTombstones;
227 void SmallPtrSetImpl::swap(SmallPtrSetImpl &RHS) {
228 if (this == &RHS) return;
230 // We can only avoid copying elements if neither set is small.
231 if (!this->isSmall() && !RHS.isSmall()) {
232 std::swap(this->CurArray, RHS.CurArray);
233 std::swap(this->CurArraySize, RHS.CurArraySize);
234 std::swap(this->NumElements, RHS.NumElements);
235 std::swap(this->NumTombstones, RHS.NumTombstones);
239 // FIXME: From here on we assume that both sets have the same small size.
241 // If only RHS is small, copy the small elements into LHS and move the pointer
243 if (!this->isSmall() && RHS.isSmall()) {
244 std::copy(RHS.SmallArray, RHS.SmallArray+RHS.NumElements, this->SmallArray);
245 std::swap(this->NumElements, RHS.NumElements);
246 std::swap(this->CurArraySize, RHS.CurArraySize);
247 RHS.CurArray = this->CurArray;
248 RHS.NumTombstones = this->NumTombstones;
249 this->CurArray = this->SmallArray;
250 this->NumTombstones = 0;
254 // If only LHS is small, copy the small elements into RHS and move the pointer
256 if (this->isSmall() && !RHS.isSmall()) {
257 std::copy(this->SmallArray, this->SmallArray+this->NumElements,
259 std::swap(RHS.NumElements, this->NumElements);
260 std::swap(RHS.CurArraySize, this->CurArraySize);
261 this->CurArray = RHS.CurArray;
262 this->NumTombstones = RHS.NumTombstones;
263 RHS.CurArray = RHS.SmallArray;
264 RHS.NumTombstones = 0;
268 // Both a small, just swap the small elements.
269 assert(this->isSmall() && RHS.isSmall());
270 assert(this->CurArraySize == RHS.CurArraySize);
271 unsigned MaxElems = std::max(this->NumElements, RHS.NumElements);
272 std::swap_ranges(this->SmallArray, this->SmallArray+MaxElems, RHS.SmallArray);
273 std::swap(this->NumElements, RHS.NumElements);
276 SmallPtrSetImpl::~SmallPtrSetImpl() {