LLVM  4.0.0
SetVector.h
Go to the documentation of this file.
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/ArrayRef.h"
24 #include "llvm/ADT/DenseSet.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/Support/Compiler.h"
27 #include <algorithm>
28 #include <cassert>
29 #include <iterator>
30 #include <vector>
31 
32 namespace llvm {
33 
34 /// \brief A vector that has set insertion semantics.
35 ///
36 /// This adapter class provides a way to keep a set of things that also has the
37 /// property of a deterministic iteration order. The order of iteration is the
38 /// order of insertion.
39 template <typename T, typename Vector = std::vector<T>,
40  typename Set = DenseSet<T>>
41 class SetVector {
42 public:
43  typedef T value_type;
44  typedef T key_type;
45  typedef T& reference;
46  typedef const T& const_reference;
47  typedef Set set_type;
48  typedef Vector vector_type;
54 
55  /// \brief Construct an empty SetVector
56  SetVector() = default;
57 
58  /// \brief Initialize a SetVector with a range of elements
59  template<typename It>
60  SetVector(It Start, It End) {
61  insert(Start, End);
62  }
63 
64  ArrayRef<T> getArrayRef() const { return vector_; }
65 
66  /// Clear the SetVector and return the underlying vector.
67  Vector takeVector() {
68  set_.clear();
69  return std::move(vector_);
70  }
71 
72  /// \brief Determine if the SetVector is empty or not.
73  bool empty() const {
74  return vector_.empty();
75  }
76 
77  /// \brief Determine the number of elements in the SetVector.
78  size_type size() const {
79  return vector_.size();
80  }
81 
82  /// \brief Get an iterator to the beginning of the SetVector.
84  return vector_.begin();
85  }
86 
87  /// \brief Get a const_iterator to the beginning of the SetVector.
89  return vector_.begin();
90  }
91 
92  /// \brief Get an iterator to the end of the SetVector.
94  return vector_.end();
95  }
96 
97  /// \brief Get a const_iterator to the end of the SetVector.
98  const_iterator end() const {
99  return vector_.end();
100  }
101 
102  /// \brief Get an reverse_iterator to the end of the SetVector.
104  return vector_.rbegin();
105  }
106 
107  /// \brief Get a const_reverse_iterator to the end of the SetVector.
109  return vector_.rbegin();
110  }
111 
112  /// \brief Get a reverse_iterator to the beginning of the SetVector.
114  return vector_.rend();
115  }
116 
117  /// \brief Get a const_reverse_iterator to the beginning of the SetVector.
119  return vector_.rend();
120  }
121 
122  /// \brief Return the last element of the SetVector.
123  const T &back() const {
124  assert(!empty() && "Cannot call back() on empty SetVector!");
125  return vector_.back();
126  }
127 
128  /// \brief Index into the SetVector.
130  assert(n < vector_.size() && "SetVector access out of range!");
131  return vector_[n];
132  }
133 
134  /// \brief Insert a new element into the SetVector.
135  /// \returns true if the element was inserted into the SetVector.
136  bool insert(const value_type &X) {
137  bool result = set_.insert(X).second;
138  if (result)
139  vector_.push_back(X);
140  return result;
141  }
142 
143  /// \brief Insert a range of elements into the SetVector.
144  template<typename It>
145  void insert(It Start, It End) {
146  for (; Start != End; ++Start)
147  if (set_.insert(*Start).second)
148  vector_.push_back(*Start);
149  }
150 
151  /// \brief Remove an item from the set vector.
152  bool remove(const value_type& X) {
153  if (set_.erase(X)) {
154  typename vector_type::iterator I = find(vector_, X);
155  assert(I != vector_.end() && "Corrupted SetVector instances!");
156  vector_.erase(I);
157  return true;
158  }
159  return false;
160  }
161 
162  /// Erase a single element from the set vector.
163  /// \returns an iterator pointing to the next element that followed the
164  /// element erased. This is the end of the SetVector if the last element is
165  /// erased.
167  const key_type &V = *I;
168  assert(set_.count(V) && "Corrupted SetVector instances!");
169  set_.erase(V);
170 
171  // FIXME: No need to use the non-const iterator when built with
172  // std:vector.erase(const_iterator) as defined in C++11. This is for
173  // compatibility with non-standard libstdc++ up to 4.8 (fixed in 4.9).
174  auto NI = vector_.begin();
175  std::advance(NI, std::distance<iterator>(NI, I));
176 
177  return vector_.erase(NI);
178  }
179 
180  /// \brief Remove items from the set vector based on a predicate function.
181  ///
182  /// This is intended to be equivalent to the following code, if we could
183  /// write it:
184  ///
185  /// \code
186  /// V.erase(remove_if(V, P), V.end());
187  /// \endcode
188  ///
189  /// However, SetVector doesn't expose non-const iterators, making any
190  /// algorithm like remove_if impossible to use.
191  ///
192  /// \returns true if any element is removed.
193  template <typename UnaryPredicate>
194  bool remove_if(UnaryPredicate P) {
195  typename vector_type::iterator I =
196  llvm::remove_if(vector_, TestAndEraseFromSet<UnaryPredicate>(P, set_));
197  if (I == vector_.end())
198  return false;
199  vector_.erase(I, vector_.end());
200  return true;
201  }
202 
203  /// \brief Count the number of elements of a given key in the SetVector.
204  /// \returns 0 if the element is not in the SetVector, 1 if it is.
205  size_type count(const key_type &key) const {
206  return set_.count(key);
207  }
208 
209  /// \brief Completely clear the SetVector
210  void clear() {
211  set_.clear();
212  vector_.clear();
213  }
214 
215  /// \brief Remove the last element of the SetVector.
216  void pop_back() {
217  assert(!empty() && "Cannot remove an element from an empty SetVector!");
218  set_.erase(back());
219  vector_.pop_back();
220  }
221 
223  T Ret = back();
224  pop_back();
225  return Ret;
226  }
227 
228  bool operator==(const SetVector &that) const {
229  return vector_ == that.vector_;
230  }
231 
232  bool operator!=(const SetVector &that) const {
233  return vector_ != that.vector_;
234  }
235 
236  /// \brief Compute This := This u S, return whether 'This' changed.
237  /// TODO: We should be able to use set_union from SetOperations.h, but
238  /// SetVector interface is inconsistent with DenseSet.
239  template <class STy>
240  bool set_union(const STy &S) {
241  bool Changed = false;
242 
243  for (typename STy::const_iterator SI = S.begin(), SE = S.end(); SI != SE;
244  ++SI)
245  if (insert(*SI))
246  Changed = true;
247 
248  return Changed;
249  }
250 
251  /// \brief Compute This := This - B
252  /// TODO: We should be able to use set_subtract from SetOperations.h, but
253  /// SetVector interface is inconsistent with DenseSet.
254  template <class STy>
255  void set_subtract(const STy &S) {
256  for (typename STy::const_iterator SI = S.begin(), SE = S.end(); SI != SE;
257  ++SI)
258  remove(*SI);
259  }
260 
261 private:
262  /// \brief A wrapper predicate designed for use with std::remove_if.
263  ///
264  /// This predicate wraps a predicate suitable for use with std::remove_if to
265  /// call set_.erase(x) on each element which is slated for removal.
266  template <typename UnaryPredicate>
267  class TestAndEraseFromSet {
268  UnaryPredicate P;
269  set_type &set_;
270 
271  public:
272  TestAndEraseFromSet(UnaryPredicate P, set_type &set_)
273  : P(std::move(P)), set_(set_) {}
274 
275  template <typename ArgumentT>
276  bool operator()(const ArgumentT &Arg) {
277  if (P(Arg)) {
278  set_.erase(Arg);
279  return true;
280  }
281  return false;
282  }
283  };
284 
285  set_type set_; ///< The set.
286  vector_type vector_; ///< The vector.
287 };
288 
289 /// \brief A SetVector that performs no allocations if smaller than
290 /// a certain size.
291 template <typename T, unsigned N>
293  : public SetVector<T, SmallVector<T, N>, SmallDenseSet<T, N>> {
294 public:
295  SmallSetVector() = default;
296 
297  /// \brief Initialize a SmallSetVector with a range of elements
298  template<typename It>
299  SmallSetVector(It Start, It End) {
300  this->insert(Start, End);
301  }
302 };
303 
304 } // end namespace llvm
305 
306 #endif // LLVM_ADT_SETVECTOR_H
const_iterator end() const
Get a const_iterator to the end of the SetVector.
Definition: SetVector.h:98
LLVM_NODISCARD T pop_back_val()
Definition: SetVector.h:222
auto remove_if(R &&Range, UnaryPredicate P) -> decltype(std::begin(Range))
Provide wrappers to std::remove_if which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:776
const_reverse_iterator rbegin() const
Get a const_reverse_iterator to the end of the SetVector.
Definition: SetVector.h:108
iterator erase(iterator I)
Erase a single element from the set vector.
Definition: SetVector.h:166
vector_type::const_iterator const_iterator
Definition: SetVector.h:50
iterator end()
Get an iterator to the end of the SetVector.
Definition: SetVector.h:93
SetVector()=default
Construct an empty SetVector.
vector_type::size_type size_type
Definition: SetVector.h:53
size_type size() const
Determine the number of elements in the SetVector.
Definition: SetVector.h:78
const_iterator begin() const
Get a const_iterator to the beginning of the SetVector.
Definition: SetVector.h:88
const_reference operator[](size_type n) const
Index into the SetVector.
Definition: SetVector.h:129
SmallSetVector()=default
static void advance(T &it, size_t Val)
reverse_iterator rbegin()
Get an reverse_iterator to the end of the SetVector.
Definition: SetVector.h:103
void pop_back()
Remove the last element of the SetVector.
Definition: SetVector.h:216
bool operator==(const SetVector &that) const
Definition: SetVector.h:228
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:136
iterator begin()
Get an iterator to the beginning of the SetVector.
Definition: SetVector.h:83
bool empty() const
Determine if the SetVector is empty or not.
Definition: SetVector.h:73
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
#define P(N)
This is an important base class in LLVM.
Definition: Constant.h:42
bool set_union(const STy &S)
Compute This := This u S, return whether 'This' changed.
Definition: SetVector.h:240
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
static const unsigned End
void insert(It Start, It End)
Insert a range of elements into the SetVector.
Definition: SetVector.h:145
Vector vector_type
Definition: SetVector.h:48
SetVector(It Start, It End)
Initialize a SetVector with a range of elements.
Definition: SetVector.h:60
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:292
Vector takeVector()
Clear the SetVector and return the underlying vector.
Definition: SetVector.h:67
auto find(R &&Range, const T &Val) -> decltype(std::begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:757
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: SmallVector.h:105
void set_subtract(const STy &S)
Compute This := This - B TODO: We should be able to use set_subtract from SetOperations.h, but SetVector interface is inconsistent with DenseSet.
Definition: SetVector.h:255
const_reverse_iterator rend() const
Get a const_reverse_iterator to the beginning of the SetVector.
Definition: SetVector.h:118
bool operator!=(const SetVector &that) const
Definition: SetVector.h:232
void clear()
Completely clear the SetVector.
Definition: SetVector.h:210
size_type count(const key_type &key) const
Count the number of elements of a given key in the SetVector.
Definition: SetVector.h:205
reverse_iterator rend()
Get a reverse_iterator to the beginning of the SetVector.
Definition: SetVector.h:113
#define I(x, y, z)
Definition: MD5.cpp:54
vector_type::const_reverse_iterator reverse_iterator
Definition: SetVector.h:51
const T & back() const
Return the last element of the SetVector.
Definition: SetVector.h:123
#define LLVM_NODISCARD
LLVM_NODISCARD - Warn if a type or return value is discarded.
Definition: Compiler.h:132
vector_type::const_reverse_iterator const_reverse_iterator
Definition: SetVector.h:52
const T & const_reference
Definition: SetVector.h:46
ArrayRef< T > getArrayRef() const
Definition: SetVector.h:64
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
SmallSetVector(It Start, It End)
Initialize a SmallSetVector with a range of elements.
Definition: SetVector.h:299
vector_type::const_iterator iterator
Definition: SetVector.h:49
A vector that has set insertion semantics.
Definition: SetVector.h:41
bool remove_if(UnaryPredicate P)
Remove items from the set vector based on a predicate function.
Definition: SetVector.h:194