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