DynamicSelectionList: optimize updating on buffer modification
Now that we know selections are sorted, we can get the set of selections needing updating in log(n) time using a binary search, for modification not changing the line count, this makes updating selections run in log(n) instead of n.
This commit is contained in:
parent
354ae7ad89
commit
e6c635be34
|
@ -72,32 +72,73 @@ void DynamicSelectionList::check_invariant() const
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static void update_insert(BufferIterator& it,
|
namespace
|
||||||
const BufferCoord& begin, const BufferCoord& end)
|
|
||||||
{
|
{
|
||||||
BufferCoord coord = it.coord();
|
|
||||||
if (coord < begin)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (begin.line == coord.line)
|
template<template <bool, bool> class UpdateFunc>
|
||||||
coord.column = end.column + coord.column - begin.column;
|
void on_buffer_change(SelectionList& sels, const BufferCoord& begin, const BufferCoord& end, LineCount end_line)
|
||||||
coord.line += end.line - begin.line;
|
{
|
||||||
|
auto update_beg = std::lower_bound(sels.begin(), sels.end(), begin,
|
||||||
|
[](const Selection& s, const BufferCoord& c) { return std::max(s.first(), s.last()).coord() < c; });
|
||||||
|
auto update_only_line_beg = std::upper_bound(sels.begin(), sels.end(), end_line,
|
||||||
|
[](LineCount l, const Selection& s) { return l < std::min(s.first(), s.last()).line(); });
|
||||||
|
|
||||||
it = coord;
|
if (update_beg != update_only_line_beg)
|
||||||
|
{
|
||||||
|
// for the first one, we are not sure if min < begin
|
||||||
|
UpdateFunc<false, false>{}(update_beg->first(), begin, end);
|
||||||
|
UpdateFunc<false, false>{}(update_beg->last(), begin, end);
|
||||||
|
}
|
||||||
|
for (auto it = update_beg+1; it < update_only_line_beg; ++it)
|
||||||
|
{
|
||||||
|
UpdateFunc<false, true>{}(it->first(), begin, end);
|
||||||
|
UpdateFunc<false, true>{}(it->last(), begin, end);
|
||||||
|
}
|
||||||
|
if (end.line > begin.line)
|
||||||
|
{
|
||||||
|
for (auto it = update_only_line_beg; it != sels.end(); ++it)
|
||||||
|
{
|
||||||
|
UpdateFunc<true, true>{}(it->first(), begin, end);
|
||||||
|
UpdateFunc<true, true>{}(it->last(), begin, end);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void update_erase(BufferIterator& it,
|
template<bool assume_different_line, bool assume_greater_than_begin>
|
||||||
const BufferCoord& begin, const BufferCoord& end)
|
struct UpdateInsert
|
||||||
|
{
|
||||||
|
void operator()(BufferIterator& it,
|
||||||
|
const BufferCoord& begin, const BufferCoord& end) const
|
||||||
{
|
{
|
||||||
BufferCoord coord = it.coord();
|
BufferCoord coord = it.coord();
|
||||||
if (coord < begin)
|
if (assume_different_line)
|
||||||
|
assert(begin.line < coord.line);
|
||||||
|
if (not assume_greater_than_begin and coord < begin)
|
||||||
return;
|
return;
|
||||||
|
if (not assume_different_line and begin.line == coord.line)
|
||||||
|
coord.column = end.column + coord.column - begin.column;
|
||||||
|
|
||||||
if (coord <= end)
|
coord.line += end.line - begin.line;
|
||||||
|
it = coord;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<bool assume_different_line, bool assume_greater_than_begin>
|
||||||
|
struct UpdateErase
|
||||||
|
{
|
||||||
|
void operator()(BufferIterator& it,
|
||||||
|
const BufferCoord& begin, const BufferCoord& end) const
|
||||||
|
{
|
||||||
|
BufferCoord coord = it.coord();
|
||||||
|
if (not assume_greater_than_begin and coord < begin)
|
||||||
|
return;
|
||||||
|
if (assume_different_line)
|
||||||
|
assert(end.line < coord.line);
|
||||||
|
if (not assume_different_line and coord <= end)
|
||||||
coord = it.buffer().clamp(begin);
|
coord = it.buffer().clamp(begin);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (end.line == coord.line)
|
if (not assume_different_line and end.line == coord.line)
|
||||||
{
|
{
|
||||||
coord.line = begin.line;
|
coord.line = begin.line;
|
||||||
coord.column = begin.column + coord.column - end.column;
|
coord.column = begin.column + coord.column - end.column;
|
||||||
|
@ -107,27 +148,18 @@ static void update_erase(BufferIterator& it,
|
||||||
}
|
}
|
||||||
it = coord;
|
it = coord;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
void DynamicSelectionList::on_insert(const BufferIterator& begin, const BufferIterator& end)
|
void DynamicSelectionList::on_insert(const BufferIterator& begin, const BufferIterator& end)
|
||||||
{
|
{
|
||||||
const BufferCoord begin_coord{begin.coord()};
|
on_buffer_change<UpdateInsert>(*this, begin.coord(), end.coord(), begin.line());
|
||||||
const BufferCoord end_coord{end.coord()};
|
|
||||||
for (auto& sel : *this)
|
|
||||||
{
|
|
||||||
update_insert(sel.first(), begin_coord, end_coord);
|
|
||||||
update_insert(sel.last(), begin_coord, end_coord);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DynamicSelectionList::on_erase(const BufferIterator& begin, const BufferIterator& end)
|
void DynamicSelectionList::on_erase(const BufferIterator& begin, const BufferIterator& end)
|
||||||
{
|
{
|
||||||
const BufferCoord begin_coord{begin.coord()};
|
on_buffer_change<UpdateErase>(*this, begin.coord(), end.coord(), end.line());
|
||||||
const BufferCoord end_coord{end.coord()};
|
|
||||||
for (auto& sel : *this)
|
|
||||||
{
|
|
||||||
update_erase(sel.first(), begin_coord, end_coord);
|
|
||||||
update_erase(sel.last(), begin_coord, end_coord);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user