SmallPtrSet.h revision 2a8bf425bd0aff1a6406805c095d99089a1dfaae
1c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner//===- llvm/ADT/SmallPtrSet.h - 'Normally small' pointer set ----*- C++ -*-===// 2c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner// 3c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner// The LLVM Compiler Infrastructure 4c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner// 57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source 67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details. 7c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner// 8c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner//===----------------------------------------------------------------------===// 9c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner// 1018b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner// This file defines the SmallPtrSet class. See the doxygen comment for 1118b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner// SmallPtrSetImpl for more details on the algorithm used. 12c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner// 13c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner//===----------------------------------------------------------------------===// 14c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner 15c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner#ifndef LLVM_ADT_SMALLPTRSET_H 16c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner#define LLVM_ADT_SMALLPTRSET_H 17c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner 18c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner#include <cassert> 19c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner#include <cstring> 207c718222cdb624f4c420b3d7b6eeb12fffc05603Douglas Gregor#include <iterator> 218b67f774e9c38b7718b2b300b628388f966df4e0Chandler Carruth#include "llvm/System/DataTypes.h" 228ee1b5e916249d1a64ad8cf99b08a0e160f9d052Chris Lattner#include "llvm/Support/PointerLikeTypeTraits.h" 23c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner 24c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnernamespace llvm { 25c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner 265471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohmanclass SmallPtrSetIteratorImpl; 275471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohman 2818b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// SmallPtrSetImpl - This is the common code shared among all the 2918b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// SmallPtrSet<>'s, which is almost everything. SmallPtrSet has two modes, one 3018b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// for small and one for large sets. 3118b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// 3218b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// Small sets use an array of pointers allocated in the SmallPtrSet object, 3318b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// which is treated as a simple array of pointers. When a pointer is added to 3418b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// the set, the array is scanned to see if the element already exists, if not 3518b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// the element is 'pushed back' onto the array. If we run out of space in the 3618b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// array, we grow into the 'large set' case. SmallSet should be used when the 3718b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// sets are often small. In this case, no memory allocation is used, and only 3818b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// light-weight and cache-efficient scanning is used. 3918b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// 4018b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// Large sets use a classic exponentially-probed hash table. Empty buckets are 4118b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// represented with an illegal pointer value (-1) to allow null pointers to be 4218b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// inserted. Tombstones are represented with another illegal pointer value 4318b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// (-2), to allow deletion. The hash table is resized when the table is 3/4 or 4418b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// more. When this happens, the table is doubled in size. 4518b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// 46c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerclass SmallPtrSetImpl { 475471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohman friend class SmallPtrSetIteratorImpl; 48c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerprotected: 492a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands /// SmallArray - Points to a fixed size set of buckets, used in 'small mode'. 502a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands const void **SmallArray; 512a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands /// CurArray - This is the current set of buckets. If equal to SmallArray, 522a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands /// then the set is in 'small mode'. 53e992a56ae93140171f5044079e8d317f784c8cc1Owen Anderson const void **CurArray; 54c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner /// CurArraySize - The allocated size of CurArray, always a power of two. 55c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner /// Note that CurArray points to an array that has CurArraySize+1 elements in 56c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner /// it, so that the end iterator actually points to valid memory. 57c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner unsigned CurArraySize; 583a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman 59c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner // If small, this is # elts allocated consequtively 60c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner unsigned NumElements; 61e237cf934fcb8a25746e068f543fbd6db44eaa70Chris Lattner unsigned NumTombstones; 62ac58a16f8584f38198cc7800bd37a896125268b7Jeff Cohen 63ac58a16f8584f38198cc7800bd37a896125268b7Jeff Cohen // Helper to copy construct a SmallPtrSet. 642a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands SmallPtrSetImpl(const void **SmallStorage, const SmallPtrSetImpl& that); 652a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands explicit SmallPtrSetImpl(const void **SmallStorage, unsigned SmallSize) : 662a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands SmallArray(SmallStorage), CurArray(SmallStorage), CurArraySize(SmallSize) { 67c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner assert(SmallSize && (SmallSize & (SmallSize-1)) == 0 && 68c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner "Initial size must be a power of two!"); 69c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner // The end pointer, always valid, is set to a valid element to help the 70c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner // iterator. 71c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner CurArray[SmallSize] = 0; 72c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner clear(); 73c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner } 74845b31de0c58e1f667063170191262079d311052Reid Spencer ~SmallPtrSetImpl(); 753a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman 765471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohmanpublic: 77894d264f3ee965645656ddd6566acbe4d3a3ed55Chris Lattner bool empty() const { return size() == 0; } 78894d264f3ee965645656ddd6566acbe4d3a3ed55Chris Lattner unsigned size() const { return NumElements; } 793a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman 80c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner void clear() { 8142e4bdf2577946380ce1529d5716e48b33d4186dChris Lattner // If the capacity of the array is huge, and the # elements used is small, 8242e4bdf2577946380ce1529d5716e48b33d4186dChris Lattner // shrink the array. 8342e4bdf2577946380ce1529d5716e48b33d4186dChris Lattner if (!isSmall() && NumElements*4 < CurArraySize && CurArraySize > 32) 8442e4bdf2577946380ce1529d5716e48b33d4186dChris Lattner return shrink_and_clear(); 853a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman 86c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner // Fill the array with empty markers. 87c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner memset(CurArray, -1, CurArraySize*sizeof(void*)); 88c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner NumElements = 0; 89e237cf934fcb8a25746e068f543fbd6db44eaa70Chris Lattner NumTombstones = 0; 90c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner } 913a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman 92373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattnerprotected: 935471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohman static void *getTombstoneMarker() { return reinterpret_cast<void*>(-2); } 945471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohman static void *getEmptyMarker() { 955471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohman // Note that -1 is chosen to make clear() efficiently implementable with 965471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohman // memset and because it's not a valid pointer value. 975471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohman return reinterpret_cast<void*>(-1); 985471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohman } 995471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohman 100373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner /// insert_imp - This returns true if the pointer was new to the set, false if 101373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner /// it was already in the set. This is hidden from the client so that the 102373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner /// derived class can check that the right type of pointer is passed in. 103373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner bool insert_imp(const void * Ptr); 1043a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman 105373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner /// erase_imp - If the set contains the specified pointer, remove it and 106373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner /// return true, otherwise return false. This is hidden from the client so 107373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner /// that the derived class can check that the right type of pointer is passed 108373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner /// in. 109373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner bool erase_imp(const void * Ptr); 1103a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman 111373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner bool count_imp(const void * Ptr) const { 112c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner if (isSmall()) { 113c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner // Linear search for the item. 11442e4bdf2577946380ce1529d5716e48b33d4186dChris Lattner for (const void *const *APtr = SmallArray, 11542e4bdf2577946380ce1529d5716e48b33d4186dChris Lattner *const *E = SmallArray+NumElements; APtr != E; ++APtr) 116c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner if (*APtr == Ptr) 117c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner return true; 118c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner return false; 119c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner } 1203a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman 121c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner // Big set case. 122c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner return *FindBucketFor(Ptr) == Ptr; 123c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner } 1243a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman 125c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerprivate: 1262a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands bool isSmall() const { return CurArray == SmallArray; } 1270b930852cf1a9899ae82dd6c31b43e754a77dcb0Chris Lattner 128e992a56ae93140171f5044079e8d317f784c8cc1Owen Anderson unsigned Hash(const void *Ptr) const { 12934cd4a484e532cc463fd5a4bf59b88d13c5467c1Evan Cheng return static_cast<unsigned>(((uintptr_t)Ptr >> 4) & (CurArraySize-1)); 130c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner } 131e992a56ae93140171f5044079e8d317f784c8cc1Owen Anderson const void * const *FindBucketFor(const void *Ptr) const; 13242e4bdf2577946380ce1529d5716e48b33d4186dChris Lattner void shrink_and_clear(); 1333a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman 134c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner /// Grow - Allocate a larger backing store for the buckets and move it over. 135c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner void Grow(); 1363a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman 13791f0158d4d1b16b8615126b05582d421cfb14089Chris Lattner void operator=(const SmallPtrSetImpl &RHS); // DO NOT IMPLEMENT. 13891f0158d4d1b16b8615126b05582d421cfb14089Chris Lattnerprotected: 13991f0158d4d1b16b8615126b05582d421cfb14089Chris Lattner void CopyFrom(const SmallPtrSetImpl &RHS); 140c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner}; 141c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner 142c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner/// SmallPtrSetIteratorImpl - This is the common base class shared between all 143c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner/// instances of SmallPtrSetIterator. 144c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerclass SmallPtrSetIteratorImpl { 145c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerprotected: 146e992a56ae93140171f5044079e8d317f784c8cc1Owen Anderson const void *const *Bucket; 147c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerpublic: 148950a4c40b823cd4f09dc71be635229246dfd6cacDan Gohman explicit SmallPtrSetIteratorImpl(const void *const *BP) : Bucket(BP) { 149c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner AdvanceIfNotValid(); 150c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner } 1513a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman 152c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner bool operator==(const SmallPtrSetIteratorImpl &RHS) const { 153c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner return Bucket == RHS.Bucket; 154c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner } 155c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner bool operator!=(const SmallPtrSetIteratorImpl &RHS) const { 156c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner return Bucket != RHS.Bucket; 157c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner } 1583a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman 159c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerprotected: 160c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner /// AdvanceIfNotValid - If the current bucket isn't valid, advance to a bucket 161c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner /// that is. This is guaranteed to stop because the end() bucket is marked 162c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner /// valid. 163c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner void AdvanceIfNotValid() { 164c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner while (*Bucket == SmallPtrSetImpl::getEmptyMarker() || 165c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner *Bucket == SmallPtrSetImpl::getTombstoneMarker()) 166c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner ++Bucket; 167c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner } 168c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner}; 169c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner 170c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner/// SmallPtrSetIterator - This implements a const_iterator for SmallPtrSet. 171c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnertemplate<typename PtrTy> 172c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerclass SmallPtrSetIterator : public SmallPtrSetIteratorImpl { 1738ee1b5e916249d1a64ad8cf99b08a0e160f9d052Chris Lattner typedef PointerLikeTypeTraits<PtrTy> PtrTraits; 1747c718222cdb624f4c420b3d7b6eeb12fffc05603Douglas Gregor 175c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerpublic: 1767c718222cdb624f4c420b3d7b6eeb12fffc05603Douglas Gregor typedef PtrTy value_type; 1777c718222cdb624f4c420b3d7b6eeb12fffc05603Douglas Gregor typedef PtrTy reference; 1787c718222cdb624f4c420b3d7b6eeb12fffc05603Douglas Gregor typedef PtrTy pointer; 1797c718222cdb624f4c420b3d7b6eeb12fffc05603Douglas Gregor typedef std::ptrdiff_t difference_type; 1807c718222cdb624f4c420b3d7b6eeb12fffc05603Douglas Gregor typedef std::forward_iterator_tag iterator_category; 1817c718222cdb624f4c420b3d7b6eeb12fffc05603Douglas Gregor 182950a4c40b823cd4f09dc71be635229246dfd6cacDan Gohman explicit SmallPtrSetIterator(const void *const *BP) 183950a4c40b823cd4f09dc71be635229246dfd6cacDan Gohman : SmallPtrSetIteratorImpl(BP) {} 184c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner 185c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner // Most methods provided by baseclass. 1863a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman 187e992a56ae93140171f5044079e8d317f784c8cc1Owen Anderson const PtrTy operator*() const { 1886de603071879bdc5d7d663826354c24a9d176469Chris Lattner return PtrTraits::getFromVoidPointer(const_cast<void*>(*Bucket)); 189c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner } 1903a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman 191c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner inline SmallPtrSetIterator& operator++() { // Preincrement 192c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner ++Bucket; 193c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner AdvanceIfNotValid(); 194c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner return *this; 195c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner } 1963a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman 197c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner SmallPtrSetIterator operator++(int) { // Postincrement 198c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner SmallPtrSetIterator tmp = *this; ++*this; return tmp; 199c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner } 200c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner}; 201c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner 202af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner/// NextPowerOfTwo - This is a helper template that rounds N up to the next 203af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner/// power of two. 204af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattnertemplate<unsigned N> 205af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattnerstruct NextPowerOfTwo; 206af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner 207af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner/// NextPowerOfTwoH - If N is not a power of two, increase it. This is a helper 208af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner/// template used to implement NextPowerOfTwo. 209af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattnertemplate<unsigned N, bool isPowerTwo> 210af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattnerstruct NextPowerOfTwoH { 211af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner enum { Val = N }; 212af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner}; 213af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattnertemplate<unsigned N> 214af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattnerstruct NextPowerOfTwoH<N, false> { 215af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner enum { 216af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner // We could just use NextVal = N+1, but this converges faster. N|(N-1) sets 217af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner // the right-most zero bits to one all at once, e.g. 0b0011000 -> 0b0011111. 218155b6220f79ca2dbd47193c1c061e6e129b977abChris Lattner Val = NextPowerOfTwo<(N|(N-1)) + 1>::Val 219af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner }; 220af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner}; 221af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner 222af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattnertemplate<unsigned N> 223af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattnerstruct NextPowerOfTwo { 224af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner enum { Val = NextPowerOfTwoH<N, (N&(N-1)) == 0>::Val }; 225af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner}; 2266de603071879bdc5d7d663826354c24a9d176469Chris Lattner 227c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner 228252968a76f4b34aa26aa33d447d0256a148ad554Bob Wilson/// SmallPtrSet - This class implements a set which is optimized for holding 2290b930852cf1a9899ae82dd6c31b43e754a77dcb0Chris Lattner/// SmallSize or less elements. This internally rounds up SmallSize to the next 2300b930852cf1a9899ae82dd6c31b43e754a77dcb0Chris Lattner/// power of two if it is not already a power of two. See the comments above 2310b930852cf1a9899ae82dd6c31b43e754a77dcb0Chris Lattner/// SmallPtrSetImpl for details of the algorithm. 232c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnertemplate<class PtrType, unsigned SmallSize> 233c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerclass SmallPtrSet : public SmallPtrSetImpl { 234af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner // Make sure that SmallSize is a power of two, round up if not. 235af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner enum { SmallSizePowTwo = NextPowerOfTwo<SmallSize>::Val }; 2362a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands /// SmallStorage - Fixed size storage used in 'small mode'. The extra element 2372a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands /// ensures that the end iterator actually points to valid memory. 2382a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands const void *SmallStorage[SmallSizePowTwo+1]; 2398ee1b5e916249d1a64ad8cf99b08a0e160f9d052Chris Lattner typedef PointerLikeTypeTraits<PtrType> PtrTraits; 240c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerpublic: 2412a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands SmallPtrSet() : SmallPtrSetImpl(SmallStorage, SmallSizePowTwo) {} 2422a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands SmallPtrSet(const SmallPtrSet &that) : SmallPtrSetImpl(SmallStorage, that) {} 2433a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman 244ab8fea5283de0931e1da5dc91b4df2f734ba0206Chris Lattner template<typename It> 2452a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands SmallPtrSet(It I, It E) : SmallPtrSetImpl(SmallStorage, SmallSizePowTwo) { 246086f18626750482318612a10c3027b946b4e36e4Chris Lattner insert(I, E); 247ab8fea5283de0931e1da5dc91b4df2f734ba0206Chris Lattner } 2483a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman 249373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner /// insert - This returns true if the pointer was new to the set, false if it 250373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner /// was already in the set. 2516de603071879bdc5d7d663826354c24a9d176469Chris Lattner bool insert(PtrType Ptr) { 2526de603071879bdc5d7d663826354c24a9d176469Chris Lattner return insert_imp(PtrTraits::getAsVoidPointer(Ptr)); 2536de603071879bdc5d7d663826354c24a9d176469Chris Lattner } 2543a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman 255373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner /// erase - If the set contains the specified pointer, remove it and return 256373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner /// true, otherwise return false. 2576de603071879bdc5d7d663826354c24a9d176469Chris Lattner bool erase(PtrType Ptr) { 2586de603071879bdc5d7d663826354c24a9d176469Chris Lattner return erase_imp(PtrTraits::getAsVoidPointer(Ptr)); 2596de603071879bdc5d7d663826354c24a9d176469Chris Lattner } 2603a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman 261373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner /// count - Return true if the specified pointer is in the set. 2626de603071879bdc5d7d663826354c24a9d176469Chris Lattner bool count(PtrType Ptr) const { 2636de603071879bdc5d7d663826354c24a9d176469Chris Lattner return count_imp(PtrTraits::getAsVoidPointer(Ptr)); 2646de603071879bdc5d7d663826354c24a9d176469Chris Lattner } 2653a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman 266373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner template <typename IterT> 267373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner void insert(IterT I, IterT E) { 268373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner for (; I != E; ++I) 269373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner insert(*I); 270373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner } 2713a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman 272c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner typedef SmallPtrSetIterator<PtrType> iterator; 273c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner typedef SmallPtrSetIterator<PtrType> const_iterator; 274c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner inline iterator begin() const { 275c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner return iterator(CurArray); 276c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner } 277c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner inline iterator end() const { 278c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner return iterator(CurArray+CurArraySize); 279c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner } 2803a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman 28191f0158d4d1b16b8615126b05582d421cfb14089Chris Lattner // Allow assignment from any smallptrset with the same element type even if it 28291f0158d4d1b16b8615126b05582d421cfb14089Chris Lattner // doesn't have the same smallsize. 2834d6f96d6997738419627ebe13dd6cb9c88a98fd7Owen Anderson const SmallPtrSet<PtrType, SmallSize>& 28495fd3189f84de5953c75d67403e7db9acdd5005dChris Lattner operator=(const SmallPtrSet<PtrType, SmallSize> &RHS) { 28591f0158d4d1b16b8615126b05582d421cfb14089Chris Lattner CopyFrom(RHS); 28691f0158d4d1b16b8615126b05582d421cfb14089Chris Lattner return *this; 28791f0158d4d1b16b8615126b05582d421cfb14089Chris Lattner } 28891f0158d4d1b16b8615126b05582d421cfb14089Chris Lattner 289c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner}; 290c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner 291c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner} 292c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner 293c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner#endif 294