Convert SetVector to be a true adapter class and add SmallSetVector.
[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 was developed by Reid Spencer and is distributed under
6 // the University of Illinois Open Source 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 <vector>
25 #include <cassert>
26 #include <algorithm>
27
28 namespace llvm {
29
30 /// This adapter class provides a way to keep a set of things that also has the
31 /// property of a deterministic iteration order. The order of iteration is the
32 /// order of insertion.
33 /// @brief A vector that has set insertion semantics.
34 template <typename T, typename Vector = std::vector<T>,
35                       typename Set = std::set<T> >
36 class SetVector {
37 public:
38   typedef T value_type;
39   typedef T key_type;
40   typedef T& reference;
41   typedef const T& const_reference;
42   typedef Set set_type;
43   typedef Vector vector_type;
44   typedef typename vector_type::const_iterator iterator;
45   typedef typename vector_type::const_iterator const_iterator;
46   typedef typename vector_type::size_type size_type;
47
48   /// @brief Construct an empty SetVector
49   SetVector() {}
50
51   /// @brief Initialize a SetVector with a range of elements
52   template<typename It>
53   SetVector(It Start, It End) {
54     insert(Start, End);
55   }
56
57   /// @brief Determine if the SetVector is empty or not.
58   bool empty() const {
59     return vector_.empty();
60   }
61
62   /// @brief Determine the number of elements in the SetVector.
63   size_type size() const {
64     return vector_.size();
65   }
66
67   /// @brief Get an iterator to the beginning of the SetVector.
68   iterator begin() {
69     return vector_.begin();
70   }
71
72   /// @brief Get a const_iterator to the beginning of the SetVector.
73   const_iterator begin() const {
74     return vector_.begin();
75   }
76
77   /// @brief Get an iterator to the end of the SetVector.
78   iterator end() {
79     return vector_.end();
80   }
81
82   /// @brief Get a const_iterator to the end of the SetVector.
83   const_iterator end() const {
84     return vector_.end();
85   }
86
87   /// @brief Return the last element of the SetVector.
88   const T &back() const {
89     assert(!empty() && "Cannot call back() on empty SetVector!");
90     return vector_.back();
91   }
92
93   /// @brief Index into the SetVector.
94   const_reference operator[](size_type n) const {
95     assert(n < vector_.size() && "SetVector access out of range!");
96     return vector_[n];
97   }
98
99   /// @returns true iff the element was inserted into the SetVector.
100   /// @brief Insert a new element into the SetVector.
101   bool insert(const value_type &X) {
102     bool result = set_.insert(X).second;
103     if (result)
104       vector_.push_back(X);
105     return result;
106   }
107
108   /// @brief Insert a range of elements into the SetVector.
109   template<typename It>
110   void insert(It Start, It End) {
111     for (; Start != End; ++Start)
112       if (set_.insert(*Start).second)
113         vector_.push_back(*Start);
114   }
115
116   /// @brief Remove an item from the set vector.
117   void remove(const value_type& X) {
118     if (0 < set_.erase(X)) {
119       typename vector_type::iterator I =
120         std::find(vector_.begin(), vector_.end(), X);
121       assert(I != vector_.end() && "Corrupted SetVector instances!");
122       vector_.erase(I);
123     }
124   }
125
126
127   /// @returns 0 if the element is not in the SetVector, 1 if it is.
128   /// @brief Count the number of elements of a given key in the SetVector.
129   size_type count(const key_type &key) const {
130     return set_.count(key);
131   }
132
133   /// @brief Completely clear the SetVector
134   void clear() {
135     set_.clear();
136     vector_.clear();
137   }
138
139   /// @brief Remove the last element of the SetVector.
140   void pop_back() {
141     assert(!empty() && "Cannot remove an element from an empty SetVector!");
142     set_.erase(back());
143     vector_.pop_back();
144   }
145
146 private:
147   set_type set_;         ///< The set.
148   vector_type vector_;   ///< The vector.
149 };
150
151 /// SmallSetVector - A SetVector that performs no allocations if smaller than
152 /// a certain size.
153 template <typename T, unsigned N>
154 class SmallSetVector : public SetVector<T, SmallVector<T, N>, SmallSet<T, N> > {
155   SmallSetVector() {}
156   
157   /// @brief Initialize a SmallSetVector with a range of elements
158   template<typename It>
159   SmallSetVector(It Start, It End) {
160     this->insert(Start, End);
161   }
162 };
163
164 } // End llvm namespace
165
166 // vim: sw=2 ai
167 #endif