From d3824f321125de438951f4b1e04c526dbc6ae844 Mon Sep 17 00:00:00 2001 From: Jason Felice Date: Mon, 8 Mar 2021 16:51:43 -0500 Subject: [PATCH] Use inplace_merge() for ranges --- src/highlighters.cc | 27 +++++++++++++++++++++------ src/highlighters.hh | 1 + 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/highlighters.cc b/src/highlighters.cc index c3b996a8c..912c41fef 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -1523,14 +1523,16 @@ private: BufferCoord& get_first(RangeAndString& r) { return std::get<0>(r).first; } BufferCoord& get_last(RangeAndString& r) { return std::get<0>(r).last; } +bool option_element_compare(RangeAndString const& lhs, RangeAndString const& rhs) +{ + return std::get<0>(lhs).first == std::get<0>(rhs).first ? + std::get<0>(lhs).last < std::get<0>(rhs).last + : std::get<0>(lhs).first < std::get<0>(rhs).first; +} + void option_list_postprocess(Vector& opt) { - std::sort(opt.begin(), opt.end(), - [](auto& lhs, auto& rhs) { - return std::get<0>(lhs).first == std::get<0>(rhs).first ? - std::get<0>(lhs).last < std::get<0>(rhs).last - : std::get<0>(lhs).first < std::get<0>(rhs).first; - }); + std::sort(opt.begin(), opt.end(), option_element_compare); } void option_update(RangeAndStringList& opt, const Context& context) @@ -1538,6 +1540,19 @@ void option_update(RangeAndStringList& opt, const Context& context) update_ranges(context.buffer(), opt.prefix, opt.list); } +bool option_add_from_strings(Vector& opt, ConstArrayView strs) +{ + auto vec = option_from_strings(Meta::Type>{}, strs); + if (vec.empty()) + return false; + auto middle = opt.insert(opt.end(), + std::make_move_iterator(vec.begin()), + std::make_move_iterator(vec.end())); + std::sort(middle, opt.end(), option_element_compare); + std::inplace_merge(opt.begin(), middle, opt.end(), option_element_compare); + return true; +} + struct RangesHighlighter : OptionBasedHighlighter { using RangesHighlighter::OptionBasedHighlighter::OptionBasedHighlighter; diff --git a/src/highlighters.hh b/src/highlighters.hh index c635b8dfe..f68f0b26c 100644 --- a/src/highlighters.hh +++ b/src/highlighters.hh @@ -38,6 +38,7 @@ constexpr StringView option_type_name(Meta::Type) } void option_update(RangeAndStringList& opt, const Context& context); void option_list_postprocess(Vector& opt); +bool option_add_from_strings(Vector& opt, ConstArrayView strs); }