Skip to content

Commit ce6c236

Browse files
authored
[ADT][NFC] Simplify SmallSet (#109412)
- Remove dependence on `STLExtras.h`. - Remove unused header inclusions. - Make `count` use `contains` for deduplication. - Replace hand-written linear scans on Vector by `std::find`.
1 parent 0c31ea5 commit ce6c236

File tree

2 files changed

+9
-29
lines changed

2 files changed

+9
-29
lines changed

clang/lib/Basic/TargetID.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "clang/Basic/TargetID.h"
10+
#include "llvm/ADT/STLExtras.h"
1011
#include "llvm/ADT/SmallSet.h"
1112
#include "llvm/Support/raw_ostream.h"
1213
#include "llvm/TargetParser/TargetParser.h"

llvm/include/llvm/ADT/SmallSet.h

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,10 @@
1616

1717
#include "llvm/ADT/SmallPtrSet.h"
1818
#include "llvm/ADT/SmallVector.h"
19-
#include "llvm/ADT/STLExtras.h"
2019
#include "llvm/ADT/iterator.h"
21-
#include "llvm/Support/Compiler.h"
22-
#include "llvm/Support/type_traits.h"
2320
#include <cstddef>
2421
#include <functional>
2522
#include <set>
26-
#include <type_traits>
2723
#include <utility>
2824

2925
namespace llvm {
@@ -139,10 +135,6 @@ class SmallSet {
139135
SmallVector<T, N> Vector;
140136
std::set<T, C> Set;
141137

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-
146138
// In small mode SmallPtrSet uses linear search for the elements, so it is
147139
// not a good idea to choose this value too high. You may consider using a
148140
// DenseSet<> instead if you expect many elements in the set.
@@ -163,13 +155,7 @@ class SmallSet {
163155
}
164156

165157
/// 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-
}
158+
size_type count(const T &V) const { return contains(V) ? 1 : 0; }
173159

174160
/// insert - Insert an element into the set if it isn't already there.
175161
/// Returns a pair. The first value of it is an iterator to the inserted
@@ -181,7 +167,7 @@ class SmallSet {
181167
return std::make_pair(const_iterator(I), Inserted);
182168
}
183169

184-
VIterator I = vfind(V);
170+
auto I = std::find(Vector.begin(), Vector.end(), V);
185171
if (I != Vector.end()) // Don't reinsert if it already exists.
186172
return std::make_pair(const_iterator(I), false);
187173
if (Vector.size() < N) {
@@ -206,11 +192,11 @@ class SmallSet {
206192
bool erase(const T &V) {
207193
if (!isSmall())
208194
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-
}
195+
auto I = std::find(Vector.begin(), Vector.end(), V);
196+
if (I != Vector.end()) {
197+
Vector.erase(I);
198+
return true;
199+
}
214200
return false;
215201
}
216202

@@ -234,19 +220,12 @@ class SmallSet {
234220
/// Check if the SmallSet contains the given element.
235221
bool contains(const T &V) const {
236222
if (isSmall())
237-
return vfind(V) != Vector.end();
223+
return std::find(Vector.begin(), Vector.end(), V) != Vector.end();
238224
return Set.find(V) != Set.end();
239225
}
240226

241227
private:
242228
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-
}
250229
};
251230

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

0 commit comments

Comments
 (0)