COVISE Core
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
covise_list.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 EC_LIST_H
9 #define EC_LIST_H
10 //#include <stream.h>
11 
12 /*
13  $Log: covise_list.h,v $
14  * Revision 1.2 1993/10/08 19:21:13 zrhk0125
15  * destructor now deletes all members
16  *
17  * Revision 1.1 93/09/25 20:45:40 zrhk0125
18  * Initial revision
19  *
20 */
21 #ifndef NULL
22 #define NULL 0L
23 #endif
24 /***********************************************************************\
25  ** **
26  ** List class Version: 1.01 **
27  ** **
28  ** **
29  ** Description : A templated class to handle lists. **
30  ** ListElement is an element of this list **
31  ** **
32  ** Classes : List, ListElement **
33  ** **
34  ** Copyright (C) 1993 by University of Stuttgart **
35  ** Computer Center (RUS) **
36  ** Allmandring 30 **
37  ** 7000 Stuttgart 80 **
38  ** **
39  ** **
40  ** Author : A. Wierse (RUS) **
41  ** **
42  ** History : **
43  ** 15.04.93 Ver 1.0 **
44  ** 26.05.93 Ver 1.01 current() added **
45  ** **
46  ** **
47  ** **
48 \***********************************************************************/
49 namespace covise
50 {
51 
52 template <class T>
53 class List;
54 
55 template <class T>
56 class ListElement // element of the list
57 {
58  friend class List<T>;
59 
60 private:
61  T *data; // pointer to data
62  ListElement<T> *next; // pointer to next list element
63  ListElement() // initialize with 0
64  {
65  data = NULL;
66  next = NULL;
67  };
68  ListElement(T *d) // initialize with data
69  {
70  data = d;
71  next = NULL;
72  };
74 };
75 
76 template <class T>
77 class List // list class
78 {
79 private:
80  ListElement<T> *list; // root of the list
81  ListElement<T> *last; // last element in list
82  ListElement<T> *iter; // iterator for next() requests
83  int mElemCount; // Counter for number fo elements added
84 
85 public:
86  List()
87  {
88  list = last = iter = NULL;
89  mElemCount = 0;
90  };
91  ~List();
92  void add(T *d); // add new element
93  void remove(T *d); // remove element
94 #if defined(__hpux) || defined(_SX)
95  T *next();
96 #else
97  T *next()
98  {
99  if (iter == NULL)
100  return NULL;
101  ListElement<T> *tmpptr;
102  tmpptr = iter;
103  iter = iter->next;
104  return tmpptr->data;
105  }; // get next element (increments iter)
106 #endif
107 
108  T *current()
109  {
110  if (iter != NULL)
111  return iter->data;
112  else
113  return 0;
114  }; // get current element (keeps iter)
115 
116  void reset()
117  { // reset iter to begin of the list
118  iter = list;
119  };
120 
121  T *get_last() // get last element in list
122  {
123  T *res;
124  if (last)
125  res = last->data;
126  else
127  res = NULL;
128  return res;
129  };
130 
131  T *get_first() // get first element in list
132  {
133  T *res;
134  if (list)
135  res = list->data;
136  else
137  res = NULL;
138  return res;
139  };
140  void clear();
141  void print();
142  T *at(int i)
143  {
144  if (list == NULL)
145  {
146  return NULL;
147  }
148 
149  ListElement<T> *ptr = list;
150  ListElement<T> *tmp = NULL;
151 
152  while (ptr->next != NULL && i > 0)
153  {
154  i--;
155  tmp = ptr->next;
156  ptr->next = tmp->next;
157  }
158 
159  return ptr->data;
160  };
161  int count()
162  {
163  return mElemCount;
164  };
165 };
166 
167 #if defined(__hpux) || defined(_SX)
168 template <class T>
169 inline T *List<T>::next()
170 {
171  if (iter == NULL)
172  return NULL;
173 
174  ListElement<T> *tmpptr;
175  tmpptr = iter;
176  iter = iter->next;
177  return tmpptr->data;
178 }
179 #endif
180 
181 template <class T>
182 inline void List<T>::add(T *d)
183 {
184  if (list == NULL)
185  {
186  list = new ListElement<T>(d);
187  last = list;
188  mElemCount++;
189  return;
190  }
191 
192  if (list->data == NULL)
193  {
194  list->data = d;
195  last = list;
196  mElemCount++;
197  return;
198  }
199 
200  ListElement<T> *ptr = new ListElement<T>(d);
201  ListElement<T> *tmpptr = list;
202 
203  while (tmpptr->next != NULL)
204  tmpptr = tmpptr->next;
205 
206  tmpptr->next = ptr;
207  last = ptr;
208  mElemCount++;
209 
210  return;
211 }
212 
213 template <class T>
214 inline void List<T>::remove(T *d)
215 {
216  if (list == NULL)
217  return;
218 
219  if (list->data == d)
220  {
221  ListElement<T> *tmp = list;
222  list = list->next;
223  delete tmp;
224  if (list == NULL)
225  last = NULL;
226  mElemCount--;
227  return;
228  }
229 
230  ListElement<T> *ptr = list;
231 
232  while (ptr->next != NULL)
233  {
234  if (ptr->next->data == d)
235  {
236  if (ptr->next == last)
237  last = ptr;
238  if (ptr->next == iter)
239  iter = ptr;
240  ListElement<T> *tmp = ptr->next;
241  ptr->next = tmp->next;
242  delete tmp;
243  if (list == NULL)
244  last = NULL;
245  mElemCount--;
246  return;
247  }
248  ptr = ptr->next;
249  }
250  if (list == NULL)
251  last = NULL;
252  return;
253 }
254 
255 template <class T>
256 inline void List<T>::clear()
257 {
258  ListElement<T> *tmp;
259  ListElement<T> *ptr;
260  ptr = list;
261  while (ptr)
262  {
263  tmp = ptr->next;
264  delete ptr;
265  ptr = tmp;
266  }
267  list = NULL;
268  last = NULL;
269  iter = NULL;
270  mElemCount = 0;
271  return;
272 }
273 
274 template <class T>
275 inline void List<T>::print()
276 {
277  ListElement<T> *ptr = list;
278  ptr->data->print();
279  while ((ptr = ptr->next) != NULL)
280  ptr->data->print();
281 }
282 
283 template <class T>
285 {
286  ListElement<T> *tmp;
287  while (list && list->next)
288  {
289  tmp = list->next;
290  delete list;
291  list = tmp;
292  mElemCount = 0;
293  }
294 }
295 }
296 #endif
T * next()
Definition: covise_list.h:97
ListElement()
Definition: covise_list.h:63
ListElement< T > * iter
Definition: covise_list.h:82
T * at(int i)
Definition: covise_list.h:142
ListElement< T > * next
Definition: covise_list.h:62
int count()
Definition: covise_list.h:161
GLuint res
Definition: khronos-glext.h:10588
T * get_first()
Definition: covise_list.h:131
#define NULL
Definition: covise_list.h:22
T * data
Definition: covise_list.h:61
ListElement(T *d)
Definition: covise_list.h:68
List()
Definition: covise_list.h:86
void add(T *d)
Definition: covise_list.h:182
Definition: covise_list.h:53
T * current()
Definition: covise_list.h:108
void print()
Definition: covise_list.h:275
void clear()
Definition: covise_list.h:256
int mElemCount
Definition: covise_list.h:83
void remove(T *d)
Definition: covise_list.h:214
T * get_last()
Definition: covise_list.h:121
~ListElement()
Definition: covise_list.h:73
~List()
Definition: covise_list.h:284
void reset()
Definition: covise_list.h:116
Definition: covise_list.h:56
ListElement< T > * list
Definition: covise_list.h:80
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: khronos-glext.h:6354
ListElement< T > * last
Definition: covise_list.h:81