diff --git a/mystl/my_deque.h b/mystl/my_deque.h index c7341dd..da12b8c 100644 --- a/mystl/my_deque.h +++ b/mystl/my_deque.h @@ -203,10 +203,7 @@ public: } void clear() override { - size_ = 0; - start_chunk = 0; - start_pos = 0; - ensure_capacity(0); + while(!empty()) pop(); } }; diff --git a/utils/ActionManager.h b/utils/ActionManager.h index 979dc24..e2156fd 100644 --- a/utils/ActionManager.h +++ b/utils/ActionManager.h @@ -3,6 +3,7 @@ #include "../mystl/my_stack.h" #include +#include enum class EditActiontype { Initialize, @@ -18,9 +19,11 @@ struct EditAction { class ActionManager { private: - MyStack> undoStack_, redoStack_; + MyStack> undoStack_; + MyStack> redoStack_; std::string content_; +public: MyVector calculateEditActions(const std::string &str1, const std::string &str2) { // 规定Editor只会在删除或者插入时提交,因此只会同时存在一种操作 int m = str1.length(); int n = str2.length(); @@ -98,7 +101,6 @@ private: return newContent; } -public: ActionManager() { } ActionManager(const std::string &content) { @@ -107,7 +109,7 @@ public: void updateContent(const std::string &content) { redoStack_.clear(); - MyVector actions = calculateEditActions(content_, content); + MyVector actions = calculateEditActions(content, content_); content_ = content; undoStack_.push(actions); } @@ -119,7 +121,7 @@ public: MyVector actions = undoStack_.top(); undoStack_.pop(); std::string newContent = applyEditAction(actions, content_); - redoStack_.push(calculateEditActions(content_, newContent)); + redoStack_.push(calculateEditActions(newContent, content_)); content_ = newContent; } @@ -130,11 +132,11 @@ public: MyVector actions = redoStack_.top(); redoStack_.pop(); std::string newContent = applyEditAction(actions, content_); - undoStack_.push(calculateEditActions(content_, newContent)); + undoStack_.push(calculateEditActions(newContent, content_)); content_ = newContent; } - void setContent(const std::string &content) { + void setOriginContent(std::string content) { content_ = content; undoStack_.clear(); redoStack_.clear(); diff --git a/utils/ActionManager_test.cpp b/utils/ActionManager_test.cpp index 9cfbe3f..71746b7 100644 --- a/utils/ActionManager_test.cpp +++ b/utils/ActionManager_test.cpp @@ -7,6 +7,7 @@ using namespace std; int main() { ActionManager am; MyVector v; + std::string s0 = "i can see you, but u dont love me"; v = am.calculateEditActions("i can see you, but u dont love me", "i cant hug u, but i love you"); printf("Actions: %zu\n", v.size()); //replay test @@ -19,5 +20,29 @@ int main() { } } printf("%s\n", s1.c_str()); + + am.setOriginContent(s0); + + am.updateContent("i can not see you, but u dont love me"); + am.updateContent("i can not see you, but u love me"); + am.updateContent("i can not see you, but u love me too"); + + printf("%s\n", am.getContent().c_str()); + + am.undo(); + + printf("%s\n", am.getContent().c_str()); + + am.undo(); + + printf("%s\n", am.getContent().c_str()); + + am.redo(); + + printf("%s\n", am.getContent().c_str()); + + am.updateContent("i can not see you, but u still love me"); + + printf("%s\n", am.getContent().c_str()); return 0; } \ No newline at end of file