1 //===- llvm/ADT/SmallPtrSet.cpp - 'Normally small' pointer set ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source 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"
19 bool SmallPtrSetImpl::insert(void *Ptr) {
21 // Check to see if it is already in the set.
22 for (void **APtr = SmallArray, **E = SmallArray+NumElements;
27 // Nope, there isn't. If we stay small, just 'pushback' now.
28 if (NumElements < CurArraySize-1) {
29 SmallArray[NumElements++] = Ptr;
32 // Otherwise, hit the big set case, which will call grow.
35 // If more than 3/4 of the array is full, grow.
36 if (NumElements*4 >= CurArraySize*3 ||
37 CurArraySize-(NumElements+NumTombstones) < CurArraySize/8)
40 // Okay, we know we have space. Find a hash bucket.
41 void **Bucket = const_cast<void**>(FindBucketFor(Ptr));
42 if (*Bucket == Ptr) return false; // Already inserted, good.
44 // Otherwise, insert it!
45 if (*Bucket == getTombstoneMarker())
48 ++NumElements; // Track density.
52 bool SmallPtrSetImpl::erase(void *Ptr) {
54 // Check to see if it is in the set.
55 for (void **APtr = SmallArray, **E = SmallArray+NumElements;
58 // If it is in the set, replace this element.
60 E[-1] = getEmptyMarker();
68 // Okay, we know we have space. Find a hash bucket.
69 void **Bucket = const_cast<void**>(FindBucketFor(Ptr));
70 if (*Bucket != Ptr) return false; // Not in the set?
72 // Set this as a tombstone.
73 *Bucket = getTombstoneMarker();
79 void * const *SmallPtrSetImpl::FindBucketFor(void *Ptr) const {
80 unsigned Bucket = Hash(Ptr);
81 unsigned ArraySize = CurArraySize;
82 unsigned ProbeAmt = 1;
83 void *const *Array = CurArray;
84 void *const *Tombstone = 0;
86 // Found Ptr's bucket?
87 if (Array[Bucket] == Ptr)
90 // If we found an empty bucket, the pointer doesn't exist in the set.
91 // Return a tombstone if we've seen one so far, or the empty bucket if
93 if (Array[Bucket] == getEmptyMarker())
94 return Tombstone ? Tombstone : Array+Bucket;
96 // If this is a tombstone, remember it. If Ptr ends up not in the set, we
97 // prefer to return it than something that would require more probing.
98 if (Array[Bucket] == getTombstoneMarker() && !Tombstone)
99 Tombstone = Array+Bucket; // Remember the first tombstone found.
101 // It's a hash collision or a tombstone. Reprobe.
102 Bucket = (Bucket + ProbeAmt++) & (ArraySize-1);
106 /// Grow - Allocate a larger backing store for the buckets and move it over.
108 void SmallPtrSetImpl::Grow() {
109 // Allocate at twice as many buckets, but at least 128.
110 unsigned OldSize = CurArraySize;
111 unsigned NewSize = OldSize < 64 ? 128 : OldSize*2;
113 void **OldBuckets = CurArray;
114 bool WasSmall = isSmall();
116 // Install the new array. Clear all the buckets to empty.
117 CurArray = new void*[NewSize+1];
118 CurArraySize = NewSize;
119 memset(CurArray, -1, NewSize*sizeof(void*));
121 // The end pointer, always valid, is set to a valid element to help the
123 CurArray[NewSize] = 0;
125 // Copy over all the elements.
127 // Small sets store their elements in order.
128 for (void **BucketPtr = OldBuckets, **E = OldBuckets+NumElements;
129 BucketPtr != E; ++BucketPtr) {
130 void *Elt = *BucketPtr;
131 *const_cast<void**>(FindBucketFor(Elt)) = Elt;
134 // Copy over all valid entries.
135 for (void **BucketPtr = OldBuckets, **E = OldBuckets+OldSize;
136 BucketPtr != E; ++BucketPtr) {
137 // Copy over the element if it is valid.
138 void *Elt = *BucketPtr;
139 if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
140 *const_cast<void**>(FindBucketFor(Elt)) = Elt;
143 delete [] OldBuckets;
148 SmallPtrSetImpl::SmallPtrSetImpl(const SmallPtrSetImpl& that) {
149 NumElements = that.NumElements;
151 if (that.isSmall()) {
152 CurArraySize = that.CurArraySize;
153 CurArray = &SmallArray[0];
154 // Copy the entire contents of the array, including the -1's and the null
156 memcpy(CurArray, that.CurArray, sizeof(void*)*(CurArraySize+1));
158 CurArraySize = that.NumElements < 64 ? 128 : that.CurArraySize*2;
159 CurArray = new void*[CurArraySize+1];
160 memset(CurArray, -1, CurArraySize*sizeof(void*));
162 // The end pointer, always valid, is set to a valid element to help the
164 CurArray[CurArraySize] = 0;
166 // Copy over all valid entries.
167 for (void **BucketPtr = that.CurArray, **E = that.CurArray+that.CurArraySize;
168 BucketPtr != E; ++BucketPtr) {
169 // Copy over the element if it is valid.
170 void *Elt = *BucketPtr;
171 if (Elt != getTombstoneMarker() && Elt != getEmptyMarker())
172 *const_cast<void**>(FindBucketFor(Elt)) = Elt;
177 /// CopyFrom - implement operator= from a smallptrset that has the same pointer
178 /// type, but may have a different small size.
179 void SmallPtrSetImpl::CopyFrom(const SmallPtrSetImpl &RHS) {
180 if (isSmall() && RHS.isSmall())
181 assert(CurArraySize == RHS.CurArraySize &&
182 "Cannot assign sets with different small sizes");
183 NumElements = RHS.NumElements;
184 NumTombstones = RHS.NumTombstones;
186 // If we're not currently small, and we don't have the same heap size,
187 // free our heap allocated storage
188 if (!isSmall() && CurArraySize != RHS.CurArraySize)
191 // If we're becoming small, prepare to insert into our stack space
193 CurArray = &SmallArray[0];
194 // Otherwise, allocate new heap space (unless we were the same size)
195 else if (CurArraySize != RHS.CurArraySize)
196 CurArray = new void*[RHS.CurArraySize+1];
198 // Copy over the new array size
199 CurArraySize = RHS.CurArraySize;
201 // Copy over the contents from the other set
202 memcpy(CurArray, RHS.CurArray, sizeof(void*)*(CurArraySize+1));