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