• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- llvm/ADT/SmallPtrSet.h - 'Normally small' pointer set ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the SmallPtrSet class.  See the doxygen comment for
11 // SmallPtrSetImplBase for more details on the algorithm used.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_ADT_SMALLPTRSET_H
16 #define LLVM_ADT_SMALLPTRSET_H
17 
18 #include "llvm/Config/abi-breaking.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/PointerLikeTypeTraits.h"
21 #include <cassert>
22 #include <cstddef>
23 #include <cstring>
24 #include <cstdlib>
25 #include <initializer_list>
26 #include <iterator>
27 #include <utility>
28 
29 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
30 namespace llvm {
31 template <class T = void> struct ReverseIterate { static bool value; };
32 template <class T> bool ReverseIterate<T>::value = false;
33 }
34 #endif
35 
36 namespace llvm {
37 
38 /// SmallPtrSetImplBase - This is the common code shared among all the
39 /// SmallPtrSet<>'s, which is almost everything.  SmallPtrSet has two modes, one
40 /// for small and one for large sets.
41 ///
42 /// Small sets use an array of pointers allocated in the SmallPtrSet object,
43 /// which is treated as a simple array of pointers.  When a pointer is added to
44 /// the set, the array is scanned to see if the element already exists, if not
45 /// the element is 'pushed back' onto the array.  If we run out of space in the
46 /// array, we grow into the 'large set' case.  SmallSet should be used when the
47 /// sets are often small.  In this case, no memory allocation is used, and only
48 /// light-weight and cache-efficient scanning is used.
49 ///
50 /// Large sets use a classic exponentially-probed hash table.  Empty buckets are
51 /// represented with an illegal pointer value (-1) to allow null pointers to be
52 /// inserted.  Tombstones are represented with another illegal pointer value
53 /// (-2), to allow deletion.  The hash table is resized when the table is 3/4 or
54 /// more.  When this happens, the table is doubled in size.
55 ///
56 class SmallPtrSetImplBase {
57   friend class SmallPtrSetIteratorImpl;
58 
59 protected:
60   /// SmallArray - Points to a fixed size set of buckets, used in 'small mode'.
61   const void **SmallArray;
62   /// CurArray - This is the current set of buckets.  If equal to SmallArray,
63   /// then the set is in 'small mode'.
64   const void **CurArray;
65   /// CurArraySize - The allocated size of CurArray, always a power of two.
66   unsigned CurArraySize;
67 
68   /// Number of elements in CurArray that contain a value or are a tombstone.
69   /// If small, all these elements are at the beginning of CurArray and the rest
70   /// is uninitialized.
71   unsigned NumNonEmpty;
72   /// Number of tombstones in CurArray.
73   unsigned NumTombstones;
74 
75   // Helpers to copy and move construct a SmallPtrSet.
76   SmallPtrSetImplBase(const void **SmallStorage,
77                       const SmallPtrSetImplBase &that);
78   SmallPtrSetImplBase(const void **SmallStorage, unsigned SmallSize,
79                       SmallPtrSetImplBase &&that);
80 
SmallPtrSetImplBase(const void ** SmallStorage,unsigned SmallSize)81   explicit SmallPtrSetImplBase(const void **SmallStorage, unsigned SmallSize)
82       : SmallArray(SmallStorage), CurArray(SmallStorage),
83         CurArraySize(SmallSize), NumNonEmpty(0), NumTombstones(0) {
84     assert(SmallSize && (SmallSize & (SmallSize-1)) == 0 &&
85            "Initial size must be a power of two!");
86   }
87 
~SmallPtrSetImplBase()88   ~SmallPtrSetImplBase() {
89     if (!isSmall())
90       free(CurArray);
91   }
92 
93 public:
94   typedef unsigned size_type;
95 
96   SmallPtrSetImplBase &operator=(const SmallPtrSetImplBase &) = delete;
97 
empty()98   LLVM_NODISCARD bool empty() const { return size() == 0; }
size()99   size_type size() const { return NumNonEmpty - NumTombstones; }
100 
clear()101   void clear() {
102     // If the capacity of the array is huge, and the # elements used is small,
103     // shrink the array.
104     if (!isSmall()) {
105       if (size() * 4 < CurArraySize && CurArraySize > 32)
106         return shrink_and_clear();
107       // Fill the array with empty markers.
108       memset(CurArray, -1, CurArraySize * sizeof(void *));
109     }
110 
111     NumNonEmpty = 0;
112     NumTombstones = 0;
113   }
114 
115 protected:
getTombstoneMarker()116   static void *getTombstoneMarker() { return reinterpret_cast<void*>(-2); }
117 
getEmptyMarker()118   static void *getEmptyMarker() {
119     // Note that -1 is chosen to make clear() efficiently implementable with
120     // memset and because it's not a valid pointer value.
121     return reinterpret_cast<void*>(-1);
122   }
123 
EndPointer()124   const void **EndPointer() const {
125     return isSmall() ? CurArray + NumNonEmpty : CurArray + CurArraySize;
126   }
127 
128   /// insert_imp - This returns true if the pointer was new to the set, false if
129   /// it was already in the set.  This is hidden from the client so that the
130   /// derived class can check that the right type of pointer is passed in.
insert_imp(const void * Ptr)131   std::pair<const void *const *, bool> insert_imp(const void *Ptr) {
132     if (isSmall()) {
133       // Check to see if it is already in the set.
134       const void **LastTombstone = nullptr;
135       for (const void **APtr = SmallArray, **E = SmallArray + NumNonEmpty;
136            APtr != E; ++APtr) {
137         const void *Value = *APtr;
138         if (Value == Ptr)
139           return std::make_pair(APtr, false);
140         if (Value == getTombstoneMarker())
141           LastTombstone = APtr;
142       }
143 
144       // Did we find any tombstone marker?
145       if (LastTombstone != nullptr) {
146         *LastTombstone = Ptr;
147         --NumTombstones;
148         return std::make_pair(LastTombstone, true);
149       }
150 
151       // Nope, there isn't.  If we stay small, just 'pushback' now.
152       if (NumNonEmpty < CurArraySize) {
153         SmallArray[NumNonEmpty++] = Ptr;
154         return std::make_pair(SmallArray + (NumNonEmpty - 1), true);
155       }
156       // Otherwise, hit the big set case, which will call grow.
157     }
158     return insert_imp_big(Ptr);
159   }
160 
161   /// erase_imp - If the set contains the specified pointer, remove it and
162   /// return true, otherwise return false.  This is hidden from the client so
163   /// that the derived class can check that the right type of pointer is passed
164   /// in.
165   bool erase_imp(const void * Ptr);
166 
count_imp(const void * Ptr)167   bool count_imp(const void * Ptr) const {
168     if (isSmall()) {
169       // Linear search for the item.
170       for (const void *const *APtr = SmallArray,
171                       *const *E = SmallArray + NumNonEmpty; APtr != E; ++APtr)
172         if (*APtr == Ptr)
173           return true;
174       return false;
175     }
176 
177     // Big set case.
178     return *FindBucketFor(Ptr) == Ptr;
179   }
180 
181 private:
isSmall()182   bool isSmall() const { return CurArray == SmallArray; }
183 
184   std::pair<const void *const *, bool> insert_imp_big(const void *Ptr);
185 
186   const void * const *FindBucketFor(const void *Ptr) const;
187   void shrink_and_clear();
188 
189   /// Grow - Allocate a larger backing store for the buckets and move it over.
190   void Grow(unsigned NewSize);
191 
192 protected:
193   /// swap - Swaps the elements of two sets.
194   /// Note: This method assumes that both sets have the same small size.
195   void swap(SmallPtrSetImplBase &RHS);
196 
197   void CopyFrom(const SmallPtrSetImplBase &RHS);
198   void MoveFrom(unsigned SmallSize, SmallPtrSetImplBase &&RHS);
199 
200 private:
201   /// Code shared by MoveFrom() and move constructor.
202   void MoveHelper(unsigned SmallSize, SmallPtrSetImplBase &&RHS);
203   /// Code shared by CopyFrom() and copy constructor.
204   void CopyHelper(const SmallPtrSetImplBase &RHS);
205 };
206 
207 /// SmallPtrSetIteratorImpl - This is the common base class shared between all
208 /// instances of SmallPtrSetIterator.
209 class SmallPtrSetIteratorImpl {
210 protected:
211   const void *const *Bucket;
212   const void *const *End;
213 
214 public:
SmallPtrSetIteratorImpl(const void * const * BP,const void * const * E)215   explicit SmallPtrSetIteratorImpl(const void *const *BP, const void*const *E)
216     : Bucket(BP), End(E) {
217 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
218     if (ReverseIterate<bool>::value) {
219       RetreatIfNotValid();
220       return;
221     }
222 #endif
223     AdvanceIfNotValid();
224   }
225 
226   bool operator==(const SmallPtrSetIteratorImpl &RHS) const {
227     return Bucket == RHS.Bucket;
228   }
229   bool operator!=(const SmallPtrSetIteratorImpl &RHS) const {
230     return Bucket != RHS.Bucket;
231   }
232 
233 protected:
234   /// AdvanceIfNotValid - If the current bucket isn't valid, advance to a bucket
235   /// that is.   This is guaranteed to stop because the end() bucket is marked
236   /// valid.
AdvanceIfNotValid()237   void AdvanceIfNotValid() {
238     assert(Bucket <= End);
239     while (Bucket != End &&
240            (*Bucket == SmallPtrSetImplBase::getEmptyMarker() ||
241             *Bucket == SmallPtrSetImplBase::getTombstoneMarker()))
242       ++Bucket;
243   }
244 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
RetreatIfNotValid()245   void RetreatIfNotValid() {
246     --Bucket;
247     assert(Bucket <= End);
248     while (Bucket != End &&
249            (*Bucket == SmallPtrSetImplBase::getEmptyMarker() ||
250             *Bucket == SmallPtrSetImplBase::getTombstoneMarker())) {
251       --Bucket;
252     }
253   }
254 #endif
255 };
256 
257 /// SmallPtrSetIterator - This implements a const_iterator for SmallPtrSet.
258 template<typename PtrTy>
259 class SmallPtrSetIterator : public SmallPtrSetIteratorImpl {
260   typedef PointerLikeTypeTraits<PtrTy> PtrTraits;
261 
262 public:
263   typedef PtrTy                     value_type;
264   typedef PtrTy                     reference;
265   typedef PtrTy                     pointer;
266   typedef std::ptrdiff_t            difference_type;
267   typedef std::forward_iterator_tag iterator_category;
268 
SmallPtrSetIterator(const void * const * BP,const void * const * E)269   explicit SmallPtrSetIterator(const void *const *BP, const void *const *E)
270     : SmallPtrSetIteratorImpl(BP, E) {}
271 
272   // Most methods provided by baseclass.
273 
274   const PtrTy operator*() const {
275     assert(Bucket < End);
276     return PtrTraits::getFromVoidPointer(const_cast<void*>(*Bucket));
277   }
278 
279   inline SmallPtrSetIterator& operator++() {          // Preincrement
280 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
281     if (ReverseIterate<bool>::value) {
282       RetreatIfNotValid();
283       return *this;
284     }
285 #endif
286     ++Bucket;
287     AdvanceIfNotValid();
288     return *this;
289   }
290 
291   SmallPtrSetIterator operator++(int) {        // Postincrement
292     SmallPtrSetIterator tmp = *this;
293 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
294     if (ReverseIterate<bool>::value) {
295       --*this;
296       return tmp;
297     }
298 #endif
299     ++*this;
300     return tmp;
301   }
302 };
303 
304 /// RoundUpToPowerOfTwo - This is a helper template that rounds N up to the next
305 /// power of two (which means N itself if N is already a power of two).
306 template<unsigned N>
307 struct RoundUpToPowerOfTwo;
308 
309 /// RoundUpToPowerOfTwoH - If N is not a power of two, increase it.  This is a
310 /// helper template used to implement RoundUpToPowerOfTwo.
311 template<unsigned N, bool isPowerTwo>
312 struct RoundUpToPowerOfTwoH {
313   enum { Val = N };
314 };
315 template<unsigned N>
316 struct RoundUpToPowerOfTwoH<N, false> {
317   enum {
318     // We could just use NextVal = N+1, but this converges faster.  N|(N-1) sets
319     // the right-most zero bits to one all at once, e.g. 0b0011000 -> 0b0011111.
320     Val = RoundUpToPowerOfTwo<(N|(N-1)) + 1>::Val
321   };
322 };
323 
324 template<unsigned N>
325 struct RoundUpToPowerOfTwo {
326   enum { Val = RoundUpToPowerOfTwoH<N, (N&(N-1)) == 0>::Val };
327 };
328 
329 /// \brief A templated base class for \c SmallPtrSet which provides the
330 /// typesafe interface that is common across all small sizes.
331 ///
332 /// This is particularly useful for passing around between interface boundaries
333 /// to avoid encoding a particular small size in the interface boundary.
334 template <typename PtrType>
335 class SmallPtrSetImpl : public SmallPtrSetImplBase {
336   typedef PointerLikeTypeTraits<PtrType> PtrTraits;
337 
338 protected:
339   // Constructors that forward to the base.
340   SmallPtrSetImpl(const void **SmallStorage, const SmallPtrSetImpl &that)
341       : SmallPtrSetImplBase(SmallStorage, that) {}
342   SmallPtrSetImpl(const void **SmallStorage, unsigned SmallSize,
343                   SmallPtrSetImpl &&that)
344       : SmallPtrSetImplBase(SmallStorage, SmallSize, std::move(that)) {}
345   explicit SmallPtrSetImpl(const void **SmallStorage, unsigned SmallSize)
346       : SmallPtrSetImplBase(SmallStorage, SmallSize) {}
347 
348 public:
349   typedef SmallPtrSetIterator<PtrType> iterator;
350   typedef SmallPtrSetIterator<PtrType> const_iterator;
351 
352   SmallPtrSetImpl(const SmallPtrSetImpl &) = delete;
353 
354   /// Inserts Ptr if and only if there is no element in the container equal to
355   /// Ptr. The bool component of the returned pair is true if and only if the
356   /// insertion takes place, and the iterator component of the pair points to
357   /// the element equal to Ptr.
358   std::pair<iterator, bool> insert(PtrType Ptr) {
359     auto p = insert_imp(PtrTraits::getAsVoidPointer(Ptr));
360     return std::make_pair(iterator(p.first, EndPointer()), p.second);
361   }
362 
363   /// erase - If the set contains the specified pointer, remove it and return
364   /// true, otherwise return false.
365   bool erase(PtrType Ptr) {
366     return erase_imp(PtrTraits::getAsVoidPointer(Ptr));
367   }
368 
369   /// count - Return 1 if the specified pointer is in the set, 0 otherwise.
370   size_type count(PtrType Ptr) const {
371     return count_imp(PtrTraits::getAsVoidPointer(Ptr)) ? 1 : 0;
372   }
373 
374   template <typename IterT>
375   void insert(IterT I, IterT E) {
376     for (; I != E; ++I)
377       insert(*I);
378   }
379 
380   void insert(std::initializer_list<PtrType> IL) {
381     insert(IL.begin(), IL.end());
382   }
383 
384   inline iterator begin() const {
385 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
386     if (ReverseIterate<bool>::value)
387       return endPtr();
388 #endif
389     return iterator(CurArray, EndPointer());
390   }
391   inline iterator end() const {
392 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
393     if (ReverseIterate<bool>::value)
394       return iterator(CurArray, CurArray);
395 #endif
396     return endPtr();
397   }
398 
399 private:
400   inline iterator endPtr() const {
401     const void *const *End = EndPointer();
402     return iterator(End, End);
403   }
404 };
405 
406 /// SmallPtrSet - This class implements a set which is optimized for holding
407 /// SmallSize or less elements.  This internally rounds up SmallSize to the next
408 /// power of two if it is not already a power of two.  See the comments above
409 /// SmallPtrSetImplBase for details of the algorithm.
410 template<class PtrType, unsigned SmallSize>
411 class SmallPtrSet : public SmallPtrSetImpl<PtrType> {
412   // In small mode SmallPtrSet uses linear search for the elements, so it is
413   // not a good idea to choose this value too high. You may consider using a
414   // DenseSet<> instead if you expect many elements in the set.
415   static_assert(SmallSize <= 32, "SmallSize should be small");
416 
417   typedef SmallPtrSetImpl<PtrType> BaseT;
418 
419   // Make sure that SmallSize is a power of two, round up if not.
420   enum { SmallSizePowTwo = RoundUpToPowerOfTwo<SmallSize>::Val };
421   /// SmallStorage - Fixed size storage used in 'small mode'.
422   const void *SmallStorage[SmallSizePowTwo];
423 
424 public:
425   SmallPtrSet() : BaseT(SmallStorage, SmallSizePowTwo) {}
426   SmallPtrSet(const SmallPtrSet &that) : BaseT(SmallStorage, that) {}
427   SmallPtrSet(SmallPtrSet &&that)
428       : BaseT(SmallStorage, SmallSizePowTwo, std::move(that)) {}
429 
430   template<typename It>
431   SmallPtrSet(It I, It E) : BaseT(SmallStorage, SmallSizePowTwo) {
432     this->insert(I, E);
433   }
434 
435   SmallPtrSet(std::initializer_list<PtrType> IL)
436       : BaseT(SmallStorage, SmallSizePowTwo) {
437     this->insert(IL.begin(), IL.end());
438   }
439 
440   SmallPtrSet<PtrType, SmallSize> &
441   operator=(const SmallPtrSet<PtrType, SmallSize> &RHS) {
442     if (&RHS != this)
443       this->CopyFrom(RHS);
444     return *this;
445   }
446 
447   SmallPtrSet<PtrType, SmallSize> &
448   operator=(SmallPtrSet<PtrType, SmallSize> &&RHS) {
449     if (&RHS != this)
450       this->MoveFrom(SmallSizePowTwo, std::move(RHS));
451     return *this;
452   }
453 
454   SmallPtrSet<PtrType, SmallSize> &
455   operator=(std::initializer_list<PtrType> IL) {
456     this->clear();
457     this->insert(IL.begin(), IL.end());
458     return *this;
459   }
460 
461   /// swap - Swaps the elements of two sets.
462   void swap(SmallPtrSet<PtrType, SmallSize> &RHS) {
463     SmallPtrSetImplBase::swap(RHS);
464   }
465 };
466 
467 } // end namespace llvm
468 
469 namespace std {
470 
471   /// Implement std::swap in terms of SmallPtrSet swap.
472   template<class T, unsigned N>
473   inline void swap(llvm::SmallPtrSet<T, N> &LHS, llvm::SmallPtrSet<T, N> &RHS) {
474     LHS.swap(RHS);
475   }
476 
477 } // end namespace std
478 
479 #endif // LLVM_ADT_SMALLPTRSET_H
480