Skip to content

Commit a2438ce

Browse files
committed
[ADT] Use range-based helper functions in SmallSet
Replace code that relies on iterators by LLVM helper functions that take ranges. This makes the code simpler and more readable.
1 parent 58c88d6 commit a2438ce

File tree

1 file changed

+10
-26
lines changed

1 file changed

+10
-26
lines changed

llvm/include/llvm/ADT/SmallSet.h

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,6 @@ class SmallSet {
139139
SmallVector<T, N> Vector;
140140
std::set<T, C> Set;
141141

142-
using VIterator = typename SmallVector<T, N>::const_iterator;
143-
using SIterator = typename std::set<T, C>::const_iterator;
144-
using mutable_iterator = typename SmallVector<T, N>::iterator;
145-
146142
// In small mode SmallPtrSet uses linear search for the elements, so it is
147143
// not a good idea to choose this value too high. You may consider using a
148144
// DenseSet<> instead if you expect many elements in the set.
@@ -163,13 +159,7 @@ class SmallSet {
163159
}
164160

165161
/// count - Return 1 if the element is in the set, 0 otherwise.
166-
size_type count(const T &V) const {
167-
if (isSmall()) {
168-
// Since the collection is small, just do a linear search.
169-
return vfind(V) == Vector.end() ? 0 : 1;
170-
}
171-
return Set.count(V);
172-
}
162+
size_type count(const T &V) const { return contains(V) ? 1 : 0; }
173163

174164
/// insert - Insert an element into the set if it isn't already there.
175165
/// Returns a pair. The first value of it is an iterator to the inserted
@@ -181,7 +171,7 @@ class SmallSet {
181171
return std::make_pair(const_iterator(I), Inserted);
182172
}
183173

184-
VIterator I = vfind(V);
174+
auto I = llvm::find(Vector, V);
185175
if (I != Vector.end()) // Don't reinsert if it already exists.
186176
return std::make_pair(const_iterator(I), false);
187177
if (Vector.size() < N) {
@@ -206,11 +196,12 @@ class SmallSet {
206196
bool erase(const T &V) {
207197
if (!isSmall())
208198
return Set.erase(V);
209-
for (mutable_iterator I = Vector.begin(), E = Vector.end(); I != E; ++I)
210-
if (*I == V) {
211-
Vector.erase(I);
212-
return true;
213-
}
199+
200+
auto It = llvm::find(Vector, V);
201+
if (It != Vector.end()) {
202+
Vector.erase(It);
203+
return true;
204+
}
214205
return false;
215206
}
216207

@@ -234,19 +225,12 @@ class SmallSet {
234225
/// Check if the SmallSet contains the given element.
235226
bool contains(const T &V) const {
236227
if (isSmall())
237-
return vfind(V) != Vector.end();
238-
return Set.find(V) != Set.end();
228+
return llvm::is_contained(Vector, V);
229+
return llvm::is_contained(Set, V);
239230
}
240231

241232
private:
242233
bool isSmall() const { return Set.empty(); }
243-
244-
VIterator vfind(const T &V) const {
245-
for (VIterator I = Vector.begin(), E = Vector.end(); I != E; ++I)
246-
if (*I == V)
247-
return I;
248-
return Vector.end();
249-
}
250234
};
251235

252236
/// If this set is of pointer values, transparently switch over to using

0 commit comments

Comments
 (0)