]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/dlist.h
!!! I didn't run the regression tests.!!!
[bacula/bacula] / bacula / src / lib / dlist.h
1 /*
2  *   Version $Id$
3  */
4
5 /*
6    Copyright (C) 2000-2004 Kern Sibbald and John Walker
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2 of
11    the License, or (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but 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 General Public
19    License along with this program; if not, write to the Free
20    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21    MA 02111-1307, USA.
22
23  */
24
25 /* ========================================================================
26  * 
27  *   Doubly linked list  -- dlist
28  *
29  */
30
31 #define M_ABORT 1
32
33 /* In case you want to specifically specify the offset to the link */
34 #define OFFSET(item, link) ((char *)(link) - (char *)(item))
35 /* 
36  * There is a lot of extra casting here to work around the fact
37  * that some compilers (Sun and Visual C++) do not accept
38  * (void *) as an lvalue on the left side of an equal.
39  *
40  * Loop var through each member of list
41  */
42 #define foreach_dlist(var, list) \
43     for((var)=NULL; (*((void **)&(var))=(void*)((list)->next(var))); )
44
45 #ifdef the_old_way
46 #define foreach_dlist(var, list) \
47         for((var)=NULL; (((void *)(var))=(list)->next(var)); )
48 #endif
49
50
51 struct dlink {
52    void *next;
53    void *prev;
54 };
55
56 class dlist : public SMARTALLOC {
57    void *head;
58    void *tail;
59    int16_t loffset;
60    uint32_t num_items;
61 public:
62    dlist(void *item, dlink *link);
63    dlist(void);
64    ~dlist() { destroy(); }
65    void init(void *item, dlink *link);
66    void prepend(void *item);
67    void append(void *item);
68    void insert_before(void *item, void *where);
69    void insert_after(void *item, void *where);
70    void *unique_binary_insert(void *item, int compare(void *item1, void *item2));
71    void binary_insert(void *item, int compare(void *item1, void *item2));
72    void remove(void *item);
73    bool empty() const;
74    int  size() const;
75    void *next(const void *item) const;
76    void *prev(const void *item) const;
77    void destroy();
78    void *first() const;
79    void *last() const;
80 };
81
82
83 /*                            
84  * This allows us to do explicit initialization,
85  *   allowing us to mix C++ classes inside malloc'ed
86  *   C structures. Define before called in constructor.
87  */
88 inline void dlist::init(void *item, dlink *link) 
89 {
90    head = tail = NULL;
91    loffset = (char *)link - (char *)item;
92    if (loffset < 0 || loffset > 5000) {
93       Emsg0(M_ABORT, 0, "Improper dlist initialization.\n");
94    }
95    num_items = 0;
96 }
97
98 /*             
99  * Constructor called with the address of a 
100  *   member of the list (not the list head), and
101  *   the address of the link within that member.
102  * If the link is at the beginning of the list member,
103  *   then there is no need to specify the link address 
104  *   since the offset is zero.
105  */
106 inline dlist::dlist(void *item, dlink *link)
107 {
108    init(item, link);
109 }
110
111 /* Constructor with link at head of item */
112 inline dlist::dlist(void) : head(0), tail(0), loffset(0), num_items(0)
113 {
114 }
115
116 inline bool dlist::empty() const
117 {
118    return head == NULL;
119 }
120
121 inline int dlist::size() const
122 {
123    return num_items;
124 }
125
126    
127
128 inline void * dlist::first() const
129 {
130    return head;
131 }
132
133 inline void * dlist::last() const
134 {
135    return tail;
136 }