-
Notifications
You must be signed in to change notification settings - Fork 256
/
alist.h
178 lines (154 loc) · 4.4 KB
/
alist.h
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
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2003-2012 Free Software Foundation Europe e.V.
Copyright (C) 2016-2016 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
* Kern Sibbald, June MMIII
*/
/**
* @file
* alist header file
*/
/**
* There is a lot of extra casting here to work around the fact
* that some compilers (Sun and Visual C++) do not accept
* (void *) as an lvalue on the left side of an equal.
*
* Loop var through each member of list
* Loop var through each member of list using an increasing index.
* Loop var through each member of list using an decreasing index.
*/
#ifdef HAVE_TYPEOF
#define foreach_alist(var, list) \
for ( (var)=list ? (typeof((var)))(list)->first() : NULL; \
(var); \
(var) = (typeof(var))(list)->next() )
#define foreach_alist_index(inx, var, list) \
for ( (inx) = 0; \
list ? ((var) = (typeof((var)))(list)->get((inx))) : NULL; \
(inx)++ )
#define foreach_alist_rindex(inx, var, list) \
for ( list ? (inx) = ((list)->size() - 1) : 0; \
list ? ((var) = (typeof((var)))(list)->get((inx))) : NULL; \
(inx)--)
#else
#define foreach_alist(var, list) \
for ( list ? (*((void **)&(var))=(void*)((list)->first())) : NULL; \
(var); \
(*((void **)&(var))=(void*)((list)->next())) )
#define foreach_alist_index(inx, var, list) \
for ( (inx) = 0; \
list ? ((*((void **)&(var)) = (void*)((list)->get((inx))))) : NULL; \
(inx)++ )
#define foreach_alist_rindex(inx, var, list) \
for ( list ? (inx) = ((list)->size() - 1) : 0; \
list ? ((*((void **)&(var)) = (void*)((list)->get((inx))))) : NULL; \
(inx)-- )
#endif
/**
* Second arg of init
*/
enum {
owned_by_alist = true,
not_owned_by_alist = false
};
/**
* Array list -- much like a simplified STL vector
* array of pointers to inserted items
*/
class DLL_IMP_EXP alist : public SMARTALLOC {
void **items;
int num_items;
int max_items;
int num_grow;
int cur_item;
bool own_items;
void grow_list(void);
public:
alist(int num = 1, bool own=true);
~alist();
void init(int num = 1, bool own=true);
void append(void *item);
void prepend(void *item);
void *remove(int index);
void *get(int index);
bool empty() const;
void *prev();
void *next();
void *first();
void *last();
void * operator [](int index) const;
int current() const { return cur_item; };
int size() const;
void destroy();
void grow(int num);
/*
* Use it as a stack, pushing and poping from the end
*/
void push(void *item) { append(item); };
void *pop() { return remove(num_items - 1); };
};
/**
* Define index operator []
*/
inline void * alist::operator [](int index) const {
if (index < 0 || index >= num_items) {
return NULL;
}
return items[index];
}
inline bool alist::empty() const
{
return num_items == 0;
}
/**
* This allows us to do explicit initialization,
* allowing us to mix C++ classes inside malloc'ed
* C structures. Define before called in constructor.
*/
inline void alist::init(int num, bool own)
{
items = NULL;
num_items = 0;
max_items = 0;
num_grow = num;
own_items = own;
}
/* Constructor */
inline alist::alist(int num, bool own)
{
init(num, own);
}
/* Destructor */
inline alist::~alist()
{
destroy();
}
/* Current size of list */
inline int alist::size() const
{
/*
* Check for null pointer, which allows test
* on size to succeed even if nothing put in
* alist.
*/
return num_items;
}
/* How much to grow by each time */
inline void alist::grow(int num)
{
num_grow = num;
}