-
Notifications
You must be signed in to change notification settings - Fork 0
/
sli_tokenstack.h
125 lines (103 loc) · 1.94 KB
/
sli_tokenstack.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
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
119
120
121
122
123
124
125
/*
* tokenstack.h
*
* This file is part of NEST
*
* Copyright (C) 2004 by
* The NEST Initiative
*
* See the file AUTHORS for details.
*
* Permission is granted to compile and modify
* this file for non-commercial use.
* See the file LICENSE for details.
*
*/
#ifndef SLI_TOKENSTACK_H
#define SLI_TOKENSTACK_H
/*
SLI's stack for tokens
*/
#include "sli_token.h"
#include "sli_array.h"
/* This stack implementation assumes that functions are only called,
if the necessary pre-requisites are fulfilled. The code will break
otherwise.
*/
namespace sli3
{
class TokenStack : private TokenArray
{
public:
TokenStack(size_t n )
: TokenArray(0,Token(),n) {}
TokenStack(const TokenArray& ta)
: TokenArray(ta) {}
using TokenArray::reserve;
using TokenArray::reserve_token;
void clear(void)
{
erase(begin(),end());
}
void push(const Token& e)
{
push_back(e);
}
void pop(void)
{
pop_back();
}
void pop(size_t n)
{
erase(end()-n, end());
}
Token& top(void)
{
return *(end()-1);
}
const Token& top(void) const
{
return *(end()-1);
}
const Token& pick(size_t i) const
{
return *(end()-i-1);
}
Token& pick(size_t i)
{
return *(end()-i-1);
}
using TokenArray::empty;
void swap(void)
{
(end()-1)->swap(*(end()-2));
}
void swap(Token &e)
{
(end()-1)->swap(e);
}
void index(size_t i)
{
push(pick(i));
}
void roll(size_t n, long k)
{
if (k>=0)
{
rotate(end()-n,end()-(k%n),end());
}
else
{
rotate(end()-n,end()-(n+k)%n,end());
}
}
size_t size(void) const {return TokenArray::capacity();}
size_t load(void) const {return TokenArray::size();}
void dump(std::ostream &) const;
TokenArray * toArray(void) const
{
return new TokenArray(*this);
}
};
}
#endif