-
Notifications
You must be signed in to change notification settings - Fork 0
/
Util.h
332 lines (268 loc) · 5.8 KB
/
Util.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
#ifndef UTIL_H
#define UTIL_H
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void utilInitialize ();
#define PRIME_PATH_LENGTH 1024 /* apparently, don't believe PATH_MAX */
extern char UserDir[PRIME_PATH_LENGTH];
int RNG (int max); /* 0 to max-1, inclusive */
int RNG (int min, int max); /* min to max, inclusive */
int NDX (int n, int x); /* rolls n dice with x faces on them */
int D20 (void); /* rolls 20 sided dice */
void shuffle (void *array, int nmemb, int size);
extern char IsVowelLookupTable [256];
int mini (int x, int y);
int maxi (int x, int y);
#define SHBUFLEN 256
char *GetBuf ();
//char (*GetBuff ())[SHBUFLEN];
#define YOUR(_X) ((_X)->your())
#define THE(_X) ((_X)->the())
#define AN(_X) ((_X)->an())
#define THESE(_X) ((_X)->these())
#define isvowel(_c) IsVowelLookupTable[((int)_c)]
/* Yeah, I shoulda used the STL vector... Eit! -- CADV */
template <class T>
class shVector
{
public:
shVector (int capacity = 4);
~shVector ();
int add (T item);
int count ();
T get (int index);
T *getPtr (int index);
void set (int index, T item);
int find (T item); /* returns idx of item, -1 if not found*/
int remove (T item);
T removeByIndex (int index);
void reset ();
void sort (int (* compareFunc) (T *, T *));
void ensure (int capacity);
void setCount (int size);
protected:
void grow (int newCapacity);
T *mItems;
int mCount;
int mCapacity;
int mDestroyItems;
};
template <class T>
class shHeap : public shVector <T> /* T must be a pointer type */
{
public:
shHeap (int (*compareFunc) (T, T),
int capacity = 4);
int insert (T item);
T extract ();
int (* mCompareFunc) (T, T);
};
//constructor
template <class T>
shVector<T>::shVector (int capacity)
{
mCapacity = capacity;
mItems = (T*) malloc (sizeof (T) * mCapacity);
mCount = 0;
mDestroyItems = 0;
}
template <class T>
shVector<T>::~shVector ()
{
if (mDestroyItems) {
int i;
for (i = 0; i < mCount; i++) {
//delete mItems[i];
}
}
free (mItems);
}
template <class T>
void
shVector<T>::ensure (int capacity)
{
if (mCapacity < capacity) {
grow (capacity);
}
}
/* used by SaveLoad.cpp */
template <class T>
void
shVector<T>::setCount (int size)
{
ensure (size);
mCount = size;
}
template <class T>
void
shVector<T>::grow (int newCapacity)
{
mItems = (T*) realloc (mItems, sizeof(T) * newCapacity);
mCapacity = newCapacity;
}
template <class T>
void
shVector<T>::reset ()
{
mCount = 0;
}
template <class T>
int
shVector<T>::add (T item)
{
if (++mCount == mCapacity) {
grow (mCapacity * 2);
}
mItems[mCount-1] = item;
return mCount - 1;
}
template <class T>
void
shVector<T>::set (int index, T item)
{
ensure (index * 2);
mItems[index] = item;
if (mCount <= index) {
mCount = index + 1;
}
}
template <class T>
int
shVector<T>::count ()
{
return mCount;
}
template <class T>
T
shVector<T>::get (int index)
{
return mItems[index];
}
template <class T>
T *
shVector<T>::getPtr (int index)
{
return &mItems[index];
}
template <class T>
int
shVector<T>::find (T item)
{
int i;
for (i = 0; i < mCount; i++) {
if (item == mItems[i]) {
return i;
}
}
return -1;
}
template <class T>
int
shVector<T>::remove (T item)
{
int i;
for (i = 0; i < mCount; i++) {
if (item == mItems[i]) {
if (i < mCount-1) {
memmove (&mItems[i], &mItems[i+1],
(mCount - i -1) * sizeof (T));
}
--mCount;
return i;
}
}
return -1;
}
template <class T>
T
shVector<T>::removeByIndex (int index)
{
T item = mItems[index];
if (index < mCount-1) {
memmove (&mItems[index], &mItems[index+1],
(mCount - index - 1) * sizeof (T));
}
--mCount;
return item;
}
void insertionsort (void *array, size_t nmemb, size_t size,
int (*compar) (const void *, const void *));
template <class T>
void
shVector<T>::sort (int (* compareFunc) (T *, T *))
{
/* must be a stable sort (mergesort is preferred) */
insertionsort (mItems, mCount, sizeof (T),
(int (*)(const void *, const void *)) compareFunc);
}
//constructor
template <class T>
shHeap<T>::shHeap (int (*compareFunc) (T, T),
int capacity)
: shVector<T> (capacity)
{
mCompareFunc = compareFunc;
}
template <class T>
int
shHeap<T>::insert (T item)
{
if (++this->mCount == this->mCapacity) {
this->grow (this->mCapacity * 2);
}
int i = this->mCount - 1;
while (i > 0 and
mCompareFunc (item, this->mItems[(i-1)/2]) < 0)
{
this->mItems[i] = this->mItems[(i-1)/2];
i = (i-1) / 2;
}
this->mItems[i] = item;
return i;
}
template <class T>
T
shHeap<T>::extract ()
{
T res;
int i, l, r, smallest;
if (0 == this->mCount) {
return NULL;
}
res = this->mItems[0];
this->mItems[0] = this->mItems[--this->mCount];
i = 0;
heapify:
l = 2 * i + 1;
r = 2 * i + 2;
if (l < this->mCount and
mCompareFunc (this->mItems[l], this->mItems[i]) < 0)
{
smallest = l;
} else {
smallest = i;
}
if (r < this->mCount and
mCompareFunc (this->mItems[r], this->mItems[smallest]) < 0)
{
smallest = r;
}
if (smallest != i) {
T tmp = this->mItems[i];
this->mItems[i] = this->mItems[smallest];
this->mItems[smallest] = tmp;
i = smallest;
goto heapify;
}
return res;
}
struct shLogFile
{
FILE *mDbgFile;
bool open ();
void close ();
void log (const char *format, ...);
};
extern struct shLogFile debug;
#endif