-
Notifications
You must be signed in to change notification settings - Fork 665
/
FastStack.h
55 lines (43 loc) · 1.01 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
#ifndef _PYC_FASTSTACK_H
#define _PYC_FASTSTACK_H
#include "ASTNode.h"
#include <stack>
class FastStack {
public:
FastStack(int size) : m_ptr(-1) { m_stack.resize(size); }
FastStack(const FastStack& copy)
: m_stack(copy.m_stack), m_ptr(copy.m_ptr) { }
FastStack& operator=(const FastStack& copy)
{
m_stack = copy.m_stack;
m_ptr = copy.m_ptr;
return *this;
}
void push(PycRef<ASTNode> node)
{
if (static_cast<int>(m_stack.size()) == m_ptr + 1)
m_stack.emplace_back(nullptr);
m_stack[++m_ptr] = std::move(node);
}
void pop()
{
if (m_ptr > -1)
m_stack[m_ptr--] = nullptr;
}
PycRef<ASTNode> top() const
{
if (m_ptr > -1)
return m_stack[m_ptr];
else
return nullptr;
}
bool empty() const
{
return m_ptr == -1;
}
private:
std::vector<PycRef<ASTNode>> m_stack;
int m_ptr;
};
typedef std::stack<FastStack> stackhist_t;
#endif