Skip to content

Commit 8d9af32

Browse files
authored
Add contains() to custom containers and use where possible. NFC (#8547)
This matches the C++20 API that we cannot quite use yet. At least not until #8218 lands.
1 parent 0380ce9 commit 8d9af32

6 files changed

Lines changed: 26 additions & 24 deletions

File tree

src/passes/DeadArgumentElimination2.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,7 +1496,7 @@ void DAE2::collectStats() {
14961496
if (auto* loc = std::get_if<FuncParamLoc>(&node)) {
14971497
auto [funcIndex, paramIndex] = *loc;
14981498
for (auto loc : parent.funcInfos[funcIndex].callerParams[paramIndex]) {
1499-
if (optimizedNodes.count(loc)) {
1499+
if (optimizedNodes.contains(loc)) {
15001500
push(loc);
15011501
}
15021502
}
@@ -1505,7 +1505,7 @@ void DAE2::collectStats() {
15051505
if (auto it = parent.typeTreeInfos.find(funcType);
15061506
it != parent.typeTreeInfos.end()) {
15071507
for (auto loc : it->second.callerParams[paramIndex]) {
1508-
if (optimizedNodes.count(loc)) {
1508+
if (optimizedNodes.contains(loc)) {
15091509
push(loc);
15101510
}
15111511
}

src/passes/TypeMerging.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ bool TypeMerging::merge(MergeKind kind) {
382382
auto chain = type.getDescriptorChain();
383383
bool hasCast =
384384
std::any_of(chain.begin(), chain.end(), [&](HeapType t) -> bool {
385-
return castTypes.count(t);
385+
return castTypes.contains(t);
386386
});
387387
if (hasCast || !privateTypes.count(type)) {
388388
ensurePartition(type);
@@ -396,7 +396,7 @@ bool TypeMerging::merge(MergeKind kind) {
396396
super &&
397397
std::any_of(chain.begin(), chain.end(), [&](HeapType t) -> bool {
398398
auto super = t.getDeclaredSuperType();
399-
return super && exactCastTypes.count(*super);
399+
return super && exactCastTypes.contains(*super);
400400
});
401401
if (!super || !shapeEq(type, *super) || superHasExactCast) {
402402
// Create a new partition for this type and bail.

src/support/insert_ordered.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ template<typename T> struct InsertOrderedSet {
7474
}
7575

7676
size_t count(const T& val) const { return Map.count(val); }
77+
bool contains(const T& val) const { return Map.find(val) != Map.end(); }
7778

7879
InsertOrderedSet() = default;
7980
InsertOrderedSet(const InsertOrderedSet& other) { *this = other; }
@@ -160,6 +161,7 @@ template<typename Key, typename T> struct InsertOrderedMap {
160161
size_t size() const { return Map.size(); }
161162
bool empty() const { return Map.empty(); }
162163
size_t count(const Key& k) const { return Map.count(k); }
164+
bool contains(const Key& k) const { return Map.find(k) != Map.end(); }
163165

164166
InsertOrderedMap() = default;
165167
InsertOrderedMap(const InsertOrderedMap& other) {

src/support/small_set.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,17 +185,17 @@ class SmallSetBase {
185185
}
186186
}
187187

188-
size_t count(const T& x) const {
188+
bool contains(const T& x) const {
189189
if (usingFixed()) {
190190
// Do a linear search.
191191
for (size_t i = 0; i < fixed.used; i++) {
192192
if (fixed.storage[i] == x) {
193-
return 1;
193+
return true;
194194
}
195195
}
196-
return 0;
196+
return false;
197197
} else {
198-
return flexible.count(x);
198+
return flexible.find(x) != flexible.end();
199199
}
200200
}
201201

@@ -222,11 +222,11 @@ class SmallSetBase {
222222
if (usingFixed()) {
223223
return std::all_of(fixed.storage.begin(),
224224
fixed.storage.begin() + fixed.used,
225-
[&other](const T& x) { return other.count(x); });
225+
[&other](const T& x) { return other.contains(x); });
226226
} else if (other.usingFixed()) {
227227
return std::all_of(other.fixed.storage.begin(),
228228
other.fixed.storage.begin() + other.fixed.used,
229-
[this](const T& x) { return count(x); });
229+
[this](const T& x) { return contains(x); });
230230
} else {
231231
return flexible == other.flexible;
232232
}

src/wasm/parsing.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ struct DuplicateNameScanner
115115
// TODO: This could be done in a single insert operation that checks
116116
// whether we actually inserted, if we improved
117117
// SmallSetBase::insert to return a value like std::set does.
118-
if (seen.count(name)) {
118+
if (seen.contains(name)) {
119119
// A name has been defined more than once; we'll need to fix that.
120120
ok = false;
121121
} else {

test/example/small_set.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ template<typename T>
1111
void assertContents(T& t, const std::vector<int>& expectedContents) {
1212
assert(t.size() == expectedContents.size());
1313
for (auto item : expectedContents) {
14-
assert(t.count(item) == 1);
14+
assert(t.contains(item));
1515
}
1616
// Also test this using an iterator and a const iterator to also get
1717
// coverage there.
@@ -69,29 +69,29 @@ template<typename T> void testAPI() {
6969
assert(t.size() == 3);
7070

7171
// unwind by erasing (in the opposite direction from before)
72-
assert(t.count(1) == 1);
73-
assert(t.count(2) == 1);
74-
assert(t.count(3) == 1);
75-
assert(t.count(1337) == 0);
72+
assert(t.contains(1));
73+
assert(t.contains(2));
74+
assert(t.contains(3));
75+
assert(!t.contains(1337));
7676

7777
t.erase(1);
78-
assert(t.count(1) == 0);
78+
assert(!t.contains(1));
7979

8080
assert(t.size() == 2);
8181

82-
assert(t.count(2) == 1);
82+
assert(t.contains(2));
8383
t.erase(2);
84-
assert(t.count(2) == 0);
84+
assert(!t.contains(2));
8585

8686
assert(t.size() == 1);
8787

88-
assert(t.count(3) == 1);
88+
assert(t.contains(3));
8989
t.erase(3);
9090

91-
assert(t.count(1) == 0);
92-
assert(t.count(2) == 0);
93-
assert(t.count(3) == 0);
94-
assert(t.count(1337) == 0);
91+
assert(!t.contains(1));
92+
assert(!t.contains(2));
93+
assert(!t.contains(3));
94+
assert(!t.contains(1337));
9595

9696
assert(t.size() == 0);
9797
}

0 commit comments

Comments
 (0)