-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStudentUndo.cpp
89 lines (76 loc) · 1.66 KB
/
StudentUndo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "StudentUndo.h"
Undo* createUndo()
{
return new StudentUndo;
}
void StudentUndo::submit(const Action action, int row, int col, char ch) {
UndoAction submitAction;
submitAction.m_action = action;
submitAction.m_row = row;
submitAction.m_col = col;
submitAction.m_char = ch;
m_actions.push(submitAction);
}
StudentUndo::Action StudentUndo::get(int& row, int& col, int& count, std::string& text) {
if (m_actions.empty())
return ERROR;
count = 1;
text = "";
row = m_actions.top().m_row;
col = m_actions.top().m_col;
if (m_actions.top().m_action == INSERT)
{
//check for batching
m_actions.pop();
int prevCol = col;
while (!m_actions.empty() && m_actions.top().m_action == INSERT && m_actions.top().m_row == row && m_actions.top().m_col == prevCol - 1)
{
count++;
m_actions.pop();
prevCol--;
}
return DELETE;
}
if (m_actions.top().m_action == DELETE)
{
text = m_actions.top().m_char;
m_actions.pop();
int prevCol = col;
while (!m_actions.empty() && m_actions.top().m_action == DELETE && m_actions.top().m_row == row)
{
if (m_actions.top().m_col == prevCol + 1)
{
text += m_actions.top().m_char;
m_actions.pop();
prevCol++;
}
else if (m_actions.top().m_col == prevCol)
{
text = m_actions.top().m_char + text;
m_actions.pop();
}
else
break;
}
return INSERT;
}
if (m_actions.top().m_action == SPLIT)
{
m_actions.pop();
return JOIN;
}
if (m_actions.top().m_action == JOIN)
{
m_actions.pop();
return SPLIT;
}
return ERROR;
}
void StudentUndo::clear() {
while (!m_actions.empty())
m_actions.pop();
}
//pops top item from each stack
void StudentUndo::popStacks()
{
}