| 1 |
//======================================================================== |
| 2 |
// |
| 3 |
// GList.h |
| 4 |
// |
| 5 |
// Copyright 2001-2003 Glyph & Cog, LLC |
| 6 |
// |
| 7 |
//======================================================================== |
| 8 |
|
| 9 |
#ifndef GLIST_H |
| 10 |
#define GLIST_H |
| 11 |
|
| 12 |
#include <aconf.h> |
| 13 |
|
| 14 |
#include "gtypes.h" |
| 15 |
|
| 16 |
//------------------------------------------------------------------------ |
| 17 |
// GList |
| 18 |
//------------------------------------------------------------------------ |
| 19 |
|
| 20 |
class GList { |
| 21 |
public: |
| 22 |
|
| 23 |
// Create an empty list. |
| 24 |
GList(); |
| 25 |
|
| 26 |
// Create an empty list with space for <size1> elements. |
| 27 |
GList(int sizeA); |
| 28 |
|
| 29 |
// Destructor - does not free pointed-to objects. |
| 30 |
~GList(); |
| 31 |
|
| 32 |
//----- general |
| 33 |
|
| 34 |
// Get the number of elements. |
| 35 |
int getLength() { return length; } |
| 36 |
|
| 37 |
// Returns a (shallow) copy of this list. |
| 38 |
GList *copy(); |
| 39 |
|
| 40 |
//----- ordered list support |
| 41 |
|
| 42 |
// Return the <i>th element. |
| 43 |
// Assumes 0 <= i < length. |
| 44 |
void *get(int i) { return data[i]; } |
| 45 |
|
| 46 |
// Replace the <i>th element. |
| 47 |
// Assumes 0 <= i < length. |
| 48 |
void put(int i, void *p) { data[i] = p; } |
| 49 |
|
| 50 |
// Append an element to the end of the list. |
| 51 |
void append(void *p); |
| 52 |
|
| 53 |
// Append another list to the end of this one. |
| 54 |
void append(GList *list); |
| 55 |
|
| 56 |
// Insert an element at index <i>. |
| 57 |
// Assumes 0 <= i <= length. |
| 58 |
void insert(int i, void *p); |
| 59 |
|
| 60 |
// Deletes and returns the element at index <i>. |
| 61 |
// Assumes 0 <= i < length. |
| 62 |
void *del(int i); |
| 63 |
|
| 64 |
// Sort the list accoring to the given comparison function. |
| 65 |
// NB: this sorts an array of pointers, so the pointer args need to |
| 66 |
// be double-dereferenced. |
| 67 |
void sort(int (*cmp)(const void *ptr1, const void *ptr2)); |
| 68 |
|
| 69 |
// Sort <n> items starting at <first>. |
| 70 |
void sort(int first, int n, int (*cmp)(const void *ptr1, const void *ptr2)); |
| 71 |
|
| 72 |
// Reverse the list. |
| 73 |
void reverse(); |
| 74 |
|
| 75 |
//----- control |
| 76 |
|
| 77 |
// Set allocation increment to <inc>. If inc > 0, that many |
| 78 |
// elements will be allocated every time the list is expanded. |
| 79 |
// If inc <= 0, the list will be doubled in size. |
| 80 |
void setAllocIncr(int incA) { inc = incA; } |
| 81 |
|
| 82 |
private: |
| 83 |
|
| 84 |
void expand(); |
| 85 |
void shrink(); |
| 86 |
|
| 87 |
void **data; // the list elements |
| 88 |
int size; // size of data array |
| 89 |
int length; // number of elements on list |
| 90 |
int inc; // allocation increment |
| 91 |
}; |
| 92 |
|
| 93 |
#define deleteGList(list, T) \ |
| 94 |
do { \ |
| 95 |
GList *_list = (list); \ |
| 96 |
{ \ |
| 97 |
int _i; \ |
| 98 |
for (_i = 0; _i < _list->getLength(); ++_i) { \ |
| 99 |
delete (T*)_list->get(_i); \ |
| 100 |
} \ |
| 101 |
delete _list; \ |
| 102 |
} \ |
| 103 |
} while (0) |
| 104 |
|
| 105 |
#endif |