2 Bacula® - The Network Backup Solution
4 Copyright (C) 2005-2011 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)
258 if ((down && !left(x) && right(x)) || (!down && right(x))) {
259 /* Move down to right one */
262 /* Then all the way down left */
269 /* We have gone down all we can, so now go up */
271 /* If at head, we are done */
276 /* Go up one and return parent */
282 /* x is item, y is below and to right, then rotated to below left */
283 void rblist::left_rotate(void *item)
290 set_right(x, left(y));
292 set_parent(left(y), x);
294 set_parent(y, parent(x));
295 /* if no parent then we have a new head */
298 } else if (x == left(parent(x))) {
299 set_left(parent(x), y);
301 set_right(parent(x), y);
307 void rblist::right_rotate(void *item)
313 set_left(y, right(x));
315 set_parent(right(x), y);
317 set_parent(x, parent(y));
318 /* if no parent then we have a new head */
321 } else if (y == left(parent(y))) {
322 set_left(parent(y), x);
324 set_right(parent(y), x);
331 void rblist::remove(void *item)
335 /* Destroy the tree contents. Not totally working */
336 void rblist::destroy()
341 // printf("head=%p first=%p left=%p right=%p\n", head, x, left(x), right(x));
342 for ( ; (y=any(x)); ) {
343 /* Prune the last item */
345 if (x == left(parent(x))) {
346 set_left(parent(x), NULL);
347 } else if (x == right(parent(x))) {
348 set_right(parent(x), NULL);
351 if (!left(x) && !right(x)) {
355 // if (num_items<30) {
356 // printf("free nitems=%d item=%p left=%p right=%p\n", num_items, x, left(x), right(x));
358 free((void *)x); /* free previous node */
361 x = y; /* save last node */
367 // printf("free nitems=%d item=%p left=%p right=%p\n", num_items, x, left(x), right(x));
372 // printf("Free head\n");
375 // printf("free nitems=%d\n", num_items);
389 static int my_compare(void *item1, void *item2)
393 jcr1 = (MYJCR *)item1;
394 jcr2 = (MYJCR *)item2;
395 comp = strcmp(jcr1->buf, jcr2->buf);
396 //Dmsg3(000, "compare=%d: %s to %s\n", comp, jcr1->buf, jcr2->buf);
408 /* Now do a binary insert for the tree */
409 jcr_chain = New(rblist());
411 printf("append %d items\n", CNT*CNT*CNT);
414 for (int i=0; i<CNT; i++) {
415 for (int j=0; j<CNT; j++) {
416 for (int k=0; k<CNT; k++) {
418 if ((count & 0x3FF) == 0) {
419 Dmsg1(000, "At %d\n", count);
421 jcr = (MYJCR *)malloc(sizeof(MYJCR));
422 memset(jcr, 0, sizeof(MYJCR));
423 jcr->buf = bstrdup(buf);
424 // printf("buf=%p %s\n", jcr, jcr->buf);
425 jcr1 = (MYJCR *)jcr_chain->insert((void *)jcr, my_compare);
427 Dmsg2(000, "Insert of %s vs %s failed.\n", jcr->buf, jcr1->buf);
437 printf("%d items appended\n", CNT*CNT*CNT);
438 printf("num_items=%d\n", jcr_chain->size());
440 jcr = (MYJCR *)malloc(sizeof(MYJCR));
441 memset(jcr, 0, sizeof(MYJCR));
443 jcr->buf = bstrdup("a");
444 if ((jcr1=(MYJCR *)jcr_chain->search((void *)jcr, my_compare))) {
445 printf("One less failed!!!! Got: %s\n", jcr1->buf);
447 printf("One less: OK\n");
451 jcr->buf = bstrdup("ZZZZZZZZZZZZZZZZ");
452 if ((jcr1=(MYJCR *)jcr_chain->search((void *)jcr, my_compare))) {
453 printf("One greater failed!!!! Got:%s\n", jcr1->buf);
455 printf("One greater: OK\n");
459 jcr->buf = bstrdup("AAA");
460 if ((jcr1=(MYJCR *)jcr_chain->search((void *)jcr, my_compare))) {
461 printf("Search for AAA got %s\n", jcr1->buf);
463 printf("Search for AAA not found\n");
467 jcr->buf = bstrdup("ZZZ");
468 if ((jcr1 = (MYJCR *)jcr_chain->search((void *)jcr, my_compare))) {
469 printf("Search for ZZZ got %s\n", jcr1->buf);
471 printf("Search for ZZZ not found\n");
477 printf("Find each of %d items in tree.\n", count);
478 for (jcr=(MYJCR *)jcr_chain->first(); jcr; (jcr=(MYJCR *)jcr_chain->next((void *)jcr)) ) {
479 // printf("Got: %s\n", jcr->buf);
480 if (!jcr_chain->search((void *)jcr, my_compare)) {
481 printf("rblist binary_search item not found = %s\n", jcr->buf);
484 printf("Free each of %d items in tree.\n", count);
485 for (jcr=(MYJCR *)jcr_chain->first(); jcr; (jcr=(MYJCR *)jcr_chain->next((void *)jcr)) ) {
486 // printf("Free: %p %s\n", jcr, jcr->buf);
490 printf("num_items=%d\n", jcr_chain->size());
494 sm_dump(true); /* unit test */