WPILibC++  2019.3.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
SmallPtrSet.h
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 WPIUTIL_WPI_SMALLPTRSET_H
16 #define WPIUTIL_WPI_SMALLPTRSET_H
17 
18 #include "wpi/Compiler.h"
19 #include "wpi/PointerLikeTypeTraits.h"
20 #include "wpi/type_traits.h"
21 #include <cassert>
22 #include <cstddef>
23 #include <cstdint>
24 #include <cstdlib>
25 #include <cstring>
26 #include <initializer_list>
27 #include <iterator>
28 #include <utility>
29 
30 namespace wpi {
31 
51  friend class SmallPtrSetIteratorImpl;
52 
53 protected:
55  const void **SmallArray;
58  const void **CurArray;
60  unsigned CurArraySize;
61 
65  unsigned NumNonEmpty;
67  unsigned NumTombstones;
68 
69  // Helpers to copy and move construct a SmallPtrSet.
70  SmallPtrSetImplBase(const void **SmallStorage,
71  const SmallPtrSetImplBase &that);
72  SmallPtrSetImplBase(const void **SmallStorage, unsigned SmallSize,
73  SmallPtrSetImplBase &&that);
74 
75  explicit SmallPtrSetImplBase(const void **SmallStorage, unsigned SmallSize)
76  : SmallArray(SmallStorage), CurArray(SmallStorage),
77  CurArraySize(SmallSize), NumNonEmpty(0), NumTombstones(0) {
78  assert(SmallSize && (SmallSize & (SmallSize-1)) == 0 &&
79  "Initial size must be a power of two!");
80  }
81 
83  if (!isSmall())
84  free(CurArray);
85  }
86 
87 public:
88  using size_type = unsigned;
89 
90  SmallPtrSetImplBase &operator=(const SmallPtrSetImplBase &) = delete;
91 
92  LLVM_NODISCARD bool empty() const { return size() == 0; }
93  size_type size() const { return NumNonEmpty - NumTombstones; }
94 
95  void clear() {
96  // If the capacity of the array is huge, and the # elements used is small,
97  // shrink the array.
98  if (!isSmall()) {
99  if (size() * 4 < CurArraySize && CurArraySize > 32)
100  return shrink_and_clear();
101  // Fill the array with empty markers.
102  memset(CurArray, -1, CurArraySize * sizeof(void *));
103  }
104 
105  NumNonEmpty = 0;
106  NumTombstones = 0;
107  }
108 
109 protected:
110  static void *getTombstoneMarker() { return reinterpret_cast<void*>(-2); }
111 
112  static void *getEmptyMarker() {
113  // Note that -1 is chosen to make clear() efficiently implementable with
114  // memset and because it's not a valid pointer value.
115  return reinterpret_cast<void*>(-1);
116  }
117 
118  const void **EndPointer() const {
119  return isSmall() ? CurArray + NumNonEmpty : CurArray + CurArraySize;
120  }
121 
125  std::pair<const void *const *, bool> insert_imp(const void *Ptr) {
126  if (isSmall()) {
127  // Check to see if it is already in the set.
128  const void **LastTombstone = nullptr;
129  for (const void **APtr = SmallArray, **E = SmallArray + NumNonEmpty;
130  APtr != E; ++APtr) {
131  const void *Value = *APtr;
132  if (Value == Ptr)
133  return std::make_pair(APtr, false);
134  if (Value == getTombstoneMarker())
135  LastTombstone = APtr;
136  }
137 
138  // Did we find any tombstone marker?
139  if (LastTombstone != nullptr) {
140  *LastTombstone = Ptr;
141  --NumTombstones;
142  return std::make_pair(LastTombstone, true);
143  }
144 
145  // Nope, there isn't. If we stay small, just 'pushback' now.
146  if (NumNonEmpty < CurArraySize) {
147  SmallArray[NumNonEmpty++] = Ptr;
148  return std::make_pair(SmallArray + (NumNonEmpty - 1), true);
149  }
150  // Otherwise, hit the big set case, which will call grow.
151  }
152  return insert_imp_big(Ptr);
153  }
154 
159  bool erase_imp(const void * Ptr) {
160  const void *const *P = find_imp(Ptr);
161  if (P == EndPointer())
162  return false;
163 
164  const void **Loc = const_cast<const void **>(P);
165  assert(*Loc == Ptr && "broken find!");
166  *Loc = getTombstoneMarker();
167  NumTombstones++;
168  return true;
169  }
170 
174  const void *const * find_imp(const void * Ptr) const {
175  if (isSmall()) {
176  // Linear search for the item.
177  for (const void *const *APtr = SmallArray,
178  *const *E = SmallArray + NumNonEmpty; APtr != E; ++APtr)
179  if (*APtr == Ptr)
180  return APtr;
181  return EndPointer();
182  }
183 
184  // Big set case.
185  auto *Bucket = FindBucketFor(Ptr);
186  if (*Bucket == Ptr)
187  return Bucket;
188  return EndPointer();
189  }
190 
191 private:
192  bool isSmall() const { return CurArray == SmallArray; }
193 
194  std::pair<const void *const *, bool> insert_imp_big(const void *Ptr);
195 
196  const void * const *FindBucketFor(const void *Ptr) const;
197  void shrink_and_clear();
198 
200  void Grow(unsigned NewSize);
201 
202 protected:
205  void swap(SmallPtrSetImplBase &RHS);
206 
207  void CopyFrom(const SmallPtrSetImplBase &RHS);
208  void MoveFrom(unsigned SmallSize, SmallPtrSetImplBase &&RHS);
209 
210 private:
212  void MoveHelper(unsigned SmallSize, SmallPtrSetImplBase &&RHS);
214  void CopyHelper(const SmallPtrSetImplBase &RHS);
215 };
216 
220 protected:
221  const void *const *Bucket;
222  const void *const *End;
223 
224 public:
225  explicit SmallPtrSetIteratorImpl(const void *const *BP, const void*const *E)
226  : Bucket(BP), End(E) {
228  }
229 
230  bool operator==(const SmallPtrSetIteratorImpl &RHS) const {
231  return Bucket == RHS.Bucket;
232  }
233  bool operator!=(const SmallPtrSetIteratorImpl &RHS) const {
234  return Bucket != RHS.Bucket;
235  }
236 
237 protected:
242  assert(Bucket <= End);
243  while (Bucket != End &&
244  (*Bucket == SmallPtrSetImplBase::getEmptyMarker() ||
245  *Bucket == SmallPtrSetImplBase::getTombstoneMarker()))
246  ++Bucket;
247  }
248  void RetreatIfNotValid() {
249  assert(Bucket >= End);
250  while (Bucket != End &&
251  (Bucket[-1] == SmallPtrSetImplBase::getEmptyMarker() ||
252  Bucket[-1] == SmallPtrSetImplBase::getTombstoneMarker())) {
253  --Bucket;
254  }
255  }
256 };
257 
259 template <typename PtrTy>
262 
263 public:
264  using value_type = PtrTy;
265  using reference = PtrTy;
266  using pointer = PtrTy;
267  using difference_type = std::ptrdiff_t;
268  using iterator_category = std::forward_iterator_tag;
269 
270  explicit SmallPtrSetIterator(const void *const *BP, const void *const *E)
271  : SmallPtrSetIteratorImpl(BP, E) {}
272 
273  // Most methods provided by baseclass.
274 
275  const PtrTy operator*() const {
276  assert(Bucket < End);
277  return PtrTraits::getFromVoidPointer(const_cast<void*>(*Bucket));
278  }
279 
280  inline SmallPtrSetIterator& operator++() { // Preincrement
281  ++Bucket;
283  return *this;
284  }
285 
286  SmallPtrSetIterator operator++(int) { // Postincrement
287  SmallPtrSetIterator tmp = *this;
288  ++*this;
289  return tmp;
290  }
291 };
292 
295 template<unsigned N>
297 
300 template<unsigned N, bool isPowerTwo>
302  enum { Val = N };
303 };
304 template<unsigned N>
305 struct RoundUpToPowerOfTwoH<N, false> {
306  enum {
307  // We could just use NextVal = N+1, but this converges faster. N|(N-1) sets
308  // the right-most zero bits to one all at once, e.g. 0b0011000 -> 0b0011111.
309  Val = RoundUpToPowerOfTwo<(N|(N-1)) + 1>::Val
310  };
311 };
312 
313 template<unsigned N>
314 struct RoundUpToPowerOfTwo {
315  enum { Val = RoundUpToPowerOfTwoH<N, (N&(N-1)) == 0>::Val };
316 };
317 
323 template <typename PtrType>
325  using ConstPtrType = typename add_const_past_pointer<PtrType>::type;
328 
329 protected:
330  // Constructors that forward to the base.
331  SmallPtrSetImpl(const void **SmallStorage, const SmallPtrSetImpl &that)
332  : SmallPtrSetImplBase(SmallStorage, that) {}
333  SmallPtrSetImpl(const void **SmallStorage, unsigned SmallSize,
334  SmallPtrSetImpl &&that)
335  : SmallPtrSetImplBase(SmallStorage, SmallSize, std::move(that)) {}
336  explicit SmallPtrSetImpl(const void **SmallStorage, unsigned SmallSize)
337  : SmallPtrSetImplBase(SmallStorage, SmallSize) {}
338 
339 public:
342  using key_type = ConstPtrType;
343  using value_type = PtrType;
344 
345  SmallPtrSetImpl(const SmallPtrSetImpl &) = delete;
346 
351  std::pair<iterator, bool> insert(PtrType Ptr) {
352  auto p = insert_imp(PtrTraits::getAsVoidPointer(Ptr));
353  return std::make_pair(makeIterator(p.first), p.second);
354  }
355 
358  bool erase(PtrType Ptr) {
359  return erase_imp(PtrTraits::getAsVoidPointer(Ptr));
360  }
361 
363  size_type count(ConstPtrType Ptr) const { return find(Ptr) != end() ? 1 : 0; }
364  iterator find(ConstPtrType Ptr) const {
365  return makeIterator(find_imp(ConstPtrTraits::getAsVoidPointer(Ptr)));
366  }
367 
368  template <typename IterT>
369  void insert(IterT I, IterT E) {
370  for (; I != E; ++I)
371  insert(*I);
372  }
373 
374  void insert(std::initializer_list<PtrType> IL) {
375  insert(IL.begin(), IL.end());
376  }
377 
378  iterator begin() const {
379  return makeIterator(CurArray);
380  }
381  iterator end() const { return makeIterator(EndPointer()); }
382 
383 private:
385  iterator makeIterator(const void *const *P) const {
386  return iterator(P, EndPointer());
387  }
388 };
389 
394 template<class PtrType, unsigned SmallSize>
395 class SmallPtrSet : public SmallPtrSetImpl<PtrType> {
396  // In small mode SmallPtrSet uses linear search for the elements, so it is
397  // not a good idea to choose this value too high. You may consider using a
398  // DenseSet<> instead if you expect many elements in the set.
399  static_assert(SmallSize <= 32, "SmallSize should be small");
400 
401  using BaseT = SmallPtrSetImpl<PtrType>;
402 
403  // Make sure that SmallSize is a power of two, round up if not.
404  enum { SmallSizePowTwo = RoundUpToPowerOfTwo<SmallSize>::Val };
406  const void *SmallStorage[SmallSizePowTwo];
407 
408 public:
409  SmallPtrSet() : BaseT(SmallStorage, SmallSizePowTwo) {}
410  SmallPtrSet(const SmallPtrSet &that) : BaseT(SmallStorage, that) {}
411  SmallPtrSet(SmallPtrSet &&that)
412  : BaseT(SmallStorage, SmallSizePowTwo, std::move(that)) {}
413 
414  template<typename It>
415  SmallPtrSet(It I, It E) : BaseT(SmallStorage, SmallSizePowTwo) {
416  this->insert(I, E);
417  }
418 
419  SmallPtrSet(std::initializer_list<PtrType> IL)
420  : BaseT(SmallStorage, SmallSizePowTwo) {
421  this->insert(IL.begin(), IL.end());
422  }
423 
425  operator=(const SmallPtrSet<PtrType, SmallSize> &RHS) {
426  if (&RHS != this)
427  this->CopyFrom(RHS);
428  return *this;
429  }
430 
432  operator=(SmallPtrSet<PtrType, SmallSize> &&RHS) {
433  if (&RHS != this)
434  this->MoveFrom(SmallSizePowTwo, std::move(RHS));
435  return *this;
436  }
437 
439  operator=(std::initializer_list<PtrType> IL) {
440  this->clear();
441  this->insert(IL.begin(), IL.end());
442  return *this;
443  }
444 
448  }
449 };
450 
451 } // end namespace wpi
452 
453 namespace std {
454 
456  template<class T, unsigned N>
457  inline void swap(wpi::SmallPtrSet<T, N> &LHS, wpi::SmallPtrSet<T, N> &RHS) {
458  LHS.swap(RHS);
459  }
460 
461 } // end namespace std
462 
463 #endif // LLVM_ADT_SMALLPTRSET_H
RoundUpToPowerOfTwo - This is a helper template that rounds N up to the next power of two (which mean...
Definition: SmallPtrSet.h:296
bool erase_imp(const void *Ptr)
erase_imp - If the set contains the specified pointer, remove it and return true, otherwise return fa...
Definition: SmallPtrSet.h:159
unsigned NumTombstones
Number of tombstones in CurArray.
Definition: SmallPtrSet.h:67
unsigned CurArraySize
CurArraySize - The allocated size of CurArray, always a power of two.
Definition: SmallPtrSet.h:60
Definition: optional.h:885
std::pair< const void *const *, bool > insert_imp(const void *Ptr)
insert_imp - This returns true if the pointer was new to the set, false if it was already in the set...
Definition: SmallPtrSet.h:125
const void ** SmallArray
SmallArray - Points to a fixed size set of buckets, used in 'small mode'.
Definition: SmallPtrSet.h:55
A traits type that is used to handle pointer types and things that are just wrappers for pointers as ...
Definition: PointerLikeTypeTraits.h:26
SmallPtrSetImplBase - This is the common code shared among all the SmallPtrSet<>'s, which is almost everything.
Definition: SmallPtrSet.h:50
WPILib C++ utilities (wpiutil) namespace.
Definition: SmallString.h:21
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:324
const void ** CurArray
CurArray - This is the current set of buckets.
Definition: SmallPtrSet.h:58
unsigned NumNonEmpty
Number of elements in CurArray that contain a value or are a tombstone.
Definition: SmallPtrSet.h:65
RoundUpToPowerOfTwoH - If N is not a power of two, increase it.
Definition: SmallPtrSet.h:301
void swap(SmallPtrSetImplBase &RHS)
swap - Swaps the elements of two sets.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:363
void AdvanceIfNotValid()
AdvanceIfNotValid - If the current bucket isn't valid, advance to a bucket that is.
Definition: SmallPtrSet.h:241
const void *const * find_imp(const void *Ptr) const
Returns the raw pointer needed to construct an iterator.
Definition: SmallPtrSet.h:174
SmallPtrSetIterator - This implements a const_iterator for SmallPtrSet.
Definition: SmallPtrSet.h:260
bool erase(PtrType Ptr)
erase - If the set contains the specified pointer, remove it and return true, otherwise return false...
Definition: SmallPtrSet.h:358
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:395
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:351
SmallPtrSetIteratorImpl - This is the common base class shared between all instances of SmallPtrSetIt...
Definition: SmallPtrSet.h:219