Get rid of some cruft in the insert method.
[oota-llvm.git] / include / llvm / ADT / SetVector.h
1 //===- SetVector.h - A set with insertion 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 //===----------------------------------------------------------------------===//
16
17 #ifndef SUPPORT_SETVECTOR_H
18 #define SUPPORT_SETVECTOR_H
19
20 #include <set>
21 #include <vector>
22
23 namespace llvm {
24
25 /// This class provides a way to keep a set of things that also has the 
26 /// property of a deterministic iteration order. The order of iteration is the
27 /// order of insertion.
28 /// @breif A vector that has set insertion semantics.
29 template <typename T>
30 class SetVector {
31
32 public:
33   typedef T value_type;
34   typedef T key_type;
35   typedef T& reference;
36   typedef const T& const_reference;
37   typedef std::set<value_type> set_type;
38   typedef std::vector<value_type> vector_type;
39   typedef typename vector_type::iterator iterator;
40   typedef typename vector_type::const_iterator const_iterator;
41   typedef typename vector_type::size_type size_type;
42
43   /// @brief Completely clear the SetVector
44   void clear() {
45     set_.clear();
46     vector_.clear();
47   }
48
49   /// @brief Determine if the SetVector is empty or not.
50   bool empty() const {
51     return vector_.empty();
52   }
53
54   /// @brief Determine the number of elements in the SetVector.
55   size_type size() const {
56     return vector_.size();
57   }
58
59   /// @brief Get an iterator to the beginning of the SetVector.
60   iterator begin() {
61     return vector_.begin();
62   }
63
64   /// @brief Get a const_iterator to the beginning of the SetVector.
65   const_iterator begin() const {
66     return vector_.begin();
67   }
68
69   /// @brief Get an iterator to the end of the SetVector.
70   iterator end() {
71     return vector_.end();
72   }
73
74   /// @brief Get a const_iterator to the end of the SetVector.
75   const_iterator end() const {
76     return vector_.end();
77   }
78
79   /// @brief Index into the SetVector.
80   const_reference operator[](size_type n) const {
81       return vector_[n];
82   }
83
84   /// @returns true iff the element was inserted into the SetVector.
85   /// @brief Insert a new element into the SetVector.
86   bool insert( const value_type& X ) {
87     bool result = set_.insert(X).second;
88     if ( result ) {
89       vector_.push_back(X);
90     }
91     return result;
92   }
93
94   /// @returns 0 if the element is not in the SetVector, 1 if it is.
95   /// @brief Count the number of elements of a given key in the SetVector.
96   size_type count( const key_type& key ) const {
97     return set_.count(key);
98   }
99
100 private:
101   set_type set_;         ///< The set.
102   vector_type vector_;   ///< The vector.
103 };
104
105 } // End llvm namespace
106
107 // vim: sw=2 ai
108 #endif