修复了MyDeque的clear()方法

This commit is contained in:
梦凌汐 2024-12-19 09:23:16 +08:00
parent 00b7386cfb
commit 0a4ab3c433
3 changed files with 34 additions and 10 deletions

View File

@ -203,10 +203,7 @@ public:
} }
void clear() override { void clear() override {
size_ = 0; while(!empty()) pop();
start_chunk = 0;
start_pos = 0;
ensure_capacity(0);
} }
}; };

View File

@ -3,6 +3,7 @@
#include "../mystl/my_stack.h" #include "../mystl/my_stack.h"
#include <string> #include <string>
#include <iostream>
enum class EditActiontype { enum class EditActiontype {
Initialize, Initialize,
@ -18,9 +19,11 @@ struct EditAction {
class ActionManager { class ActionManager {
private: private:
MyStack<MyVector<EditAction>> undoStack_, redoStack_; MyStack<MyVector<EditAction>> undoStack_;
MyStack<MyVector<EditAction>> redoStack_;
std::string content_; std::string content_;
public:
MyVector<EditAction> calculateEditActions(const std::string &str1, const std::string &str2) { // 规定Editor只会在删除或者插入时提交因此只会同时存在一种操作 MyVector<EditAction> calculateEditActions(const std::string &str1, const std::string &str2) { // 规定Editor只会在删除或者插入时提交因此只会同时存在一种操作
int m = str1.length(); int m = str1.length();
int n = str2.length(); int n = str2.length();
@ -98,7 +101,6 @@ private:
return newContent; return newContent;
} }
public:
ActionManager() { ActionManager() {
} }
ActionManager(const std::string &content) { ActionManager(const std::string &content) {
@ -107,7 +109,7 @@ public:
void updateContent(const std::string &content) { void updateContent(const std::string &content) {
redoStack_.clear(); redoStack_.clear();
MyVector<EditAction> actions = calculateEditActions(content_, content); MyVector<EditAction> actions = calculateEditActions(content, content_);
content_ = content; content_ = content;
undoStack_.push(actions); undoStack_.push(actions);
} }
@ -119,7 +121,7 @@ public:
MyVector<EditAction> actions = undoStack_.top(); MyVector<EditAction> actions = undoStack_.top();
undoStack_.pop(); undoStack_.pop();
std::string newContent = applyEditAction(actions, content_); std::string newContent = applyEditAction(actions, content_);
redoStack_.push(calculateEditActions(content_, newContent)); redoStack_.push(calculateEditActions(newContent, content_));
content_ = newContent; content_ = newContent;
} }
@ -130,11 +132,11 @@ public:
MyVector<EditAction> actions = redoStack_.top(); MyVector<EditAction> actions = redoStack_.top();
redoStack_.pop(); redoStack_.pop();
std::string newContent = applyEditAction(actions, content_); std::string newContent = applyEditAction(actions, content_);
undoStack_.push(calculateEditActions(content_, newContent)); undoStack_.push(calculateEditActions(newContent, content_));
content_ = newContent; content_ = newContent;
} }
void setContent(const std::string &content) { void setOriginContent(std::string content) {
content_ = content; content_ = content;
undoStack_.clear(); undoStack_.clear();
redoStack_.clear(); redoStack_.clear();

View File

@ -7,6 +7,7 @@ using namespace std;
int main() { int main() {
ActionManager am; ActionManager am;
MyVector<EditAction> v; MyVector<EditAction> 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"); 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()); printf("Actions: %zu\n", v.size());
//replay test //replay test
@ -19,5 +20,29 @@ int main() {
} }
} }
printf("%s\n", s1.c_str()); 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; return 0;
} }