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
18f630e49efc7bf3f1716b6daab3c2cc11a908754aCraig Topper#include "llvm/Support/Compiler.h"
19f630e49efc7bf3f1716b6daab3c2cc11a908754aCraig Topper#include "llvm/Support/DataTypes.h"
20f630e49efc7bf3f1716b6daab3c2cc11a908754aCraig Topper#include "llvm/Support/PointerLikeTypeTraits.h"
21c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner#include <cassert>
22476b242fe7a61e5f9ac6214b0bc5c680d24f152eNick Lewycky#include <cstddef>
23c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner#include <cstring>
247c718222cdb624f4c420b3d7b6eeb12fffc05603Douglas Gregor#include <iterator>
25c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner
26c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnernamespace llvm {
27c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner
285471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohmanclass SmallPtrSetIteratorImpl;
295471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohman
3018b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// SmallPtrSetImpl - This is the common code shared among all the
3118b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// SmallPtrSet<>'s, which is almost everything.  SmallPtrSet has two modes, one
3218b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// for small and one for large sets.
3318b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner///
3418b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// Small sets use an array of pointers allocated in the SmallPtrSet object,
3518b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// which is treated as a simple array of pointers.  When a pointer is added to
3618b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// the set, the array is scanned to see if the element already exists, if not
3718b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// the element is 'pushed back' onto the array.  If we run out of space in the
3818b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// array, we grow into the 'large set' case.  SmallSet should be used when the
3918b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// sets are often small.  In this case, no memory allocation is used, and only
4018b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// light-weight and cache-efficient scanning is used.
4118b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner///
4218b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// Large sets use a classic exponentially-probed hash table.  Empty buckets are
4318b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// represented with an illegal pointer value (-1) to allow null pointers to be
4418b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// inserted.  Tombstones are represented with another illegal pointer value
4518b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// (-2), to allow deletion.  The hash table is resized when the table is 3/4 or
4618b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner/// more.  When this happens, the table is doubled in size.
4718b69106b7a66ac6abde37d1be6d0d952796ed04Chris Lattner///
48c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerclass SmallPtrSetImpl {
495471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohman  friend class SmallPtrSetIteratorImpl;
50c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerprotected:
512a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands  /// SmallArray - Points to a fixed size set of buckets, used in 'small mode'.
522a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands  const void **SmallArray;
532a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands  /// CurArray - This is the current set of buckets.  If equal to SmallArray,
542a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands  /// then the set is in 'small mode'.
55e992a56ae93140171f5044079e8d317f784c8cc1Owen Anderson  const void **CurArray;
56c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  /// CurArraySize - The allocated size of CurArray, always a power of two.
57c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  /// Note that CurArray points to an array that has CurArraySize+1 elements in
58c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  /// it, so that the end iterator actually points to valid memory.
59c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  unsigned CurArraySize;
603a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
61ab4c366274a582dd8146b2820c6b999cad5fce36Duncan Sands  // If small, this is # elts allocated consecutively
62c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  unsigned NumElements;
63e237cf934fcb8a25746e068f543fbd6db44eaa70Chris Lattner  unsigned NumTombstones;
64ac58a16f8584f38198cc7800bd37a896125268b7Jeff Cohen
65ac58a16f8584f38198cc7800bd37a896125268b7Jeff Cohen  // Helper to copy construct a SmallPtrSet.
662a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands  SmallPtrSetImpl(const void **SmallStorage, const SmallPtrSetImpl& that);
672a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands  explicit SmallPtrSetImpl(const void **SmallStorage, unsigned SmallSize) :
682a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands    SmallArray(SmallStorage), CurArray(SmallStorage), CurArraySize(SmallSize) {
69c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    assert(SmallSize && (SmallSize & (SmallSize-1)) == 0 &&
70c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner           "Initial size must be a power of two!");
71c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    // The end pointer, always valid, is set to a valid element to help the
72c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    // iterator.
73c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    CurArray[SmallSize] = 0;
74c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    clear();
75c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  }
76845b31de0c58e1f667063170191262079d311052Reid Spencer  ~SmallPtrSetImpl();
773a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
785471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohmanpublic:
79894d264f3ee965645656ddd6566acbe4d3a3ed55Chris Lattner  bool empty() const { return size() == 0; }
80894d264f3ee965645656ddd6566acbe4d3a3ed55Chris Lattner  unsigned size() const { return NumElements; }
813a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
82c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  void clear() {
8342e4bdf2577946380ce1529d5716e48b33d4186dChris Lattner    // If the capacity of the array is huge, and the # elements used is small,
8442e4bdf2577946380ce1529d5716e48b33d4186dChris Lattner    // shrink the array.
8542e4bdf2577946380ce1529d5716e48b33d4186dChris Lattner    if (!isSmall() && NumElements*4 < CurArraySize && CurArraySize > 32)
8642e4bdf2577946380ce1529d5716e48b33d4186dChris Lattner      return shrink_and_clear();
873a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
88c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    // Fill the array with empty markers.
89c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    memset(CurArray, -1, CurArraySize*sizeof(void*));
90c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    NumElements = 0;
91e237cf934fcb8a25746e068f543fbd6db44eaa70Chris Lattner    NumTombstones = 0;
92c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  }
933a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
94373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattnerprotected:
955471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohman  static void *getTombstoneMarker() { return reinterpret_cast<void*>(-2); }
965471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohman  static void *getEmptyMarker() {
975471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohman    // Note that -1 is chosen to make clear() efficiently implementable with
985471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohman    // memset and because it's not a valid pointer value.
995471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohman    return reinterpret_cast<void*>(-1);
1005471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohman  }
1015471a00977ab8ce3c88414dfbad99c0296d58508Dan Gohman
102373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  /// insert_imp - This returns true if the pointer was new to the set, false if
103373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  /// it was already in the set.  This is hidden from the client so that the
104373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  /// derived class can check that the right type of pointer is passed in.
105373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  bool insert_imp(const void * Ptr);
1063a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
107373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  /// erase_imp - If the set contains the specified pointer, remove it and
108373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  /// return true, otherwise return false.  This is hidden from the client so
109373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  /// that the derived class can check that the right type of pointer is passed
110373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  /// in.
111373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  bool erase_imp(const void * Ptr);
1123a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
113373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  bool count_imp(const void * Ptr) const {
114c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    if (isSmall()) {
115c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner      // Linear search for the item.
11642e4bdf2577946380ce1529d5716e48b33d4186dChris Lattner      for (const void *const *APtr = SmallArray,
11742e4bdf2577946380ce1529d5716e48b33d4186dChris Lattner                      *const *E = SmallArray+NumElements; APtr != E; ++APtr)
118c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner        if (*APtr == Ptr)
119c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner          return true;
120c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner      return false;
121c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    }
1223a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
123c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    // Big set case.
124c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    return *FindBucketFor(Ptr) == Ptr;
125c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  }
1263a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
127c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerprivate:
1282a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands  bool isSmall() const { return CurArray == SmallArray; }
1290b930852cf1a9899ae82dd6c31b43e754a77dcb0Chris Lattner
130e992a56ae93140171f5044079e8d317f784c8cc1Owen Anderson  const void * const *FindBucketFor(const void *Ptr) const;
13142e4bdf2577946380ce1529d5716e48b33d4186dChris Lattner  void shrink_and_clear();
1323a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
133c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  /// Grow - Allocate a larger backing store for the buckets and move it over.
134e10fff6f8802d6ab4045d9d0bb22e6f37e6d3d0bJakob Stoklund Olesen  void Grow(unsigned NewSize);
1353a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
136f630e49efc7bf3f1716b6daab3c2cc11a908754aCraig Topper  void operator=(const SmallPtrSetImpl &RHS) LLVM_DELETED_FUNCTION;
13791f0158d4d1b16b8615126b05582d421cfb14089Chris Lattnerprotected:
1382945a32ffd0bf079de1b23db12bc8a0de596a167Benjamin Kramer  /// swap - Swaps the elements of two sets.
1392945a32ffd0bf079de1b23db12bc8a0de596a167Benjamin Kramer  /// Note: This method assumes that both sets have the same small size.
1402945a32ffd0bf079de1b23db12bc8a0de596a167Benjamin Kramer  void swap(SmallPtrSetImpl &RHS);
1412945a32ffd0bf079de1b23db12bc8a0de596a167Benjamin Kramer
14291f0158d4d1b16b8615126b05582d421cfb14089Chris Lattner  void CopyFrom(const SmallPtrSetImpl &RHS);
143c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner};
144c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner
145c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner/// SmallPtrSetIteratorImpl - This is the common base class shared between all
146c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner/// instances of SmallPtrSetIterator.
147c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerclass SmallPtrSetIteratorImpl {
148c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerprotected:
149e992a56ae93140171f5044079e8d317f784c8cc1Owen Anderson  const void *const *Bucket;
150c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerpublic:
151950a4c40b823cd4f09dc71be635229246dfd6cacDan Gohman  explicit SmallPtrSetIteratorImpl(const void *const *BP) : Bucket(BP) {
152c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    AdvanceIfNotValid();
153c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  }
1543a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
155c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  bool operator==(const SmallPtrSetIteratorImpl &RHS) const {
156c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    return Bucket == RHS.Bucket;
157c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  }
158c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  bool operator!=(const SmallPtrSetIteratorImpl &RHS) const {
159c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    return Bucket != RHS.Bucket;
160c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  }
1613a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
162c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerprotected:
163c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  /// AdvanceIfNotValid - If the current bucket isn't valid, advance to a bucket
164c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  /// that is.   This is guaranteed to stop because the end() bucket is marked
165c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  /// valid.
166c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  void AdvanceIfNotValid() {
167c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    while (*Bucket == SmallPtrSetImpl::getEmptyMarker() ||
168c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner           *Bucket == SmallPtrSetImpl::getTombstoneMarker())
169c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner      ++Bucket;
170c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  }
171c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner};
172c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner
173c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner/// SmallPtrSetIterator - This implements a const_iterator for SmallPtrSet.
174c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnertemplate<typename PtrTy>
175c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerclass SmallPtrSetIterator : public SmallPtrSetIteratorImpl {
1768ee1b5e916249d1a64ad8cf99b08a0e160f9d052Chris Lattner  typedef PointerLikeTypeTraits<PtrTy> PtrTraits;
1777c718222cdb624f4c420b3d7b6eeb12fffc05603Douglas Gregor
178c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerpublic:
1797c718222cdb624f4c420b3d7b6eeb12fffc05603Douglas Gregor  typedef PtrTy                     value_type;
1807c718222cdb624f4c420b3d7b6eeb12fffc05603Douglas Gregor  typedef PtrTy                     reference;
1817c718222cdb624f4c420b3d7b6eeb12fffc05603Douglas Gregor  typedef PtrTy                     pointer;
1827c718222cdb624f4c420b3d7b6eeb12fffc05603Douglas Gregor  typedef std::ptrdiff_t            difference_type;
1837c718222cdb624f4c420b3d7b6eeb12fffc05603Douglas Gregor  typedef std::forward_iterator_tag iterator_category;
1847c718222cdb624f4c420b3d7b6eeb12fffc05603Douglas Gregor
185950a4c40b823cd4f09dc71be635229246dfd6cacDan Gohman  explicit SmallPtrSetIterator(const void *const *BP)
186950a4c40b823cd4f09dc71be635229246dfd6cacDan Gohman    : SmallPtrSetIteratorImpl(BP) {}
187c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner
188c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  // Most methods provided by baseclass.
1893a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
190e992a56ae93140171f5044079e8d317f784c8cc1Owen Anderson  const PtrTy operator*() const {
1916de603071879bdc5d7d663826354c24a9d176469Chris Lattner    return PtrTraits::getFromVoidPointer(const_cast<void*>(*Bucket));
192c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  }
1933a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
194c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  inline SmallPtrSetIterator& operator++() {          // Preincrement
195c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    ++Bucket;
196c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    AdvanceIfNotValid();
197c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    return *this;
198c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  }
1993a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
200c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  SmallPtrSetIterator operator++(int) {        // Postincrement
201c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    SmallPtrSetIterator tmp = *this; ++*this; return tmp;
202c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  }
203c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner};
204c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner
205a71f0e1f2a7d6d2f030f147d6d426e5bb3b56328Duncan Sands/// RoundUpToPowerOfTwo - This is a helper template that rounds N up to the next
2062e502577ab3645ab5c54434671d299e35c2245ccDuncan Sands/// power of two (which means N itself if N is already a power of two).
207af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattnertemplate<unsigned N>
208a71f0e1f2a7d6d2f030f147d6d426e5bb3b56328Duncan Sandsstruct RoundUpToPowerOfTwo;
209af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner
210a71f0e1f2a7d6d2f030f147d6d426e5bb3b56328Duncan Sands/// RoundUpToPowerOfTwoH - If N is not a power of two, increase it.  This is a
211a71f0e1f2a7d6d2f030f147d6d426e5bb3b56328Duncan Sands/// helper template used to implement RoundUpToPowerOfTwo.
212af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattnertemplate<unsigned N, bool isPowerTwo>
213a71f0e1f2a7d6d2f030f147d6d426e5bb3b56328Duncan Sandsstruct RoundUpToPowerOfTwoH {
214af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner  enum { Val = N };
215af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner};
216af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattnertemplate<unsigned N>
217a71f0e1f2a7d6d2f030f147d6d426e5bb3b56328Duncan Sandsstruct RoundUpToPowerOfTwoH<N, false> {
218af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner  enum {
219af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner    // We could just use NextVal = N+1, but this converges faster.  N|(N-1) sets
220af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner    // the right-most zero bits to one all at once, e.g. 0b0011000 -> 0b0011111.
221a71f0e1f2a7d6d2f030f147d6d426e5bb3b56328Duncan Sands    Val = RoundUpToPowerOfTwo<(N|(N-1)) + 1>::Val
222af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner  };
223af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner};
224af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner
225af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattnertemplate<unsigned N>
226a71f0e1f2a7d6d2f030f147d6d426e5bb3b56328Duncan Sandsstruct RoundUpToPowerOfTwo {
227a71f0e1f2a7d6d2f030f147d6d426e5bb3b56328Duncan Sands  enum { Val = RoundUpToPowerOfTwoH<N, (N&(N-1)) == 0>::Val };
228af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner};
2296de603071879bdc5d7d663826354c24a9d176469Chris Lattner
230c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner
231252968a76f4b34aa26aa33d447d0256a148ad554Bob Wilson/// SmallPtrSet - This class implements a set which is optimized for holding
2320b930852cf1a9899ae82dd6c31b43e754a77dcb0Chris Lattner/// SmallSize or less elements.  This internally rounds up SmallSize to the next
2330b930852cf1a9899ae82dd6c31b43e754a77dcb0Chris Lattner/// power of two if it is not already a power of two.  See the comments above
2340b930852cf1a9899ae82dd6c31b43e754a77dcb0Chris Lattner/// SmallPtrSetImpl for details of the algorithm.
235c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnertemplate<class PtrType, unsigned SmallSize>
236c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerclass SmallPtrSet : public SmallPtrSetImpl {
237af3056c97e56106af7c3b9a5c856d1dc1d21e358Chris Lattner  // Make sure that SmallSize is a power of two, round up if not.
238a71f0e1f2a7d6d2f030f147d6d426e5bb3b56328Duncan Sands  enum { SmallSizePowTwo = RoundUpToPowerOfTwo<SmallSize>::Val };
2392a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands  /// SmallStorage - Fixed size storage used in 'small mode'.  The extra element
2402a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands  /// ensures that the end iterator actually points to valid memory.
2412a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands  const void *SmallStorage[SmallSizePowTwo+1];
2428ee1b5e916249d1a64ad8cf99b08a0e160f9d052Chris Lattner  typedef PointerLikeTypeTraits<PtrType> PtrTraits;
243c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattnerpublic:
2442a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands  SmallPtrSet() : SmallPtrSetImpl(SmallStorage, SmallSizePowTwo) {}
2452a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands  SmallPtrSet(const SmallPtrSet &that) : SmallPtrSetImpl(SmallStorage, that) {}
2463a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
247ab8fea5283de0931e1da5dc91b4df2f734ba0206Chris Lattner  template<typename It>
2482a8bf425bd0aff1a6406805c095d99089a1dfaaeDuncan Sands  SmallPtrSet(It I, It E) : SmallPtrSetImpl(SmallStorage, SmallSizePowTwo) {
249086f18626750482318612a10c3027b946b4e36e4Chris Lattner    insert(I, E);
250ab8fea5283de0931e1da5dc91b4df2f734ba0206Chris Lattner  }
2513a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
252373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  /// insert - This returns true if the pointer was new to the set, false if it
253373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  /// was already in the set.
2546de603071879bdc5d7d663826354c24a9d176469Chris Lattner  bool insert(PtrType Ptr) {
2556de603071879bdc5d7d663826354c24a9d176469Chris Lattner    return insert_imp(PtrTraits::getAsVoidPointer(Ptr));
2566de603071879bdc5d7d663826354c24a9d176469Chris Lattner  }
2573a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
258373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  /// erase - If the set contains the specified pointer, remove it and return
259373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  /// true, otherwise return false.
2606de603071879bdc5d7d663826354c24a9d176469Chris Lattner  bool erase(PtrType Ptr) {
2616de603071879bdc5d7d663826354c24a9d176469Chris Lattner    return erase_imp(PtrTraits::getAsVoidPointer(Ptr));
2626de603071879bdc5d7d663826354c24a9d176469Chris Lattner  }
2633a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
264373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  /// count - Return true if the specified pointer is in the set.
2656de603071879bdc5d7d663826354c24a9d176469Chris Lattner  bool count(PtrType Ptr) const {
2666de603071879bdc5d7d663826354c24a9d176469Chris Lattner    return count_imp(PtrTraits::getAsVoidPointer(Ptr));
2676de603071879bdc5d7d663826354c24a9d176469Chris Lattner  }
2683a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
269373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  template <typename IterT>
270373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  void insert(IterT I, IterT E) {
271373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner    for (; I != E; ++I)
272373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner      insert(*I);
273373a733be031f52cebbbcdb15ab5997d9b5f9f17Chris Lattner  }
2743a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
275c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  typedef SmallPtrSetIterator<PtrType> iterator;
276c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  typedef SmallPtrSetIterator<PtrType> const_iterator;
277c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  inline iterator begin() const {
278c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    return iterator(CurArray);
279c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  }
280c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  inline iterator end() const {
281c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner    return iterator(CurArray+CurArraySize);
282c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner  }
2833a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
28491f0158d4d1b16b8615126b05582d421cfb14089Chris Lattner  // Allow assignment from any smallptrset with the same element type even if it
28591f0158d4d1b16b8615126b05582d421cfb14089Chris Lattner  // doesn't have the same smallsize.
2864d6f96d6997738419627ebe13dd6cb9c88a98fd7Owen Anderson  const SmallPtrSet<PtrType, SmallSize>&
28795fd3189f84de5953c75d67403e7db9acdd5005dChris Lattner  operator=(const SmallPtrSet<PtrType, SmallSize> &RHS) {
28891f0158d4d1b16b8615126b05582d421cfb14089Chris Lattner    CopyFrom(RHS);
28991f0158d4d1b16b8615126b05582d421cfb14089Chris Lattner    return *this;
29091f0158d4d1b16b8615126b05582d421cfb14089Chris Lattner  }
29191f0158d4d1b16b8615126b05582d421cfb14089Chris Lattner
2922945a32ffd0bf079de1b23db12bc8a0de596a167Benjamin Kramer  /// swap - Swaps the elements of two sets.
2932945a32ffd0bf079de1b23db12bc8a0de596a167Benjamin Kramer  void swap(SmallPtrSet<PtrType, SmallSize> &RHS) {
2942945a32ffd0bf079de1b23db12bc8a0de596a167Benjamin Kramer    SmallPtrSetImpl::swap(RHS);
2952945a32ffd0bf079de1b23db12bc8a0de596a167Benjamin Kramer  }
296c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner};
297c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner
298c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner}
299c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner
3002945a32ffd0bf079de1b23db12bc8a0de596a167Benjamin Kramernamespace std {
3012945a32ffd0bf079de1b23db12bc8a0de596a167Benjamin Kramer  /// Implement std::swap in terms of SmallPtrSet swap.
3022945a32ffd0bf079de1b23db12bc8a0de596a167Benjamin Kramer  template<class T, unsigned N>
3032945a32ffd0bf079de1b23db12bc8a0de596a167Benjamin Kramer  inline void swap(llvm::SmallPtrSet<T, N> &LHS, llvm::SmallPtrSet<T, N> &RHS) {
3042945a32ffd0bf079de1b23db12bc8a0de596a167Benjamin Kramer    LHS.swap(RHS);
3052945a32ffd0bf079de1b23db12bc8a0de596a167Benjamin Kramer  }
3062945a32ffd0bf079de1b23db12bc8a0de596a167Benjamin Kramer}
3072945a32ffd0bf079de1b23db12bc8a0de596a167Benjamin Kramer
308c95dc987e9e369c1c63819dbc4f76ab9b913772cChris Lattner#endif
309