]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/rblist.h
Real fix of bug #1897
[bacula/bacula] / bacula / src / lib / rblist.h
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2005-2007 Free Software Foundation Europe e.V.
5
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
11    in the file LICENSE.
12
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.
17
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
21    02110-1301, USA.
22
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.
27 */
28
29 /*
30  *   Version $Id$
31  */
32
33 /* ========================================================================
34  *
35  *   red-black binary tree routines -- rblist.h
36  *
37  *    Kern Sibbald, MMV
38  *
39  */
40
41 #define M_ABORT 1
42
43 /*
44  * There is a lot of extra casting here to work around the fact
45  * that some compilers (Sun and Visual C++) do not accept
46  * (bnode *) as an lvalue on the left side of an equal.
47  *
48  * Loop var through each member of list
49  */
50 #ifdef HAVE_TYPEOF
51 #define foreach_rblist(var, tree) \
52    for (((var)=(typeof(var))(tree)->first()); (var); ((var)=(typeof(var))(tree)->next(var)))
53 #else
54 #define foreach_rblist(var, tree) \
55    for ((*((void **)&(var))=(void*)((tree)->first())); (var); (*((void **)&(var))=(void*)((tree)->next(var))))
56 #endif
57
58 struct rblink {
59    void *parent;
60    void *left;
61    void *right;
62    bool red;
63 };
64
65 class rblist : public SMARTALLOC {
66    void *head;
67    int16_t loffset;
68    uint32_t num_items;
69    bool down;
70    void left_rotate(void *item);
71    void right_rotate(void *item);
72 public:
73    rblist(void *item, rblink *link);
74    rblist(void);
75    ~rblist(void) { destroy(); }
76    void init(void *item, rblink *link);
77    void set_parent(void *item, void *parent);
78    void set_left(void *item, void *left);
79    void set_right(void *item, void *right);
80    void set_red(void *item, bool red);
81    void *parent(const void *item) const;
82    void *left(const void *item) const;
83    void *right(const void *item) const;
84    bool red(const void *item) const;
85    void *insert(void *item, int compare(void *item1, void *item2));
86    void *search(void *item, int compare(void *item1, void *item2));
87    void *first(void);
88    void *next(void *item);
89    void *any(void *item);
90    void remove(void *item);
91    bool empty(void) const;
92    int size(void) const;
93    void destroy(void);
94 };
95
96
97 /*
98  * This allows us to do explicit initialization,
99  *   allowing us to mix C++ classes inside malloc'ed
100  *   C structures. Define before called in constructor.
101  */
102 inline void rblist::init(void *item, rblink *link)
103 {
104    head = NULL;
105    loffset = (int)((char *)link - (char *)item);
106    if (loffset < 0 || loffset > 5000) {
107       Emsg0(M_ABORT, 0, "Improper rblist initialization.\n");
108    }
109    num_items = 0;
110 }
111
112 inline rblist::rblist(void *item, rblink *link)
113 {
114    init(item, link);
115 }
116
117 /* Constructor with link at head of item */
118 inline rblist::rblist(void): head(0), loffset(0), num_items(0)
119 {
120 }
121
122 inline void rblist::set_parent(void *item, void *parent)
123 {
124    ((rblink *)(((char *)item)+loffset))->parent = parent;
125 }
126
127 inline void rblist::set_left(void *item, void *left)
128 {
129    ((rblink *)(((char *)item)+loffset))->left = left;
130 }
131
132 inline void rblist::set_right(void *item, void *right)
133 {
134    ((rblink *)(((char *)item)+loffset))->right = right;
135 }
136
137 inline void rblist::set_red(void *item, bool red)
138 {
139    ((rblink *)(((char *)item)+loffset))->red = red;
140 }
141
142 inline bool rblist::empty(void) const
143 {
144    return head == NULL;
145 }
146
147 inline int rblist::size() const
148 {
149    return num_items;
150 }
151
152 inline void *rblist::parent(const void *item) const
153 {
154    return ((rblink *)(((char *)item)+loffset))->parent;
155 }
156
157 inline void *rblist::left(const void *item) const
158 {
159    return ((rblink *)(((char *)item)+loffset))->left;
160 }
161
162 inline void *rblist::right(const void *item) const
163 {
164    return ((rblink *)(((char *)item)+loffset))->right;
165 }
166
167 inline bool rblist::red(const void *item) const
168 {
169    return ((rblink *)(((char *)item)+loffset))->red;
170 }