2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2015 Kern Sibbald
5 Copyright (C) 2005-2014 Free Software Foundation Europe e.V.
7 The original author of Bacula is Kern Sibbald, with contributions
8 from many others, a complete list can be found in the file AUTHORS.
10 You may use this file and others of this release according to the
11 license defined in the LICENSE file, which includes the Affero General
12 Public License, v3.0 ("AGPLv3") and some additional permissions and
13 terms pursuant to its AGPLv3 Section 7.
15 This notice must be preserved when any source code is
16 conveyed and/or propagated.
18 Bacula(R) is a registered trademark of Kern Sibbald.
20 /* ========================================================================
22 * red-black binary tree routines -- rblist.h
31 * There is a lot of extra casting here to work around the fact
32 * that some compilers (Sun and Visual C++) do not accept
33 * (bnode *) as an lvalue on the left side of an equal.
35 * Loop var through each member of list
38 #define foreach_rblist(var, tree) \
39 for (((var)=(typeof(var))(tree)->first()); (var); ((var)=(typeof(var))(tree)->next(var)))
41 #define foreach_rblist(var, tree) \
42 for ((*((void **)&(var))=(void*)((tree)->first())); (var); (*((void **)&(var))=(void*)((tree)->next(var))))
52 class rblist : public SMARTALLOC {
57 void left_rotate(void *item);
58 void right_rotate(void *item);
60 rblist(void *item, rblink *link);
62 ~rblist(void) { destroy(); }
63 void init(void *item, rblink *link);
64 void set_parent(void *item, void *parent);
65 void set_left(void *item, void *left);
66 void set_right(void *item, void *right);
67 void set_red(void *item, bool red);
68 void *parent(const void *item) const;
69 void *left(const void *item) const;
70 void *right(const void *item) const;
71 bool red(const void *item) const;
72 void *insert(void *item, int compare(void *item1, void *item2));
73 void *search(void *item, int compare(void *item1, void *item2));
75 void *next(void *item);
76 void *any(void *item);
77 void remove(void *item);
78 bool empty(void) const;
85 * This allows us to do explicit initialization,
86 * allowing us to mix C++ classes inside malloc'ed
87 * C structures. Define before called in constructor.
89 inline void rblist::init(void *item, rblink *link)
92 loffset = (int)((char *)link - (char *)item);
93 if (loffset < 0 || loffset > 5000) {
94 Emsg0(M_ABORT, 0, "Improper rblist initialization.\n");
99 inline rblist::rblist(void *item, rblink *link)
104 /* Constructor with link at head of item */
105 inline rblist::rblist(void): head(0), loffset(0), num_items(0)
109 inline void rblist::set_parent(void *item, void *parent)
111 ((rblink *)(((char *)item)+loffset))->parent = parent;
114 inline void rblist::set_left(void *item, void *left)
116 ((rblink *)(((char *)item)+loffset))->left = left;
119 inline void rblist::set_right(void *item, void *right)
121 ((rblink *)(((char *)item)+loffset))->right = right;
124 inline void rblist::set_red(void *item, bool red)
126 ((rblink *)(((char *)item)+loffset))->red = red;
129 inline bool rblist::empty(void) const
134 inline int rblist::size() const
139 inline void *rblist::parent(const void *item) const
141 return ((rblink *)(((char *)item)+loffset))->parent;
144 inline void *rblist::left(const void *item) const
146 return ((rblink *)(((char *)item)+loffset))->left;
149 inline void *rblist::right(const void *item) const
151 return ((rblink *)(((char *)item)+loffset))->right;
154 inline bool rblist::red(const void *item) const
156 return ((rblink *)(((char *)item)+loffset))->red;