Loading...
Searching...
No Matches
GenericQueue.h
Go to the documentation of this file.
1/* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2
3This file is part of the TerraLib - a Framework for building GIS enabled applications.
4
5TerraLib is free software: you can redistribute it and/or modify
6it under the terms of the GNU Lesser General Public License as published by
7the Free Software Foundation, either version 3 of the License,
8or (at your option) any later version.
9
10TerraLib is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU Lesser General Public License for more details.
14
15You should have received a copy of the GNU Lesser General Public License
16along with TerraLib. See COPYING. If not, write to
17TerraLib Team at <terralib-team@terralib.org>.
18*/
19
20/*!
21 \file terralib/common/GenericQueue.h
22
23 \brief Implements a generic queue.
24*/
25
26#ifndef __TERRALIB_COMMON_INTERNAL_GENERICQUEUE_H
27#define __TERRALIB_COMMON_INTERNAL_GENERICQUEUE_H
28
29// TerraLib
30#include "Exception.h"
31
32namespace te
33{
34 namespace common
35 {
36 /*!
37 \struct NodeT
38
39 \brief Struct that represents a node in the Queue.
40
41 \tparam T Data type of the data stored in a node.
42
43 \ingroup common
44 */
45 template <typename T>
46 struct NodeT
47 {
48 T m_value; //!< Stored value.
49 NodeT<T>* m_next; //!< Pointer to the next node.
50
51 /*!
52 \brief Constructor.
53 \param v The value to be stored.
54 */
55
56 NodeT(T v)
57 {
58 m_value = v; // Stores the value.
59 m_next = 0; // Sets the next null;
60 }
61
62 /*!
63 \brief Destructor.
64 */
66 {
67 delete m_next;
68 }
69 };
70
71 /*!
72 \struct QueueT
73
74 \brief Struct that implements the generic queue.
75
76 Queues have well known behavior: first in, first out.
77
78 \tparam T Type of the data to be stored.
79
80 \note The queue DOES NOT takes the ownership of the pointers stored in it.
81
82 \ingroup common
83 */
84 template <typename T>
85 struct QueueT
86 {
87 NodeT<T>* m_first; //!< Pointer to the first node.
88 NodeT<T>* m_last; //!< Pointer to the last node.
89 unsigned int m_size; //!< Number of elements contained in the queue.
90
91 /*!
92 \brief Constructor.
93 */
95 {
96 m_first = 0; // Sets first pointer to null.
97 m_last = m_first; // Sets last pointer to null.
98 m_size = 0; // Sets the size to 0.
99 }
100
101 /*!
102 \brief Destructor.
103 */
105 {
106 while(m_size > 0) // Do while queue not empty.
107 remove(0); // Removes the first element.
108 }
109
110 /*!
111 \brief Adds an element to the end of the queue.
112
113 \param v Element to be stored.
114 */
115 void add(const T& v)
116 {
117 NodeT<T>* novo = new NodeT<T>(v);
118
119 if(m_first == 0) // Fila vazia.
120 {
121 m_first = novo; // Aponta o primeiro para novo.
122 m_last = m_first; // Aponta o último para o primeiro.
123 }
124 else // Fila não vazia.
125 {
126 m_last->m_next = novo; // Aponta o próximo do último para o novo.
127 m_last = novo; // Aponta o último para o novo.
128 }
129
130 m_size++;
131 }
132
133 /*!
134 * \brief Removes the element at position \a pos.
135 *
136 * \param pos The position of the element in the queue.
137 *
138 * \return The elemen stored at position \a pos.
139 *
140 * \exception If \a pos is greater or equals than the queue size, a te::common::Exception will be raised.
141 */
142 T remove(const unsigned int& pos)
143 {
144 if(pos >= m_size)
145 throw Exception("Queue remove error: index out of boundaries.");
146
147 T res;
148 NodeT<T>* aux = m_first;
149
150 if(pos == 0) // Remove first
151 {
152 m_first = m_first->m_next;
153 res = aux->m_value;
154 aux->m_next = 0;
155 delete aux;
156 }
157 else
158 {
159 for(unsigned int i = 0; i < pos - 1; i++)
160 aux = aux->m_next;
161
162 NodeT<T>* aux2 = aux->m_next;
163
164 if(aux2->m_next == 0) // Remove last
165 {
166 aux->m_next = 0;
167 m_last = aux;
168 res = aux2->m_value;
169 delete aux2;
170 }
171 else
172 {
173 // Remove in the midle
174 aux->m_next = aux2->m_next;
175 aux2->m_next = 0;
176 res = aux2->m_value;
177 delete aux2;
178 }
179 }
180
181 m_size--;
182
183 return res;
184 }
185
186 /*!
187 * \brief Inserts a data at a specific position.
188 *
189 * \param v Value to be stored.
190 *
191 * \param pos Position where to put it.
192 *
193 * \exception If \a pos were greater than the queue size a te::common::Exception will be raised.
194 */
195 void insert(const T& v, const unsigned int& pos)
196 {
197 if(pos > m_size)
198 throw Exception("Queue insert error: index out of boundaries.");
199
200 NodeT<T>* novo = new NodeT<T>(v);
201
202 if(pos == 0) // insert at the beggining
203 {
204 novo->m_next = m_first;
205 m_first = novo;
206
207 if(m_size == 0)
208 m_last = m_first;
209 }
210 else if(pos == m_size) // insert at the end
211 {
212 m_last->m_next = novo;
213 m_last = novo;
214 }
215 else
216 {
217 NodeT<T>* aux = m_first; // Insert between two nodes
218
219 for(unsigned int i = 0; i < pos - 1; i++)
220 aux = aux->m_next;
221
222 NodeT<T>* aux2 = aux->m_next;
223
224 aux->m_next = novo;
225 novo->m_next = aux2;
226 }
227
228 m_size++;
229 }
230
231 /*!
232 * \brief Returns the value stored at \a pos position of the queue.
233 *
234 * \param pos The required position.
235 *
236 * \return The value stored at required position.
237 *
238 * \exception If \a pos were greater or equal than queue size a te::common::Exception will be raised.
239 */
240 T getValue(const unsigned int& pos) const
241 {
242 if(pos >= m_size)
243 throw Exception("Queue getValue error: index out of boundaries.");
244
245 NodeT<T>* aux = m_first;
246
247 for(unsigned int i = 0; i < pos; i++)
248 aux = aux->m_next;
249
250 return aux->m_value;
251 }
252
253 /*!
254 * \brief Return the position of a value in the queue.
255 *
256 * \param v The value we are searching for.
257 *
258 * \return The position of the value in the queue.
259 *
260 * \exception If queue were empty or does not contains the \a v, a te::common::Exception will be raised.
261 */
262 unsigned int position(const T& v) const
263 {
264 if(m_size > 0)
265 {
266 NodeT<T>* aux = m_first;
267 unsigned int count = 0;
268
269 while(aux != 0)
270 {
271 if(aux->m_value == v)
272 return count;
273
274 count++;
275 aux = aux->m_next;
276 }
277 }
278
279 throw Exception("Queue does not contains data.");
280 }
281
282 /*!
283 * \brief Returns the size of the queue.
284 *
285 * \return The number of items stored at the queue.
286 */
287 unsigned int getSize() const
288 {
289 return m_size;
290 }
291 };
292 }
293}
294
295#endif // __TERRALIB_COMMON_INTERNAL_GENERICQUEUE_H
This class is designed to declare objects to be thrown as exceptions by TerraLib.
Definition: Exception.h:59
TerraLib.
Struct that represents a node in the Queue.
Definition: GenericQueue.h:47
~NodeT()
Destructor.
Definition: GenericQueue.h:65
T m_value
Stored value.
Definition: GenericQueue.h:48
NodeT(T v)
Constructor.
Definition: GenericQueue.h:56
NodeT< T > * m_next
Pointer to the next node.
Definition: GenericQueue.h:49
Struct that implements the generic queue.
Definition: GenericQueue.h:86
T getValue(const unsigned int &pos) const
Returns the value stored at pos position of the queue.
Definition: GenericQueue.h:240
NodeT< T > * m_last
Pointer to the last node.
Definition: GenericQueue.h:88
unsigned int getSize() const
Returns the size of the queue.
Definition: GenericQueue.h:287
void add(const T &v)
Adds an element to the end of the queue.
Definition: GenericQueue.h:115
T remove(const unsigned int &pos)
Removes the element at position pos.
Definition: GenericQueue.h:142
~QueueT()
Destructor.
Definition: GenericQueue.h:104
void insert(const T &v, const unsigned int &pos)
Inserts a data at a specific position.
Definition: GenericQueue.h:195
unsigned int position(const T &v) const
Return the position of a value in the queue.
Definition: GenericQueue.h:262
unsigned int m_size
Number of elements contained in the queue.
Definition: GenericQueue.h:89
QueueT()
Constructor.
Definition: GenericQueue.h:94
NodeT< T > * m_first
Pointer to the first node.
Definition: GenericQueue.h:87
An exception class for the XML module.