Skip to content

Commit 578e7c3

Browse files
committed
Core/Utils: Added .contains member function to FlatSet
(cherry picked from commit a27e3a52a0cdfae8cf5cbb787e944f4f76319f26)
1 parent 611527b commit 578e7c3

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

src/common/Containers/FlatSet.h

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,28 @@ class FlatSet
3939
auto end() { return _storage.end(); }
4040
auto end() const { return _storage.end(); }
4141

42+
bool contains(Key const& value) const
43+
{
44+
return std::binary_search(this->begin(), this->end(), value, Compare());
45+
}
46+
4247
auto find(Key const& value) const
4348
{
49+
auto compare = Compare();
4450
auto end = this->end();
45-
auto itr = std::lower_bound(this->begin(), end, value, Compare());
46-
if (itr != end && Compare()(value, *itr))
51+
auto itr = std::lower_bound(this->begin(), end, value, compare);
52+
if (itr != end && compare(value, *itr))
4753
itr = end;
4854

4955
return itr;
5056
}
5157

5258
auto find(Key const& value)
5359
{
60+
auto compare = Compare();
5461
auto end = this->end();
55-
auto itr = std::lower_bound(this->begin(), end, value, Compare());
56-
if (itr != end && Compare()(value, *itr))
62+
auto itr = std::lower_bound(this->begin(), end, value, compare);
63+
if (itr != end && compare(value, *itr))
5764
itr = end;
5865

5966
return itr;
@@ -63,9 +70,10 @@ class FlatSet
6370
std::pair<iterator, bool> emplace(Args&&... args)
6471
{
6572
Key newElement(std::forward<Args>(args)...);
73+
auto compare = Compare();
6674
auto end = this->end();
67-
auto itr = std::lower_bound(this->begin(), end, newElement, Compare());
68-
if (itr != end && !Compare()(newElement, *itr))
75+
auto itr = std::lower_bound(this->begin(), end, newElement, compare);
76+
if (itr != end && !compare(newElement, *itr))
6977
return { itr, false };
7078

7179
return { _storage.emplace(itr, std::move(newElement)), true };
@@ -88,15 +96,8 @@ class FlatSet
8896

8997
void shrink_to_fit() { _storage.shrink_to_fit(); }
9098

91-
friend bool operator==(FlatSet const& left, FlatSet const& right)
92-
{
93-
return left._storage == right._storage;
94-
}
95-
96-
friend bool operator!=(FlatSet const& left, FlatSet const& right)
97-
{
98-
return !(left == right);
99-
}
99+
friend std::strong_ordering operator<=>(FlatSet const& left, FlatSet const& right) = default;
100+
friend bool operator==(FlatSet const& left, FlatSet const& right) = default;
100101

101102
private:
102103
KeyContainer _storage;

0 commit comments

Comments
 (0)