Teach the new SROA to handle cases where an alloca that has already been
[oota-llvm.git] / include / llvm / ADT / SetVector.h
1 //===- llvm/ADT/SetVector.h - Set with insert order iteration ---*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements a set that has insertion order iteration
11 // characteristics. This is useful for keeping a set of things that need to be
12 // visited later but in a deterministic order (insertion order). The interface
13 // is purposefully minimal.
14 //
15 // This file defines SetVector and SmallSetVector, which performs no allocations
16 // if the SetVector has less than a certain number of elements.
17 //
18 //===----------------------------------------------------------------------===//
19
20 #ifndef LLVM_ADT_SETVECTOR_H
21 #define LLVM_ADT_SETVECTOR_H
22
23 #include "llvm/ADT/SmallSet.h"
24 #include <algorithm>
25 #include <cassert>
26 #include <vector>
27
28 namespace llvm {
29
30 /// \brief A vector that has set insertion semantics.
31 ///
32 /// This adapter class provides a way to keep a set of things that also has the
33 /// property of a deterministic iteration order. The order of iteration is the
34 /// order of insertion.
35 template <typename T, typename Vector = std::vector<T>,
36                       typename Set = SmallSet<T, 16> >
37 class SetVector {
38 public:
39   typedef T value_type;
40   typedef T key_type;
41   typedef T& reference;
42   typedef const T& const_reference;
43   typedef Set set_type;
44   typedef Vector vector_type;
45   typedef typename vector_type::const_iterator iterator;
46   typedef typename vector_type::const_iterator const_iterator;
47   typedef typename vector_type::size_type size_type;
48
49   /// \brief Construct an empty SetVector
50   SetVector() {}
51
52   /// \brief Initialize a SetVector with a range of elements
53   template<typename It>
54   SetVector(It Start, It End) {
55     insert(Start, End);
56   }
57
58   /// \brief Determine if the SetVector is empty or not.
59   bool empty() const {
60     return vector_.empty();
61   }
62
63   /// \brief Determine the number of elements in the SetVector.
64   size_type size() const {
65     return vector_.size();
66   }
67
68   /// \brief Get an iterator to the beginning of the SetVector.
69   iterator begin() {
70     return vector_.begin();
71   }
72
73   /// \brief Get a const_iterator to the beginning of the SetVector.
74   const_iterator begin() const {
75     return vector_.begin();
76   }
77
78   /// \brief Get an iterator to the end of the SetVector.
79   iterator end() {
80     return vector_.end();
81   }
82
83   /// \brief Get a const_iterator to the end of the SetVector.
84   const_iterator end() const {
85     return vector_.end();
86   }
87
88   /// \brief Return the last element of the SetVector.
89   const T &back() const {
90     assert(!empty() && "Cannot call back() on empty SetVector!");
91     return vector_.back();
92   }
93
94   /// \brief Index into the SetVector.
95   const_reference operator[](size_type n) const {
96     assert(n < vector_.size() && "SetVector access out of range!");
97     return vector_[n];
98   }
99
100   /// \brief Insert a new element into the SetVector.
101   /// \returns true iff the element was inserted into the SetVector.
102   bool insert(const value_type &X) {
103     bool result = set_.insert(X);
104     if (result)
105       vector_.push_back(X);
106     return result;
107   }
108
109   /// \brief Insert a range of elements into the SetVector.
110   template<typename It>
111   void insert(It Start, It End) {
112     for (; Start != End; ++Start)
113       if (set_.insert(*Start))
114         vector_.push_back(*Start);
115   }
116
117   /// \brief Remove an item from the set vector.
118   bool remove(const value_type& X) {
119     if (set_.erase(X)) {
120       typename vector_type::iterator I =
121         std::find(vector_.begin(), vector_.end(), X);
122       assert(I != vector_.end() && "Corrupted SetVector instances!");
123       vector_.erase(I);
124       return true;
125     }
126     return false;
127   }
128
129   /// \brief Remove items from the set vector based on a predicate function.
130   ///
131   /// This is intended to be equivalent to the following code, if we could
132   /// write it:
133   ///
134   /// \code
135   ///   V.erase(std::remove_if(V.begin(), V.end(), P), V.end());
136   /// \endcode
137   ///
138   /// However, SetVector doesn't expose non-const iterators, making any
139   /// algorithm like remove_if impossible to use.
140   ///
141   /// \returns true if any element is removed.
142   template <typename UnaryPredicate>
143   bool remove_if(UnaryPredicate P) {
144     typename vector_type::iterator B = std::remove_if(vector_.begin(),
145                                                       vector_.end(), P),
146                                    E = vector_.end();
147     if (B == E)
148       return false;
149     for (typename vector_type::iterator I = B; I != E; ++I)
150       set_.erase(*I);
151     vector_.erase(B, E);
152     return true;
153   }
154
155
156   /// \brief Count the number of elements of a given key in the SetVector.
157   /// \returns 0 if the element is not in the SetVector, 1 if it is.
158   size_type count(const key_type &key) const {
159     return set_.count(key);
160   }
161
162   /// \brief Completely clear the SetVector
163   void clear() {
164     set_.clear();
165     vector_.clear();
166   }
167
168   /// \brief Remove the last element of the SetVector.
169   void pop_back() {
170     assert(!empty() && "Cannot remove an element from an empty SetVector!");
171     set_.erase(back());
172     vector_.pop_back();
173   }
174   
175   T pop_back_val() {
176     T Ret = back();
177     pop_back();
178     return Ret;
179   }
180
181   bool operator==(const SetVector &that) const {
182     return vector_ == that.vector_;
183   }
184
185   bool operator!=(const SetVector &that) const {
186     return vector_ != that.vector_;
187   }
188
189 private:
190   set_type set_;         ///< The set.
191   vector_type vector_;   ///< The vector.
192 };
193
194 /// \brief A SetVector that performs no allocations if smaller than
195 /// a certain size.
196 template <typename T, unsigned N>
197 class SmallSetVector : public SetVector<T, SmallVector<T, N>, SmallSet<T, N> > {
198 public:
199   SmallSetVector() {}
200
201   /// \brief Initialize a SmallSetVector with a range of elements
202   template<typename It>
203   SmallSetVector(It Start, It End) {
204     this->insert(Start, End);
205   }
206 };
207
208 } // End llvm namespace
209
210 // vim: sw=2 ai
211 #endif