filters

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