Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
itpp
base
stack.h
Go to the documentation of this file.
1
35
#ifndef STACK_H
36
#define STACK_H
37
38
#include <
itpp/base/itassert.h
>
39
40
41
namespace
itpp
42
{
43
65
template
<
class
T>
66
class
Stack
67
{
68
public
:
70
Stack
();
72
Stack
(
int
n);
74
Stack
(
const
Stack<T>
&s);
76
virtual
~Stack
();
77
79
T
pop
();
81
T
peek
()
const
;
83
void
push
(T v);
85
void
clear
();
86
88
void
operator=
(
const
Stack<T>
&s);
89
91
int
size
()
const
{
return
ndata; }
93
int
no_elements
()
const
{
return
valptr; }
95
void
set_size
(
int
n,
bool
copy =
false
);
96
97
private
:
98
int
valptr;
99
int
ndata;
100
T *data;
101
102
private
:
103
void
alloc(
int
n);
104
void
free();
105
};
106
107
// --------------------------- Implementation starts here ----------------------------------
108
109
template
<
class
T>
110
Stack<T>::Stack
()
111
{
112
data = 0;
113
ndata = 0;
114
valptr = 0;
115
}
116
117
template
<
class
T>
118
Stack<T>::Stack
(
int
n)
119
{
120
alloc(n);
121
valptr = 0;
122
}
123
124
template
<
class
T>
125
Stack<T>::Stack
(
const
Stack<T>
&s)
126
{
127
data = NULL;
128
ndata = 0;
129
valptr = s.valptr;
130
alloc(s.ndata);
131
for
(
int
i = 0; i < s.ndata; i++)
132
data[i] = s.data[i];
133
}
134
135
template
<
class
T>
136
Stack<T>::~Stack
()
137
{
138
free();
139
}
140
141
template
<
class
T>
142
T
Stack<T>::pop
()
143
{
144
it_error_if
(valptr == 0,
"Stack<T>::pop: Empty stack"
);
145
valptr--;
146
return
data[valptr];
147
}
148
149
template
<
class
T>
150
T
Stack<T>::peek
()
const
151
{
152
it_error_if
(valptr == 0,
"Stack<T>::peek: Empty stack"
);
153
return
data[valptr-1];
154
}
155
156
template
<
class
T>
157
void
Stack<T>::push
(T v)
158
{
159
it_error_if
(valptr >= ndata,
"Stack<T>::push: Full stack"
);
160
data[valptr] = v;
161
valptr++;
162
}
163
164
template
<
class
T>
165
void
Stack<T>::clear
()
166
{
167
valptr = 0;
168
}
169
170
template
<
class
T>
171
void
Stack<T>::alloc
(
int
n)
172
{
173
if
(n == 0) {
174
data = NULL;
175
ndata = 0;
176
}
177
else
{
178
data =
new
T[n];
179
it_assert_debug
(data != 0,
"Out of memory in Stack::alloc"
);
180
}
181
ndata = n;
182
}
183
184
template
<
class
T>
185
void
Stack<T>::free()
186
{
187
188
delete
[] data;
189
190
data = 0;
191
ndata = 0;
192
}
193
194
template
<
class
T>
195
void
Stack<T>::operator=
(
const
Stack<T>
&s)
196
{
197
set_size(s.ndata);
198
for
(
int
i = 0; i < ndata; i++)
199
data[i] = s.data[i];
200
valptr = 0;
201
}
202
203
template
<
class
T>
204
void
Stack<T>::set_size
(
int
sz,
bool
copy)
205
{
206
int
i,
min
;
207
T *tmp;
208
209
if
(ndata == sz)
210
return
;
211
212
if
(copy) {
213
tmp = data;
214
min = ndata < sz ? ndata : sz;
215
alloc(sz);
216
for
(i = 0; i <
min
; i++)
217
data[i] = tmp[i];
218
delete
[] tmp;
219
}
220
else
{
221
free();
222
alloc(sz);
223
}
224
ndata = sz;
225
}
226
227
}
// namespace itpp
228
229
#endif // #ifndef STACK_H
Generated on Sat May 25 2013 16:32:20 for IT++ by
Doxygen
1.8.2