From db8a4ca318e894713a396c36f13e8c614b166647 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 11 May 2014 19:44:19 +0100 Subject: [PATCH] SelectionList no longer inherit from std::vector --- src/dynamic_selection_list.hh | 2 +- src/normal.cc | 10 ++++----- src/selection.hh | 39 ++++++++++++++++++++++++++++++++--- src/selectors.cc | 10 ++++----- 4 files changed, 47 insertions(+), 14 deletions(-) diff --git a/src/dynamic_selection_list.hh b/src/dynamic_selection_list.hh index 7da71395..4bfbd39a 100644 --- a/src/dynamic_selection_list.hh +++ b/src/dynamic_selection_list.hh @@ -13,7 +13,7 @@ public: using iterator = SelectionList::iterator; using const_iterator = SelectionList::const_iterator; - DynamicSelectionList(Buffer& buffer, SelectionList selections = {}); + DynamicSelectionList(Buffer& buffer, SelectionList selections = { Selection{} }); DynamicSelectionList& operator=(SelectionList selections); void check_invariant() const; diff --git a/src/normal.cc b/src/normal.cc index 1b1f9152..062f512c 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -845,7 +845,7 @@ void indent(Context& context, int) for (auto line = sel.min().line; line < sel.max().line+1; ++line) { if (indent_empty or buffer[line].length() > 1) - sels.emplace_back(line, line); + sels.push_back({line, line}); } } if (not sels.empty()) @@ -881,12 +881,12 @@ void deindent(Context& context, int) else { if (deindent_incomplete and width != 0) - sels.emplace_back(line, ByteCoord{line, column-1}); + sels.push_back({ line, ByteCoord{line, column-1} }); break; } if (width == indent_width) { - sels.emplace_back(line, ByteCoord{line, column}); + sels.push_back({ line, ByteCoord{line, column} }); break; } } @@ -1245,7 +1245,7 @@ public: auto it = std::upper_bound(m_ranges.begin(), m_ranges.end(), begin, [](ByteCoord c, const Selection& sel) { return c < sel.min(); }); - m_ranges.emplace(it, begin, buffer.char_prev(end)); + m_ranges.insert(it, Selection{ begin, buffer.char_prev(end) }); } void on_erase(const Buffer& buffer, ByteCoord begin, ByteCoord end) @@ -1255,7 +1255,7 @@ public: auto it = std::upper_bound(m_ranges.begin(), m_ranges.end(), pos, [](ByteCoord c, const Selection& sel) { return c < sel.min(); }); - m_ranges.emplace(it, pos, pos); + m_ranges.insert(it, Selection{ pos, pos }); } SelectionList& ranges() { return m_ranges; } diff --git a/src/selection.hh b/src/selection.hh index af15c4d3..61e1cc54 100644 --- a/src/selection.hh +++ b/src/selection.hh @@ -55,11 +55,11 @@ static bool compare_selections(const Selection& lhs, const Selection& rhs) return lhs.min() < rhs.min(); } -struct SelectionList : std::vector +struct SelectionList { SelectionList() = default; - SelectionList(ByteCoord c) : std::vector{Selection{c,c}} {} - SelectionList(Selection s) : std::vector{s} {} + SelectionList(ByteCoord c) : m_selections{Selection{c,c}} {} + SelectionList(Selection s) : m_selections{s} {} void update_insert(const Buffer& buffer, ByteCoord begin, ByteCoord end); void update_erase(const Buffer& buffer, ByteCoord begin, ByteCoord end); @@ -73,6 +73,38 @@ struct SelectionList : std::vector void rotate_main(int count) { m_main = (m_main + count) % size(); } + void push_back(const Selection& sel) { m_selections.push_back(sel); } + void push_back(Selection&& sel) { m_selections.push_back(std::move(sel)); } + + Selection& operator[](size_t i) { return m_selections[i]; } + const Selection& operator[](size_t i) const { return m_selections[i]; } + + using iterator = std::vector::iterator; + iterator begin() { return m_selections.begin(); } + iterator end() { return m_selections.end(); } + + using const_iterator = std::vector::const_iterator; + const_iterator begin() const { return m_selections.begin(); } + const_iterator end() const { return m_selections.end(); } + + template + iterator insert(Args... args) + { + return m_selections.insert(std::forward(args)...); + } + + template + iterator erase(Args... args) + { + return m_selections.erase(std::forward(args)...); + } + + 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; } + template void merge_overlapping(OverlapsFunc overlaps) { @@ -117,6 +149,7 @@ struct SelectionList : std::vector private: size_t m_main = 0; + std::vector m_selections; }; } diff --git a/src/selectors.cc b/src/selectors.cc index cd8292a1..5d98be52 100644 --- a/src/selectors.cc +++ b/src/selectors.cc @@ -456,9 +456,9 @@ void select_all_matches(const Buffer& buffer, SelectionList& selections, for (auto& match : *re_it) captures.emplace_back(match.first, match.second); - result.emplace_back(begin.coord(), - (begin == end ? end : utf8::previous(end)).coord(), - std::move(captures)); + result.push_back({ begin.coord(), + (begin == end ? end : utf8::previous(end)).coord(), + std::move(captures) }); } } if (result.empty()) @@ -483,11 +483,11 @@ void split_selections(const Buffer& buffer, SelectionList& selections, { BufferIterator end = (*re_it)[0].first; - result.emplace_back(begin.coord(), (begin == end) ? end.coord() : utf8::previous(end).coord()); + result.push_back({ begin.coord(), (begin == end) ? end.coord() : utf8::previous(end).coord() }); begin = (*re_it)[0].second; } if (begin.coord() <= sel.max()) - result.emplace_back(begin.coord(), sel.max()); + result.push_back({ begin.coord(), sel.max() }); } result.set_main_index(result.size() - 1); selections = std::move(result);