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::size_type size_type;
49 /// \brief Construct an empty SetVector
52 /// \brief Initialize a SetVector with a range of elements
54 SetVector(It Start, It End) {
58 /// \brief Determine if the SetVector is empty or not.
60 return vector_.empty();
63 /// \brief Determine the number of elements in the SetVector.
64 size_type size() const {
65 return vector_.size();
68 /// \brief Get an iterator to the beginning of the SetVector.
70 return vector_.begin();
73 /// \brief Get a const_iterator to the beginning of the SetVector.
74 const_iterator begin() const {
75 return vector_.begin();
78 /// \brief Get an iterator to the end of the SetVector.
83 /// \brief Get a const_iterator to the end of the SetVector.
84 const_iterator end() const {
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();
94 /// \brief Index into the SetVector.
95 const_reference operator[](size_type n) const {
96 assert(n < vector_.size() && "SetVector access out of range!");
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);
105 vector_.push_back(X);
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);
117 /// \brief Remove an item from the set vector.
118 bool remove(const value_type& X) {
120 typename vector_type::iterator I =
121 std::find(vector_.begin(), vector_.end(), X);
122 assert(I != vector_.end() && "Corrupted SetVector instances!");
129 /// \brief Remove items from the set vector based on a predicate function.
131 /// This is intended to be equivalent to the following code, if we could
135 /// V.erase(std::remove_if(V.begin(), V.end(), P), V.end());
138 /// However, SetVector doesn't expose non-const iterators, making any
139 /// algorithm like remove_if impossible to use.
141 /// \returns true if any element is removed.
142 template <typename UnaryPredicate>
143 bool remove_if(UnaryPredicate P) {
144 typename vector_type::iterator I
145 = std::remove_if(vector_.begin(), vector_.end(),
146 TestAndEraseFromSet<UnaryPredicate>(P, set_));
147 if (I == vector_.end())
149 vector_.erase(I, vector_.end());
154 /// \brief Count the number of elements of a given key in the SetVector.
155 /// \returns 0 if the element is not in the SetVector, 1 if it is.
156 size_type count(const key_type &key) const {
157 return set_.count(key);
160 /// \brief Completely clear the SetVector
166 /// \brief Remove the last element of the SetVector.
168 assert(!empty() && "Cannot remove an element from an empty SetVector!");
179 bool operator==(const SetVector &that) const {
180 return vector_ == that.vector_;
183 bool operator!=(const SetVector &that) const {
184 return vector_ != that.vector_;
188 /// \brief A wrapper predicate designed for use with std::remove_if.
190 /// This predicate wraps a predicate suitable for use with std::remove_if to
191 /// call set_.erase(x) on each element which is slated for removal.
192 template <typename UnaryPredicate>
193 class TestAndEraseFromSet {
198 typedef typename UnaryPredicate::argument_type argument_type;
200 TestAndEraseFromSet(UnaryPredicate P, set_type &set_) : P(P), set_(set_) {}
202 bool operator()(argument_type Arg) {
211 set_type set_; ///< The set.
212 vector_type vector_; ///< The vector.
215 /// \brief A SetVector that performs no allocations if smaller than
217 template <typename T, unsigned N>
218 class SmallSetVector : public SetVector<T, SmallVector<T, N>, SmallSet<T, N> > {
222 /// \brief Initialize a SmallSetVector with a range of elements
223 template<typename It>
224 SmallSetVector(It Start, It End) {
225 this->insert(Start, End);
229 } // End llvm namespace