LLVM API Documentation
00001 //===- llvm/ADT/SmallPtrSet.h - 'Normally small' pointer set ----*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the SmallPtrSet class. See the doxygen comment for 00011 // SmallPtrSetImpl for more details on the algorithm used. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_ADT_SMALLPTRSET_H 00016 #define LLVM_ADT_SMALLPTRSET_H 00017 00018 #include <cassert> 00019 #include <cstring> 00020 #include <iterator> 00021 #include "llvm/Support/DataTypes.h" 00022 #include "llvm/Support/PointerLikeTypeTraits.h" 00023 00024 namespace llvm { 00025 00026 class SmallPtrSetIteratorImpl; 00027 00028 /// SmallPtrSetImpl - This is the common code shared among all the 00029 /// SmallPtrSet<>'s, which is almost everything. SmallPtrSet has two modes, one 00030 /// for small and one for large sets. 00031 /// 00032 /// Small sets use an array of pointers allocated in the SmallPtrSet object, 00033 /// which is treated as a simple array of pointers. When a pointer is added to 00034 /// the set, the array is scanned to see if the element already exists, if not 00035 /// the element is 'pushed back' onto the array. If we run out of space in the 00036 /// array, we grow into the 'large set' case. SmallSet should be used when the 00037 /// sets are often small. In this case, no memory allocation is used, and only 00038 /// light-weight and cache-efficient scanning is used. 00039 /// 00040 /// Large sets use a classic exponentially-probed hash table. Empty buckets are 00041 /// represented with an illegal pointer value (-1) to allow null pointers to be 00042 /// inserted. Tombstones are represented with another illegal pointer value 00043 /// (-2), to allow deletion. The hash table is resized when the table is 3/4 or 00044 /// more. When this happens, the table is doubled in size. 00045 /// 00046 class SmallPtrSetImpl { 00047 friend class SmallPtrSetIteratorImpl; 00048 protected: 00049 /// CurArray - This is the current set of buckets. If it points to 00050 /// SmallArray, then the set is in 'small mode'. 00051 const void **CurArray; 00052 /// CurArraySize - The allocated size of CurArray, always a power of two. 00053 /// Note that CurArray points to an array that has CurArraySize+1 elements in 00054 /// it, so that the end iterator actually points to valid memory. 00055 unsigned CurArraySize; 00056 00057 // If small, this is # elts allocated consequtively 00058 unsigned NumElements; 00059 unsigned NumTombstones; 00060 const void *SmallArray[1]; // Must be last ivar. 00061 00062 // Helper to copy construct a SmallPtrSet. 00063 SmallPtrSetImpl(const SmallPtrSetImpl& that); 00064 explicit SmallPtrSetImpl(unsigned SmallSize) { 00065 assert(SmallSize && (SmallSize & (SmallSize-1)) == 0 && 00066 "Initial size must be a power of two!"); 00067 CurArray = &SmallArray[0]; 00068 CurArraySize = SmallSize; 00069 // The end pointer, always valid, is set to a valid element to help the 00070 // iterator. 00071 CurArray[SmallSize] = 0; 00072 clear(); 00073 } 00074 ~SmallPtrSetImpl(); 00075 00076 public: 00077 bool empty() const { return size() == 0; } 00078 unsigned size() const { return NumElements; } 00079 00080 void clear() { 00081 // If the capacity of the array is huge, and the # elements used is small, 00082 // shrink the array. 00083 if (!isSmall() && NumElements*4 < CurArraySize && CurArraySize > 32) 00084 return shrink_and_clear(); 00085 00086 // Fill the array with empty markers. 00087 memset(CurArray, -1, CurArraySize*sizeof(void*)); 00088 NumElements = 0; 00089 NumTombstones = 0; 00090 } 00091 00092 protected: 00093 static void *getTombstoneMarker() { return reinterpret_cast<void*>(-2); } 00094 static void *getEmptyMarker() { 00095 // Note that -1 is chosen to make clear() efficiently implementable with 00096 // memset and because it's not a valid pointer value. 00097 return reinterpret_cast<void*>(-1); 00098 } 00099 00100 /// insert_imp - This returns true if the pointer was new to the set, false if 00101 /// it was already in the set. This is hidden from the client so that the 00102 /// derived class can check that the right type of pointer is passed in. 00103 bool insert_imp(const void * Ptr); 00104 00105 /// erase_imp - If the set contains the specified pointer, remove it and 00106 /// return true, otherwise return false. This is hidden from the client so 00107 /// that the derived class can check that the right type of pointer is passed 00108 /// in. 00109 bool erase_imp(const void * Ptr); 00110 00111 bool count_imp(const void * Ptr) const { 00112 if (isSmall()) { 00113 // Linear search for the item. 00114 for (const void *const *APtr = SmallArray, 00115 *const *E = SmallArray+NumElements; APtr != E; ++APtr) 00116 if (*APtr == Ptr) 00117 return true; 00118 return false; 00119 } 00120 00121 // Big set case. 00122 return *FindBucketFor(Ptr) == Ptr; 00123 } 00124 00125 private: 00126 bool isSmall() const { return CurArray == &SmallArray[0]; } 00127 00128 unsigned Hash(const void *Ptr) const { 00129 return static_cast<unsigned>(((uintptr_t)Ptr >> 4) & (CurArraySize-1)); 00130 } 00131 const void * const *FindBucketFor(const void *Ptr) const; 00132 void shrink_and_clear(); 00133 00134 /// Grow - Allocate a larger backing store for the buckets and move it over. 00135 void Grow(); 00136 00137 void operator=(const SmallPtrSetImpl &RHS); // DO NOT IMPLEMENT. 00138 protected: 00139 void CopyFrom(const SmallPtrSetImpl &RHS); 00140 }; 00141 00142 /// SmallPtrSetIteratorImpl - This is the common base class shared between all 00143 /// instances of SmallPtrSetIterator. 00144 class SmallPtrSetIteratorImpl { 00145 protected: 00146 const void *const *Bucket; 00147 public: 00148 explicit SmallPtrSetIteratorImpl(const void *const *BP) : Bucket(BP) { 00149 AdvanceIfNotValid(); 00150 } 00151 00152 bool operator==(const SmallPtrSetIteratorImpl &RHS) const { 00153 return Bucket == RHS.Bucket; 00154 } 00155 bool operator!=(const SmallPtrSetIteratorImpl &RHS) const { 00156 return Bucket != RHS.Bucket; 00157 } 00158 00159 protected: 00160 /// AdvanceIfNotValid - If the current bucket isn't valid, advance to a bucket 00161 /// that is. This is guaranteed to stop because the end() bucket is marked 00162 /// valid. 00163 void AdvanceIfNotValid() { 00164 while (*Bucket == SmallPtrSetImpl::getEmptyMarker() || 00165 *Bucket == SmallPtrSetImpl::getTombstoneMarker()) 00166 ++Bucket; 00167 } 00168 }; 00169 00170 /// SmallPtrSetIterator - This implements a const_iterator for SmallPtrSet. 00171 template<typename PtrTy> 00172 class SmallPtrSetIterator : public SmallPtrSetIteratorImpl { 00173 typedef PointerLikeTypeTraits<PtrTy> PtrTraits; 00174 00175 public: 00176 typedef PtrTy value_type; 00177 typedef PtrTy reference; 00178 typedef PtrTy pointer; 00179 typedef std::ptrdiff_t difference_type; 00180 typedef std::forward_iterator_tag iterator_category; 00181 00182 explicit SmallPtrSetIterator(const void *const *BP) 00183 : SmallPtrSetIteratorImpl(BP) {} 00184 00185 // Most methods provided by baseclass. 00186 00187 const PtrTy operator*() const { 00188 return PtrTraits::getFromVoidPointer(const_cast<void*>(*Bucket)); 00189 } 00190 00191 inline SmallPtrSetIterator& operator++() { // Preincrement 00192 ++Bucket; 00193 AdvanceIfNotValid(); 00194 return *this; 00195 } 00196 00197 SmallPtrSetIterator operator++(int) { // Postincrement 00198 SmallPtrSetIterator tmp = *this; ++*this; return tmp; 00199 } 00200 }; 00201 00202 /// NextPowerOfTwo - This is a helper template that rounds N up to the next 00203 /// power of two. 00204 template<unsigned N> 00205 struct NextPowerOfTwo; 00206 00207 /// NextPowerOfTwoH - If N is not a power of two, increase it. This is a helper 00208 /// template used to implement NextPowerOfTwo. 00209 template<unsigned N, bool isPowerTwo> 00210 struct NextPowerOfTwoH { 00211 enum { Val = N }; 00212 }; 00213 template<unsigned N> 00214 struct NextPowerOfTwoH<N, false> { 00215 enum { 00216 // We could just use NextVal = N+1, but this converges faster. N|(N-1) sets 00217 // the right-most zero bits to one all at once, e.g. 0b0011000 -> 0b0011111. 00218 Val = NextPowerOfTwo<(N|(N-1)) + 1>::Val 00219 }; 00220 }; 00221 00222 template<unsigned N> 00223 struct NextPowerOfTwo { 00224 enum { Val = NextPowerOfTwoH<N, (N&(N-1)) == 0>::Val }; 00225 }; 00226 00227 00228 /// SmallPtrSet - This class implements a set which is optimizer for holding 00229 /// SmallSize or less elements. This internally rounds up SmallSize to the next 00230 /// power of two if it is not already a power of two. See the comments above 00231 /// SmallPtrSetImpl for details of the algorithm. 00232 template<class PtrType, unsigned SmallSize> 00233 class SmallPtrSet : public SmallPtrSetImpl { 00234 // Make sure that SmallSize is a power of two, round up if not. 00235 enum { SmallSizePowTwo = NextPowerOfTwo<SmallSize>::Val }; 00236 void *SmallArray[SmallSizePowTwo]; 00237 typedef PointerLikeTypeTraits<PtrType> PtrTraits; 00238 public: 00239 SmallPtrSet() : SmallPtrSetImpl(NextPowerOfTwo<SmallSizePowTwo>::Val) {} 00240 SmallPtrSet(const SmallPtrSet &that) : SmallPtrSetImpl(that) {} 00241 00242 template<typename It> 00243 SmallPtrSet(It I, It E) 00244 : SmallPtrSetImpl(NextPowerOfTwo<SmallSizePowTwo>::Val) { 00245 insert(I, E); 00246 } 00247 00248 /// insert - This returns true if the pointer was new to the set, false if it 00249 /// was already in the set. 00250 bool insert(PtrType Ptr) { 00251 return insert_imp(PtrTraits::getAsVoidPointer(Ptr)); 00252 } 00253 00254 /// erase - If the set contains the specified pointer, remove it and return 00255 /// true, otherwise return false. 00256 bool erase(PtrType Ptr) { 00257 return erase_imp(PtrTraits::getAsVoidPointer(Ptr)); 00258 } 00259 00260 /// count - Return true if the specified pointer is in the set. 00261 bool count(PtrType Ptr) const { 00262 return count_imp(PtrTraits::getAsVoidPointer(Ptr)); 00263 } 00264 00265 template <typename IterT> 00266 void insert(IterT I, IterT E) { 00267 for (; I != E; ++I) 00268 insert(*I); 00269 } 00270 00271 typedef SmallPtrSetIterator<PtrType> iterator; 00272 typedef SmallPtrSetIterator<PtrType> const_iterator; 00273 inline iterator begin() const { 00274 return iterator(CurArray); 00275 } 00276 inline iterator end() const { 00277 return iterator(CurArray+CurArraySize); 00278 } 00279 00280 // Allow assignment from any smallptrset with the same element type even if it 00281 // doesn't have the same smallsize. 00282 const SmallPtrSet<PtrType, SmallSize>& 00283 operator=(const SmallPtrSet<PtrType, SmallSize> &RHS) { 00284 CopyFrom(RHS); 00285 return *this; 00286 } 00287 00288 }; 00289 00290 } 00291 00292 #endif