2 Bacula® - The Network Backup Solution
4 Copyright (C) 2005-2010 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version three of the GNU Affero General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Affero General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
29 * Bacula red-black binary tree routines.
31 * rblist is a binary tree with the links being in the data item.
33 * Developped in part from ideas obtained from several online University
36 * Kern Sibbald, November MMV
42 /* ===================================================================
47 * Insert an item in the tree, but only if it is unique
48 * otherwise, the item is returned non inserted
49 * The big trick is keeping the tree balanced after the
50 * insert. We use a parent pointer to make it simpler and
53 * Returns: item if item inserted
54 * other_item if same value already exists (item not inserted)
56 void *rblist::insert(void *item, int compare(void *item1, void *item2))
59 void *last = NULL; /* last leaf if not found */
67 comp = compare(item, x);
70 } else if (comp > 0) {
77 if (found) { /* found? */
78 return found; /* yes, return item found */
81 set_right(item, NULL);
82 set_parent(item, NULL);
84 /* Handle empty tree */
91 /* Not found, so insert it on appropriate side of tree */
95 set_right(last, item);
98 set_parent(item, last);
101 /* Now we must walk up the tree balancing it */
103 while (x != head && red(parent(x))) {
104 if (parent(x) == left(parent(parent(x)))) {
105 /* Look at the right side of our grandparent */
106 y = right(parent(parent(x)));
108 /* our parent must be black */
109 set_red(parent(x), false);
111 set_red(parent(parent(x)), true);
112 x = parent(parent(x)); /* move up to grandpa */
114 if (x == right(parent(x))) { /* right side of parent? */
118 /* make parent black too */
119 set_red(parent(x), false);
120 set_red(parent(parent(x)), true);
121 right_rotate(parent(parent(x)));
124 /* Look at left side of our grandparent */
125 y = left(parent(parent(x)));
127 set_red(parent(x), false);
129 set_red(parent(parent(x)), true);
130 x = parent(parent(x)); /* move up to grandpa */
132 if (x == left(parent(x))) {
136 /* make parent black too */
137 set_red(parent(x), false);
138 set_red(parent(parent(x)), true);
139 left_rotate(parent(parent(x)));
143 /* Make sure the head is always black */
144 set_red(head, false);
151 void *rblist::search(void *item, int compare(void *item1, void *item2))
159 comp = compare(item, x);
162 } else if (comp > 0) {
173 * Get first item (i.e. lowest value)
175 void *rblist::first(void)
193 * This is a non-recursive btree walk routine that returns
194 * the items one at a time in order. I've never seen a
195 * non-recursive tree walk routine published that returns
196 * one item at a time rather than doing a callback.
198 * Return the next item in sorted order. We assume first()
199 * was called once before calling this routine.
200 * We always go down as far as we can to the left, then up, and
201 * down one to the right, and again down as far as we can to the
204 * Returns: pointer to next larger item
205 * NULL when no more items in tree
207 void *rblist::next(void *item)
216 if ((down && !left(x) && right(x)) || (!down && right(x))) {
217 /* Move down to right one */
220 /* Then all the way down left */
227 /* We have gone down all we can, so now go up */
229 /* If at head, we are done */
233 /* Move up in tree */
235 /* if coming from right, continue up */
236 if (right(parent(x)) == x) {
240 /* Coming from left, go up one -- ie. return parent */
246 * Similer to next(), but visits all right nodes when
247 * coming up the tree.
249 void *rblist::any(void *item)
254 if ((down && !left(x) && right(x)) || (!down && right(x))) {
255 /* Move down to right one */
258 /* Then all the way down left */
265 /* We have gone down all we can, so now go up */
267 /* If at head, we are done */
272 /* Go up one and return parent */
278 /* x is item, y is below and to right, then rotated to below left */
279 void rblist::left_rotate(void *item)
286 set_right(x, left(y));
288 set_parent(left(y), x);
290 set_parent(y, parent(x));
291 /* if no parent then we have a new head */
294 } else if (x == left(parent(x))) {
295 set_left(parent(x), y);
297 set_right(parent(x), y);
303 void rblist::right_rotate(void *item)
309 set_left(y, right(x));
311 set_parent(right(x), y);
313 set_parent(x, parent(y));
314 /* if no parent then we have a new head */
317 } else if (y == left(parent(y))) {
318 set_left(parent(y), x);
320 set_right(parent(y), x);
327 void rblist::remove(void *item)
331 /* Destroy the tree contents. Not totally working */
332 void rblist::destroy()
337 // printf("head=%p first=%p left=%p right=%p\n", head, x, left(x), right(x));
339 for ( ; (y=any(x)); ) {
340 /* Prune the last item */
342 if (x == left(parent(x))) {
343 set_left(parent(x), NULL);
344 } else if (x == right(parent(x))) {
345 set_right(parent(x), NULL);
348 if (!left(x) && !right(x)) {
352 // if (num_items<30) {
353 // printf("free nitems=%d item=%p left=%p right=%p\n", num_items, x, left(x), right(x));
355 free((void *)x); /* free previous node */
358 x = y; /* save last node */
364 // printf("free nitems=%d item=%p left=%p right=%p\n", num_items, x, left(x), right(x));
369 // printf("Free head\n");
372 // printf("free nitems=%d\n", num_items);
386 static int my_compare(void *item1, void *item2)
390 jcr1 = (MYJCR *)item1;
391 jcr2 = (MYJCR *)item2;
392 comp = strcmp(jcr1->buf, jcr2->buf);
393 //Dmsg3(000, "compare=%d: %s to %s\n", comp, jcr1->buf, jcr2->buf);
405 /* Now do a binary insert for the tree */
406 jcr_chain = New(rblist());
408 printf("append %d items\n", CNT*CNT*CNT);
411 for (int i=0; i<CNT; i++) {
412 for (int j=0; j<CNT; j++) {
413 for (int k=0; k<CNT; k++) {
415 if ((count & 0x3FF) == 0) {
416 Dmsg1(000, "At %d\n", count);
418 jcr = (MYJCR *)malloc(sizeof(MYJCR));
419 memset(jcr, 0, sizeof(MYJCR));
420 jcr->buf = bstrdup(buf);
421 // printf("buf=%p %s\n", jcr, jcr->buf);
422 jcr1 = (MYJCR *)jcr_chain->insert((void *)jcr, my_compare);
424 Dmsg2(000, "Insert of %s vs %s failed.\n", jcr->buf, jcr1->buf);
434 printf("%d items appended\n", CNT*CNT*CNT);
435 printf("num_items=%d\n", jcr_chain->size());
437 jcr = (MYJCR *)malloc(sizeof(MYJCR));
438 memset(jcr, 0, sizeof(MYJCR));
440 jcr->buf = bstrdup("a");
441 if ((jcr1=(MYJCR *)jcr_chain->search((void *)jcr, my_compare))) {
442 printf("One less failed!!!! Got: %s\n", jcr1->buf);
444 printf("One less: OK\n");
448 jcr->buf = bstrdup("ZZZZZZZZZZZZZZZZ");
449 if ((jcr1=(MYJCR *)jcr_chain->search((void *)jcr, my_compare))) {
450 printf("One greater failed!!!! Got:%s\n", jcr1->buf);
452 printf("One greater: OK\n");
456 jcr->buf = bstrdup("AAA");
457 if ((jcr1=(MYJCR *)jcr_chain->search((void *)jcr, my_compare))) {
458 printf("Search for AAA got %s\n", jcr1->buf);
460 printf("Search for AAA not found\n");
464 jcr->buf = bstrdup("ZZZ");
465 if ((jcr1 = (MYJCR *)jcr_chain->search((void *)jcr, my_compare))) {
466 printf("Search for ZZZ got %s\n", jcr1->buf);
468 printf("Search for ZZZ not found\n");
474 printf("Find each of %d items in tree.\n", count);
475 for (jcr=(MYJCR *)jcr_chain->first(); jcr; (jcr=(MYJCR *)jcr_chain->next((void *)jcr)) ) {
476 // printf("Got: %s\n", jcr->buf);
477 if (!jcr_chain->search((void *)jcr, my_compare)) {
478 printf("rblist binary_search item not found = %s\n", jcr->buf);
481 printf("Free each of %d items in tree.\n", count);
482 for (jcr=(MYJCR *)jcr_chain->first(); jcr; (jcr=(MYJCR *)jcr_chain->next((void *)jcr)) ) {
483 // printf("Free: %p %s\n", jcr, jcr->buf);
487 printf("num_items=%d\n", jcr_chain->size());
491 sm_dump(true); /* unit test */