COVISE Core
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
coDeque.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_DEQUE_H
9 #define YAC_DEQUE_H
10 
11 #include <string.h>
12 #include <iostream>
13 
14 //=========================================================================
15 // DEFINITION
16 //=========================================================================
17 
25 namespace covise
26 {
27 
28 template <class T>
29 class coDeque
30 {
31 public:
32  coDeque(void);
33  ~coDeque(void);
34 
36  T &operator[](int index);
38  const T &operator[](int index) const;
39 
41  void pushFront(const T &in_data);
43  void pushBack(const T &in_data);
45  void popFront(void);
47  void popBack(void);
48 
50  int isBad()
51  {
52  return error;
53  };
55  int size(void)
56  {
57  return dim;
58  };
60  void print(void);
61 
62 private:
63  T *data;
64  int dim;
65  int error;
66 };
67 
68 template <class T>
69 std::ostream &operator<<(std::ostream &str, const coDeque<T> &queue);
70 
71 //=========================================================================
72 // IMPLEMENTATION
73 //=========================================================================
74 
75 template <class T>
76 inline coDeque<T>::coDeque(void)
77 {
78  dim = 0;
79  data = NULL;
80  error = 0;
81 }
82 
83 template <class T>
85 {
86 
87  // free all memory
88  if (data)
89  delete[] data;
90 }
91 
92 //-------------------------------------------------------------------------
93 // Access functions
94 //-------------------------------------------------------------------------
95 template <class T>
96 inline const T &coDeque<T>::operator[](int index) const
97 {
98  if (index < 0 || index > (dim - 1))
99  {
100  // out of bounds !
101  static T dummy;
102  ((coDeque<T> *)this)->error = 1;
103  return dummy;
104  }
105  return (data[index]);
106 }
107 
108 template <class T>
110 {
111  if (index < 0 || index > (dim - 1))
112  {
113  // out of bounds !
114  static T dummy;
115  error = 1;
116  return dummy;
117  }
118  return (data[index]);
119 }
120 
121 template <class T>
122 inline void coDeque<T>::pushBack(const T &in_data)
123 {
124 
125  T *new_data = new T[dim + 1];
126  if (dim)
127  memcpy(new_data, data, dim * sizeof(T));
128  new_data[dim] = in_data;
129  delete[] data;
130  data = new_data;
131  dim++;
132 }
133 
134 template <class T>
135 inline void coDeque<T>::pushFront(const T &in_data)
136 {
137 
138  T *new_data = new T[dim + 1];
139  new_data[0] = in_data;
140  if (dim)
141  memcpy(&new_data[1], data, dim * sizeof(T));
142  delete[] data;
143  data = new_data;
144  dim++;
145 }
146 
147 template <class T>
148 inline void coDeque<T>::popBack(void)
149 {
150 
151  if (dim == 0)
152  return;
153 
154  if (dim == 1)
155  {
156  delete[] data;
157  data = 0L;
158  dim = 0;
159  return;
160  }
161 
162  T *new_data = new T[dim - 1];
163  memcpy(new_data, data, (dim - 1) * sizeof(T));
164  delete[] data;
165  data = new_data;
166  dim--;
167 }
168 
169 template <class T>
170 inline void coDeque<T>::popFront(void)
171 {
172 
173  if (dim == 0)
174  return;
175 
176  if (dim == 1)
177  {
178  delete[] data;
179  data = 0L;
180  dim = 0;
181  return;
182  }
183 
184  T *new_data = new T[dim - 1];
185  memcpy(new_data, &data[1], (dim - 1) * sizeof(T));
186  delete[] data;
187  data = new_data;
188  dim--;
189 }
190 
191 //-------------------------------------------------------------------------
192 // print out contents
193 //-------------------------------------------------------------------------
194 template <class T>
195 inline void coDeque<T>::print(void)
196 {
197  std::cout << "coDeque contains:" << std::endl;
198  if (!dim)
199  {
200  std::cout << "Nothing!" << std::endl;
201  }
202  else
203  for (int i = 0; i < dim; i++)
204  std::cout << "[" << i << "]: " << data[i] << std::endl;
205 }
206 
207 template <class T>
208 inline std::ostream &operator<<(std::ostream &str, const coDeque<T> &queue)
209 {
210  str << "coDeque contains:" << std::endl;
211  if (!queue->dim)
212  {
213  str << "Nothing!" << std::endl;
214  }
215  else
216  for (int i = 0; i < queue->dim; i++)
217  str << "[" << i << "]: " << queue->data[i] << std::endl;
218 
219  return str;
220 }
221 }
222 #endif // YAC_DEQUE_H
int size(void)
returns the size of the queue
Definition: coDeque.h:55
int isBad()
get the error id if something goes wrong
Definition: coDeque.h:50
void print(void)
print out the contents of the queue
Definition: coDeque.h:195
static Repl dummy("","")
void popFront(void)
pop one element from the beginning of the queue
Definition: coDeque.h:170
int dim
Definition: coDeque.h:64
#define NULL
Definition: covise_list.h:22
T & operator[](int index)
direct list access
Definition: coDeque.h:109
void pushFront(const T &in_data)
push one element at the beginning of the queue
Definition: coDeque.h:135
void pushBack(const T &in_data)
push one element at the end of the queue
Definition: coDeque.h:122
void popBack(void)
pop one element from the end of the queue
Definition: coDeque.h:148
coDeque(void)
Definition: coDeque.h:76
~coDeque(void)
Definition: coDeque.h:84
GLuint index
Definition: khronos-glext.h:6722
T * data
Definition: coDeque.h:63
int error
Definition: coDeque.h:65
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: khronos-glext.h:6354
Definition: coDeque.h:29