修复了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 {
size_ = 0;
start_chunk = 0;
start_pos = 0;
ensure_capacity(0);
while(!empty()) pop();
}
};

View File

@ -3,6 +3,7 @@
#include "../mystl/my_stack.h"
#include <string>
#include <iostream>
enum class EditActiontype {
Initialize,
@ -18,9 +19,11 @@ struct EditAction {
class ActionManager {
private:
MyStack<MyVector<EditAction>> undoStack_, redoStack_;
MyStack<MyVector<EditAction>> undoStack_;
MyStack<MyVector<EditAction>> redoStack_;
std::string content_;
public:
MyVector<EditAction> 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<EditAction> actions = calculateEditActions(content_, content);
MyVector<EditAction> actions = calculateEditActions(content, content_);
content_ = content;
undoStack_.push(actions);
}
@ -119,7 +121,7 @@ public:
MyVector<EditAction> 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<EditAction> 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();

View File

@ -7,6 +7,7 @@ using namespace std;
int main() {
ActionManager am;
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");
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;
}