SmallPtrSet.h revision ab4c366274a582dd8146b2820c6b999cad5fce36
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>
19476b242fe7a61e5f9ac6214b0bc5c680d24f152eNick Lewycky#include <cstddef>
20c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner#include <cstring>
217c718222cdb624f4c420b3d7b6eeb12fffc05603Douglas Gregor#include <iterator>
221f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/DataTypes.h"
238ee1b5e916249d1a64ad8cf99b08a0e160f9d052Chris Lattner#include "llvm/Support/PointerLikeTypeTraits.h"
24c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner
25c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnernamespace llvm {
26c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner
275471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohmanclass SmallPtrSetIteratorImpl;
285471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohman
2918b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// SmallPtrSetImpl - This is the common code shared among all the
3018b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// SmallPtrSet<>'s, which is almost everything.  SmallPtrSet has two modes, one
3118b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// for small and one for large sets.
3218b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner///
3318b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// Small sets use an array of pointers allocated in the SmallPtrSet object,
3418b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// which is treated as a simple array of pointers.  When a pointer is added to
3518b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// the set, the array is scanned to see if the element already exists, if not
3618b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// the element is 'pushed back' onto the array.  If we run out of space in the
3718b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// array, we grow into the 'large set' case.  SmallSet should be used when the
3818b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// sets are often small.  In this case, no memory allocation is used, and only
3918b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// light-weight and cache-efficient scanning is used.
4018b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner///
4118b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// Large sets use a classic exponentially-probed hash table.  Empty buckets are
4218b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// represented with an illegal pointer value (-1) to allow null pointers to be
4318b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// inserted.  Tombstones are represented with another illegal pointer value
4418b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// (-2), to allow deletion.  The hash table is resized when the table is 3/4 or
4518b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// more.  When this happens, the table is doubled in size.
4618b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner///
47c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerclass SmallPtrSetImpl {
485471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohman  friend class SmallPtrSetIteratorImpl;
49c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerprotected:
502a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands  /// SmallArray - Points to a fixed size set of buckets, used in 'small mode'.
512a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands  const void **SmallArray;
522a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands  /// CurArray - This is the current set of buckets.  If equal to SmallArray,
532a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands  /// then the set is in 'small mode'.
54e992a56ae93140171f5044079e8d317f784c8cc1Owen Anderson  const void **CurArray;
55c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  /// CurArraySize - The allocated size of CurArray, always a power of two.
56c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  /// Note that CurArray points to an array that has CurArraySize+1 elements in
57c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  /// it, so that the end iterator actually points to valid memory.
58c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  unsigned CurArraySize;
593a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
60ab4c366274a582dd8146b2820c6b999cad5fce36Duncan Sands  // If small, this is # elts allocated consecutively
61c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  unsigned NumElements;
62e237cf934fcb8a25746e068f543fbd6db44eaa70Chris Lattner  unsigned NumTombstones;
63ac58a16f8584f38198cc7800bd37a896125268b7Jeff Cohen
64ac58a16f8584f38198cc7800bd37a896125268b7Jeff Cohen  // Helper to copy construct a SmallPtrSet.
652a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands  SmallPtrSetImpl(const void **SmallStorage, const SmallPtrSetImpl& that);
662a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands  explicit SmallPtrSetImpl(const void **SmallStorage, unsigned SmallSize) :
672a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands    SmallArray(SmallStorage), CurArray(SmallStorage), CurArraySize(SmallSize) {
68c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    assert(SmallSize && (SmallSize & (SmallSize-1)) == 0 &&
69c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner           "Initial size must be a power of two!");
70c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    // The end pointer, always valid, is set to a valid element to help the
71c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    // iterator.
72c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    CurArray[SmallSize] = 0;
73c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    clear();
74c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  }
75845b31de0c58e1f667063170191262079d311052Reid Spencer  ~SmallPtrSetImpl();
763a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
775471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohmanpublic:
78894d264f3ee965645656ddd6566acbe4d3a3ed55Chris Lattner  bool empty() const { return size() == 0; }
79894d264f3ee965645656ddd6566acbe4d3a3ed55Chris Lattner  unsigned size() const { return NumElements; }
803a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
81c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  void clear() {
8242e4bdf2577946380ce1529d5716e48b33d4186dChris Lattner    // If the capacity of the array is huge, and the # elements used is small,
8342e4bdf2577946380ce1529d5716e48b33d4186dChris Lattner    // shrink the array.
8442e4bdf2577946380ce1529d5716e48b33d4186dChris Lattner    if (!isSmall() && NumElements*4 < CurArraySize && CurArraySize > 32)
8542e4bdf2577946380ce1529d5716e48b33d4186dChris Lattner      return shrink_and_clear();
863a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
87c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    // Fill the array with empty markers.
88c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    memset(CurArray, -1, CurArraySize*sizeof(void*));
89c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    NumElements = 0;
90e237cf934fcb8a25746e068f543fbd6db44eaa70Chris Lattner    NumTombstones = 0;
91c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  }
923a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
93373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattnerprotected:
945471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohman  static void *getTombstoneMarker() { return reinterpret_cast<void*>(-2); }
955471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohman  static void *getEmptyMarker() {
965471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohman    // Note that -1 is chosen to make clear() efficiently implementable with
975471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohman    // memset and because it's not a valid pointer value.
985471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohman    return reinterpret_cast<void*>(-1);
995471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohman  }
1005471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohman
101373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  /// insert_imp - This returns true if the pointer was new to the set, false if
102373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  /// it was already in the set.  This is hidden from the client so that the
103373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  /// derived class can check that the right type of pointer is passed in.
104373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  bool insert_imp(const void * Ptr);
1053a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
106373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  /// erase_imp - If the set contains the specified pointer, remove it and
107373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  /// return true, otherwise return false.  This is hidden from the client so
108373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  /// that the derived class can check that the right type of pointer is passed
109373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  /// in.
110373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  bool erase_imp(const void * Ptr);
1113a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
112373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  bool count_imp(const void * Ptr) const {
113c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    if (isSmall()) {
114c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner      // Linear search for the item.
11542e4bdf2577946380ce1529d5716e48b33d4186dChris Lattner      for (const void *const *APtr = SmallArray,
11642e4bdf2577946380ce1529d5716e48b33d4186dChris Lattner                      *const *E = SmallArray+NumElements; APtr != E; ++APtr)
117c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner        if (*APtr == Ptr)
118c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner          return true;
119c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner      return false;
120c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    }
1213a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
122c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    // Big set case.
123c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    return *FindBucketFor(Ptr) == Ptr;
124c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  }
1253a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
126c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerprivate:
1272a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands  bool isSmall() const { return CurArray == SmallArray; }
1280b930852cf1a9899ae82dd6c31b43e754a77dcb0Chris Lattner
129e992a56ae93140171f5044079e8d317f784c8cc1Owen Anderson  unsigned Hash(const void *Ptr) const {
13034cd4a484e532cc463fd5a4bf59b88d13c5467c1Evan Cheng    return static_cast<unsigned>(((uintptr_t)Ptr >> 4) & (CurArraySize-1));
131c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  }
132e992a56ae93140171f5044079e8d317f784c8cc1Owen Anderson  const void * const *FindBucketFor(const void *Ptr) const;
13342e4bdf2577946380ce1529d5716e48b33d4186dChris Lattner  void shrink_and_clear();
1343a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
135c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  /// Grow - Allocate a larger backing store for the buckets and move it over.
136c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  void Grow();
1373a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
13891f0158d4d1b16b8615126b05582d421cfb14089Chris Lattner  void operator=(const SmallPtrSetImpl &RHS);  // DO NOT IMPLEMENT.
13991f0158d4d1b16b8615126b05582d421cfb14089Chris Lattnerprotected:
14091f0158d4d1b16b8615126b05582d421cfb14089Chris Lattner  void CopyFrom(const SmallPtrSetImpl &RHS);
141c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner};
142c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner
143c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner/// SmallPtrSetIteratorImpl - This is the common base class shared between all
144c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner/// instances of SmallPtrSetIterator.
145c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerclass SmallPtrSetIteratorImpl {
146c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerprotected:
147e992a56ae93140171f5044079e8d317f784c8cc1Owen Anderson  const void *const *Bucket;
148c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerpublic:
149950a4c40b823cd4f09dc71be635229246dfd6cacDan Gohman  explicit SmallPtrSetIteratorImpl(const void *const *BP) : Bucket(BP) {
150c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    AdvanceIfNotValid();
151c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  }
1523a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
153c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  bool operator==(const SmallPtrSetIteratorImpl &RHS) const {
154c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    return Bucket == RHS.Bucket;
155c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  }
156c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  bool operator!=(const SmallPtrSetIteratorImpl &RHS) const {
157c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    return Bucket != RHS.Bucket;
158c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  }
1593a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
160c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerprotected:
161c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  /// AdvanceIfNotValid - If the current bucket isn't valid, advance to a bucket
162c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  /// that is.   This is guaranteed to stop because the end() bucket is marked
163c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  /// valid.
164c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  void AdvanceIfNotValid() {
165c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    while (*Bucket == SmallPtrSetImpl::getEmptyMarker() ||
166c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner           *Bucket == SmallPtrSetImpl::getTombstoneMarker())
167c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner      ++Bucket;
168c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  }
169c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner};
170c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner
171c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner/// SmallPtrSetIterator - This implements a const_iterator for SmallPtrSet.
172c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnertemplate<typename PtrTy>
173c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerclass SmallPtrSetIterator : public SmallPtrSetIteratorImpl {
1748ee1b5e916249d1a64ad8cf99b08a0e160f9d052Chris Lattner  typedef PointerLikeTypeTraits<PtrTy> PtrTraits;
1757c718222cdb624f4c420b3d7b6eeb12fffc05603Douglas Gregor
176c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerpublic:
1777c718222cdb624f4c420b3d7b6eeb12fffc05603Douglas Gregor  typedef PtrTy                     value_type;
1787c718222cdb624f4c420b3d7b6eeb12fffc05603Douglas Gregor  typedef PtrTy                     reference;
1797c718222cdb624f4c420b3d7b6eeb12fffc05603Douglas Gregor  typedef PtrTy                     pointer;
1807c718222cdb624f4c420b3d7b6eeb12fffc05603Douglas Gregor  typedef std::ptrdiff_t            difference_type;
1817c718222cdb624f4c420b3d7b6eeb12fffc05603Douglas Gregor  typedef std::forward_iterator_tag iterator_category;
1827c718222cdb624f4c420b3d7b6eeb12fffc05603Douglas Gregor
183950a4c40b823cd4f09dc71be635229246dfd6cacDan Gohman  explicit SmallPtrSetIterator(const void *const *BP)
184950a4c40b823cd4f09dc71be635229246dfd6cacDan Gohman    : SmallPtrSetIteratorImpl(BP) {}
185c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner
186c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  // Most methods provided by baseclass.
1873a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
188e992a56ae93140171f5044079e8d317f784c8cc1Owen Anderson  const PtrTy operator*() const {
1896de603071879bdc5d7d663826354c24a9d176469Chris Lattner    return PtrTraits::getFromVoidPointer(const_cast<void*>(*Bucket));
190c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  }
1913a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
192c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  inline SmallPtrSetIterator& operator++() {          // Preincrement
193c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    ++Bucket;
194c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    AdvanceIfNotValid();
195c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    return *this;
196c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  }
1973a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
198c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  SmallPtrSetIterator operator++(int) {        // Postincrement
199c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    SmallPtrSetIterator tmp = *this; ++*this; return tmp;
200c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  }
201c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner};
202c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner
203a71f0e1f2a7d6d2f030f147d6d426e5bb3b56328Duncan Sands/// RoundUpToPowerOfTwo - This is a helper template that rounds N up to the next
2042e502577ab3645ab5c54434671d299e35c2245ccDuncan Sands/// power of two (which means N itself if N is already a power of two).
205af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattnertemplate<unsigned N>
206a71f0e1f2a7d6d2f030f147d6d426e5bb3b56328Duncan Sandsstruct RoundUpToPowerOfTwo;
207af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner
208a71f0e1f2a7d6d2f030f147d6d426e5bb3b56328Duncan Sands/// RoundUpToPowerOfTwoH - If N is not a power of two, increase it.  This is a
209a71f0e1f2a7d6d2f030f147d6d426e5bb3b56328Duncan Sands/// helper template used to implement RoundUpToPowerOfTwo.
210af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattnertemplate<unsigned N, bool isPowerTwo>
211a71f0e1f2a7d6d2f030f147d6d426e5bb3b56328Duncan Sandsstruct RoundUpToPowerOfTwoH {
212af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner  enum { Val = N };
213af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner};
214af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattnertemplate<unsigned N>
215a71f0e1f2a7d6d2f030f147d6d426e5bb3b56328Duncan Sandsstruct RoundUpToPowerOfTwoH<N, false> {
216af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner  enum {
217af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner    // We could just use NextVal = N+1, but this converges faster.  N|(N-1) sets
218af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner    // the right-most zero bits to one all at once, e.g. 0b0011000 -> 0b0011111.
219a71f0e1f2a7d6d2f030f147d6d426e5bb3b56328Duncan Sands    Val = RoundUpToPowerOfTwo<(N|(N-1)) + 1>::Val
220af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner  };
221af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner};
222af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner
223af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattnertemplate<unsigned N>
224a71f0e1f2a7d6d2f030f147d6d426e5bb3b56328Duncan Sandsstruct RoundUpToPowerOfTwo {
225a71f0e1f2a7d6d2f030f147d6d426e5bb3b56328Duncan Sands  enum { Val = RoundUpToPowerOfTwoH<N, (N&(N-1)) == 0>::Val };
226af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner};
2276de603071879bdc5d7d663826354c24a9d176469Chris Lattner
228c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner
229252968a76f4b34aa26aa33d447d0256a148ad554Bob Wilson/// SmallPtrSet - This class implements a set which is optimized for holding
2300b930852cf1a9899ae82dd6c31b43e754a77dcb0Chris Lattner/// SmallSize or less elements.  This internally rounds up SmallSize to the next
2310b930852cf1a9899ae82dd6c31b43e754a77dcb0Chris Lattner/// power of two if it is not already a power of two.  See the comments above
2320b930852cf1a9899ae82dd6c31b43e754a77dcb0Chris Lattner/// SmallPtrSetImpl for details of the algorithm.
233c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnertemplate<class PtrType, unsigned SmallSize>
234c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerclass SmallPtrSet : public SmallPtrSetImpl {
235af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner  // Make sure that SmallSize is a power of two, round up if not.
236a71f0e1f2a7d6d2f030f147d6d426e5bb3b56328Duncan Sands  enum { SmallSizePowTwo = RoundUpToPowerOfTwo<SmallSize>::Val };
2372a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands  /// SmallStorage - Fixed size storage used in 'small mode'.  The extra element
2382a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands  /// ensures that the end iterator actually points to valid memory.
2392a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands  const void *SmallStorage[SmallSizePowTwo+1];
2408ee1b5e916249d1a64ad8cf99b08a0e160f9d052Chris Lattner  typedef PointerLikeTypeTraits<PtrType> PtrTraits;
241c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerpublic:
2422a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands  SmallPtrSet() : SmallPtrSetImpl(SmallStorage, SmallSizePowTwo) {}
2432a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands  SmallPtrSet(const SmallPtrSet &that) : SmallPtrSetImpl(SmallStorage, that) {}
2443a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
245ab8fea5283de0931e1da5dc91b4df2f734ba0206Chris Lattner  template<typename It>
2462a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands  SmallPtrSet(It I, It E) : SmallPtrSetImpl(SmallStorage, SmallSizePowTwo) {
247086f18626750482318612a10c3027b946b4e36e4Chris Lattner    insert(I, E);
248ab8fea5283de0931e1da5dc91b4df2f734ba0206Chris Lattner  }
2493a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
250373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  /// insert - This returns true if the pointer was new to the set, false if it
251373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  /// was already in the set.
2526de603071879bdc5d7d663826354c24a9d176469Chris Lattner  bool insert(PtrType Ptr) {
2536de603071879bdc5d7d663826354c24a9d176469Chris Lattner    return insert_imp(PtrTraits::getAsVoidPointer(Ptr));
2546de603071879bdc5d7d663826354c24a9d176469Chris Lattner  }
2553a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
256373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  /// erase - If the set contains the specified pointer, remove it and return
257373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  /// true, otherwise return false.
2586de603071879bdc5d7d663826354c24a9d176469Chris Lattner  bool erase(PtrType Ptr) {
2596de603071879bdc5d7d663826354c24a9d176469Chris Lattner    return erase_imp(PtrTraits::getAsVoidPointer(Ptr));
2606de603071879bdc5d7d663826354c24a9d176469Chris Lattner  }
2613a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
262373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  /// count - Return true if the specified pointer is in the set.
2636de603071879bdc5d7d663826354c24a9d176469Chris Lattner  bool count(PtrType Ptr) const {
2646de603071879bdc5d7d663826354c24a9d176469Chris Lattner    return count_imp(PtrTraits::getAsVoidPointer(Ptr));
2656de603071879bdc5d7d663826354c24a9d176469Chris Lattner  }
2663a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
267373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  template <typename IterT>
268373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  void insert(IterT I, IterT E) {
269373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner    for (; I != E; ++I)
270373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner      insert(*I);
271373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  }
2723a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
273c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  typedef SmallPtrSetIterator<PtrType> iterator;
274c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  typedef SmallPtrSetIterator<PtrType> const_iterator;
275c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  inline iterator begin() const {
276c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    return iterator(CurArray);
277c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  }
278c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  inline iterator end() const {
279c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    return iterator(CurArray+CurArraySize);
280c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  }
2813a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
28291f0158d4d1b16b8615126b05582d421cfb14089Chris Lattner  // Allow assignment from any smallptrset with the same element type even if it
28391f0158d4d1b16b8615126b05582d421cfb14089Chris Lattner  // doesn't have the same smallsize.
2844d6f96d6997738419627ebe13dd6cb9c88a98fd7Owen Anderson  const SmallPtrSet<PtrType, SmallSize>&
28595fd3189f84de5953c75d67403e7db9acdd5005dChris Lattner  operator=(const SmallPtrSet<PtrType, SmallSize> &RHS) {
28691f0158d4d1b16b8615126b05582d421cfb14089Chris Lattner    CopyFrom(RHS);
28791f0158d4d1b16b8615126b05582d421cfb14089Chris Lattner    return *this;
28891f0158d4d1b16b8615126b05582d421cfb14089Chris Lattner  }
28991f0158d4d1b16b8615126b05582d421cfb14089Chris Lattner
290c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner};
291c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner
292c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner}
293c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner
294c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner#endif
295