]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/dlist.c
Add extern C to callback functions
[bacula/bacula] / bacula / src / lib / dlist.c
1 /*
2  *  Bacula doubly linked list routines.          
3  *
4  *    dlist is a doubly linked list with the links being in the
5  *       list data item.
6  *                
7  *   Kern Sibbald, July MMIII
8  *
9  *   Version $Id$
10  *
11  */
12 /*
13    Copyright (C) 2000-2004 Kern Sibbald and John Walker
14
15    This program is free software; you can redistribute it and/or
16    modify it under the terms of the GNU General Public License as
17    published by the Free Software Foundation; either version 2 of
18    the License, or (at your option) any later version.
19
20    This program is distributed in the hope that it will be useful,
21    but WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23    General Public License for more details.
24
25    You should have received a copy of the GNU General Public
26    License along with this program; if not, write to the Free
27    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28    MA 02111-1307, USA.
29
30  */
31
32 #include "bacula.h"
33
34 /* ===================================================================
35  *    dlist
36  */
37 /*
38  * Append an item to the list
39  */
40 void dlist::append(void *item) 
41 {
42    ((dlink *)(((char *)item)+loffset))->next = NULL;
43    ((dlink *)(((char *)item)+loffset))->prev = tail;
44    if (tail) {
45       ((dlink *)(((char *)tail)+loffset))->next = item;
46    }
47    tail = item;
48    if (head == NULL) {                /* if empty list, */
49       head = item;                    /* item is head as well */
50    }
51    num_items++;
52 }
53
54 /*
55  * Append an item to the list
56  */
57 void dlist::prepend(void *item) 
58 {
59    ((dlink *)(((char *)item)+loffset))->next = head;
60    ((dlink *)(((char *)item)+loffset))->prev = NULL;
61    if (head) {
62       ((dlink *)(((char *)head)+loffset))->prev = item;
63    }
64    head = item;
65    if (tail == NULL) {                /* if empty list, */                    
66       tail = item;                    /* item is tail too */
67    }
68    num_items++;
69 }
70
71 void dlist::insert_before(void *item, void *where)       
72 {
73    dlink *where_link = (dlink *)((char *)where+loffset);     
74
75    ((dlink *)(((char *)item)+loffset))->next = where;
76    ((dlink *)(((char *)item)+loffset))->prev = where_link->prev;
77
78    if (where_link->prev) {
79       ((dlink *)(((char *)(where_link->prev))+loffset))->next = item;
80    }
81    where_link->prev = item;
82    if (head == where) {
83       head = item;
84    }
85    num_items++;
86 }
87
88 void dlist::insert_after(void *item, void *where)       
89 {
90    dlink *where_link = (dlink *)((char *)where+loffset);     
91
92    ((dlink *)(((char *)item)+loffset))->next = where_link->next;
93    ((dlink *)(((char *)item)+loffset))->prev = where;
94
95    if (where_link->next) {
96       ((dlink *)(((char *)(where_link->next))+loffset))->prev = item;
97    }
98    where_link->next = item;
99    if (tail == where) {
100       tail = item;
101    }
102    num_items++;
103 }
104
105 /*
106  * Returns: item         if item inserted
107  *          other_item   if same value already exists (item not inserted)
108  */
109 void *dlist::binary_insert(void *item, int compare(void *item1, void *item2))
110 {
111    int comp;
112    int low, high, cur;
113    void *cur_item;
114
115    if (num_items == 0) {
116     //Dmsg0(000, "Append first.\n");
117       append(item);
118       return item;
119    }
120    if (num_items == 1) {
121       comp = compare(item, first());      
122       if (comp < 0) {
123          insert_before(item, first());
124        //Dmsg0(000, "Insert before first.\n");
125          return item;
126       } else if (comp > 0) {
127          insert_after(item, first());
128        //Dmsg0(000, "Insert after first.\n");
129          return item;
130       } else {
131        //Dmsg0(000, "Same as first.\n");
132          return first();
133       }
134    }
135    /* Check against last item */
136    comp = compare(item, last());
137    if (comp > 0) {
138       append(item);
139     //Dmsg0(000, "Appended item.\n");
140       return item;
141    } else if (comp == 0) {
142     //Dmsg0(000, "Same as last.\n");
143       return last();
144    }
145    /* Check against first item */
146    comp = compare(item, first());
147    if (comp < 0) {
148       insert_before(item, first());
149     //Dmsg0(000, "Inserted item before.\n");
150       return item;
151    } else if (comp == 0) {
152     //Dmsg0(000, "Same as first.\n");
153       return first();
154    }
155    if (num_items == 2) {
156       insert_after(item, first());
157     //Dmsg0(000, "Inserted item after.\n");
158       return item;
159    }
160    low = 1;
161    high = num_items;
162    cur = 1;
163    cur_item = first();
164    while (low < high) {
165       int nxt;
166       nxt = (low + high) / 2;
167       while (nxt > cur) {
168          cur_item = next(cur_item);
169          cur++;
170       }
171       while (nxt < cur) {
172          cur_item = prev(cur_item); 
173          cur--;
174       }
175     //Dmsg1(000, "Compare item to %d\n", cur);
176       comp = compare(item, cur_item);
177     //Dmsg2(000, "Compare item to %d = %d\n", cur, comp);
178       if (comp < 0) {
179          high = cur;
180        //Dmsg2(000, "set high; low=%d high=%d\n", low, high);
181       } else if (comp > 0) {
182          low = cur + 1;
183        //Dmsg2(000, "set low; low=%d high=%d\n", low, high);
184       } else {
185        //Dmsg1(000, "Same as item %d\n", cur);
186          return cur_item;
187       }
188    }
189    if (high == cur) {
190       insert_before(item, cur_item);
191     //Dmsg1(000, "Insert before item %d\n", cur);
192    } else {
193       insert_after(item, cur_item);
194     //Dmsg1(000, "Insert after item %d\n", cur);
195    }
196    return item;
197 }
198
199
200 void dlist::remove(void *item)
201 {
202    void *xitem;
203    dlink *ilink = (dlink *)(((char *)item)+loffset);   /* item's link */
204    if (item == head) {
205       head = ilink->next;
206       if (head) {
207          ((dlink *)(((char *)head)+loffset))->prev = NULL;
208       }
209       if (item == tail) {
210          tail = ilink->prev;
211       }
212    } else if (item == tail) {
213       tail = ilink->prev;
214       if (tail) {
215          ((dlink *)(((char *)tail)+loffset))->next = NULL;
216       }
217    } else {
218       xitem = ilink->next;
219       ((dlink *)(((char *)xitem)+loffset))->prev = ilink->prev;
220       xitem = ilink->prev;
221       ((dlink *)(((char *)xitem)+loffset))->next = ilink->next;
222    }
223    num_items--;
224    if (num_items == 0) {
225       head = tail = NULL;
226    }
227 }
228
229 void * dlist::next(void *item)
230 {
231    if (item == NULL) {
232       return head;
233    }
234    return ((dlink *)(((char *)item)+loffset))->next;
235 }
236
237 void * dlist::prev(void *item)
238 {
239    if (item == NULL) {
240       return tail;
241    }
242    return ((dlink *)(((char *)item)+loffset))->prev;
243 }
244
245
246 /* Destroy the list contents */
247 void dlist::destroy()
248 {
249    for (void *n=head; n; ) {
250       void *ni = ((dlink *)(((char *)n)+loffset))->next;
251       free(n);
252       n = ni;
253    }
254    num_items = 0;
255    head = tail = NULL;
256 }
257
258
259
260 #ifdef TEST_PROGRAM
261
262 struct MYJCR {
263    char *buf;
264    dlink link;
265 };
266
267 static int my_compare(void *item1, void *item2)
268 {
269    MYJCR *jcr1, *jcr2;
270    int comp;
271    jcr1 = (MYJCR *)item1;
272    jcr2 = (MYJCR *)item2;
273    comp = strcmp(jcr1->buf, jcr2->buf);
274  //Dmsg3(000, "compare=%d: %s to %s\n", comp, jcr1->buf, jcr2->buf);
275    return comp;
276 }
277
278 int main()
279 {
280    char buf[30];
281    dlist *jcr_chain;
282    MYJCR *jcr = NULL;
283    MYJCR *jcr1;
284    MYJCR *save_jcr = NULL;
285    MYJCR *next_jcr;
286
287    jcr_chain = (dlist *)malloc(sizeof(dlist));
288    jcr_chain->init(jcr, &jcr->link);
289     
290    printf("Prepend 20 items 0-19\n");
291    for (int i=0; i<20; i++) {
292       sprintf(buf, "This is dlist item %d", i);
293       jcr = (MYJCR *)malloc(sizeof(MYJCR));
294       jcr->buf = bstrdup(buf);
295       jcr_chain->prepend(jcr);
296       if (i == 10) {
297          save_jcr = jcr;
298       }
299    }
300
301    next_jcr = (MYJCR *)jcr_chain->next(save_jcr);
302    printf("11th item=%s\n", next_jcr->buf);
303    jcr = (MYJCR *)malloc(sizeof(MYJCR));
304    jcr->buf = save_jcr->buf;
305    printf("Remove 10th item\n");
306    jcr_chain->remove(save_jcr);
307    printf("Re-insert 10th item\n");
308    jcr_chain->insert_before(jcr, next_jcr);
309    
310    printf("Print remaining list.\n");
311    foreach_dlist (jcr, jcr_chain) {
312       printf("Dlist item = %s\n", jcr->buf);
313       free(jcr->buf);
314    }
315    jcr_chain->destroy();
316    free(jcr_chain);
317
318    jcr_chain = new dlist(jcr, &jcr->link);
319    printf("append 20 items 0-19\n");
320    for (int i=0; i<20; i++) {
321       sprintf(buf, "This is dlist item %d", i);
322       jcr = (MYJCR *)malloc(sizeof(MYJCR));
323       jcr->buf = bstrdup(buf);
324       jcr_chain->append(jcr);
325       if (i == 10) {
326          save_jcr = jcr;
327       }
328    }
329
330    next_jcr = (MYJCR *)jcr_chain->next(save_jcr);
331    printf("11th item=%s\n", next_jcr->buf);
332    jcr = (MYJCR *)malloc(sizeof(MYJCR));
333    jcr->buf = save_jcr->buf;
334    printf("Remove 10th item\n");
335    jcr_chain->remove(save_jcr);
336    printf("Re-insert 10th item\n");
337    jcr_chain->insert_before(jcr, next_jcr);
338    
339    printf("Print remaining list.\n");
340    foreach_dlist (jcr, jcr_chain) {
341       printf("Dlist item = %s\n", jcr->buf);
342       free(jcr->buf);
343    }
344
345    delete jcr_chain;
346
347
348    /* Now do a binary insert for the list */
349    jcr_chain = new dlist(jcr, &jcr->link);
350 #define CNT 26
351    printf("append %d items\n", CNT*CNT*CNT);
352    strcpy(buf, "ZZZ");
353    int count = 0;
354    for (int i=0; i<CNT; i++) {
355       for (int j=0; j<CNT; j++) {
356          for (int k=0; k<CNT; k++) {
357             count++;
358             if ((count & 0x3FF) == 0) {
359                Dmsg1(000, "At %d\n", count);
360             }
361             jcr = (MYJCR *)malloc(sizeof(MYJCR));
362             jcr->buf = bstrdup(buf);
363             jcr1 = (MYJCR *)jcr_chain->binary_insert(jcr, my_compare);
364             if (jcr != jcr1) {
365                Dmsg2(000, "Insert of %s vs %s failed.\n", jcr->buf, jcr1->buf);
366             }
367             buf[1]--;
368          }
369          buf[1] = 'Z';
370          buf[2]--;
371       }
372       buf[2] = 'Z';
373       buf[0]--;
374    }
375
376    printf("Print sorted list.\n");
377    foreach_dlist (jcr, jcr_chain) {
378       printf("Dlist item = %s\n", jcr->buf);
379       free(jcr->buf);
380    }
381
382    delete jcr_chain;
383
384
385    sm_dump(false);
386
387 }
388 #endif