Use inplace_merge() for ranges

This commit is contained in:
Jason Felice 2021-03-08 16:51:43 -05:00
parent c9be6730f6
commit d3824f3211
2 changed files with 22 additions and 6 deletions

View File

@ -1523,14 +1523,16 @@ private:
BufferCoord& get_first(RangeAndString& r) { return std::get<0>(r).first; } BufferCoord& get_first(RangeAndString& r) { return std::get<0>(r).first; }
BufferCoord& get_last(RangeAndString& r) { return std::get<0>(r).last; } BufferCoord& get_last(RangeAndString& r) { return std::get<0>(r).last; }
void option_list_postprocess(Vector<RangeAndString, MemoryDomain::Options>& opt) bool option_element_compare(RangeAndString const& lhs, RangeAndString const& rhs)
{ {
std::sort(opt.begin(), opt.end(),
[](auto& lhs, auto& rhs) {
return std::get<0>(lhs).first == std::get<0>(rhs).first ? 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).last < std::get<0>(rhs).last
: std::get<0>(lhs).first < std::get<0>(rhs).first; : std::get<0>(lhs).first < std::get<0>(rhs).first;
}); }
void option_list_postprocess(Vector<RangeAndString, MemoryDomain::Options>& opt)
{
std::sort(opt.begin(), opt.end(), option_element_compare);
} }
void option_update(RangeAndStringList& opt, const Context& context) 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); update_ranges(context.buffer(), opt.prefix, opt.list);
} }
bool option_add_from_strings(Vector<RangeAndString, MemoryDomain::Options>& opt, ConstArrayView<String> strs)
{
auto vec = option_from_strings(Meta::Type<Vector<RangeAndString, MemoryDomain::Options>>{}, 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<RangeAndStringList, RangesHighlighter> struct RangesHighlighter : OptionBasedHighlighter<RangeAndStringList, RangesHighlighter>
{ {
using RangesHighlighter::OptionBasedHighlighter::OptionBasedHighlighter; using RangesHighlighter::OptionBasedHighlighter::OptionBasedHighlighter;

View File

@ -38,6 +38,7 @@ constexpr StringView option_type_name(Meta::Type<RangeAndStringList>)
} }
void option_update(RangeAndStringList& opt, const Context& context); void option_update(RangeAndStringList& opt, const Context& context);
void option_list_postprocess(Vector<RangeAndString, MemoryDomain::Options>& opt); void option_list_postprocess(Vector<RangeAndString, MemoryDomain::Options>& opt);
bool option_add_from_strings(Vector<RangeAndString, MemoryDomain::Options>& opt, ConstArrayView<String> strs);
} }