diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2014-05-12 23:25:15 +0100 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2014-05-13 20:09:06 +0100 |
| commit | ea3e92aa5e68e73fa95196332b739891bbf3f24f (patch) | |
| tree | 1b91673cdbec34a9dcb592858890fc6db1255ff9 /src/selection.hh | |
| parent | 7bc73b7ef9402f81a6bb496b03ba84197f76e642 (diff) | |
SelectionList know its buffer and timestamp
Diffstat (limited to 'src/selection.hh')
| -rw-r--r-- | src/selection.hh | 42 |
1 files changed, 19 insertions, 23 deletions
diff --git a/src/selection.hh b/src/selection.hh index 016ce5f5..9b3f747a 100644 --- a/src/selection.hh +++ b/src/selection.hh @@ -12,7 +12,7 @@ using CaptureList = std::vector<String>; struct Selection { Selection() = default; - explicit Selection(ByteCoord pos) : Selection(pos,pos) {} + Selection(ByteCoord pos) : Selection(pos,pos) {} Selection(ByteCoord anchor, ByteCoord cursor, CaptureList captures = {}) : m_anchor{anchor}, m_cursor{cursor}, @@ -57,13 +57,17 @@ static bool compare_selections(const Selection& lhs, const Selection& rhs) struct SelectionList { - SelectionList() = default; - SelectionList(ByteCoord c) : m_selections{Selection{c,c}} {} - SelectionList(Selection s) : m_selections{s} {} + SelectionList(const Buffer& buffer); + SelectionList(const Buffer& buffer, Selection s); + SelectionList(const Buffer& buffer, Selection s, size_t timestamp); + SelectionList(const Buffer& buffer, std::vector<Selection> s); + SelectionList(const Buffer& buffer, std::vector<Selection> s, size_t timestamp); void update_insert(ByteCoord begin, ByteCoord end, bool at_end); void update_erase(ByteCoord begin, ByteCoord end, bool at_end); + void update(); + void check_invariant() const; const Selection& main() const { return (*this)[m_main]; } @@ -102,8 +106,8 @@ struct SelectionList size_t size() const { return m_selections.size(); } bool empty() const { return m_selections.empty(); } - bool operator==(const SelectionList& other) const { return m_selections == other.m_selections; } - bool operator!=(const SelectionList& other) const { return m_selections != other.m_selections; } + bool operator==(const SelectionList& other) const { return m_buffer == other.m_buffer and m_selections == other.m_selections; } + bool operator!=(const SelectionList& other) const { return !((*this) == other); } template<typename OverlapsFunc> void merge_overlapping(OverlapsFunc overlaps) @@ -129,27 +133,19 @@ struct SelectionList kak_assert(std::is_sorted(begin(), end(), compare_selections)); } - void sort_and_merge_overlapping() - { - if (size() == 1) - return; - - const auto& main = this->main(); - const auto main_begin = main.min(); - m_main = std::count_if(begin(), end(), [&](const Selection& sel) { - auto begin = sel.min(); - if (begin == main_begin) - return &sel < &main; - else - return begin < main_begin; - }); - std::stable_sort(begin(), end(), compare_selections); - merge_overlapping(overlaps); - } + void sort_and_merge_overlapping(); + + const Buffer& buffer() const { return *m_buffer; } + + size_t timestamp() const { return m_timestamp; } + void set_timestamp(size_t timestamp) { m_timestamp = timestamp; } private: size_t m_main = 0; std::vector<Selection> m_selections; + + safe_ptr<const Buffer> m_buffer; + size_t m_timestamp; }; } |
