GList.h
1.72 KB
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
//
// GList.h
//
// This class defines a list container class that
// can hold items of any type. Items can be appended to
// the end of the list or inserted into a specific location
// in the list. Items can be removed from the list and
// the ordered of the remaining items is preserved.
//
// Note that the Remove message finds and removes the
// item with the given value. So if the list contains
// items of the same value, it will remove the first
// occurence of the item in the list. (This is not
// a problem for allocated pointer types, but it could
// be for other types.) If this is a problem, you may
// need to add a RemoveByIndex message.
//
#ifndef __GList__
#define __GList__
#include "AeTypes.h"
#define kDefAllocItems 32
#ifdef NDEBUG
#define invariant()
#endif
typedef enum { kForward = 0, kReverse } TListDir;
template <class T> class GList
{
public:
GList (void);
GList (int numAllocItems);
virtual ~GList (void);
Boolean Append (const T&);
Boolean Insert (const T&, int beforeIndex);
void Remove (const T&);
Boolean Find (const T&, int &);
int GetNum (void) { return fNumItems; }
T& operator[] (int);
// iteration methods
void Start (int& i, TListDir dir = kForward);
Boolean Next (int&, T&);
Boolean Prev (int&, T&);
private:
Boolean grow (void);
Boolean shrink (void);
int fNumItems;
int fNumAvailItems;
int fNumAllocItems;
T * fList;
Boolean fDidInsertFlag; // debugging only
Boolean fDidRemoveFlag; // debugging only
#ifndef NDEBUG
public:
static Boolean Test (void);
private:
void invariant (void);
#endif
};
#endif __GList__