diff options
| author | Maxime Coste <mawww@kakoune.org> | 2018-05-02 21:31:44 +1000 |
|---|---|---|
| committer | Maxime Coste <mawww@kakoune.org> | 2018-05-02 22:34:55 +1000 |
| commit | 74f90c1fc5feb34e25543a50b5371a398aba22c2 (patch) | |
| tree | 44feb19bf7659eafa832cfc38c77b55d4cd8ecbf /src/buffer.hh | |
| parent | 4288f0fb3aad98ad26c85df89d9b84de1dad1132 (diff) | |
Refactor buffer undo tree
Store the undo tree as an array of undo nodes, instead of as a
pointer based tree.
Diffstat (limited to 'src/buffer.hh')
| -rw-r--r-- | src/buffer.hh | 36 |
1 files changed, 18 insertions, 18 deletions
diff --git a/src/buffer.hh b/src/buffer.hh index 7ff5bab7..6b98e7c8 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -119,6 +119,8 @@ public: }; friend constexpr bool with_bit_ops(Meta::Type<Flags>) { return true; } + enum class HistoryId : size_t { First = 0, Invalid = (size_t)-1 }; + Buffer(String name, Flags flags, StringView data = {}, timespec fs_timestamp = InvalidTime); Buffer(const Buffer&) = delete; @@ -142,9 +144,9 @@ public: void commit_undo_group(); bool undo(size_t count = 1); bool redo(size_t count = 1); - bool move_to(size_t history_id); - size_t current_history_id() const noexcept; - size_t next_history_id() const noexcept { return m_next_history_id; } + bool move_to(HistoryId id); + HistoryId current_history_id() const noexcept { return m_history_id; } + HistoryId next_history_id() const noexcept { return (HistoryId)m_history.size(); } String string(BufferCoord begin, BufferCoord end) const; StringView substr(BufferCoord begin, BufferCoord end) const; @@ -256,27 +258,25 @@ private: using UndoGroup = Vector<Modification, MemoryDomain::BufferMeta>; - struct HistoryNode : SafeCountable, UseMemoryDomain<MemoryDomain::BufferMeta> + struct HistoryNode : UseMemoryDomain<MemoryDomain::BufferMeta> { - HistoryNode(size_t id, HistoryNode* parent); + HistoryNode(HistoryId parent); - UndoGroup undo_group; - Vector<std::unique_ptr<HistoryNode>, MemoryDomain::BufferMeta> childs; - SafePtr<HistoryNode> parent; - HistoryNode* redo_child = nullptr; // not a SafePtr to avoid lifetime issues between this and childs - size_t id; + HistoryId parent; + HistoryId redo_child = HistoryId::Invalid; TimePoint timepoint; + UndoGroup undo_group; }; - size_t m_next_history_id = 0; - HistoryNode m_history; - SafePtr<HistoryNode> m_history_cursor; - SafePtr<HistoryNode> m_last_save_history_cursor; - UndoGroup m_current_undo_group; - - void move_to(HistoryNode* history_node); + Vector<HistoryNode> m_history; + HistoryId m_history_id = HistoryId::Invalid; + HistoryId m_last_save_history_id = HistoryId::Invalid; + UndoGroup m_current_undo_group; - template<typename Func> HistoryNode* find_history_node(HistoryNode* node, const Func& func); + HistoryNode& history_node(HistoryId id) { return m_history[(size_t)id]; } + const HistoryNode& history_node(HistoryId id) const { return m_history[(size_t)id]; } + HistoryNode& current_history_node() { return m_history[(size_t)m_history_id]; } + const HistoryNode& current_history_node() const { return m_history[(size_t)m_history_id]; } Vector<Change, MemoryDomain::BufferMeta> m_changes; |
