From f79714840874a09adc294f05641f86640bb1fec0 Mon Sep 17 00:00:00 2001 From: DoctorNoobingstoneIPresume Date: Wed, 13 Jul 2022 16:04:47 +0300 Subject: [PATCH] ordered_set: operator<: Fix for a case of strict inclusion. 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. --- lib/ordered_set.h | 2 +- test/gtest/ordered_set.cpp | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/ordered_set.h b/lib/ordered_set.h index 1adfb8c5986..19747d20dd8 100644 --- a/lib/ordered_set.h +++ b/lib/ordered_set.h @@ -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; } diff --git a/test/gtest/ordered_set.cpp b/test/gtest/ordered_set.cpp index d844f919028..d921412acf3 100644 --- a/test/gtest/ordered_set.cpp +++ b/test/gtest/ordered_set.cpp @@ -93,4 +93,15 @@ TEST(ordered_set, set_intersect) { EXPECT_EQ(res, expect); } +TEST(ordered_set, x_is_strict_prefix_of_y) { + ordered_set x; + x.insert(1); + + ordered_set y; + y.insert(1); + y.insert(2); + + EXPECT_LT(x, y); +} + } // namespace Test