-
Notifications
You must be signed in to change notification settings - Fork 6
/
templatestack.h
301 lines (257 loc) · 7.11 KB
/
templatestack.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* Copyright (c) Hilmi Yildirim 2010,2011.
The software is provided on an as is basis for research purposes.
There is no additional support offered, nor are the author(s)
or their institutions liable under any circumstances.
*/
#ifndef E_TEMPLATE_STACK
#define E_TEMPLATE_STACK
#include <stdlib.h>
#include <stdio.h>
#ifndef DEFAULT_INITIAL_SIZE
#define DEFAULT_INITIAL_SIZE 16
#endif
// TemplateStack is an array based stack which grows to
// the necessary size. The standard stack operations
// are supported for Type. Caution:: Nothing should
// point to something you put in a TemplateStack if
// you let the stack grow dynamically because
// the dynamic sizing can cause something pointing
// to an element of a TemplateStack to become invalid.
// If you want to keep pointers to things in the stack,
// make sure that resizing doesn't happen or keep indexes
// into the stack instead of pointers.
//
//
// ********************************************************
//
// See the html documentation in gui.html for detailed
// instructions on how to use this template.
//
// ********************************************************
template <class Type>
class TemplateStack {
public:
TemplateStack(int size = DEFAULT_INITIAL_SIZE)
: currentSize(0) ,
maxSize(size) ,
arrayData ( (Type *) malloc(sizeof(Type) * maxSize) ) {
}
TemplateStack(TemplateStack const & originalStack)
: currentSize(originalStack.currentSize) ,
maxSize(originalStack.maxSize),
arrayData( (Type *) malloc(sizeof(Type) * maxSize) )
{
int k = 0;
while (k < currentSize)
arrayData[k] = originalStack.arrayData[k++];
}
TemplateStack const & operator=(TemplateStack const & stackToCopy)
{
currentSize = stackToCopy.currentSize;
maxSize = stackToCopy.maxSize;
if (arrayData != NULL)
free(arrayData);
arrayData = (Type *) malloc(sizeof(Type) * maxSize);
int i;
for (i = 0; i < stackToCopy.currentSize; i++) {
arrayData[i] = stackToCopy.arrayData[i];
}
return *this;
}
~TemplateStack()
{ // if arrayData hasn't been freed we must free it
if (arrayData != NULL)
free(arrayData);
}
void Destroy(void (*DestFunc)(Type) )
// DestFunc is called on each item in the stack, then arrayData is freed
{
int k=0;
int j=currentSize;
while ( k < j)
DestFunc(arrayData[k++]);
free(arrayData);
arrayData = NULL;
currentSize = 0;
}
inline Type & operator[](int k) const
{
return arrayData[k];
}
void Push(Type newInfo)
{
if (currentSize == maxSize) {
maxSize *= 2;
arrayData = SpecialRealloc(arrayData,
sizeof(Type)*maxSize);
}
arrayData[currentSize] = newInfo;
currentSize += 1;
}
void Push(const TemplateStack<Type> & stackToAdd)
{
if ( currentSize + stackToAdd.Size() > maxSize ) {
maxSize += stackToAdd.Size();
arrayData = SpecialRealloc(arrayData,
sizeof(Type)*maxSize);
}
for (int i = 0, k = stackToAdd.Size(); i < k ;)
arrayData[currentSize++] = stackToAdd[i++];
}
void Push(const TemplateStack<Type> * const stackToAdd)
{
if ( currentSize + stackToAdd->Size() > maxSize ) {
maxSize += stackToAdd->Size();
arrayData = SpecialRealloc(arrayData,
sizeof(Type)*maxSize);
}
Type * dataToAdd = stackToAdd->GetAddressOfArray();
for (int i = 0, k = stackToAdd->Size(); i < k ;)
arrayData[currentSize++] = dataToAdd[i++];
}
inline Type * Member(Type const & possibleMemberOfStack)
{
for (int k=0; k < currentSize; k++)
if (possibleMemberOfStack == arrayData[k])
return &(arrayData[k]);
return NULL;
}
inline int GetIndexOf(Type const & possibleMemberOfStack) const
{
for (int k=0; k < currentSize; k++)
if (possibleMemberOfStack == arrayData[k])
return k;
return -1;
}
inline Type Pop()
{
return(arrayData[--currentSize]);
}
inline Type * Top() const
{
return((&arrayData[(currentSize - 1)]));
}
inline Type * Bottom() const
{
return(&(arrayData[0]));
}
inline void DeleteTop()
{
currentSize--;
}
inline int Empty() const
{
return(currentSize == 0);
}
inline int NotEmpty() const
{
return(currentSize);
}
inline int Size() const
{
return currentSize;
}
inline int IndexOfTop() const
{
return (currentSize-1);
}
inline Type & ItemAtTop() const
{
return arrayData[currentSize-1];
}
inline int Capacity() const
{
return maxSize;
}
inline void SwapTwoElements(int first , int second)
{
Type temp = arrayData[first];
arrayData[first] = arrayData[second];
arrayData[second] = temp;
}
inline Type * GetAddressOfArray() const { return arrayData; }
inline void Clear() { currentSize = 0;} // clears the TemplateStack
inline void Clear(void (*DestFunc)(Type ) )
{ // calls DestFunc on everything in the array and then clears it
for (int k=0; k < currentSize; k++)
DestFunc(arrayData[k]);
currentSize = 0;
}
void InsertAtPosition(const int position,
const int size,
const Type * arrayToInsert)
{
if ((size + currentSize) >= maxSize) {
maxSize += size;
arrayData = SpecialRealloc(arrayData,
sizeof(Type)*maxSize);
}
for ( int k = currentSize+size-1, stoppingPoint=position+size-1 ;
k > stoppingPoint;k--)
arrayData[k] = arrayData[(k-size)];
for ( int k = position, j = 0; j < size;)
arrayData[k++]=arrayToInsert[j++];
currentSize+=size;
}
void InsertAtPosition(const int position, const Type itemToInsert)
{
if ((1+currentSize) == maxSize) {
maxSize *= 2;
arrayData = SpecialRealloc(arrayData,
sizeof(Type)*maxSize);
}
for (int k = currentSize; k > position; )
arrayData[k] = arrayData[--k];
arrayData[position]=itemToInsert;
currentSize++;
}
inline void RemoveAtIndex(int position)
{
if (position != --currentSize)
arrayData[position] = arrayData[currentSize];
}
void DeleteAtPosition(int position)
{
for (int k = position; k < (currentSize-1);)
arrayData[k] = arrayData[++k];
currentSize--;
}
void DeleteAtPosition(const int position, const int size)
{
for ( int k = position; k < currentSize - size + 1;)
arrayData[k] = arrayData[k+=size];
currentSize-=size;
}
inline void SetCurrentSize(const int newSize)
{
currentSize = newSize;
}
inline void ForEachItemDo(void (*function)(Type)) const
{
for (int i = 0; i < currentSize; i++)
function(arrayData[i]);
}
inline void ForEachItemDo(void (*function)(Type, void * controller),
void *controller) const
{
for (int i = 0; i < currentSize; i++)
function(arrayData[i],controller);
}
protected:
int currentSize;
int maxSize;
Type * arrayData;
private:
inline Type * SpecialRealloc(Type * arrayData, int size)
{
if (NULL == (arrayData = (Type *)
realloc(arrayData,size)))
{
printf("realloc failed for TemplateStack from %s\n",__FILE__);
printf("exiting\n");
exit(1);
}
return(arrayData);
}
};
#endif