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> |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 19 | #include <cassert> |
Reid Spencer | 5060fd0 | 2007-07-17 02:16:12 +0000 | [diff] [blame] | 20 | #include <cstdlib> |
| 21 | |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
Chandler Carruth | 173bd7e | 2014-02-03 11:24:18 +0000 | [diff] [blame] | 24 | void SmallPtrSetImplBase::shrink_and_clear() { |
Chris Lattner | 44f7d3a | 2007-08-05 07:32:14 +0000 | [diff] [blame] | 25 | assert(!isSmall() && "Can't shrink a small set!"); |
| 26 | free(CurArray); |
| 27 | |
| 28 | // Reduce the number of buckets. |
Matthias Braun | acd531c | 2016-02-15 21:38:42 +0000 | [diff] [blame] | 29 | unsigned Size = size(); |
| 30 | CurArraySize = Size > 16 ? 1 << (Log2_32_Ceil(Size) + 1) : 32; |
| 31 | NumNonEmpty = NumTombstones = 0; |
Chris Lattner | 44f7d3a | 2007-08-05 07:32:14 +0000 | [diff] [blame] | 32 | |
| 33 | // Install the new array. Clear all the buckets to empty. |
Jean-Luc Duprat | 89fe247 | 2013-03-29 22:07:12 +0000 | [diff] [blame] | 34 | CurArray = (const void**)malloc(sizeof(void*) * CurArraySize); |
Chris Lattner | 44f7d3a | 2007-08-05 07:32:14 +0000 | [diff] [blame] | 35 | assert(CurArray && "Failed to allocate memory?"); |
| 36 | memset(CurArray, -1, CurArraySize*sizeof(void*)); |
Chris Lattner | 44f7d3a | 2007-08-05 07:32:14 +0000 | [diff] [blame] | 37 | } |
| 38 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 39 | std::pair<const void *const *, bool> |
Matthias Braun | 4962bd7 | 2016-01-27 04:20:24 +0000 | [diff] [blame] | 40 | SmallPtrSetImplBase::insert_imp_big(const void *Ptr) { |
Matthias Braun | acd531c | 2016-02-15 21:38:42 +0000 | [diff] [blame] | 41 | if (LLVM_UNLIKELY(size() * 4 >= CurArraySize * 3)) { |
Jakob Stoklund Olesen | bdc1b01 | 2011-03-30 18:32:48 +0000 | [diff] [blame] | 42 | // If more than 3/4 of the array is full, grow. |
Matthias Braun | acd531c | 2016-02-15 21:38:42 +0000 | [diff] [blame] | 43 | Grow(CurArraySize < 64 ? 128 : CurArraySize * 2); |
| 44 | } else if (LLVM_UNLIKELY(CurArraySize - NumNonEmpty < CurArraySize / 8)) { |
Jakob Stoklund Olesen | bdc1b01 | 2011-03-30 18:32:48 +0000 | [diff] [blame] | 45 | // If fewer of 1/8 of the array is empty (meaning that many are filled with |
| 46 | // tombstones), rehash. |
| 47 | Grow(CurArraySize); |
| 48 | } |
Matthias Braun | f67e5c7 | 2016-01-28 04:49:07 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 50 | // Okay, we know we have space. Find a hash bucket. |
Dan Gohman | e955c48 | 2008-08-05 14:45:15 +0000 | [diff] [blame] | 51 | const void **Bucket = const_cast<const void**>(FindBucketFor(Ptr)); |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 52 | if (*Bucket == Ptr) |
| 53 | return std::make_pair(Bucket, false); // Already inserted, good. |
| 54 | |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 55 | // Otherwise, insert it! |
Chris Lattner | e346767 | 2007-02-07 01:11:25 +0000 | [diff] [blame] | 56 | if (*Bucket == getTombstoneMarker()) |
| 57 | --NumTombstones; |
Matthias Braun | acd531c | 2016-02-15 21:38:42 +0000 | [diff] [blame] | 58 | else |
| 59 | ++NumNonEmpty; // Track density. |
Dan Gohman | e955c48 | 2008-08-05 14:45:15 +0000 | [diff] [blame] | 60 | *Bucket = Ptr; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 61 | return std::make_pair(Bucket, true); |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Chandler Carruth | 173bd7e | 2014-02-03 11:24:18 +0000 | [diff] [blame] | 64 | const void * const *SmallPtrSetImplBase::FindBucketFor(const void *Ptr) const { |
Benjamin Kramer | 94988cb | 2012-04-18 10:37:32 +0000 | [diff] [blame] | 65 | unsigned Bucket = DenseMapInfo<void *>::getHashValue(Ptr) & (CurArraySize-1); |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 66 | unsigned ArraySize = CurArraySize; |
| 67 | unsigned ProbeAmt = 1; |
Owen Anderson | 49f037a | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 68 | const void *const *Array = CurArray; |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 69 | const void *const *Tombstone = nullptr; |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 70 | while (true) { |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 71 | // If we found an empty bucket, the pointer doesn't exist in the set. |
| 72 | // Return a tombstone if we've seen one so far, or the empty bucket if |
| 73 | // not. |
Benjamin Kramer | 654a85e | 2015-02-23 16:41:36 +0000 | [diff] [blame] | 74 | if (LLVM_LIKELY(Array[Bucket] == getEmptyMarker())) |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 75 | return Tombstone ? Tombstone : Array+Bucket; |
Benjamin Kramer | 654a85e | 2015-02-23 16:41:36 +0000 | [diff] [blame] | 76 | |
| 77 | // Found Ptr's bucket? |
| 78 | if (LLVM_LIKELY(Array[Bucket] == Ptr)) |
| 79 | return Array+Bucket; |
| 80 | |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 81 | // If this is a tombstone, remember it. If Ptr ends up not in the set, we |
| 82 | // prefer to return it than something that would require more probing. |
| 83 | if (Array[Bucket] == getTombstoneMarker() && !Tombstone) |
| 84 | Tombstone = Array+Bucket; // Remember the first tombstone found. |
Matthias Braun | f67e5c7 | 2016-01-28 04:49:07 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 86 | // It's a hash collision or a tombstone. Reprobe. |
| 87 | Bucket = (Bucket + ProbeAmt++) & (ArraySize-1); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /// Grow - Allocate a larger backing store for the buckets and move it over. |
| 92 | /// |
Chandler Carruth | 173bd7e | 2014-02-03 11:24:18 +0000 | [diff] [blame] | 93 | void SmallPtrSetImplBase::Grow(unsigned NewSize) { |
Owen Anderson | 49f037a | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 94 | const void **OldBuckets = CurArray; |
Matthias Braun | acd531c | 2016-02-15 21:38:42 +0000 | [diff] [blame] | 95 | const void **OldEnd = EndPointer(); |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 96 | bool WasSmall = isSmall(); |
Matthias Braun | f67e5c7 | 2016-01-28 04:49:07 +0000 | [diff] [blame] | 97 | |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 98 | // Install the new array. Clear all the buckets to empty. |
Jean-Luc Duprat | 89fe247 | 2013-03-29 22:07:12 +0000 | [diff] [blame] | 99 | CurArray = (const void**)malloc(sizeof(void*) * NewSize); |
Owen Anderson | 81990a3 | 2007-07-16 21:27:44 +0000 | [diff] [blame] | 100 | assert(CurArray && "Failed to allocate memory?"); |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 101 | CurArraySize = NewSize; |
| 102 | memset(CurArray, -1, NewSize*sizeof(void*)); |
Matthias Braun | f67e5c7 | 2016-01-28 04:49:07 +0000 | [diff] [blame] | 103 | |
Matthias Braun | acd531c | 2016-02-15 21:38:42 +0000 | [diff] [blame] | 104 | // Copy over all valid entries. |
| 105 | for (const void **BucketPtr = OldBuckets; BucketPtr != OldEnd; ++BucketPtr) { |
| 106 | // Copy over the element if it is valid. |
| 107 | const void *Elt = *BucketPtr; |
| 108 | if (Elt != getTombstoneMarker() && Elt != getEmptyMarker()) |
Owen Anderson | 49f037a | 2007-07-27 18:07:02 +0000 | [diff] [blame] | 109 | *const_cast<void**>(FindBucketFor(Elt)) = const_cast<void*>(Elt); |
Jeff Cohen | e7ce8f2 | 2007-04-14 21:50:21 +0000 | [diff] [blame] | 110 | } |
Matthias Braun | acd531c | 2016-02-15 21:38:42 +0000 | [diff] [blame] | 111 | |
| 112 | if (!WasSmall) |
| 113 | free(OldBuckets); |
| 114 | NumNonEmpty -= NumTombstones; |
| 115 | NumTombstones = 0; |
Jeff Cohen | e7ce8f2 | 2007-04-14 21:50:21 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Chandler Carruth | 173bd7e | 2014-02-03 11:24:18 +0000 | [diff] [blame] | 118 | SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage, |
Matthias Braun | f67e5c7 | 2016-01-28 04:49:07 +0000 | [diff] [blame] | 119 | const SmallPtrSetImplBase &that) { |
Duncan Sands | 7b90966 | 2010-06-30 15:02:37 +0000 | [diff] [blame] | 120 | SmallArray = SmallStorage; |
| 121 | |
Owen Anderson | 4c54024 | 2007-07-24 21:31:23 +0000 | [diff] [blame] | 122 | // If we're becoming small, prepare to insert into our stack space |
Jeff Cohen | e7ce8f2 | 2007-04-14 21:50:21 +0000 | [diff] [blame] | 123 | if (that.isSmall()) { |
Duncan Sands | 7b90966 | 2010-06-30 15:02:37 +0000 | [diff] [blame] | 124 | CurArray = SmallArray; |
Owen Anderson | 4c54024 | 2007-07-24 21:31:23 +0000 | [diff] [blame] | 125 | // Otherwise, allocate new heap space (unless we were the same size) |
Jeff Cohen | e7ce8f2 | 2007-04-14 21:50:21 +0000 | [diff] [blame] | 126 | } else { |
Jean-Luc Duprat | 89fe247 | 2013-03-29 22:07:12 +0000 | [diff] [blame] | 127 | CurArray = (const void**)malloc(sizeof(void*) * that.CurArraySize); |
Owen Anderson | 81990a3 | 2007-07-16 21:27:44 +0000 | [diff] [blame] | 128 | assert(CurArray && "Failed to allocate memory?"); |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 129 | } |
Matthias Braun | f67e5c7 | 2016-01-28 04:49:07 +0000 | [diff] [blame] | 130 | |
Matthias Braun | 924f080 | 2016-01-28 04:49:11 +0000 | [diff] [blame] | 131 | // Copy over the that array. |
| 132 | CopyHelper(that); |
Chris Lattner | 74102df | 2007-01-27 07:10:46 +0000 | [diff] [blame] | 133 | } |
Chris Lattner | 85049a4 | 2007-07-09 16:54:03 +0000 | [diff] [blame] | 134 | |
Chandler Carruth | 173bd7e | 2014-02-03 11:24:18 +0000 | [diff] [blame] | 135 | SmallPtrSetImplBase::SmallPtrSetImplBase(const void **SmallStorage, |
| 136 | unsigned SmallSize, |
| 137 | SmallPtrSetImplBase &&that) { |
Chandler Carruth | 55758e9 | 2013-11-20 11:14:33 +0000 | [diff] [blame] | 138 | SmallArray = SmallStorage; |
Matthias Braun | 924f080 | 2016-01-28 04:49:11 +0000 | [diff] [blame] | 139 | MoveHelper(SmallSize, std::move(that)); |
Chandler Carruth | 55758e9 | 2013-11-20 11:14:33 +0000 | [diff] [blame] | 140 | } |
Chandler Carruth | 55758e9 | 2013-11-20 11:14:33 +0000 | [diff] [blame] | 141 | |
Chandler Carruth | 173bd7e | 2014-02-03 11:24:18 +0000 | [diff] [blame] | 142 | void SmallPtrSetImplBase::CopyFrom(const SmallPtrSetImplBase &RHS) { |
Chandler Carruth | 480f5d2 | 2013-11-26 00:54:44 +0000 | [diff] [blame] | 143 | assert(&RHS != this && "Self-copy should be handled by the caller."); |
Chandler Carruth | 2664317 | 2013-11-26 00:44:36 +0000 | [diff] [blame] | 144 | |
Owen Anderson | e33356b | 2007-07-09 22:27:20 +0000 | [diff] [blame] | 145 | if (isSmall() && RHS.isSmall()) |
| 146 | assert(CurArraySize == RHS.CurArraySize && |
| 147 | "Cannot assign sets with different small sizes"); |
Jean-Luc Duprat | 89fe247 | 2013-03-29 22:07:12 +0000 | [diff] [blame] | 148 | |
Owen Anderson | e33356b | 2007-07-09 22:27:20 +0000 | [diff] [blame] | 149 | // If we're becoming small, prepare to insert into our stack space |
Owen Anderson | ee99010 | 2007-07-19 06:45:33 +0000 | [diff] [blame] | 150 | if (RHS.isSmall()) { |
| 151 | if (!isSmall()) |
| 152 | free(CurArray); |
Duncan Sands | 7b90966 | 2010-06-30 15:02:37 +0000 | [diff] [blame] | 153 | CurArray = SmallArray; |
Owen Anderson | e33356b | 2007-07-09 22:27:20 +0000 | [diff] [blame] | 154 | // Otherwise, allocate new heap space (unless we were the same size) |
Owen Anderson | ee99010 | 2007-07-19 06:45:33 +0000 | [diff] [blame] | 155 | } else if (CurArraySize != RHS.CurArraySize) { |
Owen Anderson | e21f270 | 2007-07-18 19:54:15 +0000 | [diff] [blame] | 156 | if (isSmall()) |
Jean-Luc Duprat | 89fe247 | 2013-03-29 22:07:12 +0000 | [diff] [blame] | 157 | CurArray = (const void**)malloc(sizeof(void*) * RHS.CurArraySize); |
Aaron Ballman | 5de2378 | 2013-11-18 17:33:32 +0000 | [diff] [blame] | 158 | else { |
| 159 | const void **T = (const void**)realloc(CurArray, |
| 160 | sizeof(void*) * RHS.CurArraySize); |
| 161 | if (!T) |
| 162 | free(CurArray); |
| 163 | CurArray = T; |
| 164 | } |
Owen Anderson | 81990a3 | 2007-07-16 21:27:44 +0000 | [diff] [blame] | 165 | assert(CurArray && "Failed to allocate memory?"); |
| 166 | } |
Matthias Braun | f67e5c7 | 2016-01-28 04:49:07 +0000 | [diff] [blame] | 167 | |
Matthias Braun | 924f080 | 2016-01-28 04:49:11 +0000 | [diff] [blame] | 168 | CopyHelper(RHS); |
| 169 | } |
| 170 | |
| 171 | void SmallPtrSetImplBase::CopyHelper(const SmallPtrSetImplBase &RHS) { |
Owen Anderson | e33356b | 2007-07-09 22:27:20 +0000 | [diff] [blame] | 172 | // Copy over the new array size |
| 173 | CurArraySize = RHS.CurArraySize; |
| 174 | |
| 175 | // Copy over the contents from the other set |
Matthias Braun | acd531c | 2016-02-15 21:38:42 +0000 | [diff] [blame] | 176 | std::copy(RHS.CurArray, RHS.EndPointer(), CurArray); |
Matthias Braun | f67e5c7 | 2016-01-28 04:49:07 +0000 | [diff] [blame] | 177 | |
Matthias Braun | acd531c | 2016-02-15 21:38:42 +0000 | [diff] [blame] | 178 | NumNonEmpty = RHS.NumNonEmpty; |
Owen Anderson | e21f270 | 2007-07-18 19:54:15 +0000 | [diff] [blame] | 179 | NumTombstones = RHS.NumTombstones; |
Chris Lattner | 85049a4 | 2007-07-09 16:54:03 +0000 | [diff] [blame] | 180 | } |
Reid Spencer | 5060fd0 | 2007-07-17 02:16:12 +0000 | [diff] [blame] | 181 | |
Chandler Carruth | 173bd7e | 2014-02-03 11:24:18 +0000 | [diff] [blame] | 182 | void SmallPtrSetImplBase::MoveFrom(unsigned SmallSize, |
| 183 | SmallPtrSetImplBase &&RHS) { |
Chandler Carruth | 55758e9 | 2013-11-20 11:14:33 +0000 | [diff] [blame] | 184 | if (!isSmall()) |
| 185 | free(CurArray); |
Matthias Braun | 924f080 | 2016-01-28 04:49:11 +0000 | [diff] [blame] | 186 | MoveHelper(SmallSize, std::move(RHS)); |
| 187 | } |
| 188 | |
| 189 | void SmallPtrSetImplBase::MoveHelper(unsigned SmallSize, |
| 190 | SmallPtrSetImplBase &&RHS) { |
| 191 | assert(&RHS != this && "Self-move should be handled by the caller."); |
Chandler Carruth | 55758e9 | 2013-11-20 11:14:33 +0000 | [diff] [blame] | 192 | |
| 193 | if (RHS.isSmall()) { |
| 194 | // Copy a small RHS rather than moving. |
| 195 | CurArray = SmallArray; |
Matthias Braun | acd531c | 2016-02-15 21:38:42 +0000 | [diff] [blame] | 196 | std::copy(RHS.CurArray, RHS.CurArray + RHS.NumNonEmpty, CurArray); |
Chandler Carruth | 55758e9 | 2013-11-20 11:14:33 +0000 | [diff] [blame] | 197 | } else { |
| 198 | CurArray = RHS.CurArray; |
| 199 | RHS.CurArray = RHS.SmallArray; |
| 200 | } |
| 201 | |
| 202 | // Copy the rest of the trivial members. |
| 203 | CurArraySize = RHS.CurArraySize; |
Matthias Braun | acd531c | 2016-02-15 21:38:42 +0000 | [diff] [blame] | 204 | NumNonEmpty = RHS.NumNonEmpty; |
Chandler Carruth | 55758e9 | 2013-11-20 11:14:33 +0000 | [diff] [blame] | 205 | NumTombstones = RHS.NumTombstones; |
Chandler Carruth | c74010d | 2013-11-20 18:29:56 +0000 | [diff] [blame] | 206 | |
| 207 | // Make the RHS small and empty. |
| 208 | RHS.CurArraySize = SmallSize; |
| 209 | assert(RHS.CurArray == RHS.SmallArray); |
Matthias Braun | acd531c | 2016-02-15 21:38:42 +0000 | [diff] [blame] | 210 | RHS.NumNonEmpty = 0; |
Chandler Carruth | c74010d | 2013-11-20 18:29:56 +0000 | [diff] [blame] | 211 | RHS.NumTombstones = 0; |
Chandler Carruth | 55758e9 | 2013-11-20 11:14:33 +0000 | [diff] [blame] | 212 | } |
Chandler Carruth | 55758e9 | 2013-11-20 11:14:33 +0000 | [diff] [blame] | 213 | |
Chandler Carruth | 173bd7e | 2014-02-03 11:24:18 +0000 | [diff] [blame] | 214 | void SmallPtrSetImplBase::swap(SmallPtrSetImplBase &RHS) { |
Benjamin Kramer | e1c34e9 | 2012-03-06 20:40:02 +0000 | [diff] [blame] | 215 | if (this == &RHS) return; |
| 216 | |
| 217 | // We can only avoid copying elements if neither set is small. |
| 218 | if (!this->isSmall() && !RHS.isSmall()) { |
| 219 | std::swap(this->CurArray, RHS.CurArray); |
| 220 | std::swap(this->CurArraySize, RHS.CurArraySize); |
Matthias Braun | acd531c | 2016-02-15 21:38:42 +0000 | [diff] [blame] | 221 | std::swap(this->NumNonEmpty, RHS.NumNonEmpty); |
Benjamin Kramer | e1c34e9 | 2012-03-06 20:40:02 +0000 | [diff] [blame] | 222 | std::swap(this->NumTombstones, RHS.NumTombstones); |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | // FIXME: From here on we assume that both sets have the same small size. |
| 227 | |
| 228 | // If only RHS is small, copy the small elements into LHS and move the pointer |
| 229 | // from LHS to RHS. |
| 230 | if (!this->isSmall() && RHS.isSmall()) { |
Matthias Braun | acd531c | 2016-02-15 21:38:42 +0000 | [diff] [blame] | 231 | assert(RHS.CurArray == RHS.SmallArray); |
| 232 | std::copy(RHS.CurArray, RHS.CurArray + RHS.NumNonEmpty, this->SmallArray); |
| 233 | std::swap(RHS.CurArraySize, this->CurArraySize); |
| 234 | std::swap(this->NumNonEmpty, RHS.NumNonEmpty); |
| 235 | std::swap(this->NumTombstones, RHS.NumTombstones); |
Benjamin Kramer | e1c34e9 | 2012-03-06 20:40:02 +0000 | [diff] [blame] | 236 | RHS.CurArray = this->CurArray; |
Benjamin Kramer | e1c34e9 | 2012-03-06 20:40:02 +0000 | [diff] [blame] | 237 | this->CurArray = this->SmallArray; |
Benjamin Kramer | e1c34e9 | 2012-03-06 20:40:02 +0000 | [diff] [blame] | 238 | return; |
| 239 | } |
| 240 | |
| 241 | // If only LHS is small, copy the small elements into RHS and move the pointer |
| 242 | // from RHS to LHS. |
| 243 | if (this->isSmall() && !RHS.isSmall()) { |
Matthias Braun | acd531c | 2016-02-15 21:38:42 +0000 | [diff] [blame] | 244 | assert(this->CurArray == this->SmallArray); |
| 245 | std::copy(this->CurArray, this->CurArray + this->NumNonEmpty, |
Benjamin Kramer | 22842f8 | 2012-03-07 22:48:42 +0000 | [diff] [blame] | 246 | RHS.SmallArray); |
Benjamin Kramer | e1c34e9 | 2012-03-06 20:40:02 +0000 | [diff] [blame] | 247 | std::swap(RHS.CurArraySize, this->CurArraySize); |
Matthias Braun | acd531c | 2016-02-15 21:38:42 +0000 | [diff] [blame] | 248 | std::swap(RHS.NumNonEmpty, this->NumNonEmpty); |
| 249 | std::swap(RHS.NumTombstones, this->NumTombstones); |
Benjamin Kramer | e1c34e9 | 2012-03-06 20:40:02 +0000 | [diff] [blame] | 250 | this->CurArray = RHS.CurArray; |
Benjamin Kramer | e1c34e9 | 2012-03-06 20:40:02 +0000 | [diff] [blame] | 251 | RHS.CurArray = RHS.SmallArray; |
Benjamin Kramer | e1c34e9 | 2012-03-06 20:40:02 +0000 | [diff] [blame] | 252 | return; |
| 253 | } |
| 254 | |
| 255 | // Both a small, just swap the small elements. |
| 256 | assert(this->isSmall() && RHS.isSmall()); |
Matthias Braun | acd531c | 2016-02-15 21:38:42 +0000 | [diff] [blame] | 257 | unsigned MinNonEmpty = std::min(this->NumNonEmpty, RHS.NumNonEmpty); |
| 258 | std::swap_ranges(this->SmallArray, this->SmallArray + MinNonEmpty, |
Benjamin Kramer | 6e8d4b8 | 2012-03-07 22:33:21 +0000 | [diff] [blame] | 259 | RHS.SmallArray); |
Matthias Braun | acd531c | 2016-02-15 21:38:42 +0000 | [diff] [blame] | 260 | if (this->NumNonEmpty > MinNonEmpty) { |
| 261 | std::copy(this->SmallArray + MinNonEmpty, |
| 262 | this->SmallArray + this->NumNonEmpty, |
| 263 | RHS.SmallArray + MinNonEmpty); |
| 264 | } else { |
| 265 | std::copy(RHS.SmallArray + MinNonEmpty, RHS.SmallArray + RHS.NumNonEmpty, |
| 266 | this->SmallArray + MinNonEmpty); |
| 267 | } |
| 268 | assert(this->CurArraySize == RHS.CurArraySize); |
| 269 | std::swap(this->NumNonEmpty, RHS.NumNonEmpty); |
| 270 | std::swap(this->NumTombstones, RHS.NumTombstones); |
Benjamin Kramer | e1c34e9 | 2012-03-06 20:40:02 +0000 | [diff] [blame] | 271 | } |