forked from zrax/pycdc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FastStack.h
81 lines (63 loc) · 1.6 KB
/
FastStack.h
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
#ifndef _PYC_FASTSTACK_H
#define _PYC_FASTSTACK_H
#include "ASTNode.h"
#include <stack>
class FastStack {
public:
FastStack(int size) : m_size(size), m_ptr(-1) {
m_stack = new PycRef<ASTNode>[m_size];
}
FastStack(const FastStack& copy) : m_size(copy.m_size), m_ptr(copy.m_ptr) {
m_stack = new PycRef<ASTNode>[m_size];
for (int i = 0; i <= m_ptr; i++)
m_stack[i] = copy.m_stack[i];
}
~FastStack() {
delete[] m_stack;
}
FastStack& operator=(const FastStack& copy) {
replace(copy);
return *this;
}
void push(PycRef<ASTNode> node) {
if (m_size == m_ptr + 1)
grow(1);
m_stack[++m_ptr] = node;
}
void pop() {
if (m_ptr > -1)
m_stack[m_ptr--] = Node_NULL;
}
PycRef<ASTNode> top() const
{
if (m_ptr > -1)
return m_stack[m_ptr];
else
return Node_NULL;
}
void replace(const FastStack& copy)
{
if (© == this)
return;
delete[] m_stack;
m_size = copy.m_size;
m_ptr = copy.m_ptr;
m_stack = new PycRef<ASTNode>[m_size];
for (int i = 0; i <= m_ptr; i++)
m_stack[i] = copy.m_stack[i];
}
void grow(int inc)
{
m_size += inc;
PycRef<ASTNode>* tmp = new PycRef<ASTNode>[m_size];
for (int i = 0; i <= m_ptr; i++)
tmp[i] = m_stack[i];
delete[] m_stack;
m_stack = tmp;
}
private:
PycRef<ASTNode>* m_stack;
int m_size, m_ptr;
};
typedef std::stack<FastStack> stackhist_t;
#endif