Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

307 lines
10 KiB

  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. // SmallPtrSetImpl for more details on the algorithm used.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_ADT_SMALLPTRSET_H
  15. #define LLVM_ADT_SMALLPTRSET_H
  16. #include "llvm/Support/Compiler.h"
  17. #include "llvm/Support/DataTypes.h"
  18. #include "llvm/Support/PointerLikeTypeTraits.h"
  19. #include <cassert>
  20. #include <cstddef>
  21. #include <cstring>
  22. #include <iterator>
  23. namespace llvm {
  24. class SmallPtrSetIteratorImpl;
  25. /// SmallPtrSetImpl - This is the common code shared among all the
  26. /// SmallPtrSet<>'s, which is almost everything. SmallPtrSet has two modes, one
  27. /// for small and one for large sets.
  28. ///
  29. /// Small sets use an array of pointers allocated in the SmallPtrSet object,
  30. /// which is treated as a simple array of pointers. When a pointer is added to
  31. /// the set, the array is scanned to see if the element already exists, if not
  32. /// the element is 'pushed back' onto the array. If we run out of space in the
  33. /// array, we grow into the 'large set' case. SmallSet should be used when the
  34. /// sets are often small. In this case, no memory allocation is used, and only
  35. /// light-weight and cache-efficient scanning is used.
  36. ///
  37. /// Large sets use a classic exponentially-probed hash table. Empty buckets are
  38. /// represented with an illegal pointer value (-1) to allow null pointers to be
  39. /// inserted. Tombstones are represented with another illegal pointer value
  40. /// (-2), to allow deletion. The hash table is resized when the table is 3/4 or
  41. /// more. When this happens, the table is doubled in size.
  42. ///
  43. class SmallPtrSetImpl {
  44. friend class SmallPtrSetIteratorImpl;
  45. protected:
  46. /// SmallArray - Points to a fixed size set of buckets, used in 'small mode'.
  47. const void **SmallArray;
  48. /// CurArray - This is the current set of buckets. If equal to SmallArray,
  49. /// then the set is in 'small mode'.
  50. const void **CurArray;
  51. /// CurArraySize - The allocated size of CurArray, always a power of two.
  52. unsigned CurArraySize;
  53. // If small, this is # elts allocated consecutively
  54. unsigned NumElements;
  55. unsigned NumTombstones;
  56. // Helper to copy construct a SmallPtrSet.
  57. SmallPtrSetImpl(const void **SmallStorage, const SmallPtrSetImpl& that);
  58. explicit SmallPtrSetImpl(const void **SmallStorage, unsigned SmallSize) :
  59. SmallArray(SmallStorage), CurArray(SmallStorage), CurArraySize(SmallSize) {
  60. assert(SmallSize && (SmallSize & (SmallSize-1)) == 0 &&
  61. "Initial size must be a power of two!");
  62. clear();
  63. }
  64. ~SmallPtrSetImpl();
  65. public:
  66. bool empty() const { return size() == 0; }
  67. unsigned size() const { return NumElements; }
  68. void clear() {
  69. // If the capacity of the array is huge, and the # elements used is small,
  70. // shrink the array.
  71. if (!isSmall() && NumElements*4 < CurArraySize && CurArraySize > 32)
  72. return shrink_and_clear();
  73. // Fill the array with empty markers.
  74. memset(CurArray, -1, CurArraySize*sizeof(void*));
  75. NumElements = 0;
  76. NumTombstones = 0;
  77. }
  78. protected:
  79. static void *getTombstoneMarker() { return reinterpret_cast<void*>(-2); }
  80. static void *getEmptyMarker() {
  81. // Note that -1 is chosen to make clear() efficiently implementable with
  82. // memset and because it's not a valid pointer value.
  83. return reinterpret_cast<void*>(-1);
  84. }
  85. /// insert_imp - This returns true if the pointer was new to the set, false if
  86. /// it was already in the set. This is hidden from the client so that the
  87. /// derived class can check that the right type of pointer is passed in.
  88. bool insert_imp(const void * Ptr);
  89. /// erase_imp - If the set contains the specified pointer, remove it and
  90. /// return true, otherwise return false. This is hidden from the client so
  91. /// that the derived class can check that the right type of pointer is passed
  92. /// in.
  93. bool erase_imp(const void * Ptr);
  94. bool count_imp(const void * Ptr) const {
  95. if (isSmall()) {
  96. // Linear search for the item.
  97. for (const void *const *APtr = SmallArray,
  98. *const *E = SmallArray+NumElements; APtr != E; ++APtr)
  99. if (*APtr == Ptr)
  100. return true;
  101. return false;
  102. }
  103. // Big set case.
  104. return *FindBucketFor(Ptr) == Ptr;
  105. }
  106. private:
  107. bool isSmall() const { return CurArray == SmallArray; }
  108. const void * const *FindBucketFor(const void *Ptr) const;
  109. void shrink_and_clear();
  110. /// Grow - Allocate a larger backing store for the buckets and move it over.
  111. void Grow(unsigned NewSize);
  112. void operator=(const SmallPtrSetImpl &RHS) LLVM_DELETED_FUNCTION;
  113. protected:
  114. /// swap - Swaps the elements of two sets.
  115. /// Note: This method assumes that both sets have the same small size.
  116. void swap(SmallPtrSetImpl &RHS);
  117. void CopyFrom(const SmallPtrSetImpl &RHS);
  118. };
  119. /// SmallPtrSetIteratorImpl - This is the common base class shared between all
  120. /// instances of SmallPtrSetIterator.
  121. class SmallPtrSetIteratorImpl {
  122. protected:
  123. const void *const *Bucket;
  124. const void *const *End;
  125. public:
  126. explicit SmallPtrSetIteratorImpl(const void *const *BP, const void*const *E)
  127. : Bucket(BP), End(E) {
  128. AdvanceIfNotValid();
  129. }
  130. bool operator==(const SmallPtrSetIteratorImpl &RHS) const {
  131. return Bucket == RHS.Bucket;
  132. }
  133. bool operator!=(const SmallPtrSetIteratorImpl &RHS) const {
  134. return Bucket != RHS.Bucket;
  135. }
  136. protected:
  137. /// AdvanceIfNotValid - If the current bucket isn't valid, advance to a bucket
  138. /// that is. This is guaranteed to stop because the end() bucket is marked
  139. /// valid.
  140. void AdvanceIfNotValid() {
  141. assert(Bucket <= End);
  142. while (Bucket != End &&
  143. (*Bucket == SmallPtrSetImpl::getEmptyMarker() ||
  144. *Bucket == SmallPtrSetImpl::getTombstoneMarker()))
  145. ++Bucket;
  146. }
  147. };
  148. /// SmallPtrSetIterator - This implements a const_iterator for SmallPtrSet.
  149. template<typename PtrTy>
  150. class SmallPtrSetIterator : public SmallPtrSetIteratorImpl {
  151. typedef PointerLikeTypeTraits<PtrTy> PtrTraits;
  152. public:
  153. typedef PtrTy value_type;
  154. typedef PtrTy reference;
  155. typedef PtrTy pointer;
  156. typedef std::ptrdiff_t difference_type;
  157. typedef std::forward_iterator_tag iterator_category;
  158. explicit SmallPtrSetIterator(const void *const *BP, const void *const *E)
  159. : SmallPtrSetIteratorImpl(BP, E) {}
  160. // Most methods provided by baseclass.
  161. const PtrTy operator*() const {
  162. assert(Bucket < End);
  163. return PtrTraits::getFromVoidPointer(const_cast<void*>(*Bucket));
  164. }
  165. inline SmallPtrSetIterator& operator++() { // Preincrement
  166. ++Bucket;
  167. AdvanceIfNotValid();
  168. return *this;
  169. }
  170. SmallPtrSetIterator operator++(int) { // Postincrement
  171. SmallPtrSetIterator tmp = *this; ++*this; return tmp;
  172. }
  173. };
  174. /// RoundUpToPowerOfTwo - This is a helper template that rounds N up to the next
  175. /// power of two (which means N itself if N is already a power of two).
  176. template<unsigned N>
  177. struct RoundUpToPowerOfTwo;
  178. /// RoundUpToPowerOfTwoH - If N is not a power of two, increase it. This is a
  179. /// helper template used to implement RoundUpToPowerOfTwo.
  180. template<unsigned N, bool isPowerTwo>
  181. struct RoundUpToPowerOfTwoH {
  182. enum { Val = N };
  183. };
  184. template<unsigned N>
  185. struct RoundUpToPowerOfTwoH<N, false> {
  186. enum {
  187. // We could just use NextVal = N+1, but this converges faster. N|(N-1) sets
  188. // the right-most zero bits to one all at once, e.g. 0b0011000 -> 0b0011111.
  189. Val = RoundUpToPowerOfTwo<(N|(N-1)) + 1>::Val
  190. };
  191. };
  192. template<unsigned N>
  193. struct RoundUpToPowerOfTwo {
  194. enum { Val = RoundUpToPowerOfTwoH<N, (N&(N-1)) == 0>::Val };
  195. };
  196. /// SmallPtrSet - This class implements a set which is optimized for holding
  197. /// SmallSize or less elements. This internally rounds up SmallSize to the next
  198. /// power of two if it is not already a power of two. See the comments above
  199. /// SmallPtrSetImpl for details of the algorithm.
  200. template<class PtrType, unsigned SmallSize>
  201. class SmallPtrSet : public SmallPtrSetImpl {
  202. // Make sure that SmallSize is a power of two, round up if not.
  203. enum { SmallSizePowTwo = RoundUpToPowerOfTwo<SmallSize>::Val };
  204. /// SmallStorage - Fixed size storage used in 'small mode'.
  205. const void *SmallStorage[SmallSizePowTwo];
  206. typedef PointerLikeTypeTraits<PtrType> PtrTraits;
  207. public:
  208. SmallPtrSet() : SmallPtrSetImpl(SmallStorage, SmallSizePowTwo) {}
  209. SmallPtrSet(const SmallPtrSet &that) : SmallPtrSetImpl(SmallStorage, that) {}
  210. template<typename It>
  211. SmallPtrSet(It I, It E) : SmallPtrSetImpl(SmallStorage, SmallSizePowTwo) {
  212. insert(I, E);
  213. }
  214. /// insert - This returns true if the pointer was new to the set, false if it
  215. /// was already in the set.
  216. bool insert(PtrType Ptr) {
  217. return insert_imp(PtrTraits::getAsVoidPointer(Ptr));
  218. }
  219. /// erase - If the set contains the specified pointer, remove it and return
  220. /// true, otherwise return false.
  221. bool erase(PtrType Ptr) {
  222. return erase_imp(PtrTraits::getAsVoidPointer(Ptr));
  223. }
  224. /// count - Return true if the specified pointer is in the set.
  225. bool count(PtrType Ptr) const {
  226. return count_imp(PtrTraits::getAsVoidPointer(Ptr));
  227. }
  228. template <typename IterT>
  229. void insert(IterT I, IterT E) {
  230. for (; I != E; ++I)
  231. insert(*I);
  232. }
  233. typedef SmallPtrSetIterator<PtrType> iterator;
  234. typedef SmallPtrSetIterator<PtrType> const_iterator;
  235. inline iterator begin() const {
  236. return iterator(CurArray, CurArray+CurArraySize);
  237. }
  238. inline iterator end() const {
  239. return iterator(CurArray+CurArraySize, CurArray+CurArraySize);
  240. }
  241. // Allow assignment from any smallptrset with the same element type even if it
  242. // doesn't have the same smallsize.
  243. const SmallPtrSet<PtrType, SmallSize>&
  244. operator=(const SmallPtrSet<PtrType, SmallSize> &RHS) {
  245. CopyFrom(RHS);
  246. return *this;
  247. }
  248. /// swap - Swaps the elements of two sets.
  249. void swap(SmallPtrSet<PtrType, SmallSize> &RHS) {
  250. SmallPtrSetImpl::swap(RHS);
  251. }
  252. };
  253. }
  254. namespace std {
  255. /// Implement std::swap in terms of SmallPtrSet swap.
  256. template<class T, unsigned N>
  257. inline void swap(llvm::SmallPtrSet<T, N> &LHS, llvm::SmallPtrSet<T, N> &RHS) {
  258. LHS.swap(RHS);
  259. }
  260. }
  261. #endif