1 //===- llvm/ADT/SetVector.h - Set with insert order iteration ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
15 // This file defines SetVector and SmallSetVector, which performs no allocations
16 // if the SetVector has less than a certain number of elements.
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_ADT_SETVECTOR_H
21 #define LLVM_ADT_SETVECTOR_H
23 #include "llvm/ADT/SmallSet.h"
30 /// \brief A vector that has set insertion semantics.
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> >
42 typedef const T& const_reference;
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::const_reverse_iterator reverse_iterator;
48 typedef typename vector_type::const_reverse_iterator const_reverse_iterator;
49 typedef typename vector_type::size_type size_type;
51 /// \brief Construct an empty SetVector
54 /// \brief Initialize a SetVector with a range of elements
56 SetVector(It Start, It End) {
60 /// \brief Determine if the SetVector is empty or not.
62 return vector_.empty();
65 /// \brief Determine the number of elements in the SetVector.
66 size_type size() const {
67 return vector_.size();
70 /// \brief Get an iterator to the beginning of the SetVector.
72 return vector_.begin();
75 /// \brief Get a const_iterator to the beginning of the SetVector.
76 const_iterator begin() const {
77 return vector_.begin();
80 /// \brief Get an iterator to the end of the SetVector.
85 /// \brief Get a const_iterator to the end of the SetVector.
86 const_iterator end() const {
90 /// \brief Get an reverse_iterator to the end of the SetVector.
91 reverse_iterator rbegin() {
92 return vector_.rbegin();
95 /// \brief Get a const_reverse_iterator to the end of the SetVector.
96 const_reverse_iterator rbegin() const {
97 return vector_.rbegin();
100 /// \brief Get a reverse_iterator to the beginning of the SetVector.
101 reverse_iterator rend() {
102 return vector_.rend();
105 /// \brief Get a const_reverse_iterator to the beginning of the SetVector.
106 const_reverse_iterator rend() const {
107 return vector_.rend();
110 /// \brief Return the last element of the SetVector.
111 const T &back() const {
112 assert(!empty() && "Cannot call back() on empty SetVector!");
113 return vector_.back();
116 /// \brief Index into the SetVector.
117 const_reference operator[](size_type n) const {
118 assert(n < vector_.size() && "SetVector access out of range!");
122 /// \brief Insert a new element into the SetVector.
123 /// \returns true iff the element was inserted into the SetVector.
124 bool insert(const value_type &X) {
125 bool result = set_.insert(X).second;
127 vector_.push_back(X);
131 /// \brief Insert a range of elements into the SetVector.
132 template<typename It>
133 void insert(It Start, It End) {
134 for (; Start != End; ++Start)
135 if (set_.insert(*Start).second)
136 vector_.push_back(*Start);
139 /// \brief Remove an item from the set vector.
140 bool remove(const value_type& X) {
142 typename vector_type::iterator I =
143 std::find(vector_.begin(), vector_.end(), X);
144 assert(I != vector_.end() && "Corrupted SetVector instances!");
151 /// \brief Remove items from the set vector based on a predicate function.
153 /// This is intended to be equivalent to the following code, if we could
157 /// V.erase(std::remove_if(V.begin(), V.end(), P), V.end());
160 /// However, SetVector doesn't expose non-const iterators, making any
161 /// algorithm like remove_if impossible to use.
163 /// \returns true if any element is removed.
164 template <typename UnaryPredicate>
165 bool remove_if(UnaryPredicate P) {
166 typename vector_type::iterator I
167 = std::remove_if(vector_.begin(), vector_.end(),
168 TestAndEraseFromSet<UnaryPredicate>(P, set_));
169 if (I == vector_.end())
171 vector_.erase(I, vector_.end());
175 /// \brief Count the number of elements of a given key in the SetVector.
176 /// \returns 0 if the element is not in the SetVector, 1 if it is.
177 size_type count(const key_type &key) const {
178 return set_.count(key);
181 /// \brief Completely clear the SetVector
187 /// \brief Remove the last element of the SetVector.
189 assert(!empty() && "Cannot remove an element from an empty SetVector!");
194 T LLVM_ATTRIBUTE_UNUSED_RESULT pop_back_val() {
200 bool operator==(const SetVector &that) const {
201 return vector_ == that.vector_;
204 bool operator!=(const SetVector &that) const {
205 return vector_ != that.vector_;
209 /// \brief A wrapper predicate designed for use with std::remove_if.
211 /// This predicate wraps a predicate suitable for use with std::remove_if to
212 /// call set_.erase(x) on each element which is slated for removal.
213 template <typename UnaryPredicate>
214 class TestAndEraseFromSet {
219 TestAndEraseFromSet(UnaryPredicate P, set_type &set_) : P(P), set_(set_) {}
221 template <typename ArgumentT>
222 bool operator()(const ArgumentT &Arg) {
231 set_type set_; ///< The set.
232 vector_type vector_; ///< The vector.
235 /// \brief A SetVector that performs no allocations if smaller than
237 template <typename T, unsigned N>
238 class SmallSetVector : public SetVector<T, SmallVector<T, N>, SmallSet<T, N> > {
242 /// \brief Initialize a SmallSetVector with a range of elements
243 template<typename It>
244 SmallSetVector(It Start, It End) {
245 this->insert(Start, End);
249 } // End llvm namespace