OpenMoHAA 0.82.0
Loading...
Searching...
No Matches
queue.h
1/*
2===========================================================================
3Copyright (C) 2015 the OpenMoHAA team
4
5This file is part of OpenMoHAA source code.
6
7OpenMoHAA source code is free software; you can redistribute it
8and/or modify it under the terms of the GNU General Public License as
9published by the Free Software Foundation; either version 2 of the License,
10or (at your option) any later version.
11
12OpenMoHAA source code is distributed in the hope that it will be
13useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with OpenMoHAA source code; if not, write to the Free Software
19Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20===========================================================================
21*/
22
23// queue.h: Generic Queue object
24//
25
26#ifndef __QUEUE_H__
27#define __QUEUE_H__
28
29#include "class.h"
30
31template<class T>
32class QueueNode : public Class
33{
34public:
35 T data;
36 QueueNode *next;
37
38 QueueNode();
39};
40
41template<class T>
42QueueNode<T>::QueueNode()
43{
44 data = NULL;
45 next = NULL;
46}
47
48template<class T>
49class Queue : public Class
50{
51private:
52 QueueNode<T> *head;
53 QueueNode<T> *tail;
54
55public:
56 Queue();
57 ~Queue();
58 void Clear(void);
59 qboolean Empty(void);
60 void Enqueue(T data);
61 T Dequeue(void);
62 void Remove(T data);
63 qboolean Inqueue(T data);
64};
65
66template<class T>
67qboolean Queue<T>::Empty
68 (
69 void
70 )
71{
72 if (head == NULL)
73 {
74 assert(!tail);
75 return true;
76 }
77
78 assert(tail);
79 return false;
80}
81
82template<class T>
83void Queue<T>::Enqueue
84 (
85 T data
86 )
87{
88 QueueNode<T> *tmp;
89
90 tmp = new QueueNode<T>;
91 assert(tmp);
92
93 tmp->data = data;
94
95 assert(!tmp->next);
96 if (!head)
97 {
98 assert(!tail);
99 head = tmp;
100 }
101 else
102 {
103 assert(tail);
104 tail->next = tmp;
105 }
106 tail = tmp;
107}
108
109template<class T>
110T Queue<T>::Dequeue
111 (
112 void
113 )
114{
115 T ptr;
116 QueueNode<T> *node;
117
118 if (!head)
119 {
120 assert(!tail);
121 return NULL;
122 }
123
124 node = head;
125 ptr = node->data;
126
127 head = node->next;
128 if (head == NULL)
129 {
130 assert(tail == node);
131 tail = NULL;
132 }
133
134 delete node;
135
136 return ptr;
137}
138
139template<class T>
140void Queue<T>::Clear
141 (
142 void
143 )
144{
145 while (!Empty())
146 {
147 Dequeue();
148 }
149}
150
151template<class T>
152Queue<T>::Queue()
153{
154 head = NULL;
155 tail = NULL;
156}
157
158template<class T>
159Queue<T>::~Queue()
160{
161 Clear();
162}
163
164template<class T>
165void Queue<T>::Remove
166 (
167 T data
168 )
169{
170 QueueNode<T> *node;
171 QueueNode<T> *prev;
172
173 if (!head)
174 {
175 assert(!tail);
176
177 gi.DPrintf("Queue::Remove : Data not found in queue\n");
178 return;
179 }
180
181 for (prev = NULL, node = head; node != NULL; prev = node, node = node->next)
182 {
183 if (node->data == data)
184 {
185 break;
186 }
187 }
188
189 if (!node)
190 {
191 gi.DPrintf("Queue::Remove : Data not found in queue\n");
192 }
193 else
194 {
195 if (!prev)
196 {
197 // at head
198 assert(node == head);
199 head = node->next;
200 if (head == NULL)
201 {
202 assert(tail == node);
203 tail = NULL;
204 }
205 }
206 else
207 {
208 prev->next = node->next;
209 if (prev->next == NULL)
210 {
211 // at tail
212 assert(tail == node);
213 tail = prev;
214 }
215 }
216
217 delete node;
218 }
219}
220
221template<class T>
222qboolean Queue<T>::Inqueue
223 (
224 T data
225 )
226{
227 QueueNode<T> *node;
228
229 for (node = head; node != NULL; node = node->next)
230 {
231 if (node->data == data)
232 {
233 return true;
234 }
235 }
236
237 return false;
238}
239
240#endif /* queue.h */
Definition queue.h:33