Skip to content

Commit

Permalink
ordered_set: operator<: Fix for a case of strict inclusion.
Browse files Browse the repository at this point in the history
When all elements compared so far in sets `x` and `y` have been equivalent
and one of the sets (`x`) ends, but the other one (`y`) does not end
(i.e. `x` is a strict prefix of `y`),
we used to get `y < x`, but with this changeset we are going to get `x < y`.

Example (in pseudocode) (with case-insensitive comparison):
  we used to get `{'A', 'b'} > {'a', 'B', 'c'}`,
  now fixed to   `{'A', 'b'} < {'a', 'B', 'c'}`.
This is similar to how `std::lexicographical_compare` behaves.
  • Loading branch information
DoctorNoobingstoneIPresume authored and DoctorNoobingstoneIPresume committed Jul 13, 2022
1 parent c770c95 commit f797148
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/ordered_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class ordered_set {
// have a set of ordered_sets (or use ordered_set as a map key).
auto it = a.data_map.begin();
for (auto &el : data_map) {
if (it == a.data_map.end()) return true;
if (it == a.data_map.end()) return false;
if (mapcmp()(el.first, it->first)) return true;
if (mapcmp()(it->first, el.first)) return false;
++it; }
Expand Down
11 changes: 11 additions & 0 deletions test/gtest/ordered_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,15 @@ TEST(ordered_set, set_intersect) {
EXPECT_EQ(res, expect);
}

TEST(ordered_set, x_is_strict_prefix_of_y) {
ordered_set<unsigned> x;
x.insert(1);

ordered_set<unsigned> y;
y.insert(1);
y.insert(2);

EXPECT_LT(x, y);
}

} // namespace Test

0 comments on commit f797148

Please sign in to comment.