COVISE Core
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
coList.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 YAC_LIST_H
9 #define YAC_LIST_H
10 
11 #include <string.h>
12 #include <iostream>
13 #include "coExport.h"
14 
15 //template<class T>
16 //class coList;
17 
18 #ifndef INLINE
19 #define INLINE inline
20 #endif
21 
22 //=========================================================================
23 // DEFINITION
24 //=========================================================================
25 
34 namespace covise
35 {
36 
37 template <class T>
38 class coList
39 {
40 
41 public:
43  coList(void);
45  coList(int amount);
47  coList(const coList<T> &v);
49  ~coList(void);
50 
51  // list access
52  // -----------
54  T &operator[](int index)
55  {
56  if (index < 0 || index > (dim - 1))
57  {
58  // cl_error( coWARNING, "coList", "[]", "Index out of bounds!" );
59  return nullValue;
60  }
61  return (data[index]);
62  }
63 
65  const T &operator[](int index) const
66  {
67  if (index < 0 || index > (dim - 1))
68  {
69  // cl_error( coWARNING, "coList", "const []", "Index out of bounds!" );
70  return nullValue;
71  }
72  return (data[index]);
73  }
74 
75  // assignment
76  // ----------
78  coList<T> &operator=(const coList<T> &v);
80  void init(const T &in_data);
82  void clear(void);
84  void set(int index, const T &in_data);
85 
86  // list size manipulation
87  // ----------------------
89  void append(const T &in_data);
91  void append(const T *in_data)
92  {
93  append(*in_data);
94  };
96  void insert(int index, const T &in_data);
98  void replace(int index, const T &in_data);
100  void remove(int index);
103  {
104  if (dim > 0)
105  return (data[--dim]);
106  else
107  return nullValue;
108  };
110  bool removeElement(const T &in_data);
112  void newSize(int nelistw_size);
114  void fillWith(const T &in_data);
116  T *getLast()
117  {
118  return &(data[currentIndex]);
119  };
121  T *first()
122  {
123  if (dim == 0)
124  return NULL;
125  currentIndex = 0;
126  return &(data[currentIndex]);
127  };
129  T *last()
130  {
131  if (dim == 0)
132  return NULL;
133  currentIndex = 0;
134  return &(data[currentIndex]);
135  };
137  T *next()
138  {
139  if (currentIndex > dim - 1)
140  return NULL;
141  currentIndex++;
142  return &(data[currentIndex]);
143  };
145  T *prev()
146  {
147  if (currentIndex <= 0)
148  return NULL;
149  dim--;
150  return &(data[currentIndex]);
151  };
152 
153  // list information
154  // ----------------
156  int find(const T &in_data) const;
158  int entries(void) const
159  {
160  return (dim);
161  };
163  int getSize(void) const
164  {
165  return (dim);
166  };
168  void print(char *name) const;
169 
170 private:
172  void incSize(void);
173  T *data;
174  int dim;
176  int allocinc;
179 };
180 
181 //=========================================================================
182 // IMPLEMENTATION
183 //=========================================================================
184 
185 template <class T>
187 {
188  // create list
189  data = 0L;
190  dim = 0;
191  allocsize = 0;
192  allocinc = 20;
193  nullValue = (T)0;
194  currentIndex = 0;
195 }
196 
197 template <class T>
199 {
200 
201  // create list
202  nullValue = (T)0;
203  dim = 0;
204  allocsize = amount;
205  allocinc = amount;
206  data = new T[allocsize];
207  currentIndex = 0;
208  for (int i = 0; i < allocsize; i++)
209  data[i] = 0L;
210 }
211 
212 template <class T>
214 {
215 
216  // copy list
217  nullValue = (T)0;
218  dim = v.dim;
219  allocsize = v.allocsize;
220  allocinc = v.allocinc;
221  data = new T[allocsize];
222  currentIndex = v.currentIndex;
223  for (int i = 0; i < dim; i++)
224  data[i] = v.data[i];
225 }
226 
227 template <class T>
229 {
230  // free all memory
231  if (data)
232  delete[] data;
233 }
234 
235 template <class T>
236 INLINE void coList<T>::append(const T &in_data)
237 {
238  if (dim == allocsize)
239  incSize();
240 
241  data[dim] = in_data;
242  dim++;
243 }
244 
245 template <class T>
247 {
248  allocsize += allocinc;
249  T *new_data = new T[allocsize];
250  // copy old list
251  for (int i = 0; i < dim; i++)
252  {
253  new_data[i] = data[i];
254  }
255  delete[] data;
256  data = new_data;
257 }
258 
259 template <class T>
260 INLINE void coList<T>::replace(int index, const T &in_data)
261 {
262 
263  if (index < 0 || index > (dim - 1))
264  return;
265 
266  delete &data[index];
267  data[index] = in_data;
268 }
269 
270 template <class T>
271 INLINE void coList<T>::insert(int index, const T &in_data)
272 {
273  int i;
274  if (dim == allocsize)
275  incSize();
276 
277  if (index >= dim || index < 0)
278  {
279  // cl_error( coWARNING, "coList", "insert", "Index out of bounds!" );
280  return;
281  }
282 
283  for (i = dim; i > index; i++)
284  data[i] = data[i - 1];
285 
286  data[index] = in_data;
287 
288  dim++;
289 }
290 
291 template <class T>
293 {
294  int i;
295 
296  if (index < 0 || index > (dim - 1))
297  return;
298  for (i = index; i < dim - 1; i++)
299  data[i] = data[i + 1];
300 
301  dim--;
302  if (currentIndex > dim - 1)
303  currentIndex = 0;
304 }
305 
306 template <class T>
307 INLINE bool coList<T>::removeElement(const T &in_data)
308 {
309 
310  int index = find(in_data);
311  if (index != -1)
312  {
313  remove(index);
314  return true;
315  }
316  else
317  return false;
318 }
319 
320 template <class T>
321 INLINE int coList<T>::find(const T &in_data) const
322 {
323 
324  if (dim == 0)
325  {
326  return (-1);
327  }
328 
329  for (int i = 0; i < dim; i++)
330  {
331  if (data[i] == in_data)
332  return (i);
333  }
334  return (-1);
335 }
336 
337 template <class T>
338 INLINE void coList<T>::newSize(int new_size)
339 {
340  allocsize = new_size;
341  T *new_data = new T[allocsize];
342  // copy old list
343  for (int i = 0; i < dim; i++)
344  {
345  new_data[i] = data[i];
346  }
347  delete[] data;
348  data = new_data;
349 }
350 
351 template <class T>
352 INLINE void coList<T>::fillWith(const T &in_data)
353 {
354 
355  for (int i = 0; i < dim; i++)
356  data[i] = in_data;
357 }
358 
359 template <class T>
361 {
362 
363  delete[] data;
364  data = 0L;
365  dim = 0;
366  allocsize = 0;
367  currentIndex = 0;
368 }
369 
370 template <class T>
372 {
373  if (this != &v)
374  {
375  delete[] data;
376  dim = v.dim;
377  allocsize = dim + allocinc;
378  currentIndex = v.currentIndex;
379  data = new T[allocsize];
380  for (int i = 0; i < dim; i++)
381  data[i] = v.data[i];
382  }
383  return (*this);
384 }
385 
386 template <class T>
387 INLINE void coList<T>::init(const T &in_data)
388 {
389 
390  for (int i = 0; i < dim; i++)
391  data[i] = in_data;
392 }
393 
394 template <class T>
395 INLINE void coList<T>::set(int index, const T &in_data)
396 {
397 
398  if (dim == 0 || index < 0 || index > (dim - 1))
399  {
400  // cl_error( coWARNING, "coList", "set", "Index out of bounds!" );
401  return;
402  }
403  else
404  data[index] = in_data;
405 }
406 
407 template <class T>
408 INLINE void coList<T>::print(char *name) const
409 {
410  int i = 0;
411 
412  std::cout << "*** coList '" << name << "' elements: ***\n";
413 
414  if (dim == 0)
415  {
416  std::cout << "empty list!\n";
417  return;
418  }
419 
420  for (i = 0; i < dim; i++)
421  {
422  std::cout << "[" << i << "]: " << data[i] << std::endl;
423  }
424  std::cout << "*** End of coList '" << name << "' ***\n";
425 }
426 }
427 #undef INLINE
428 
429 #endif // YAC_LIST_H
T nullValue
Definition: coList.h:178
GLuint const GLchar * name
Definition: khronos-glext.h:6722
T * getLast()
fills the whole with in_data
Definition: coList.h:116
void clear(void)
clear the list (frees all data)
Definition: coList.h:360
void insert(int index, const T &in_data)
insert element at the given list index
Definition: coList.h:271
void append(const T *in_data)
append element
Definition: coList.h:91
coList(void)
default constructor, list is empty
Definition: coList.h:186
void remove(int index)
remove element at position index
Definition: coList.h:292
void set(int index, const T &in_data)
set list element
Definition: coList.h:395
#define NULL
Definition: covise_list.h:22
int entries(void) const
returns the number of list enries
Definition: coList.h:158
T * data
Definition: coList.h:173
T & operator[](int index)
direct list access
Definition: coList.h:54
void incSize(void)
increase allocated Size
Definition: coList.h:246
bool removeElement(const T &in_data)
finds first occurence of element &#39;in_data&#39; and delete it
Definition: coList.h:307
T * first()
get first element and set current index to 0
Definition: coList.h:121
void fillWith(const T &in_data)
fills the whole with in_data
Definition: coList.h:352
void replace(int index, const T &in_data)
replace element at the given index
Definition: coList.h:260
const GLdouble * v
Definition: khronos-glext.h:6442
#define INLINE
Definition: coList.h:19
int currentIndex
Definition: coList.h:177
int allocsize
Definition: coList.h:175
T * next()
get next element
Definition: coList.h:137
void append(const T &in_data)
append element
Definition: coList.h:236
T * prev()
get next element
Definition: coList.h:145
int getSize(void) const
returns the number of list enries
Definition: coList.h:163
Definition: coList.h:38
void newSize(int nelistw_size)
resize the list
Definition: coList.h:338
int allocinc
Definition: coList.h:176
~coList(void)
coList destructor
Definition: coList.h:228
T * last()
get last element and set current index to dim-1
Definition: coList.h:129
GLuint index
Definition: khronos-glext.h:6722
int find(const T &in_data) const
find the first occurrence of the specified element
Definition: coList.h:321
int dim
Definition: coList.h:174
const T & operator[](int index) const
direct list access
Definition: coList.h:65
void print(char *name) const
print out the contents of the list
Definition: coList.h:408
T & removeLast()
remove last element
Definition: coList.h:102
coList< T > & operator=(const coList< T > &v)
assign from another coList
Definition: coList.h:371
void init(const T &in_data)
set all elements of the current list to in_data
Definition: coList.h:387
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: khronos-glext.h:6354