@@ -47,111 +47,132 @@ namespace swift {
47
47
template <typename ValueT, typename VectorT = std::vector<Optional<ValueT>>,
48
48
typename MapT = llvm::DenseMap<ValueT, unsigned >>
49
49
class BlotSetVector {
50
- VectorT Vector ;
51
- MapT Map ;
50
+ VectorT vector ;
51
+ MapT map ;
52
52
53
53
public:
54
54
// / Construct an empty BlotSetVector.
55
55
BlotSetVector () {}
56
56
57
- bool empty () const { return Vector .empty (); }
57
+ bool empty () const { return vector .empty (); }
58
58
59
- unsigned size () const { return Vector .size (); }
59
+ unsigned size () const { return vector .size (); }
60
60
61
61
using iterator = typename VectorT::iterator;
62
62
using const_iterator = typename VectorT::const_iterator;
63
- iterator begin () { return Vector .begin (); }
64
- iterator end () { return Vector .end (); }
65
- const_iterator begin () const { return Vector .begin (); }
66
- const_iterator end () const { return Vector .end (); }
63
+ iterator begin () { return vector .begin (); }
64
+ iterator end () { return vector .end (); }
65
+ const_iterator begin () const { return vector .begin (); }
66
+ const_iterator end () const { return vector .end (); }
67
67
llvm::iterator_range<const_iterator> getRange () const {
68
68
return {begin (), end ()};
69
69
}
70
70
71
71
using const_reverse_iterator = typename VectorT::const_reverse_iterator;
72
- const_reverse_iterator rbegin () const { return Vector .rbegin (); }
73
- const_reverse_iterator rend () const { return Vector .rend (); }
72
+ const_reverse_iterator rbegin () const { return vector .rbegin (); }
73
+ const_reverse_iterator rend () const { return vector .rend (); }
74
74
llvm::iterator_range<const_reverse_iterator> getReverseRange () const {
75
75
return {rbegin (), rend ()};
76
76
}
77
77
78
78
const Optional<ValueT> &operator [](unsigned n) const {
79
- assert (n < Vector .size () && " Out of range!" );
80
- return Vector [n];
79
+ assert (n < vector .size () && " Out of range!" );
80
+ return vector [n];
81
81
}
82
82
83
- // / Insert \p V into the SetVector if it is not in the array and return the
84
- // / index of \p V in the Set Vector. If \p V is already in the SetVector, just
85
- // / return its index in the array.
86
- unsigned insert (const ValueT &V) {
87
- auto Iter = Map.find (V);
88
- if (Iter != Map.end ())
89
- return Iter->second ;
90
-
91
- unsigned Index = Vector.size ();
92
- Map[V] = Index;
93
- Vector.push_back (V);
94
- return Index;
83
+ // / Grow the set vector so that it can contain at least \p capacity items
84
+ // / before resizing again.
85
+ // /
86
+ // / \param capacity The minimum size that the set vector will be able to grow
87
+ // / to before a resize is required to insert additional items.
88
+ void reserve (unsigned capacity) {
89
+ vector.reserve (capacity);
90
+ map.reserve (capacity);
95
91
}
96
92
97
- // / Replace \p V1 with \p V2 placing \p V2 into the position in the array
98
- // / where V1 used to be. If \p V2 is already in the set, this just erases \p
99
- // / V1.
100
- void replace (const ValueT &V1, const ValueT &V2) {
101
- auto Iter1 = Map.find (V1);
102
- assert (Iter1 != Map.end () && " Cannot replace value that is not in set" );
103
- unsigned V1Index = Iter1->second ;
104
- Map.erase (V1);
105
-
106
- auto Iter2 = Map.find (V2);
107
- if (Iter2 != Map.end ()) {
108
- Vector[V1Index] = None;
93
+ // / Insert \p value into the SetVector if it is not there already.
94
+ // /
95
+ // / \param value The item to insert if not already present.
96
+ // /
97
+ // / \returns Both the index of the item and whether it was inserted in a pair.
98
+ // / If the item was already present, its preexisting index along with
99
+ // / false will be returned. If the item was newly added, its new
100
+ // / index along with true will be returned.
101
+ std::pair<unsigned , bool > insert (const ValueT &value) {
102
+ auto iterator = map.find (value);
103
+ if (iterator != map.end ())
104
+ return {iterator->second , false };
105
+
106
+ unsigned index = vector.size ();
107
+ map[value] = index;
108
+ vector.push_back (value);
109
+ return {index, true };
110
+ }
111
+
112
+ // / Replace \p value1 with \p value2 placing \p value2 into the position in
113
+ // / the array where value1 used to be. If \p value2 is already in the set,
114
+ // / this just erases \p value1.
115
+ void replace (const ValueT &value1, const ValueT &value2) {
116
+ auto iterator1 = map.find (value1);
117
+ assert (iterator1 != map.end () && " Cannot replace value that is not in set" );
118
+ unsigned index1 = iterator1->second ;
119
+ map.erase (value1);
120
+
121
+ auto iterator2 = map.find (value2);
122
+ if (iterator2 != map.end ()) {
123
+ vector[index1] = None;
109
124
return ;
110
125
}
111
126
112
- Map[V2 ] = V1Index ;
113
- Vector[V1Index ] = V2 ;
127
+ map[value2 ] = index1 ;
128
+ vector[index1 ] = value2 ;
114
129
}
115
130
116
- // / Erase the value \p V if it is in the set. Returns true if V was
131
+ // / Erase the value \p value if it is in the set. Returns true if value was
117
132
// / successfully erased and false otherwise.
118
- bool erase (const ValueT &V ) {
119
- auto Iter = Map .find (V );
120
- if (Iter == Map .end ())
133
+ bool erase (const ValueT &value ) {
134
+ auto iterator = map .find (value );
135
+ if (iterator == map .end ())
121
136
return false ;
122
- unsigned Index = Iter ->second ;
123
- Map .erase (Iter );
124
- Vector[Index ] = None;
137
+ unsigned index = iterator ->second ;
138
+ map .erase (iterator );
139
+ vector[index ] = None;
125
140
return true ;
126
141
}
127
142
128
143
// / Return the last element of the blot map vector. Will be None if blotted.
129
144
Optional<ValueT> pop_back_val () {
130
- auto result = Vector .pop_back_val ();
145
+ auto result = vector .pop_back_val ();
131
146
if (!result)
132
147
return result;
133
- Map .erase (*result);
148
+ map .erase (*result);
134
149
return result;
135
150
}
136
151
137
- // / Attempt to lookup the index of \p V . Returns None upon failure and the
152
+ // / Attempt to lookup the index of \p value . Returns None upon failure and the
138
153
// / value on success.
139
- Optional<unsigned > getIndex (const ValueT &V ) {
140
- auto Iter = Map .find (V );
141
- if (Iter == Map .end ())
154
+ Optional<unsigned > getIndex (const ValueT &value ) {
155
+ auto iterator = map .find (value );
156
+ if (iterator == map .end ())
142
157
return None;
143
- return Iter->second ;
158
+ return iterator->second ;
159
+ }
160
+
161
+ // / Clear the backing vector and map.
162
+ void clear () {
163
+ vector.clear ();
164
+ map.clear ();
144
165
}
145
166
};
146
167
147
- template <typename ValueT, unsigned N,
148
- typename VectorT = llvm::SmallVector<Optional<ValueT>, N>,
149
- typename MapT = llvm::SmallDenseMap< ValueT, unsigned , N>>
150
- class SmallBlotSetVector : public BlotSetVector <ValueT, VectorT, MapT > {
168
+ template <typename ValueT, unsigned N>
169
+ class SmallBlotSetVector
170
+ : public BlotSetVector<ValueT, llvm::SmallVector<Optional< ValueT>, N>,
171
+ llvm::SmallDenseMap <ValueT, unsigned , N> > {
151
172
public:
152
173
SmallBlotSetVector () {}
153
174
};
154
175
155
- } // end swift namespace
176
+ } // namespace swift
156
177
157
178
#endif // SWIFT_BASIC_BLOTSETVECTOR_H
0 commit comments