Files
Pyarmor-Static-Unpack-1shot/FastStack.h

72 lines
1.5 KiB
C
Raw Normal View History

2009-07-26 10:07:13 +00:00
#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); }
2010-12-30 16:48:59 -08:00
FastStack(const FastStack& copy)
: m_stack(copy.m_stack), m_ptr(copy.m_ptr) { }
2009-07-26 10:07:13 +00:00
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);
2011-09-18 23:55:27 -07:00
m_stack[++m_ptr] = std::move(node);
2010-12-30 16:48:59 -08:00
}
2009-07-26 10:07:13 +00:00
void pop()
{
2010-12-30 16:48:59 -08:00
if (m_ptr > -1)
m_stack[m_ptr--] = nullptr;
2025-06-30 20:57:21 +05:30
else {
#ifdef BLOCK_DEBUG
fprintf(stderr, "pop from empty stack\n");
#endif
}
2010-12-30 16:48:59 -08:00
}
2009-07-26 10:07:13 +00:00
2025-06-30 23:58:54 +05:30
PycRef<ASTNode> top(int i = 1) const
2010-12-18 22:18:32 -08:00
{
2025-06-30 23:58:54 +05:30
if (i > 0) {
int idx = m_ptr + 1 - i;
if ((m_ptr > -1) && (idx >= 0))
return m_stack[idx];
else {
#ifdef BLOCK_DEBUG
fprintf(stderr, "insufficient values on stack\n");
#endif
return nullptr;
}
}
else {
fprintf(stderr, "incorrect operand %i\n", i);
return nullptr;
2025-07-01 14:01:05 +05:30
}
}
bool empty() const
{
return m_ptr == -1;
}
2009-07-26 10:07:13 +00:00
private:
std::vector<PycRef<ASTNode>> m_stack;
int m_ptr;
2009-07-26 10:07:13 +00:00
};
typedef std::stack<FastStack> stackhist_t;
#endif