-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathStack.cpp
118 lines (101 loc) · 1.73 KB
/
Stack.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "Stack.h"
#include <assert.h>
Stack::Stack(int base_stack_size)
{
stack_.reserve(base_stack_size);
}
std::size_t Stack::Size() const
{
return stack_.size();
}
StackValue * Stack::GetStackValue(int index)
{
int size = stack_.size();
if (index >= 0)
{
if (index < size)
return &stack_[index];
}
else
{
index += size;
if (index >= 0)
return &stack_[index];
}
return 0;
}
const StackValue * Stack::GetStackValue(int index) const
{
int size = stack_.size();
if (index >= 0)
{
if (index < size)
return &stack_[index];
}
else
{
index += size;
if (index >= 0)
return &stack_[index];
}
return 0;
}
StackValue * Stack::Top()
{
if (stack_.empty())
return 0;
return &stack_.back();
}
const StackValue * Stack::Top() const
{
if (stack_.empty())
return 0;
return &stack_.back();
}
void Stack::Pop(int count)
{
assert(count > 0);
int remain = static_cast<int>(stack_.size()) - count;
remain = remain < 0 ? 0 : remain;
stack_.resize(remain);
}
Value* Stack::popValue()
{
if (stack_.empty())
return nullptr;
StackValue* val = &stack_.back();
Pop();
return val->param.value;
}
void Stack::Clear()
{
stack_.clear();
}
StackValue * Stack::Push()
{
stack_.resize(stack_.size() + 1);
return Top();
}
void Stack::Push(Value *value)
{
StackValue *sv = Push();
sv->type = StackValueType_Value;
sv->param.value = value;
}
void Stack::Push(int total, int current)
{
StackValue *sv = Push();
sv->type = StackValueType_Counter;
sv->param.counter.total = total;
sv->param.counter.current = current;
}
void Stack::MarkStackValues()
{
for (auto it = stack_.begin(); it != stack_.end(); ++it)
{
StackValue *sv = &(*it);
if (sv->type == StackValueType_Value) {
//sv->param.value->Mark();
}
}
}