-
Notifications
You must be signed in to change notification settings - Fork 256
/
dlist.c
347 lines (318 loc) · 7.78 KB
/
dlist.c
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2003-2011 Free Software Foundation Europe e.V.
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.
*/
/*
* BAREOS doubly linked list routines.
*
* dlist is a doubly linked list with the links being in the list data item.
*
* Kern Sibbald, July MMIII
*/
#include "bareos.h"
/*
* Append an item to the list
*/
void dlist::append(void *item)
{
set_next(item, NULL);
set_prev(item, tail);
if (tail) {
set_next(tail, item);
}
tail = item;
if (head == NULL) { /* if empty list, */
head = item; /* item is head as well */
}
num_items++;
}
/*
* Prepend an item to the list
*/
void dlist::prepend(void *item)
{
set_next(item, head);
set_prev(item, NULL);
if (head) {
set_prev(head, item);
}
head = item;
if (tail == NULL) { /* if empty list, */
tail = item; /* item is tail too */
}
num_items++;
}
void dlist::insert_before(void *item, void *where)
{
dlink *where_link = get_link(where);
set_next(item, where);
set_prev(item, where_link->prev);
if (where_link->prev) {
set_next(where_link->prev, item);
}
where_link->prev = item;
if (head == where) {
head = item;
}
num_items++;
}
void dlist::insert_after(void *item, void *where)
{
dlink *where_link = get_link(where);
set_next(item, where_link->next);
set_prev(item, where);
if (where_link->next) {
set_prev(where_link->next, item);
}
where_link->next = item;
if (tail == where) {
tail = item;
}
num_items++;
}
/*
* Insert an item in the list, but only if it is unique
* otherwise, the item is returned non inserted
*
* Returns: item if item inserted
* other_item if same value already exists (item not inserted)
*/
void *dlist::binary_insert(void *item, int compare(void *item1, void *item2))
{
int comp;
int low, high, cur;
void *cur_item;
if (num_items == 0) {
//Dmsg0(000, "Append first.\n");
append(item);
return item;
}
if (num_items == 1) {
comp = compare(item, first());
if (comp < 0) {
prepend(item);
//Dmsg0(000, "Insert before first.\n");
return item;
} else if (comp > 0) {
insert_after(item, first());
//Dmsg0(000, "Insert after first.\n");
return item;
} else {
//Dmsg0(000, "Same as first.\n");
return first();
}
}
/* Check against last item */
comp = compare(item, last());
if (comp > 0) {
append(item);
//Dmsg0(000, "Appended item.\n");
return item;
} else if (comp == 0) {
//Dmsg0(000, "Same as last.\n");
return last();
}
/* Check against first item */
comp = compare(item, first());
if (comp < 0) {
prepend(item);
//Dmsg0(000, "Inserted item before.\n");
return item;
} else if (comp == 0) {
//Dmsg0(000, "Same as first.\n");
return first();
}
if (num_items == 2) {
insert_after(item, first());
//Dmsg0(000, "Inserted item after.\n");
return item;
}
low = 1;
high = num_items;
cur = 1;
cur_item = first();
while (low < high) {
int nxt;
nxt = (low + high) / 2;
while (nxt > cur) {
cur_item = next(cur_item);
cur++;
}
while (nxt < cur) {
cur_item = prev(cur_item);
cur--;
}
//Dmsg1(000, "Compare item to %d\n", cur);
comp = compare(item, cur_item);
//Dmsg2(000, "Compare item to %d = %d\n", cur, comp);
if (comp < 0) {
high = cur;
//Dmsg2(000, "set high; low=%d high=%d\n", low, high);
} else if (comp > 0) {
low = cur + 1;
//Dmsg2(000, "set low; low=%d high=%d\n", low, high);
} else {
//Dmsg1(000, "Same as item %d\n", cur);
return cur_item;
}
}
if (high == cur) {
insert_before(item, cur_item);
//Dmsg1(000, "Insert before item %d\n", cur);
} else {
insert_after(item, cur_item);
//Dmsg1(000, "Insert after item %d\n", cur);
}
return item;
}
/*
* Insert an item in the list, regardless if it is unique
* or not.
*/
void dlist::binary_insert_multiple(void *item, int compare(void *item1, void *item2))
{
void *ins_item = binary_insert(item, compare);
/* If identical, insert after the one found */
if (ins_item != item) {
insert_after(item, ins_item);
}
}
/*
* Search for item
*/
void *dlist::binary_search(void *item, int compare(void *item1, void *item2))
{
int comp;
int low, high, cur;
void *cur_item;
if (num_items == 0) {
return NULL;
}
cur_item = first();
if (num_items == 1) {
comp = compare(item, cur_item);
if (comp == 0) {
return cur_item;
} else {
return NULL;
}
}
low = 1;
high = num_items;
cur = 1;
cur_item = first();
while (low < high) {
int nxt;
nxt = (low + high) / 2;
/* Now get cur pointing to nxt */
while (nxt > cur) {
cur_item = next(cur_item);
cur++;
}
while (nxt < cur) {
cur_item = prev(cur_item);
cur--;
}
comp = compare(item, cur_item);
//Dmsg2(000, "Compare item to %d = %d\n", cur, comp);
if (comp < 0) {
high = cur;
//Dmsg2(000, "set high; low=%d high=%d\n", low, high);
} else if (comp > 0) {
low = cur + 1;
//Dmsg2(000, "set low; low=%d high=%d\n", low, high);
} else {
return cur_item;
}
}
/*
* low == high can only happen if low just
* got incremented from cur, and we have
* not yet tested cur+1
*/
if (low == high) {
cur_item = next(cur_item);
comp = compare(item, cur_item);
if (comp == 0) {
return cur_item;
}
}
return NULL;
}
void dlist::remove(void *item)
{
void *xitem;
dlink *ilink = get_link(item); /* item's link */
if (item == head) {
head = ilink->next;
if (head) {
set_prev(head, NULL);
}
if (item == tail) {
tail = ilink->prev;
}
} else if (item == tail) {
tail = ilink->prev;
if (tail) {
set_next(tail, NULL);
}
} else {
xitem = ilink->next;
set_prev(xitem, ilink->prev);
xitem = ilink->prev;
set_next(xitem, ilink->next);
}
num_items--;
if (num_items == 0) {
head = tail = NULL;
}
}
void *dlist::next(void *item)
{
if (item == NULL) {
return head;
}
return get_next(item);
}
void *dlist::prev(void *item)
{
if (item == NULL) {
return tail;
}
return get_prev(item);
}
/* Destroy the list contents */
void dlist::destroy()
{
for (void *n=head; n; ) {
void *ni = get_next(n);
free(n);
n = ni;
}
num_items = 0;
head = tail = NULL;
}
/* String helpers for dlist usage */
dlistString *new_dlistString(const char *str)
{
return new_dlistString(str, strlen(str));
}
dlistString *new_dlistString(const char *str, int len)
{
dlistString *node;
node = (dlistString *)malloc(sizeof(dlink) + len +1);
bstrncpy(node->c_str(), str, len + 1);
return node;
}