Chris Lattner | 74102df | 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 | f3ebc3f | 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 | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | ab5d0ba | 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 | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/ADT/SmallPtrSet.h" |
Benjamin Kramer | 94988cb | 2012-04-18 10:37:32 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/DenseMapInfo.h" |
Chris Lattner | 85049a4 | 2007-07-09 16:54:03 +0000 | [diff] [blame] | 17 | #include "llvm/Support/MathExtras.h" |
Benjamin Kramer | e1c34e9 | 2012-03-06 20:40:02 +0000 | [diff] [blame] | 18 | #include <algorithm> |
Reid Spencer | 5060fd0 | 2007-07-17 02:16:12 +0000 | [diff] [blame] | 19 | #include <cstdlib> |
| 20 | |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
Chandler Carruth | 173bd7e | 2014-02-03 11:24:18 +0000 | [diff] [blame] | 23 | void SmallPtrSetImplBase::shrink_and_clear() { |
Chris Lattner | 44f7d3a | 2007-08-05 07:32:14 +0000 | [diff] [blame] | 24 | assert(!isSmall() && "Can't shrink a small set!"); |
| 25 | free(CurArray); |
| 26 | |
| 27 | // Reduce the number of buckets. |
| 28 | CurArraySize = NumElements > 16 ? 1 << (Log2_32_Ceil(NumElements) + 1) : 32; |
| 29 | NumElements = NumTombstones = 0; |
| 30 | |
| 31 | // Install the new array. Clear all the buckets to empty. |
Jean-Luc Duprat | 89fe247 | 2013-03-29 22:07:12 +0000 | [diff] [blame] | 32 | CurArray = (const void**)malloc(sizeof(void*) * CurArraySize); |
Chris Lattner | 44f7d3a | 2007-08-05 07:32:14 +0000 | [diff] [blame] | 33 | assert(CurArray && "Failed to allocate memory?"); |
| 34 | memset(CurArray, -1, CurArraySize*sizeof(void*)); |
Chris Lattner | 44f7d3a | 2007-08-05 07:32:14 +0000 | [diff] [blame] | 35 | } |
| 36 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 37 | std::pair<const void *const *, bool> |
| 38 | SmallPtrSetImplBase::insert_imp(const void *Ptr) { |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 39 | if (isSmall()) { |
| 40 | // Check to see if it is already in the set. |
Owen Anderson | 49f037a | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 41 | for (const void **APtr = SmallArray, **E = SmallArray+NumElements; |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 42 | APtr != E; ++APtr) |
| 43 | if (*APtr == Ptr) |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 44 | return std::make_pair(APtr, false); |
| 45 | |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 46 | // Nope, there isn't. If we stay small, just 'pushback' now. |
Craig Topper | 298f638 | 2014-08-20 04:41:36 +0000 | [diff] [blame] | 47 | if (NumElements < CurArraySize) { |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 48 | SmallArray[NumElements++] = Ptr; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 49 | return std::make_pair(SmallArray + (NumElements - 1), true); |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 50 | } |
| 51 | // Otherwise, hit the big set case, which will call grow. |
| 52 | } |
| 53 | |
Jakob Stoklund Olesen | bdc1b01 | 2011-03-30 18:32:48 +0000 | [diff] [blame] | 54 | if (NumElements*4 >= CurArraySize*3) { |
| 55 | // If more than 3/4 of the array is full, grow. |
| 56 | Grow(CurArraySize < 64 ? 128 : CurArraySize*2); |
| 57 | } else if (CurArraySize-(NumElements+NumTombstones) < CurArraySize/8) { |
| 58 | // If fewer of 1/8 of the array is empty (meaning that many are filled with |
| 59 | // tombstones), rehash. |
| 60 | Grow(CurArraySize); |
| 61 | } |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 62 | |
| 63 | // Okay, we know we have space. Find a hash bucket. |
Dan Gohman | e955c48 | 2008-08-05 14:45:15 +0000 | [diff] [blame] | 64 | const void **Bucket = const_cast<const void**>(FindBucketFor(Ptr)); |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 65 | if (*Bucket == Ptr) |
| 66 | return std::make_pair(Bucket, false); // Already inserted, good. |
| 67 | |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 68 | // Otherwise, insert it! |
Chris Lattner | e346767 | 2007-02-07 01:11:25 +0000 | [diff] [blame] | 69 | if (*Bucket == getTombstoneMarker()) |
| 70 | --NumTombstones; |
Dan Gohman | e955c48 | 2008-08-05 14:45:15 +0000 | [diff] [blame] | 71 | *Bucket = Ptr; |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 72 | ++NumElements; // Track density. |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 73 | return std::make_pair(Bucket, true); |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Chandler Carruth | 173bd7e | 2014-02-03 11:24:18 +0000 | [diff] [blame] | 76 | bool SmallPtrSetImplBase::erase_imp(const void * Ptr) { |
Chris Lattner | 39ab70c | 2007-01-27 07:59:10 +0000 | [diff] [blame] | 77 | if (isSmall()) { |
| 78 | // Check to see if it is in the set. |
Owen Anderson | 49f037a | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 79 | for (const void **APtr = SmallArray, **E = SmallArray+NumElements; |
Chris Lattner | 39ab70c | 2007-01-27 07:59:10 +0000 | [diff] [blame] | 80 | APtr != E; ++APtr) |
| 81 | if (*APtr == Ptr) { |
Chris Lattner | 836e8f3 | 2007-06-21 23:23:32 +0000 | [diff] [blame] | 82 | // If it is in the set, replace this element. |
| 83 | *APtr = E[-1]; |
Chris Lattner | 39ab70c | 2007-01-27 07:59:10 +0000 | [diff] [blame] | 84 | E[-1] = getEmptyMarker(); |
| 85 | --NumElements; |
Chris Lattner | 92c5d11 | 2007-02-05 23:10:31 +0000 | [diff] [blame] | 86 | return true; |
Chris Lattner | 39ab70c | 2007-01-27 07:59:10 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | // Okay, we know we have space. Find a hash bucket. |
| 93 | void **Bucket = const_cast<void**>(FindBucketFor(Ptr)); |
| 94 | if (*Bucket != Ptr) return false; // Not in the set? |
| 95 | |
| 96 | // Set this as a tombstone. |
| 97 | *Bucket = getTombstoneMarker(); |
| 98 | --NumElements; |
Chris Lattner | e346767 | 2007-02-07 01:11:25 +0000 | [diff] [blame] | 99 | ++NumTombstones; |
Chris Lattner | 39ab70c | 2007-01-27 07:59:10 +0000 | [diff] [blame] | 100 | return true; |
| 101 | } |
| 102 | |
Chandler Carruth | 173bd7e | 2014-02-03 11:24:18 +0000 | [diff] [blame] | 103 | const void * const *SmallPtrSetImplBase::FindBucketFor(const void *Ptr) const { |
Benjamin Kramer | 94988cb | 2012-04-18 10:37:32 +0000 | [diff] [blame] | 104 | unsigned Bucket = DenseMapInfo<void *>::getHashValue(Ptr) & (CurArraySize-1); |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 105 | unsigned ArraySize = CurArraySize; |
| 106 | unsigned ProbeAmt = 1; |
Owen Anderson | 49f037a | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 107 | const void *const *Array = CurArray; |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 108 | const void *const *Tombstone = nullptr; |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 109 | while (1) { |
| 110 | // Found Ptr's bucket? |
| 111 | if (Array[Bucket] == Ptr) |
| 112 | return Array+Bucket; |
| 113 | |
| 114 | // If we found an empty bucket, the pointer doesn't exist in the set. |
| 115 | // Return a tombstone if we've seen one so far, or the empty bucket if |
| 116 | // not. |
| 117 | if (Array[Bucket] == getEmptyMarker()) |
| 118 | return Tombstone ? Tombstone : Array+Bucket; |
| 119 | |
| 120 | // If this is a tombstone, remember it. If Ptr ends up not in the set, we |
| 121 | // prefer to return it than something that would require more probing. |
| 122 | if (Array[Bucket] == getTombstoneMarker() && !Tombstone) |
| 123 | Tombstone = Array+Bucket; // Remember the first tombstone found. |
| 124 | |
| 125 | // It's a hash collision or a tombstone. Reprobe. |
| 126 | Bucket = (Bucket + ProbeAmt++) & (ArraySize-1); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /// Grow - Allocate a larger backing store for the buckets and move it over. |
| 131 | /// |
Chandler Carruth | 173bd7e | 2014-02-03 11:24:18 +0000 | [diff] [blame] | 132 | void SmallPtrSetImplBase::Grow(unsigned NewSize) { |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 133 | // Allocate at twice as many buckets, but at least 128. |
| 134 | unsigned OldSize = CurArraySize; |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 135 | |
Owen Anderson | 49f037a | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 136 | const void **OldBuckets = CurArray; |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 137 | bool WasSmall = isSmall(); |
| 138 | |
| 139 | // Install the new array. Clear all the buckets to empty. |
Jean-Luc Duprat | 89fe247 | 2013-03-29 22:07:12 +0000 | [diff] [blame] | 140 | CurArray = (const void**)malloc(sizeof(void*) * NewSize); |
Owen Anderson | 81990a3 | 2007-07-16 21:27:44 +0000 | [diff] [blame] | 141 | assert(CurArray && "Failed to allocate memory?"); |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 142 | CurArraySize = NewSize; |
| 143 | memset(CurArray, -1, NewSize*sizeof(void*)); |
| 144 | |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 145 | // Copy over all the elements. |
| 146 | if (WasSmall) { |
| 147 | // Small sets store their elements in order. |
Owen Anderson | 49f037a | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 148 | for (const void **BucketPtr = OldBuckets, **E = OldBuckets+NumElements; |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 149 | BucketPtr != E; ++BucketPtr) { |
Owen Anderson | 49f037a | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 150 | const void *Elt = *BucketPtr; |
| 151 | *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt); |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 152 | } |
| 153 | } else { |
| 154 | // Copy over all valid entries. |
Owen Anderson | 49f037a | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 155 | for (const void **BucketPtr = OldBuckets, **E = OldBuckets+OldSize; |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 156 | BucketPtr != E; ++BucketPtr) { |
| 157 | // Copy over the element if it is valid. |
Owen Anderson | 49f037a | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 158 | const void *Elt = *BucketPtr; |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 159 | if (Elt != getTombstoneMarker() && Elt != getEmptyMarker()) |
Owen Anderson | 49f037a | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 160 | *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt); |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Owen Anderson | 81990a3 | 2007-07-16 21:27:44 +0000 | [diff] [blame] | 163 | free(OldBuckets); |
Jeff Cohen | e7ce8f2 | 2007-04-14 21:50:21 +0000 | [diff] [blame] | 164 | NumTombstones = 0; |
| 165 | } |
| 166 | } |
| 167 | |
Chandler Carruth | 173bd7e | 2014-02-03 11:24:18 +0000 | [diff] [blame] | 168 | SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage, |
| 169 | const SmallPtrSetImplBase& that) { |
Duncan Sands | 7b90966 | 2010-06-30 15:02:37 +0000 | [diff] [blame] | 170 | SmallArray = SmallStorage; |
| 171 | |
Owen Anderson | 4c54024 | 2007-07-24 21:31:23 +0000 | [diff] [blame] | 172 | // If we're becoming small, prepare to insert into our stack space |
Jeff Cohen | e7ce8f2 | 2007-04-14 21:50:21 +0000 | [diff] [blame] | 173 | if (that.isSmall()) { |
Duncan Sands | 7b90966 | 2010-06-30 15:02:37 +0000 | [diff] [blame] | 174 | CurArray = SmallArray; |
Owen Anderson | 4c54024 | 2007-07-24 21:31:23 +0000 | [diff] [blame] | 175 | // Otherwise, allocate new heap space (unless we were the same size) |
Jeff Cohen | e7ce8f2 | 2007-04-14 21:50:21 +0000 | [diff] [blame] | 176 | } else { |
Jean-Luc Duprat | 89fe247 | 2013-03-29 22:07:12 +0000 | [diff] [blame] | 177 | CurArray = (const void**)malloc(sizeof(void*) * that.CurArraySize); |
Owen Anderson | 81990a3 | 2007-07-16 21:27:44 +0000 | [diff] [blame] | 178 | assert(CurArray && "Failed to allocate memory?"); |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 179 | } |
Owen Anderson | 4c54024 | 2007-07-24 21:31:23 +0000 | [diff] [blame] | 180 | |
| 181 | // Copy over the new array size |
| 182 | CurArraySize = that.CurArraySize; |
| 183 | |
| 184 | // Copy over the contents from the other set |
Jean-Luc Duprat | 89fe247 | 2013-03-29 22:07:12 +0000 | [diff] [blame] | 185 | memcpy(CurArray, that.CurArray, sizeof(void*)*CurArraySize); |
Owen Anderson | 4c54024 | 2007-07-24 21:31:23 +0000 | [diff] [blame] | 186 | |
| 187 | NumElements = that.NumElements; |
| 188 | NumTombstones = that.NumTombstones; |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 189 | } |
Chris Lattner | 85049a4 | 2007-07-09 16:54:03 +0000 | [diff] [blame] | 190 | |
Chandler Carruth | 173bd7e | 2014-02-03 11:24:18 +0000 | [diff] [blame] | 191 | SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage, |
| 192 | unsigned SmallSize, |
| 193 | SmallPtrSetImplBase &&that) { |
Chandler Carruth | 55758e9 | 2013-11-20 11:14:33 +0000 | [diff] [blame] | 194 | SmallArray = SmallStorage; |
| 195 | |
| 196 | // Copy over the basic members. |
| 197 | CurArraySize = that.CurArraySize; |
| 198 | NumElements = that.NumElements; |
| 199 | NumTombstones = that.NumTombstones; |
| 200 | |
| 201 | // When small, just copy into our small buffer. |
| 202 | if (that.isSmall()) { |
| 203 | CurArray = SmallArray; |
| 204 | memcpy(CurArray, that.CurArray, sizeof(void *) * CurArraySize); |
Craig Topper | 298f638 | 2014-08-20 04:41:36 +0000 | [diff] [blame] | 205 | } else { |
| 206 | // Otherwise, we steal the large memory allocation and no copy is needed. |
| 207 | CurArray = that.CurArray; |
| 208 | that.CurArray = that.SmallArray; |
Chandler Carruth | 55758e9 | 2013-11-20 11:14:33 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Chandler Carruth | c74010d | 2013-11-20 18:29:56 +0000 | [diff] [blame] | 211 | // Make the "that" object small and empty. |
| 212 | that.CurArraySize = SmallSize; |
| 213 | assert(that.CurArray == that.SmallArray); |
| 214 | that.NumElements = 0; |
| 215 | that.NumTombstones = 0; |
Chandler Carruth | 55758e9 | 2013-11-20 11:14:33 +0000 | [diff] [blame] | 216 | } |
Chandler Carruth | 55758e9 | 2013-11-20 11:14:33 +0000 | [diff] [blame] | 217 | |
Chris Lattner | 85049a4 | 2007-07-09 16:54:03 +0000 | [diff] [blame] | 218 | /// CopyFrom - implement operator= from a smallptrset that has the same pointer |
| 219 | /// type, but may have a different small size. |
Chandler Carruth | 173bd7e | 2014-02-03 11:24:18 +0000 | [diff] [blame] | 220 | void SmallPtrSetImplBase::CopyFrom(const SmallPtrSetImplBase &RHS) { |
Chandler Carruth | 480f5d2 | 2013-11-26 00:54:44 +0000 | [diff] [blame] | 221 | assert(&RHS != this && "Self-copy should be handled by the caller."); |
Chandler Carruth | 2664317 | 2013-11-26 00:44:36 +0000 | [diff] [blame] | 222 | |
Owen Anderson | e33356b | 2007-07-09 22:27:20 +0000 | [diff] [blame] | 223 | if (isSmall() && RHS.isSmall()) |
| 224 | assert(CurArraySize == RHS.CurArraySize && |
| 225 | "Cannot assign sets with different small sizes"); |
Jean-Luc Duprat | 89fe247 | 2013-03-29 22:07:12 +0000 | [diff] [blame] | 226 | |
Owen Anderson | e33356b | 2007-07-09 22:27:20 +0000 | [diff] [blame] | 227 | // If we're becoming small, prepare to insert into our stack space |
Owen Anderson | ee99010 | 2007-07-19 06:45:33 +0000 | [diff] [blame] | 228 | if (RHS.isSmall()) { |
| 229 | if (!isSmall()) |
| 230 | free(CurArray); |
Duncan Sands | 7b90966 | 2010-06-30 15:02:37 +0000 | [diff] [blame] | 231 | CurArray = SmallArray; |
Owen Anderson | e33356b | 2007-07-09 22:27:20 +0000 | [diff] [blame] | 232 | // Otherwise, allocate new heap space (unless we were the same size) |
Owen Anderson | ee99010 | 2007-07-19 06:45:33 +0000 | [diff] [blame] | 233 | } else if (CurArraySize != RHS.CurArraySize) { |
Owen Anderson | e21f270 | 2007-07-18 19:54:15 +0000 | [diff] [blame] | 234 | if (isSmall()) |
Jean-Luc Duprat | 89fe247 | 2013-03-29 22:07:12 +0000 | [diff] [blame] | 235 | CurArray = (const void**)malloc(sizeof(void*) * RHS.CurArraySize); |
Aaron Ballman | 5de2378 | 2013-11-18 17:33:32 +0000 | [diff] [blame] | 236 | else { |
| 237 | const void **T = (const void**)realloc(CurArray, |
| 238 | sizeof(void*) * RHS.CurArraySize); |
| 239 | if (!T) |
| 240 | free(CurArray); |
| 241 | CurArray = T; |
| 242 | } |
Owen Anderson | 81990a3 | 2007-07-16 21:27:44 +0000 | [diff] [blame] | 243 | assert(CurArray && "Failed to allocate memory?"); |
| 244 | } |
Owen Anderson | e33356b | 2007-07-09 22:27:20 +0000 | [diff] [blame] | 245 | |
| 246 | // Copy over the new array size |
| 247 | CurArraySize = RHS.CurArraySize; |
| 248 | |
| 249 | // Copy over the contents from the other set |
Jean-Luc Duprat | 89fe247 | 2013-03-29 22:07:12 +0000 | [diff] [blame] | 250 | memcpy(CurArray, RHS.CurArray, sizeof(void*)*CurArraySize); |
Owen Anderson | e21f270 | 2007-07-18 19:54:15 +0000 | [diff] [blame] | 251 | |
| 252 | NumElements = RHS.NumElements; |
| 253 | NumTombstones = RHS.NumTombstones; |
Chris Lattner | 85049a4 | 2007-07-09 16:54:03 +0000 | [diff] [blame] | 254 | } |
Reid Spencer | 5060fd0 | 2007-07-17 02:16:12 +0000 | [diff] [blame] | 255 | |
Chandler Carruth | 173bd7e | 2014-02-03 11:24:18 +0000 | [diff] [blame] | 256 | void SmallPtrSetImplBase::MoveFrom(unsigned SmallSize, |
| 257 | SmallPtrSetImplBase &&RHS) { |
Chandler Carruth | 480f5d2 | 2013-11-26 00:54:44 +0000 | [diff] [blame] | 258 | assert(&RHS != this && "Self-move should be handled by the caller."); |
| 259 | |
Chandler Carruth | 55758e9 | 2013-11-20 11:14:33 +0000 | [diff] [blame] | 260 | if (!isSmall()) |
| 261 | free(CurArray); |
| 262 | |
| 263 | if (RHS.isSmall()) { |
| 264 | // Copy a small RHS rather than moving. |
| 265 | CurArray = SmallArray; |
| 266 | memcpy(CurArray, RHS.CurArray, sizeof(void*)*RHS.CurArraySize); |
| 267 | } else { |
| 268 | CurArray = RHS.CurArray; |
| 269 | RHS.CurArray = RHS.SmallArray; |
| 270 | } |
| 271 | |
| 272 | // Copy the rest of the trivial members. |
| 273 | CurArraySize = RHS.CurArraySize; |
| 274 | NumElements = RHS.NumElements; |
| 275 | NumTombstones = RHS.NumTombstones; |
Chandler Carruth | c74010d | 2013-11-20 18:29:56 +0000 | [diff] [blame] | 276 | |
| 277 | // Make the RHS small and empty. |
| 278 | RHS.CurArraySize = SmallSize; |
| 279 | assert(RHS.CurArray == RHS.SmallArray); |
| 280 | RHS.NumElements = 0; |
| 281 | RHS.NumTombstones = 0; |
Chandler Carruth | 55758e9 | 2013-11-20 11:14:33 +0000 | [diff] [blame] | 282 | } |
Chandler Carruth | 55758e9 | 2013-11-20 11:14:33 +0000 | [diff] [blame] | 283 | |
Chandler Carruth | 173bd7e | 2014-02-03 11:24:18 +0000 | [diff] [blame] | 284 | void SmallPtrSetImplBase::swap(SmallPtrSetImplBase &RHS) { |
Benjamin Kramer | e1c34e9 | 2012-03-06 20:40:02 +0000 | [diff] [blame] | 285 | if (this == &RHS) return; |
| 286 | |
| 287 | // We can only avoid copying elements if neither set is small. |
| 288 | if (!this->isSmall() && !RHS.isSmall()) { |
| 289 | std::swap(this->CurArray, RHS.CurArray); |
| 290 | std::swap(this->CurArraySize, RHS.CurArraySize); |
| 291 | std::swap(this->NumElements, RHS.NumElements); |
| 292 | std::swap(this->NumTombstones, RHS.NumTombstones); |
| 293 | return; |
| 294 | } |
| 295 | |
| 296 | // FIXME: From here on we assume that both sets have the same small size. |
| 297 | |
| 298 | // If only RHS is small, copy the small elements into LHS and move the pointer |
| 299 | // from LHS to RHS. |
| 300 | if (!this->isSmall() && RHS.isSmall()) { |
Benjamin Kramer | 22842f8 | 2012-03-07 22:48:42 +0000 | [diff] [blame] | 301 | std::copy(RHS.SmallArray, RHS.SmallArray+RHS.CurArraySize, |
| 302 | this->SmallArray); |
Benjamin Kramer | e1c34e9 | 2012-03-06 20:40:02 +0000 | [diff] [blame] | 303 | std::swap(this->NumElements, RHS.NumElements); |
| 304 | std::swap(this->CurArraySize, RHS.CurArraySize); |
| 305 | RHS.CurArray = this->CurArray; |
| 306 | RHS.NumTombstones = this->NumTombstones; |
| 307 | this->CurArray = this->SmallArray; |
| 308 | this->NumTombstones = 0; |
| 309 | return; |
| 310 | } |
| 311 | |
| 312 | // If only LHS is small, copy the small elements into RHS and move the pointer |
| 313 | // from RHS to LHS. |
| 314 | if (this->isSmall() && !RHS.isSmall()) { |
Benjamin Kramer | 22842f8 | 2012-03-07 22:48:42 +0000 | [diff] [blame] | 315 | std::copy(this->SmallArray, this->SmallArray+this->CurArraySize, |
| 316 | RHS.SmallArray); |
Benjamin Kramer | e1c34e9 | 2012-03-06 20:40:02 +0000 | [diff] [blame] | 317 | std::swap(RHS.NumElements, this->NumElements); |
| 318 | std::swap(RHS.CurArraySize, this->CurArraySize); |
| 319 | this->CurArray = RHS.CurArray; |
| 320 | this->NumTombstones = RHS.NumTombstones; |
| 321 | RHS.CurArray = RHS.SmallArray; |
| 322 | RHS.NumTombstones = 0; |
| 323 | return; |
| 324 | } |
| 325 | |
| 326 | // Both a small, just swap the small elements. |
| 327 | assert(this->isSmall() && RHS.isSmall()); |
| 328 | assert(this->CurArraySize == RHS.CurArraySize); |
Benjamin Kramer | 22842f8 | 2012-03-07 22:48:42 +0000 | [diff] [blame] | 329 | std::swap_ranges(this->SmallArray, this->SmallArray+this->CurArraySize, |
Benjamin Kramer | 6e8d4b8 | 2012-03-07 22:33:21 +0000 | [diff] [blame] | 330 | RHS.SmallArray); |
Benjamin Kramer | e1c34e9 | 2012-03-06 20:40:02 +0000 | [diff] [blame] | 331 | std::swap(this->NumElements, RHS.NumElements); |
| 332 | } |
| 333 | |
Chandler Carruth | 173bd7e | 2014-02-03 11:24:18 +0000 | [diff] [blame] | 334 | SmallPtrSetImplBase::~SmallPtrSetImplBase() { |
Reid Spencer | 5060fd0 | 2007-07-17 02:16:12 +0000 | [diff] [blame] | 335 | if (!isSmall()) |
| 336 | free(CurArray); |
| 337 | } |