COVISE Core
DLinkList.h
Go to the documentation of this file.
1/* This file is part of COVISE.
2
3 You can use it under the terms of the GNU Lesser General Public License
4 version 2.1 or later, see lgpl-2.1.txt.
5
6 * License: LGPL 2+ */
7
8#ifndef VR_DLINK_LIST_H
9#define VR_DLINK_LIST_H
10
11// **************************************************************************
12//
13// (C) 1996
14// Computer Centre University of Stuttgart
15// Allmandring 30
16// D-70550 Stuttgart
17// Germany
18//
19//
20//
21// COVISE Basic VR Environment Library
22//
23//
24//
25// Author: D.Rantzau
26// Date : 04.05.96
27// Last :
28// **************************************************************************
29
30#ifndef NULL
31#define NULL 0L
32#endif
33
34namespace covise
35{
36
37//==========================================================================
38//
39//==========================================================================
40// struct needed to link items together
41template <class T>
42struct DLink
43{
47 DLink(const T &a)
48 : item(a){};
50 {
51 delete item;
52 }
53};
54
55//==========================================================================
56//
57//==========================================================================
58template <class T>
60{
61
62private:
63 // the list, and number of items in list
66
67public:
69 // constructor and destructor
70 DLinkList(void);
71 virtual ~DLinkList(void);
72
73 //
74 // query status of this class
75 //
76
77 // number of items in this list
78 int num(void)
79 {
80 return listItems;
81 }
82
83 // return TRUE if there is a current item
84 int is_current(void)
85 {
86 return (curr != ((DLink<T> *)0));
87 }
88
89 //
90 // routines for adding or removing items from the list
91 //
92
93 // add new item to end of list
94 DLinkList<T> &append(const T &);
95
96 // append new item after the current item
97 DLinkList<T> &insert_after(const T &);
98
99 // append new item before the current item
100 DLinkList<T> &insert_before(const T &);
101
102 // remove current item from list
103 DLinkList<T> &remove(void);
104
105 // remove last item from list
107
108 //
109 // routines to access a particular item in the list
110 //
111
112 // just return the current item, do not change which item is current
113 T current(void);
114
115 // return the Nth item; do not change current item position
116 T item(int);
117
118 // return the current item, and move the current item on to the next one
119 T get(void)
120 {
121 T retval = current();
122 next();
123 return retval;
124 }
125
126 //
127 // iterator routines:
128 //
129
130 // move the current item pointer to the Nth item
131 DLinkList<T> &set(int);
132
133 // move the current item pointer to the item which matches the given one
134 // return TRUE if found, or FALSE otherwise
135 int find(const T &);
136
137 // reset the current item pointer to the beginning of the list
139 {
140 if (head)
141 curr = head;
142 return *this;
143 }
144
145 // move the current item pointer to the next item in list
147 {
148 if (curr)
149 curr = curr->next;
150 return *this;
151 }
152
153 // move the current item pointer to the previous item in list
155 {
156 if (curr)
157 curr = curr->prev;
158 return *this;
159 }
160
161 // clear the current item pointer; make it null
163 {
164 curr = (DLink<T> *)0;
165 return *this;
166 }
167};
168
169//==========================================================================
170//
171//==========================================================================
172template <class T>
174{
175
176 head = tail = curr = NULL;
177 listItems = 0;
178 noDelete = 0;
179}
180
181//==========================================================================
182//
183//==========================================================================
184template <class T>
186{
187 if (noDelete)
188 {
189 head = tail = curr = NULL;
190 listItems = 0;
191 }
192 else
193 {
194 reset();
195 while (curr)
196 remove();
197 }
198}
199
200//==========================================================================
201// add new item to end of list
202//==========================================================================
203template <class T>
205{
206 DLink<T> *newlink = new DLink<T>(a);
207 newlink->next = NULL;
208 newlink->prev = tail;
209 tail = newlink;
210 if (newlink->prev)
211 (newlink->prev)->next = newlink;
212 else
213 head = curr = newlink;
214 listItems++;
215 return *this;
216}
217
218//==========================================================================
219// append new item after the current item
220//==========================================================================
221template <class T>
223{
224 if (curr) // yes, there is a current item
225 {
226 DLink<T> *newlink = new DLink<T>(a);
227 newlink->next = curr->next;
228 newlink->prev = curr;
229 curr->next = newlink;
230 if (newlink->next == NULL)
231 tail = newlink;
232 else
233 (newlink->next)->prev = newlink;
234 listItems++;
235 }
236 else // no current item; just append at end
237 append(a);
238 return *this;
239}
240
241//==========================================================================
242// append new item before the current item
243//==========================================================================
244template <class T>
246{
247 if (curr) // yes, there is a current item
248 {
249 DLink<T> *newlink = new DLink<T>(a);
250 newlink->next = curr;
251 newlink->prev = curr->prev;
252 curr->prev = newlink;
253 if (newlink->prev == NULL)
254 head = newlink;
255 else
256 (newlink->prev)->next = newlink;
257 listItems++;
258 }
259 else // no current item; just append at end
260 append(a);
261 return *this;
262}
263
264//==========================================================================
265// remove current item from list
266//==========================================================================
267template <class T>
269{
270 DLink<T> *oldlink = tail;
271 if (oldlink)
272 {
273 if (oldlink->prev)
274 (oldlink->prev)->next = oldlink->next;
275 if (oldlink->next)
276 (oldlink->next)->prev = oldlink->prev;
277 if (head == oldlink)
278 head = oldlink->next;
279 curr = tail = oldlink->prev;
280 if (!noDelete)
281 delete oldlink;
282 listItems--;
283 }
284 return *this;
285}
286//==========================================================================
287// remove current item from list
288//==========================================================================
289template <class T>
291{
292 DLink<T> *oldlink = curr;
293 if (oldlink)
294 {
295 if (oldlink->prev)
296 (oldlink->prev)->next = oldlink->next;
297 if (oldlink->next)
298 (oldlink->next)->prev = oldlink->prev;
299 if (head == oldlink)
300 head = oldlink->next;
301 if (tail == oldlink)
302 tail = oldlink->prev;
303 curr = oldlink->next;
304 if (!noDelete)
305 delete oldlink;
306 listItems--;
307 }
308 return *this;
309}
310
311//==========================================================================
312// return the Nth item; do not change current item position
313//==========================================================================
314template <class T>
316{
317 DLink<T> *link = head;
318 while (link && n > 0)
319 {
320 link = link->next;
321 n--;
322 }
323 if (link && n == 0)
324 return link->item;
325 else
326 return head->item; // problem if NO items in list
327}
328
329//==========================================================================
330// just return the current item, do not change which item is current
331//==========================================================================
332template <class T>
334{
335 DLink<T> *link = curr;
336 if (!link)
337 {
338 reset();
339 return (NULL);
340 }
341 return link->item;
342}
343
344//==========================================================================
345// move the current item pointer to the Nth item
346//==========================================================================
347template <class T>
349{
350 reset();
351 for (int i = 0; i < N; i++)
352 next();
353 return *this;
354}
355
356//==========================================================================
357// move the current item pointer to the item which matches the given one
358// return TRUE if found, or FALSE otherwise
359//==========================================================================
360template <class T>
362{
363 reset();
364 while (curr && curr->item != a)
365 next();
366 return (curr != NULL);
367}
368}
369#endif
#define NULL
Definition: DLinkList.h:31
GLdouble n
Definition: khronos-glext.h:8447
GLboolean reset
Definition: khronos-glext.h:6369
GLboolean GLboolean GLboolean GLboolean a
Definition: khronos-glext.h:6895
list of all chemical elements
Definition: coConfig.h:27
Definition: DLinkList.h:43
T item
Definition: DLinkList.h:44
DLink< T > * prev
Definition: DLinkList.h:45
DLink< T > * next
Definition: DLinkList.h:46
DLink(const T &a)
Definition: DLinkList.h:47
~DLink()
Definition: DLinkList.h:49
Definition: DLinkList.h:60
int num(void)
Definition: DLinkList.h:78
DLinkList< T > & reset(void)
Definition: DLinkList.h:138
DLink< T > * curr
Definition: DLinkList.h:64
int listItems
Definition: DLinkList.h:65
DLinkList< T > & insert_after(const T &)
Definition: DLinkList.h:222
DLinkList< T > & insert_before(const T &)
Definition: DLinkList.h:245
DLinkList< T > & prev(void)
Definition: DLinkList.h:154
DLinkList< T > & next(void)
Definition: DLinkList.h:146
DLinkList< T > & set(int)
Definition: DLinkList.h:348
T get(void)
Definition: DLinkList.h:119
DLinkList< T > & clear(void)
Definition: DLinkList.h:162
int noDelete
Definition: DLinkList.h:68
DLinkList< T > & append(const T &)
Definition: DLinkList.h:204
DLinkList< T > & remove(void)
Definition: DLinkList.h:290
DLink< T > * tail
Definition: DLinkList.h:64
T current(void)
Definition: DLinkList.h:333
DLinkList(void)
Definition: DLinkList.h:173
virtual ~DLinkList(void)
Definition: DLinkList.h:185
int is_current(void)
Definition: DLinkList.h:84
DLink< T > * head
Definition: DLinkList.h:64
int find(const T &)
Definition: DLinkList.h:361
T item(int)
Definition: DLinkList.h:315
DLinkList< T > & removeLast(void)
Definition: DLinkList.h:268