llvm.org GIT mirror llvm / 4bb2867
First version of a vector with uniqueness constraints (or a set with deterministic, insertion-order iteration). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@14702 91177308-0d34-0410-b5e6-96231b3b80d8 Reid Spencer 16 years ago
2 changed file(s) with 216 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 //===- SetVector.h - A set with insertion order iteration -------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by Reid Spencer and is distributed under
5 // the University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements a set that has insertion order iteration
10 // characteristics. This is useful for keeping a set of things that need to be
11 // visited later but in a deterministic order (insertion order). The interface
12 // is purposefully minimal.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef SUPPORT_SETVECTOR_H
17 #define SUPPORT_SETVECTOR_H
18
19 #include
20 #include
21
22 namespace llvm {
23
24 /// This class provides a way to keep a set of things that also has the
25 /// property of a deterministic iteration order. The order of iteration is the
26 /// order of insertion.
27 /// @breif A vector that has set insertion semantics.
28 template
29 class SetVector {
30
31 public:
32 typedef T value_type;
33 typedef T key_type;
34 typedef T& reference;
35 typedef const T& const_reference;
36 typedef std::set set_type;
37 typedef std::vector vector_type;
38 typedef typename vector_type::iterator iterator;
39 typedef typename vector_type::const_iterator const_iterator;
40 typedef typename vector_type::size_type size_type;
41
42 /// @brief Completely clear the SetVector
43 void clear() {
44 set_.clear();
45 vector_.clear();
46 }
47
48 /// @brief Determine if the SetVector is empty or not.
49 bool empty() const {
50 return vector_.empty();
51 }
52
53 /// @brief Determine the number of elements in the SetVector.
54 size_type size() const {
55 return vector_.size();
56 }
57
58 /// @brief Get an iterator to the beginning of the SetVector.
59 iterator begin() {
60 return vector_.begin();
61 }
62
63 /// @brief Get a const_iterator to the beginning of the SetVector.
64 const_iterator begin() const {
65 return vector_.begin();
66 }
67
68 /// @brief Get an iterator to the end of the SetVector.
69 iterator end() {
70 return vector_.end();
71 }
72
73 /// @brief Get a const_iterator to the end of the SetVector.
74 const_iterator end() const {
75 return vector_.end();
76 }
77
78 /// @brief Index into the SetVector.
79 const_reference operator[](size_type n) const {
80 return vector_[n];
81 }
82
83 /// @returns true iff the element was inserted into the SetVector.
84 /// @brief Insert a new element into the SetVector.
85 bool insert( const value_type& X ) {
86 std::pair insertion_result = set_.insert(X);
87 if ( insertion_result.second ) {
88 vector_.push_back(X);
89 }
90 return insertion_result.second;
91 }
92
93 /// @returns 0 if the element is not in the SetVector, 1 if it is.
94 /// @brief Count the number of elements of a given key in the SetVector.
95 size_type count( const key_type& key ) const {
96 return set_.count(key);
97 }
98
99 private:
100 set_type set_; ///< The set.
101 vector_type vector_; ///< The vector.
102 };
103
104 } // End llvm namespace
105
106 // vim: sw=2 ai
107 #endif
0 //===- SetVector.h - A set with insertion order iteration -------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by Reid Spencer and is distributed under
5 // the University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements a set that has insertion order iteration
10 // characteristics. This is useful for keeping a set of things that need to be
11 // visited later but in a deterministic order (insertion order). The interface
12 // is purposefully minimal.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef SUPPORT_SETVECTOR_H
17 #define SUPPORT_SETVECTOR_H
18
19 #include
20 #include
21
22 namespace llvm {
23
24 /// This class provides a way to keep a set of things that also has the
25 /// property of a deterministic iteration order. The order of iteration is the
26 /// order of insertion.
27 /// @breif A vector that has set insertion semantics.
28 template
29 class SetVector {
30
31 public:
32 typedef T value_type;
33 typedef T key_type;
34 typedef T& reference;
35 typedef const T& const_reference;
36 typedef std::set set_type;
37 typedef std::vector vector_type;
38 typedef typename vector_type::iterator iterator;
39 typedef typename vector_type::const_iterator const_iterator;
40 typedef typename vector_type::size_type size_type;
41
42 /// @brief Completely clear the SetVector
43 void clear() {
44 set_.clear();
45 vector_.clear();
46 }
47
48 /// @brief Determine if the SetVector is empty or not.
49 bool empty() const {
50 return vector_.empty();
51 }
52
53 /// @brief Determine the number of elements in the SetVector.
54 size_type size() const {
55 return vector_.size();
56 }
57
58 /// @brief Get an iterator to the beginning of the SetVector.
59 iterator begin() {
60 return vector_.begin();
61 }
62
63 /// @brief Get a const_iterator to the beginning of the SetVector.
64 const_iterator begin() const {
65 return vector_.begin();
66 }
67
68 /// @brief Get an iterator to the end of the SetVector.
69 iterator end() {
70 return vector_.end();
71 }
72
73 /// @brief Get a const_iterator to the end of the SetVector.
74 const_iterator end() const {
75 return vector_.end();
76 }
77
78 /// @brief Index into the SetVector.
79 const_reference operator[](size_type n) const {
80 return vector_[n];
81 }
82
83 /// @returns true iff the element was inserted into the SetVector.
84 /// @brief Insert a new element into the SetVector.
85 bool insert( const value_type& X ) {
86 std::pair insertion_result = set_.insert(X);
87 if ( insertion_result.second ) {
88 vector_.push_back(X);
89 }
90 return insertion_result.second;
91 }
92
93 /// @returns 0 if the element is not in the SetVector, 1 if it is.
94 /// @brief Count the number of elements of a given key in the SetVector.
95 size_type count( const key_type& key ) const {
96 return set_.count(key);
97 }
98
99 private:
100 set_type set_; ///< The set.
101 vector_type vector_; ///< The vector.
102 };
103
104 } // End llvm namespace
105
106 // vim: sw=2 ai
107 #endif