summaryrefslogtreecommitdiffhomepage
path: root/FoundationEx/List.inl
blob: e39cc27ba5cd3c9da44aade2b104d04c471f9e43 (plain)
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*  Starshatter: The Open Source Project
    Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors
    Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
    Copyright (c) 1997-2006, Destroyer Studios LLC.

    AUTHOR:       John DiCamillo


    OVERVIEW
    ========
    Implementation of the List class template
*/

// +-------------------------------------------------------------------+

void Print(const char* fmt, ...);

// +-------------------------------------------------------------------+

template <class T>
List<T>::List(const List<T>& l)
  : items(l.items), extent(l.extent)
{
#ifdef MEM_DEBUG
   array = new(__FILE__,__LINE__) PTR[extent];
#else
   array = new PTR[extent];
#endif

   for (int i = 0; i < extent; i++)
      array[i] = l.array[i];
}

template <class T>
void List<T>::clear()
{
   delete [] array;
   items       = 0;
   extent  = 0;
   array       = 0;
}

template <class T>
void List<T>::destroy()
{
   if (items) {
      items = 0;  // prevent dangerous re-entrancy

      for (int i = 0; i < extent; i++)
         delete array[i];

      delete [] array;
      items       = 0;
      extent  = 0;
      array       = 0;
   }
}

// +-------------------------------------------------------------------+

template <class T>
bool List<T>::check(int& index) const
{
   if (index < 0) {
      Print("Bounds error in List(%08x) T=%s index=%d min=0\n", (int)this, T::TYPENAME(), index);
      index = 0;
   }

   else if (index >= items) {
      Print("Bounds error in List(%08x) T=%s index=%d max=%d\n", (int)this, T::TYPENAME(), index, items-1);
      index = items-1;
   }

   return (index >= 0 && index < items);
}

// +-------------------------------------------------------------------+

template <class T>
T*& List<T>::operator[](int index)
{
   if (check(index))
      return array[index];

   if (!array || !extent)
      resize(1);

   return array[0];
}

template <class T>
T* List<T>::operator[](int index) const
{
   if (check(index))
      return array[index];
   return 0;
}

template <class T>
T*& List<T>::at(int index)
{
   if (check(index))
      return array[index];

   if (!array || !extent)
      resize(1);

   return array[0];
}

template <class T>
T* List<T>::at(int index) const
{
   if (check(index))
      return array[index];
   return 0;
}

// +-------------------------------------------------------------------+

template <class T>
void List<T>::resize(int newsize)
{
   if (newsize > extent) {
      extent = 16 * (newsize/16 + 1);

#ifdef MEM_DEBUG
      T** v = new(__FILE__,__LINE__) PTR[extent];
#else
      T** v = new PTR[extent];
#endif
      int i;
      for (i = 0; i < items; i++)
         v[i] = array[i];

      for (; i < extent; i++)
         v[i] = 0;

      delete [] array;
      array = v;
   }
}

// +-------------------------------------------------------------------+

template <class T>
void List<T>::append(const T* item)
{
   if (item) {
      if (items+1 > extent) resize(items+1);
      array[items++] = (T*)item;
   }
}

template <class T>
void List<T>::append(List<T>& list)
{
   if (&list != this && list.items > 0) {
      int need = items + list.items;
      if (need > extent) resize(need);

      for (int i = 0; i < list.items; i++)
         array[items++] = list.array[i];
   }
}

// +-------------------------------------------------------------------+

template <class T>
void List<T>::insert(const T* item, int index)
{
   if (item && index >= 0 && index <= items) {
      if (items+1 > extent) resize(items+1);

      // slide right:
      for (int i = items; i > index; i--)
         array[i] = array[i-1];

      array[index] = (T*)item;
      items++;
   }
}

// +-------------------------------------------------------------------+

template <class T>
void List<T>::insertSort(const T* item)
{
    if (item) {
        int i;
        for (i = 0; i < items; i++) {
            if (*item < *array[i])
                break;
        }

        insert(item, i);
    }
}

// +-------------------------------------------------------------------+

template <class T>
T* List<T>::remove(const T* val)
{
   if (items == 0 || val == 0)
      return 0;

   for (int i = 0; i < items; i++) {
      if (array[i] == val) {
         return removeIndex(i);
      }
   }

   return 0;
}

// +-------------------------------------------------------------------+

template <class T>
T* List<T>::removeIndex(int index)
{
   if (!check(index))
      return 0;

   T* tmp = array[index];
   array[index] = 0;

   // slide left:
   for (int i = index; i < items-1; i++)
      array[i] = array[i+1];

   // blank out the hole we just created:
   array[items-1] = 0;

   items--;
   return tmp;
}

// +-------------------------------------------------------------------+

template <class T>
bool List<T>::contains(const T* val) const
{
   if (val) {
      if (index(val) != -1)
          return true;
   }

   return false;
}

// +-------------------------------------------------------------------+

template <class T>
int List<T>::count(const T* val) const
{
   int c = 0;

   if (val) {
      for (int i = 0; i < items; i++) {
         if (array[i] && ((*array[i])==(*val)))
            c++;
      }
   }

   return c;
}

// +-------------------------------------------------------------------+

template <class T>
int List<T>::index(const T* val) const
{
   if (val) {
      for (int i = 0; i < items; i++) {
         if (array[i] && ((*array[i])==(*val)))
            return i;
      }
   }

   return -1;
}

// +-------------------------------------------------------------------+

template <class T>
T* List<T>::find(const T* val) const
{
   if (val) {
      for (int i = 0; i < items; i++) {
         if (array[i] && ((*array[i])==(*val)))
            return array[i];
      }
   }

   return 0;
}

// +-------------------------------------------------------------------+

template <class T>
void List<T>::swap(T** a, int i, int j)
{
   if (i >= 0 && i < items && j >= 0 && j < items && i != j) {
      T* t = a[i];
      a[i] = a[j];
      a[j] = t;
   }
}

template <class T>
void List<T>::qsort(T** a, int lo0, int hi0)
{
   int lo = lo0;
   int hi = hi0;

   // zero or one element list, nothing to do:
   if (lo >= hi) {
      return;
   }

   // two element list, swap if needed:
   else if (lo == hi-1) {
      if (*a[hi] < *a[lo]) {
         swap(a, lo, hi);
      }
      return;
   }

   // pick a pivot, and move it out of the way:
   int mid   = (lo+hi)/2;
   T*  pivot = a[mid];
   a[mid]    = a[hi];
   a[hi]     = pivot;

   while (lo < hi) {
      while ((*a[lo] <= *pivot) && lo < hi) lo++;
      while ((*pivot <= *a[hi]) && lo < hi) hi--;

      if (lo < hi) {
         swap(a, lo, hi);
      }
   }

   // Put the pivot into its final location:
   a[hi0] = a[hi];
   a[hi] = pivot;

    qsort(a, lo0, lo-1);
    qsort(a, hi+1, hi0);
}

template <class T>
void List<T>::sort()
{
   if (items < 2)
      return;

   qsort(array, 0, items-1);
}

template <class T>
void List<T>::shuffle()
{
   if (items < 3)
      return;

   for (int s = 0; s < 5; s++) {
      for (int i = 0; i < items; i++) {
         int j = (rand()>>4) % items;
         swap(array, i, j);
      }
   }
}

// +===================================================================+

template <class T>
T* ListIter<T>::value()
{
   if (list && step >= 0 && step < list->items)
      return list->array[step];

   return 0;
}

// +-------------------------------------------------------------------+

template <class T>
T* ListIter<T>::removeItem()
{
   if (list && step >= 0 && step < list->items)
      return list->removeIndex(step--);

   return 0;
}

// +-------------------------------------------------------------------+

template <class T>
T* ListIter<T>::next()
{
   if (list && step >= -1 && step < list->items-1)
      return list->array[++step];

   return 0;
}

template <class T>
T* ListIter<T>::prev()
{
   if (list && step > 0 && step < list->items)
      return list->array[--step];

   return 0;
}

// +-------------------------------------------------------------------+

template <class T>
void ListIter<T>::attach(List<T>& l)
{
   list = &l;
   step = -1;
}

// +-------------------------------------------------------------------+

template <class T>
int ListIter<T>::size()
{
   if (!list) return 0;
   return list->items;
}

// +-------------------------------------------------------------------+

template <class T>
List<T>& ListIter<T>::container()
{
   return *list;
}