3ab202d8d71c6448276181357f1ffc0784ec64a9
[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/DenseSet.h"
24 #include "llvm/ADT/SmallSet.h"
25 #include <algorithm>
26 #include <cassert>
27 #include <vector>
28
29 namespace llvm {
30
31 /// \brief A vector that has set insertion semantics.
32 ///
33 /// This adapter class provides a way to keep a set of things that also has the
34 /// property of a deterministic iteration order. The order of iteration is the
35 /// order of insertion.
36 template <typename T, typename Vector = std::vector<T>,
37           typename Set = DenseSet<T>>
38 class SetVector {
39 public:
40   typedef T value_type;
41   typedef T key_type;
42   typedef T& reference;
43   typedef const T& const_reference;
44   typedef Set set_type;
45   typedef Vector vector_type;
46   typedef typename vector_type::const_iterator iterator;
47   typedef typename vector_type::const_iterator const_iterator;
48   typedef typename vector_type::const_reverse_iterator reverse_iterator;
49   typedef typename vector_type::const_reverse_iterator const_reverse_iterator;
50   typedef typename vector_type::size_type size_type;
51
52   /// \brief Construct an empty SetVector
53   SetVector() {}
54
55   /// \brief Initialize a SetVector with a range of elements
56   template<typename It>
57   SetVector(It Start, It End) {
58     insert(Start, End);
59   }
60
61   /// \brief Determine if the SetVector is empty or not.
62   bool empty() const {
63     return vector_.empty();
64   }
65
66   /// \brief Determine the number of elements in the SetVector.
67   size_type size() const {
68     return vector_.size();
69   }
70
71   /// \brief Get an iterator to the beginning of the SetVector.
72   iterator begin() {
73     return vector_.begin();
74   }
75
76   /// \brief Get a const_iterator to the beginning of the SetVector.
77   const_iterator begin() const {
78     return vector_.begin();
79   }
80
81   /// \brief Get an iterator to the end of the SetVector.
82   iterator end() {
83     return vector_.end();
84   }
85
86   /// \brief Get a const_iterator to the end of the SetVector.
87   const_iterator end() const {
88     return vector_.end();
89   }
90
91   /// \brief Get an reverse_iterator to the end of the SetVector.
92   reverse_iterator rbegin() {
93     return vector_.rbegin();
94   }
95
96   /// \brief Get a const_reverse_iterator to the end of the SetVector.
97   const_reverse_iterator rbegin() const {
98     return vector_.rbegin();
99   }
100
101   /// \brief Get a reverse_iterator to the beginning of the SetVector.
102   reverse_iterator rend() {
103     return vector_.rend();
104   }
105
106   /// \brief Get a const_reverse_iterator to the beginning of the SetVector.
107   const_reverse_iterator rend() const {
108     return vector_.rend();
109   }
110
111   /// \brief Return the last element of the SetVector.
112   const T &back() const {
113     assert(!empty() && "Cannot call back() on empty SetVector!");
114     return vector_.back();
115   }
116
117   /// \brief Index into the SetVector.
118   const_reference operator[](size_type n) const {
119     assert(n < vector_.size() && "SetVector access out of range!");
120     return vector_[n];
121   }
122
123   /// \brief Insert a new element into the SetVector.
124   /// \returns true iff the element was inserted into the SetVector.
125   bool insert(const value_type &X) {
126     bool result = set_.insert(X).second;
127     if (result)
128       vector_.push_back(X);
129     return result;
130   }
131
132   /// \brief Insert a range of elements into the SetVector.
133   template<typename It>
134   void insert(It Start, It End) {
135     for (; Start != End; ++Start)
136       if (set_.insert(*Start).second)
137         vector_.push_back(*Start);
138   }
139
140   /// \brief Remove an item from the set vector.
141   bool remove(const value_type& X) {
142     if (set_.erase(X)) {
143       typename vector_type::iterator I =
144         std::find(vector_.begin(), vector_.end(), X);
145       assert(I != vector_.end() && "Corrupted SetVector instances!");
146       vector_.erase(I);
147       return true;
148     }
149     return false;
150   }
151
152   /// \brief Remove items from the set vector based on a predicate function.
153   ///
154   /// This is intended to be equivalent to the following code, if we could
155   /// write it:
156   ///
157   /// \code
158   ///   V.erase(std::remove_if(V.begin(), V.end(), P), V.end());
159   /// \endcode
160   ///
161   /// However, SetVector doesn't expose non-const iterators, making any
162   /// algorithm like remove_if impossible to use.
163   ///
164   /// \returns true if any element is removed.
165   template <typename UnaryPredicate>
166   bool remove_if(UnaryPredicate P) {
167     typename vector_type::iterator I
168       = std::remove_if(vector_.begin(), vector_.end(),
169                        TestAndEraseFromSet<UnaryPredicate>(P, set_));
170     if (I == vector_.end())
171       return false;
172     vector_.erase(I, vector_.end());
173     return true;
174   }
175
176   /// \brief Count the number of elements of a given key in the SetVector.
177   /// \returns 0 if the element is not in the SetVector, 1 if it is.
178   size_type count(const key_type &key) const {
179     return set_.count(key);
180   }
181
182   /// \brief Completely clear the SetVector
183   void clear() {
184     set_.clear();
185     vector_.clear();
186   }
187
188   /// \brief Remove the last element of the SetVector.
189   void pop_back() {
190     assert(!empty() && "Cannot remove an element from an empty SetVector!");
191     set_.erase(back());
192     vector_.pop_back();
193   }
194
195   T LLVM_ATTRIBUTE_UNUSED_RESULT pop_back_val() {
196     T Ret = back();
197     pop_back();
198     return Ret;
199   }
200
201   bool operator==(const SetVector &that) const {
202     return vector_ == that.vector_;
203   }
204
205   bool operator!=(const SetVector &that) const {
206     return vector_ != that.vector_;
207   }
208
209 private:
210   /// \brief A wrapper predicate designed for use with std::remove_if.
211   ///
212   /// This predicate wraps a predicate suitable for use with std::remove_if to
213   /// call set_.erase(x) on each element which is slated for removal.
214   template <typename UnaryPredicate>
215   class TestAndEraseFromSet {
216     UnaryPredicate P;
217     set_type &set_;
218
219   public:
220     TestAndEraseFromSet(UnaryPredicate P, set_type &set_) : P(P), set_(set_) {}
221
222     template <typename ArgumentT>
223     bool operator()(const ArgumentT &Arg) {
224       if (P(Arg)) {
225         set_.erase(Arg);
226         return true;
227       }
228       return false;
229     }
230   };
231
232   set_type set_;         ///< The set.
233   vector_type vector_;   ///< The vector.
234 };
235
236 /// \brief A SetVector that performs no allocations if smaller than
237 /// a certain size.
238 template <typename T, unsigned N>
239 class SmallSetVector : public SetVector<T, SmallVector<T, N>, SmallSet<T, N> > {
240 public:
241   SmallSetVector() {}
242
243   /// \brief Initialize a SmallSetVector with a range of elements
244   template<typename It>
245   SmallSetVector(It Start, It End) {
246     this->insert(Start, End);
247   }
248 };
249
250 } // End llvm namespace
251
252 // vim: sw=2 ai
253 #endif