1 //===- llvm/ADT/SmallPtrSet.h - 'Normally small' pointer set ----*- C++ -*-===//
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 defines the SmallPtrSet class. See the doxygen comment for
11 // SmallPtrSetImplBase for more details on the algorithm used.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ADT_SMALLPTRSET_H
16 #define LLVM_ADT_SMALLPTRSET_H
18 #include "llvm/Config/abi-breaking.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/PointerLikeTypeTraits.h"
21 #include "llvm/Support/type_traits.h"
26 #include <initializer_list>
32 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
33 template <class T = void> struct ReverseIterate { static bool value; };
34 #if LLVM_ENABLE_REVERSE_ITERATION
35 template <class T> bool ReverseIterate<T>::value = true;
37 template <class T> bool ReverseIterate<T>::value = false;
41 /// SmallPtrSetImplBase - This is the common code shared among all the
42 /// SmallPtrSet<>'s, which is almost everything. SmallPtrSet has two modes, one
43 /// for small and one for large sets.
45 /// Small sets use an array of pointers allocated in the SmallPtrSet object,
46 /// which is treated as a simple array of pointers. When a pointer is added to
47 /// the set, the array is scanned to see if the element already exists, if not
48 /// the element is 'pushed back' onto the array. If we run out of space in the
49 /// array, we grow into the 'large set' case. SmallSet should be used when the
50 /// sets are often small. In this case, no memory allocation is used, and only
51 /// light-weight and cache-efficient scanning is used.
53 /// Large sets use a classic exponentially-probed hash table. Empty buckets are
54 /// represented with an illegal pointer value (-1) to allow null pointers to be
55 /// inserted. Tombstones are represented with another illegal pointer value
56 /// (-2), to allow deletion. The hash table is resized when the table is 3/4 or
57 /// more. When this happens, the table is doubled in size.
59 class SmallPtrSetImplBase {
60 friend class SmallPtrSetIteratorImpl;
63 /// SmallArray - Points to a fixed size set of buckets, used in 'small mode'.
64 const void **SmallArray;
65 /// CurArray - This is the current set of buckets. If equal to SmallArray,
66 /// then the set is in 'small mode'.
67 const void **CurArray;
68 /// CurArraySize - The allocated size of CurArray, always a power of two.
69 unsigned CurArraySize;
71 /// Number of elements in CurArray that contain a value or are a tombstone.
72 /// If small, all these elements are at the beginning of CurArray and the rest
75 /// Number of tombstones in CurArray.
76 unsigned NumTombstones;
78 // Helpers to copy and move construct a SmallPtrSet.
79 SmallPtrSetImplBase(const void **SmallStorage,
80 const SmallPtrSetImplBase &that);
81 SmallPtrSetImplBase(const void **SmallStorage, unsigned SmallSize,
82 SmallPtrSetImplBase &&that);
84 explicit SmallPtrSetImplBase(const void **SmallStorage, unsigned SmallSize)
85 : SmallArray(SmallStorage), CurArray(SmallStorage),
86 CurArraySize(SmallSize), NumNonEmpty(0), NumTombstones(0) {
87 assert(SmallSize && (SmallSize & (SmallSize-1)) == 0 &&
88 "Initial size must be a power of two!");
91 ~SmallPtrSetImplBase() {
97 using size_type = unsigned;
99 SmallPtrSetImplBase &operator=(const SmallPtrSetImplBase &) = delete;
101 LLVM_NODISCARD bool empty() const { return size() == 0; }
102 size_type size() const { return NumNonEmpty - NumTombstones; }
105 // If the capacity of the array is huge, and the # elements used is small,
108 if (size() * 4 < CurArraySize && CurArraySize > 32)
109 return shrink_and_clear();
110 // Fill the array with empty markers.
111 memset(CurArray, -1, CurArraySize * sizeof(void *));
119 static void *getTombstoneMarker() { return reinterpret_cast<void*>(-2); }
121 static void *getEmptyMarker() {
122 // Note that -1 is chosen to make clear() efficiently implementable with
123 // memset and because it's not a valid pointer value.
124 return reinterpret_cast<void*>(-1);
127 const void **EndPointer() const {
128 return isSmall() ? CurArray + NumNonEmpty : CurArray + CurArraySize;
131 /// insert_imp - This returns true if the pointer was new to the set, false if
132 /// it was already in the set. This is hidden from the client so that the
133 /// derived class can check that the right type of pointer is passed in.
134 std::pair<const void *const *, bool> insert_imp(const void *Ptr) {
136 // Check to see if it is already in the set.
137 const void **LastTombstone = nullptr;
138 for (const void **APtr = SmallArray, **E = SmallArray + NumNonEmpty;
140 const void *Value = *APtr;
142 return std::make_pair(APtr, false);
143 if (Value == getTombstoneMarker())
144 LastTombstone = APtr;
147 // Did we find any tombstone marker?
148 if (LastTombstone != nullptr) {
149 *LastTombstone = Ptr;
151 return std::make_pair(LastTombstone, true);
154 // Nope, there isn't. If we stay small, just 'pushback' now.
155 if (NumNonEmpty < CurArraySize) {
156 SmallArray[NumNonEmpty++] = Ptr;
157 return std::make_pair(SmallArray + (NumNonEmpty - 1), true);
159 // Otherwise, hit the big set case, which will call grow.
161 return insert_imp_big(Ptr);
164 /// erase_imp - If the set contains the specified pointer, remove it and
165 /// return true, otherwise return false. This is hidden from the client so
166 /// that the derived class can check that the right type of pointer is passed
168 bool erase_imp(const void * Ptr) {
169 const void *const *P = find_imp(Ptr);
170 if (P == EndPointer())
173 const void **Loc = const_cast<const void **>(P);
174 assert(*Loc == Ptr && "broken find!");
175 *Loc = getTombstoneMarker();
180 /// Returns the raw pointer needed to construct an iterator. If element not
181 /// found, this will be EndPointer. Otherwise, it will be a pointer to the
182 /// slot which stores Ptr;
183 const void *const * find_imp(const void * Ptr) const {
185 // Linear search for the item.
186 for (const void *const *APtr = SmallArray,
187 *const *E = SmallArray + NumNonEmpty; APtr != E; ++APtr)
194 auto *Bucket = FindBucketFor(Ptr);
201 bool isSmall() const { return CurArray == SmallArray; }
203 std::pair<const void *const *, bool> insert_imp_big(const void *Ptr);
205 const void * const *FindBucketFor(const void *Ptr) const;
206 void shrink_and_clear();
208 /// Grow - Allocate a larger backing store for the buckets and move it over.
209 void Grow(unsigned NewSize);
212 /// swap - Swaps the elements of two sets.
213 /// Note: This method assumes that both sets have the same small size.
214 void swap(SmallPtrSetImplBase &RHS);
216 void CopyFrom(const SmallPtrSetImplBase &RHS);
217 void MoveFrom(unsigned SmallSize, SmallPtrSetImplBase &&RHS);
220 /// Code shared by MoveFrom() and move constructor.
221 void MoveHelper(unsigned SmallSize, SmallPtrSetImplBase &&RHS);
222 /// Code shared by CopyFrom() and copy constructor.
223 void CopyHelper(const SmallPtrSetImplBase &RHS);
226 /// SmallPtrSetIteratorImpl - This is the common base class shared between all
227 /// instances of SmallPtrSetIterator.
228 class SmallPtrSetIteratorImpl {
230 const void *const *Bucket;
231 const void *const *End;
234 explicit SmallPtrSetIteratorImpl(const void *const *BP, const void*const *E)
235 : Bucket(BP), End(E) {
236 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
237 if (ReverseIterate<bool>::value) {
245 bool operator==(const SmallPtrSetIteratorImpl &RHS) const {
246 return Bucket == RHS.Bucket;
248 bool operator!=(const SmallPtrSetIteratorImpl &RHS) const {
249 return Bucket != RHS.Bucket;
253 /// AdvanceIfNotValid - If the current bucket isn't valid, advance to a bucket
254 /// that is. This is guaranteed to stop because the end() bucket is marked
256 void AdvanceIfNotValid() {
257 assert(Bucket <= End);
258 while (Bucket != End &&
259 (*Bucket == SmallPtrSetImplBase::getEmptyMarker() ||
260 *Bucket == SmallPtrSetImplBase::getTombstoneMarker()))
263 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
264 void RetreatIfNotValid() {
265 assert(Bucket >= End);
266 while (Bucket != End &&
267 (Bucket[-1] == SmallPtrSetImplBase::getEmptyMarker() ||
268 Bucket[-1] == SmallPtrSetImplBase::getTombstoneMarker())) {
275 /// SmallPtrSetIterator - This implements a const_iterator for SmallPtrSet.
276 template<typename PtrTy>
277 class SmallPtrSetIterator : public SmallPtrSetIteratorImpl {
278 using PtrTraits = PointerLikeTypeTraits<PtrTy>;
281 using value_type = PtrTy;
282 using reference = PtrTy;
283 using pointer = PtrTy;
284 using difference_type = std::ptrdiff_t;
285 using iterator_category = std::forward_iterator_tag;
287 explicit SmallPtrSetIterator(const void *const *BP, const void *const *E)
288 : SmallPtrSetIteratorImpl(BP, E) {}
290 // Most methods provided by baseclass.
292 const PtrTy operator*() const {
293 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
294 if (ReverseIterate<bool>::value) {
295 assert(Bucket > End);
296 return PtrTraits::getFromVoidPointer(const_cast<void *>(Bucket[-1]));
299 assert(Bucket < End);
300 return PtrTraits::getFromVoidPointer(const_cast<void*>(*Bucket));
303 inline SmallPtrSetIterator& operator++() { // Preincrement
304 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
305 if (ReverseIterate<bool>::value) {
316 SmallPtrSetIterator operator++(int) { // Postincrement
317 SmallPtrSetIterator tmp = *this;
323 /// RoundUpToPowerOfTwo - This is a helper template that rounds N up to the next
324 /// power of two (which means N itself if N is already a power of two).
326 struct RoundUpToPowerOfTwo;
328 /// RoundUpToPowerOfTwoH - If N is not a power of two, increase it. This is a
329 /// helper template used to implement RoundUpToPowerOfTwo.
330 template<unsigned N, bool isPowerTwo>
331 struct RoundUpToPowerOfTwoH {
335 struct RoundUpToPowerOfTwoH<N, false> {
337 // We could just use NextVal = N+1, but this converges faster. N|(N-1) sets
338 // the right-most zero bits to one all at once, e.g. 0b0011000 -> 0b0011111.
339 Val = RoundUpToPowerOfTwo<(N|(N-1)) + 1>::Val
344 struct RoundUpToPowerOfTwo {
345 enum { Val = RoundUpToPowerOfTwoH<N, (N&(N-1)) == 0>::Val };
348 /// \brief A templated base class for \c SmallPtrSet which provides the
349 /// typesafe interface that is common across all small sizes.
351 /// This is particularly useful for passing around between interface boundaries
352 /// to avoid encoding a particular small size in the interface boundary.
353 template <typename PtrType>
354 class SmallPtrSetImpl : public SmallPtrSetImplBase {
355 using ConstPtrType = typename add_const_past_pointer<PtrType>::type;
356 using PtrTraits = PointerLikeTypeTraits<PtrType>;
357 using ConstPtrTraits = PointerLikeTypeTraits<ConstPtrType>;
360 // Constructors that forward to the base.
361 SmallPtrSetImpl(const void **SmallStorage, const SmallPtrSetImpl &that)
362 : SmallPtrSetImplBase(SmallStorage, that) {}
363 SmallPtrSetImpl(const void **SmallStorage, unsigned SmallSize,
364 SmallPtrSetImpl &&that)
365 : SmallPtrSetImplBase(SmallStorage, SmallSize, std::move(that)) {}
366 explicit SmallPtrSetImpl(const void **SmallStorage, unsigned SmallSize)
367 : SmallPtrSetImplBase(SmallStorage, SmallSize) {}
370 using iterator = SmallPtrSetIterator<PtrType>;
371 using const_iterator = SmallPtrSetIterator<PtrType>;
372 using key_type = ConstPtrType;
373 using value_type = PtrType;
375 SmallPtrSetImpl(const SmallPtrSetImpl &) = delete;
377 /// Inserts Ptr if and only if there is no element in the container equal to
378 /// Ptr. The bool component of the returned pair is true if and only if the
379 /// insertion takes place, and the iterator component of the pair points to
380 /// the element equal to Ptr.
381 std::pair<iterator, bool> insert(PtrType Ptr) {
382 auto p = insert_imp(PtrTraits::getAsVoidPointer(Ptr));
383 return std::make_pair(makeIterator(p.first), p.second);
386 /// erase - If the set contains the specified pointer, remove it and return
387 /// true, otherwise return false.
388 bool erase(PtrType Ptr) {
389 return erase_imp(PtrTraits::getAsVoidPointer(Ptr));
391 /// count - Return 1 if the specified pointer is in the set, 0 otherwise.
392 size_type count(ConstPtrType Ptr) const { return find(Ptr) != end() ? 1 : 0; }
393 iterator find(ConstPtrType Ptr) const {
394 return makeIterator(find_imp(ConstPtrTraits::getAsVoidPointer(Ptr)));
397 template <typename IterT>
398 void insert(IterT I, IterT E) {
403 void insert(std::initializer_list<PtrType> IL) {
404 insert(IL.begin(), IL.end());
407 iterator begin() const {
408 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
409 if (ReverseIterate<bool>::value)
410 return makeIterator(EndPointer() - 1);
412 return makeIterator(CurArray);
414 iterator end() const { return makeIterator(EndPointer()); }
417 /// Create an iterator that dereferences to same place as the given pointer.
418 iterator makeIterator(const void *const *P) const {
419 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
420 if (ReverseIterate<bool>::value)
421 return iterator(P == EndPointer() ? CurArray : P + 1, CurArray);
423 return iterator(P, EndPointer());
427 /// SmallPtrSet - This class implements a set which is optimized for holding
428 /// SmallSize or less elements. This internally rounds up SmallSize to the next
429 /// power of two if it is not already a power of two. See the comments above
430 /// SmallPtrSetImplBase for details of the algorithm.
431 template<class PtrType, unsigned SmallSize>
432 class SmallPtrSet : public SmallPtrSetImpl<PtrType> {
433 // In small mode SmallPtrSet uses linear search for the elements, so it is
434 // not a good idea to choose this value too high. You may consider using a
435 // DenseSet<> instead if you expect many elements in the set.
436 static_assert(SmallSize <= 32, "SmallSize should be small");
438 using BaseT = SmallPtrSetImpl<PtrType>;
440 // Make sure that SmallSize is a power of two, round up if not.
441 enum { SmallSizePowTwo = RoundUpToPowerOfTwo<SmallSize>::Val };
442 /// SmallStorage - Fixed size storage used in 'small mode'.
443 const void *SmallStorage[SmallSizePowTwo];
446 SmallPtrSet() : BaseT(SmallStorage, SmallSizePowTwo) {}
447 SmallPtrSet(const SmallPtrSet &that) : BaseT(SmallStorage, that) {}
448 SmallPtrSet(SmallPtrSet &&that)
449 : BaseT(SmallStorage, SmallSizePowTwo, std::move(that)) {}
451 template<typename It>
452 SmallPtrSet(It I, It E) : BaseT(SmallStorage, SmallSizePowTwo) {
456 SmallPtrSet(std::initializer_list<PtrType> IL)
457 : BaseT(SmallStorage, SmallSizePowTwo) {
458 this->insert(IL.begin(), IL.end());
461 SmallPtrSet<PtrType, SmallSize> &
462 operator=(const SmallPtrSet<PtrType, SmallSize> &RHS) {
468 SmallPtrSet<PtrType, SmallSize> &
469 operator=(SmallPtrSet<PtrType, SmallSize> &&RHS) {
471 this->MoveFrom(SmallSizePowTwo, std::move(RHS));
475 SmallPtrSet<PtrType, SmallSize> &
476 operator=(std::initializer_list<PtrType> IL) {
478 this->insert(IL.begin(), IL.end());
482 /// swap - Swaps the elements of two sets.
483 void swap(SmallPtrSet<PtrType, SmallSize> &RHS) {
484 SmallPtrSetImplBase::swap(RHS);
488 } // end namespace llvm
492 /// Implement std::swap in terms of SmallPtrSet swap.
493 template<class T, unsigned N>
494 inline void swap(llvm::SmallPtrSet<T, N> &LHS, llvm::SmallPtrSet<T, N> &RHS) {
498 } // end namespace std
500 #endif // LLVM_ADT_SMALLPTRSET_H