-
Notifications
You must be signed in to change notification settings - Fork 1
/
TemplateStack.H
executable file
·370 lines (324 loc) · 9.26 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#ifndef E_TEMPLATE_STACK
#define E_TEMPLATE_STACK
#include <stdlib.h>
#include <stdio.h>
#include "misc.h"
#include <iostream>
#ifndef DEFAULT_INITIAL_SIZE
#define DEFAULT_INITIAL_SIZE 16
#endif
using std::ostream;
using std::endl;
// 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 {
// friend ostream & operator<<<Type>(ostream &,TemplateStack<Type> &) ;
public:
TemplateStack(int size = DEFAULT_INITIAL_SIZE)
: currentSize(0) ,
maxSize(size) ,
arrayData ( (Type *) SafeMalloc(sizeof(Type) * maxSize) ) {
}
TemplateStack(TemplateStack const & originalStack)
: currentSize(originalStack.currentSize) ,
maxSize(originalStack.maxSize),
arrayData( (Type *) SafeMalloc(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 *) SafeMalloc(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
{
#ifdef DEBUG_ASSERT
Assert((k < maxSize) && (k >= 0),
"request for out of range element in TemplateStack::operator[]");
if (k > currentSize)
printf("Warning uninitialized element accesed in TemplateStack::operator[]\n");
#endif
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()
{
#ifdef DEBUG_ASSERT
VERIFY(currentSize >0);
#endif
return(arrayData[--currentSize]);
}
inline Type * Top() const
{
#ifdef DEBUG_ASSERT
VERIFY(currentSize > 0);
#endif
return((&arrayData[(currentSize - 1)]));
}
inline Type * Bottom() const
{
return(&(arrayData[0]));
}
inline void DeleteTop()
{
#ifdef DEBUG_ASSERT
VERIFY(currentSize >0);
#endif
currentSize--;
}
inline int Empty() const
{
return(currentSize == 0);
}
inline int NotEmpty() const
{
return(currentSize);
}
inline int Size() const
{
return currentSize;
}
inline int IndexOfTop() const
{
#ifdef DEBUG_ASSERT
VERIFY(currentSize >0);
#endif
return (currentSize-1);
}
inline Type & ItemAtTop() const
{
#ifdef DEBUG_ASSERT
VERIFY(currentSize >0);
#endif
return arrayData[currentSize-1];
}
inline int Capacity() const
{
return maxSize;
}
inline void SwapTwoElements(int first , int second)
{
#ifdef DEBUG_ASSERT
VERIFY( (first >= 0) && (first < currentSize)) ;
VERIFY( (second >= 0) && (second < currentSize) );
VERIFY(first != second);
#endif
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)
{
#ifdef DEBUG_ASSERT
Assert( (position >= 0) && (position < currentSize),
"In TemplateStack::InsertAtPosition: position out of range");
#endif
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)
{
#ifdef DEBUG_ASSERT
Assert( (position >= 0) && (position < currentSize),
"In TemplateStack::InsertAtPosition: position out of range");
#endif
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)
{
#ifdef DEBUG_ASSERT
Assert( ( (position >= 0) && (position < currentSize) ) ,
"Error in TemplateStack::RemoveIndex: position out of range");
#endif
if (position != --currentSize)
arrayData[position] = arrayData[currentSize];
}
void DeleteAtPosition(int position)
{
#ifdef DEBUG_ASSERT
Assert( ( (position >= 0) && (position < currentSize) ) ,
"Error in TemplateStack::Delete: position out of range");
#endif
for (int k = position; k < (currentSize-1);)
arrayData[k] = arrayData[++k];
currentSize--;
}
void DeleteAtPosition(const int position, const int size)
{
#ifdef DEBUG_ASSERT
Assert( ( (position >= 0) && (position < currentSize) ),
"Error in TemplateStack::Delete: position out of range");
Assert( (position + size) <= currentSize,
"Error in TemplateStack::Delete: size too big!");
#endif
for ( int k = position; k < currentSize - size + 1;)
arrayData[k] = arrayData[k+=size];
currentSize-=size;
}
inline void SetCurrentSize(const int newSize)
{
#ifdef DEBUG_ASSERT
VERIFY(newSize >= 0);
#endif
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 defined(WARN_WHEN_RESIZE)
cerr << "Warning doing resize in TemplateStack" << endl;
#elif defined(DO_NOT_ALLOW_RESIZE)
exitprogram("Resizing in TemplateStack when DO_NOT_ALLOW_RESIZE is defined",1);
#endif
if (NULL == (arrayData = (Type *)
realloc(arrayData,size)))
{
printf("realloc failed for TemplateStack from %s\n",__FILE__);
printf("exiting\n");
ExitProgramMacro("Error in TemplateStack::SpecialRealloc");
}
return(arrayData);
}
};
template <class Type>
ostream & operator<<(ostream & s, TemplateStack<Type> & theStack)
{
s << "TemplateStack has currentSize = " << theStack.currentSize << endl;
s << "maxSize = " << theStack.maxSize << endl;
s << "Contents of this TemplateStack are :";
s << endl;
for (int k = 0; k < theStack.currentSize ; k++)
s << theStack.arrayData[k] << endl;
return s;
}
#endif