aboutsummaryrefslogtreecommitdiffstats
path: root/util
diff options
context:
space:
mode:
authorswarmer <swarmer@yandex-team.com>2024-09-26 22:15:27 +0300
committerswarmer <swarmer@yandex-team.com>2024-09-26 22:29:56 +0300
commit9598e6ce746a6ae937ccc291c9c5e64db14ce62e (patch)
tree62236ad1aadb33c8d677290c805267714623ac7e /util
parent8a41c7311c51ea81eaae2626bbe464956f2a8288 (diff)
downloadydb-9598e6ce746a6ae937ccc291c9c5e64db14ce62e.tar.gz
Support move-only key types in the Max/MinElementBy routines
It is also slightly faster for non-trivial key types. commit_hash:4104cabfe8dc9a51174034c62aae25be16b57bf9
Diffstat (limited to 'util')
-rw-r--r--util/generic/algorithm.h8
-rw-r--r--util/generic/algorithm_ut.cpp10
2 files changed, 14 insertions, 4 deletions
diff --git a/util/generic/algorithm.h b/util/generic/algorithm.h
index a770ab3033..d1f48c479d 100644
--- a/util/generic/algorithm.h
+++ b/util/generic/algorithm.h
@@ -21,11 +21,11 @@ namespace NPrivate {
auto bestValue = func(*begin);
auto bestPos = begin;
- for (auto i = ++begin; i != end; ++i) {
- auto curValue = func(*i);
+ for (++begin; begin != end; ++begin) {
+ auto curValue = func(*begin);
if (pred(curValue, bestValue)) {
- bestValue = curValue;
- bestPos = i;
+ bestValue = std::move(curValue);
+ bestPos = begin;
}
}
diff --git a/util/generic/algorithm_ut.cpp b/util/generic/algorithm_ut.cpp
index 51b6e12264..8609026797 100644
--- a/util/generic/algorithm_ut.cpp
+++ b/util/generic/algorithm_ut.cpp
@@ -752,6 +752,16 @@ Y_UNIT_TEST_SUITE(TAlgorithm) {
MinElementBy(empty, functor);
}
+ Y_UNIT_TEST(MinMaxElementMovableKeys) {
+ const TString strings[] = {"one", "two", "three", "four"};
+ struct TMoveOnlyKey: TString, TMoveOnly {
+ using TString::TString;
+ };
+ auto keyFn = [](TString s) { return TMoveOnlyKey{std::move(s)}; };
+ UNIT_ASSERT_STRINGS_EQUAL(*MaxElementBy(strings, keyFn), "two");
+ UNIT_ASSERT_STRINGS_EQUAL(*MinElementBy(strings, keyFn), "four");
+ }
+
Y_UNIT_TEST(TestApplyToMany) {
int res = 0;
ApplyToMany([&res](auto v) { res += v; }, 1, 2, 3, 4, 5);