Skip to content

Commit 44d09dc

Browse files
committed
Remove all <=> operators and instead use comparator. Fixing #2
1 parent 5203ccf commit 44d09dc

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

include/miniselect/private/median_common.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@ template <class Iter, class Compare,
270270
class DiffType = typename std::iterator_traits<Iter>::difference_type>
271271
inline DiffType median_index(const Iter r, DiffType a, DiffType b, DiffType c,
272272
Compare&& comp) {
273-
if (r[a] > r[c]) std::swap(a, c);
274-
if (r[b] > r[c]) return c;
273+
if (comp(r[c], r[a])) std::swap(a, c);
274+
if (comp(r[c], r[b])) return c;
275275
if (comp(r[b], r[a])) return a;
276276
return b;
277277
}
@@ -286,7 +286,6 @@ template <bool leanRight, class Iter, class Compare,
286286
inline DiffType median_index(Iter r, DiffType a, DiffType b, DiffType c,
287287
DiffType d, Compare&& comp) {
288288
if (comp(r[d], r[c])) std::swap(c, d);
289-
assert(r[c] <= r[d]);
290289
if (leanRight) {
291290
if (comp(r[c], r[a])) {
292291
assert(comp(r[c], r[a]) && !comp(r[d], r[c])); // so r[c]) is out

testing/test_select.cpp

+27-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ struct IndirectLess {
3131
IndirectLess &operator=(IndirectLess &&) = default;
3232
};
3333

34+
struct CustomInt {
35+
size_t x = 0;
36+
bool operator<(const CustomInt& other) const {
37+
return x < other.x;
38+
}
39+
};
40+
3441
template <typename Selector>
3542
class SelectTest : public ::testing::Test {
3643
public:
@@ -135,7 +142,7 @@ class SelectTest : public ::testing::Test {
135142

136143
static void TestCustomComparators() {
137144
std::vector<std::unique_ptr<int>> v(1000);
138-
for (int i = 0; static_cast<std::size_t>(i) < v.size(); ++i) {
145+
for (size_t i = 0; i < v.size(); ++i) {
139146
v[i] = std::make_unique<int>(i);
140147
}
141148
Selector::Select(v.begin(), v.begin() + v.size() / 2, v.end(),
@@ -151,6 +158,21 @@ class SelectTest : public ::testing::Test {
151158
}
152159
}
153160

161+
static void TestOnlyOperatorLess() {
162+
std::vector<CustomInt> v(1000);
163+
for (size_t i = 0; i < v.size(); ++i) {
164+
v[i].x = v.size() - i - 1;
165+
}
166+
Selector::Select(v.begin(), v.begin() + v.size() / 2, v.end());
167+
EXPECT_EQ(v[v.size() / 2].x, v.size() / 2);
168+
for (size_t i = 0; i < v.size() / 2; ++i) {
169+
EXPECT_LE(v[i].x, v.size() / 2);
170+
}
171+
for (size_t i = v.size() / 2; i < v.size(); ++i) {
172+
EXPECT_GE(v[i].x, v.size() / 2);
173+
}
174+
}
175+
154176
static void TestRepeat(size_t N, size_t M) {
155177
ASSERT_NE(N, 0);
156178
ASSERT_GT(N, M);
@@ -239,6 +261,10 @@ TYPED_TEST(SelectTest, TestComparators) {
239261
TestFixture::TestCustomComparators();
240262
}
241263

264+
TYPED_TEST(SelectTest, TestOnlyOperatorLess) {
265+
TestFixture::TestOnlyOperatorLess();
266+
}
267+
242268
TYPED_TEST(SelectTest, TestRepeats) { TestFixture::TestManyRepeats(); }
243269

244270
TYPED_TEST(SelectTest, TestRandomAccessIterators) {

testing/test_sort.cpp

+25-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <memory>
1111
#include <random>
1212
#include <string>
13+
#include <tuple>
1314
#include <vector>
1415

1516
#include "test_common.h"
@@ -31,6 +32,13 @@ struct IndirectLess {
3132
IndirectLess &operator=(IndirectLess &&) = default;
3233
};
3334

35+
struct CustomInt {
36+
size_t x = 0;
37+
bool operator<(const CustomInt& other) const {
38+
return x < other.x;
39+
}
40+
};
41+
3442
template <typename Sorter>
3543
class PartialSortTest : public ::testing::Test {
3644
public:
@@ -122,15 +130,26 @@ class PartialSortTest : public ::testing::Test {
122130

123131
static void TestCustomComparators() {
124132
std::vector<std::unique_ptr<int>> v(1000);
125-
for (int i = 0; static_cast<std::size_t>(i) < v.size(); ++i) {
133+
for (size_t i = 0; i < v.size(); ++i) {
126134
v[i] = std::make_unique<int>(i);
127135
}
128136
Sorter::Sort(v.begin(), v.begin() + v.size() / 2, v.end(), IndirectLess{});
129-
for (int i = 0; static_cast<std::size_t>(i) < v.size() / 2; ++i) {
137+
for (size_t i = 0; i < v.size() / 2; ++i) {
130138
ASSERT_NE(v[i], nullptr);
131139
EXPECT_EQ(*v[i], i);
132140
}
133141
}
142+
143+
static void TestOnlyOperatorLess() {
144+
std::vector<CustomInt> v(1000);
145+
for (size_t i = 0; i < v.size(); ++i) {
146+
v[i].x = v.size() - i - 1;
147+
}
148+
Sorter::Sort(v.begin(), v.begin() + v.size() / 2, v.end());
149+
for (size_t i = 0; i < v.size() / 2; ++i) {
150+
EXPECT_EQ(v[i].x, i);
151+
}
152+
}
134153
};
135154

136155
TYPED_TEST_SUITE(PartialSortTest, algorithms::All);
@@ -169,6 +188,10 @@ TYPED_TEST(PartialSortTest, TestRandomAccessIterators) {
169188
TestFixture::TestRandomAccessIterators();
170189
}
171190

191+
TYPED_TEST(PartialSortTest, TestOnlyOperatorLess) {
192+
TestFixture::TestOnlyOperatorLess();
193+
}
194+
172195
// The standard says that the order of other elements is unspecified even if
173196
// nothing should be sorted so it fails for libcxx and PDQ which is Ok. Saving
174197
// this test for a reference.

0 commit comments

Comments
 (0)