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