0
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
1 /*
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
2 CircularQueue
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
3 Copyright (C) 2002 Eric Wing
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
4
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
5 This library is free software; you can redistribute it and/or
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
6 modify it under the terms of the GNU Library General Public
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
7 License as published by the Free Software Foundation; either
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
8 version 2 of the License, or (at your option) any later version.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
9
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
10 This library is distributed in the hope that it will be useful,
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
13 Library General Public License for more details.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
14
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
15 You should have received a copy of the GNU Library General Public
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
16 License along with this library; if not, write to the Free
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
18 */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
19
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
20 #include "CircularQueue.h"
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
21 #include <stddef.h> /* for NULL */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
22 #include <stdlib.h> /* for malloc/free */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
23 #include <stdio.h> /* for debugging */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
24
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
25 CircularQueueUnsignedInt* CircularQueueUnsignedInt_CreateQueue(unsigned int max_size)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
26 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
27 CircularQueueUnsignedInt* ret_ptr;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
28 if(max_size < 1)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
29 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
30 return NULL;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
31 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
32 ret_ptr = (CircularQueueUnsignedInt*)malloc(sizeof(CircularQueueUnsignedInt));
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
33 if(NULL == ret_ptr)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
34 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
35 /* Out of memory */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
36 return NULL;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
37 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
38 ret_ptr->internalQueue = (unsigned int*)malloc(sizeof(unsigned int) * max_size);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
39 if(NULL == ret_ptr->internalQueue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
40 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
41 /* Out of memory */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
42 free(ret_ptr);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
43 return NULL;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
44 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
45 ret_ptr->maxSize = max_size;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
46 ret_ptr->currentSize = 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
47 ret_ptr->headIndex = 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
48 ret_ptr->tailIndex = 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
49
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
50 return ret_ptr;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
51 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
52
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
53 void CircularQueueUnsignedInt_FreeQueue(CircularQueueUnsignedInt* queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
54 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
55 if(NULL == queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
56 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
57 return;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
58 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
59
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
60 free(queue->internalQueue);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
61 free(queue);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
62 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
63
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
64 /**
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
65 * Returns 1 if successful, 0 if failure.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
66 */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
67 unsigned int CircularQueueUnsignedInt_PushBack(CircularQueueUnsignedInt* queue, unsigned int value)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
68 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
69 unsigned int temp_index;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
70 if(NULL == queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
71 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
72 return 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
73 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
74 if(queue->currentSize >= queue->maxSize)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
75 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
76 return 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
77 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
78 temp_index = queue->tailIndex + 1;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
79 if(temp_index >= queue->maxSize)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
80 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
81 /* need to wrap tail index around */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
82 temp_index = 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
83 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
84 /* So with my implementation, the tail index actually points
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
85 * to the slot right after the last value.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
86 * So we will enter the value in the current tail, and then increment
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
87 * the tail.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
88 * Note that in a full queue, the head and tail will be the same (I think).
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
89 */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
90 queue->internalQueue[queue->tailIndex] = value;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
91 queue->tailIndex = temp_index;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
92 queue->currentSize++;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
93
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
94 return 1;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
95 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
96
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
97 /**
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
98 * Returns 1 if successful, 0 if failure.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
99 */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
100 unsigned int CircularQueueUnsignedInt_PushFront(CircularQueueUnsignedInt* queue, unsigned int value)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
101 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
102 unsigned int temp_index;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
103 if(NULL == queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
104 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
105 return 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
106 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
107 if(queue->currentSize >= queue->maxSize)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
108 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
109 return 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
110 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
111
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
112 /* This check is needed to prevent the unsigned int from overflowing. */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
113 if(0 == queue->headIndex)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
114 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
115 /* Need to wrap head index around */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
116 temp_index = queue->maxSize - 1;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
117 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
118 else
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
119 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
120 temp_index = queue->headIndex - 1;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
121 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
122 /* So unlike the tail, the head index actually points to the element
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
123 * at the head, and not an element before (or after) the head.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
124 */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
125 queue->internalQueue[temp_index] = value;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
126 queue->headIndex = temp_index;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
127 queue->currentSize++;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
128 return 1;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
129 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
130
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
131 unsigned int CircularQueueUnsignedInt_PopFront(CircularQueueUnsignedInt* queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
132 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
133 unsigned int temp_index;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
134 if(NULL == queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
135 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
136 return 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
137 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
138 if(queue->currentSize == 0)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
139 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
140 return 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
141 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
142 temp_index = queue->headIndex + 1;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
143 if(temp_index >= queue->maxSize)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
144 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
145 /* need to wrap tail index around */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
146 temp_index = 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
147 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
148 queue->headIndex = temp_index;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
149 queue->currentSize--;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
150 return 1;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
151 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
152
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
153
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
154 unsigned int CircularQueueUnsignedInt_PopBack(CircularQueueUnsignedInt* queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
155 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
156 unsigned int temp_index;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
157 if(NULL == queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
158 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
159 return 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
160 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
161 if(queue->currentSize == 0)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
162 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
163 return 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
164 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
165
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
166 /* This check is needed to prevent the unsigned int from overflowing. */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
167 if(0 == queue->tailIndex)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
168 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
169 temp_index = queue->maxSize - 1;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
170 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
171 else
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
172 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
173 temp_index = queue->tailIndex - 1;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
174 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
175
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
176 queue->tailIndex = temp_index;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
177 queue->currentSize--;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
178 return 1;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
179 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
180
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
181 unsigned int CircularQueueUnsignedInt_Front(CircularQueueUnsignedInt* queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
182 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
183 if(NULL == queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
184 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
185 return 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
186 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
187 if(0 == queue->currentSize)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
188 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
189 return 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
190 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
191 return queue->internalQueue[queue->headIndex];
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
192 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
193
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
194
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
195 unsigned int CircularQueueUnsignedInt_Back(CircularQueueUnsignedInt* queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
196 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
197 unsigned int temp_index;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
198 if(NULL == queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
199 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
200 return 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
201 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
202 if(0 == queue->currentSize)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
203 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
204 return 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
205 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
206 if(0 == queue->tailIndex)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
207 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
208 /* need to wrap tail index around */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
209 temp_index = queue->maxSize-1;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
210 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
211 else
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
212 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
213 temp_index = queue->tailIndex-1;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
214 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
215
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
216 return queue->internalQueue[temp_index];
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
217 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
218
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
219 unsigned int CircularQueueUnsignedInt_Size(CircularQueueUnsignedInt* queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
220 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
221 if(NULL == queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
222 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
223 return 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
224 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
225 return queue->currentSize;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
226 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
227
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
228 unsigned int CircularQueueUnsignedInt_MaxSize(CircularQueueUnsignedInt* queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
229 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
230 if(NULL == queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
231 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
232 return 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
233 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
234 return queue->maxSize;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
235 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
236
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
237 void CircularQueueUnsignedInt_Clear(CircularQueueUnsignedInt* queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
238 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
239 if(NULL == queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
240 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
241 return;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
242 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
243
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
244 queue->currentSize = 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
245 queue->headIndex = 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
246 queue->tailIndex = 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
247 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
248
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
249 void CircularQueueUnsignedInt_Print(CircularQueueUnsignedInt* queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
250 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
251 unsigned int i;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
252 unsigned int count;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
253 if(NULL == queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
254 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
255 return;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
256 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
257 fprintf(stderr, "Queue: ");
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
258 for(count=0, i=queue->headIndex; count<queue->currentSize; count++, i++)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
259 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
260 if(i >= queue->maxSize)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
261 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
262 i=0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
263 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
264 fprintf(stderr, "%d ", queue->internalQueue[i]);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
265 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
266 fprintf(stderr, "\n");
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
267 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
268
|
1
|
269 unsigned int CircularQueueUnsignedInt_ValueAtIndex(CircularQueueUnsignedInt* queue, unsigned int the_index)
|
|
270 {
|
|
271 unsigned int i;
|
|
272 if(NULL == queue)
|
|
273 {
|
|
274 return 0;
|
|
275 }
|
|
276 if(the_index >= queue->currentSize)
|
|
277 {
|
|
278 return 0;
|
|
279 }
|
|
280 i = (queue->headIndex + the_index) % queue->currentSize;
|
|
281 // fprintf(stderr, "%d\n", queue->internalQueue[i]);
|
|
282 return queue->internalQueue[i];
|
|
283 }
|
|
284
|
0
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
285 /*
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
286 * Implementation for void* version starts here.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
287 */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
288
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
289 CircularQueueVoid* CircularQueueVoid_CreateQueue(unsigned int max_size)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
290 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
291 CircularQueueVoid* ret_ptr;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
292 if(max_size < 1)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
293 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
294 return NULL;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
295 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
296 ret_ptr = (CircularQueueVoid*)malloc(sizeof(CircularQueueVoid));
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
297 if(NULL == ret_ptr)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
298 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
299 /* Out of memory */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
300 return NULL;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
301 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
302 ret_ptr->internalQueue = (void**)malloc(sizeof(void*) * max_size);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
303 if(NULL == ret_ptr->internalQueue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
304 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
305 /* Out of memory */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
306 free(ret_ptr);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
307 return NULL;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
308 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
309 ret_ptr->maxSize = max_size;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
310 ret_ptr->currentSize = 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
311 ret_ptr->headIndex = 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
312 ret_ptr->tailIndex = 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
313
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
314 return ret_ptr;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
315 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
316
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
317 void CircularQueueVoid_FreeQueue(CircularQueueVoid* queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
318 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
319 if(NULL == queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
320 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
321 return;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
322 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
323
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
324 free(queue->internalQueue);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
325 free(queue);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
326 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
327
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
328 /**
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
329 * Returns 1 if successful, 0 if failure.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
330 */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
331 unsigned int CircularQueueVoid_PushBack(CircularQueueVoid* queue, void* value)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
332 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
333 unsigned int temp_index;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
334 if(NULL == queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
335 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
336 return 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
337 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
338 if(queue->currentSize >= queue->maxSize)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
339 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
340 return 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
341 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
342 temp_index = queue->tailIndex + 1;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
343 if(temp_index >= queue->maxSize)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
344 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
345 /* need to wrap tail index around */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
346 temp_index = 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
347 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
348 queue->internalQueue[queue->tailIndex] = value;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
349 queue->tailIndex = temp_index;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
350 queue->currentSize++;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
351
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
352 return 1;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
353 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
354
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
355 /**
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
356 * Returns 1 if successful, 0 if failure.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
357 */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
358 unsigned int CircularQueueVoid_PushFront(CircularQueueVoid* queue, void* value)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
359 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
360 unsigned int temp_index;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
361 if(NULL == queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
362 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
363 return 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
364 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
365 if(queue->currentSize >= queue->maxSize)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
366 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
367 return 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
368 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
369
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
370 /* This check is needed to prevent the unsigned int from overflowing. */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
371 if(0 == queue->headIndex)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
372 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
373 /* Need to wrap head index around */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
374 temp_index = queue->maxSize - 1;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
375 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
376 else
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
377 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
378 temp_index = queue->headIndex - 1;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
379 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
380 queue->internalQueue[temp_index] = value;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
381 queue->headIndex = temp_index;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
382 queue->currentSize++;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
383 return 1;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
384 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
385
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
386 unsigned int CircularQueueVoid_PopFront(CircularQueueVoid* queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
387 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
388 unsigned int temp_index;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
389 if(NULL == queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
390 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
391 return 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
392 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
393 if(queue->currentSize == 0)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
394 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
395 return 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
396 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
397 temp_index = queue->headIndex + 1;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
398 if(temp_index >= queue->maxSize)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
399 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
400 /* need to wrap tail index around */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
401 temp_index = 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
402 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
403 queue->headIndex = temp_index;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
404 queue->currentSize--;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
405 return 1;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
406 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
407
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
408
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
409 unsigned int CircularQueueVoid_PopBack(CircularQueueVoid* queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
410 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
411 unsigned int temp_index;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
412 if(NULL == queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
413 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
414 return 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
415 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
416 if(queue->currentSize == 0)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
417 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
418 return 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
419 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
420
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
421 /* This check is needed to prevent the unsigned int from overflowing. */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
422 if(0 == queue->tailIndex)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
423 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
424 temp_index = queue->maxSize - 1;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
425 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
426 else
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
427 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
428 temp_index = queue->tailIndex - 1;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
429 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
430
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
431 queue->tailIndex = temp_index;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
432 queue->currentSize--;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
433 return 1;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
434 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
435
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
436 void* CircularQueueVoid_Front(CircularQueueVoid* queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
437 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
438 if(NULL == queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
439 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
440 return 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
441 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
442 if(0 == queue->currentSize)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
443 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
444 return 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
445 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
446 return queue->internalQueue[queue->headIndex];
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
447 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
448
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
449
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
450 void* CircularQueueVoid_Back(CircularQueueVoid* queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
451 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
452 unsigned int temp_index;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
453 if(NULL == queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
454 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
455 return 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
456 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
457 if(0 == queue->currentSize)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
458 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
459 return 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
460 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
461 if(0 == queue->tailIndex)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
462 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
463 /* need to wrap tail index around */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
464 temp_index = queue->maxSize-1;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
465 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
466 else
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
467 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
468 temp_index = queue->tailIndex-1;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
469 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
470
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
471 return queue->internalQueue[temp_index];
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
472 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
473
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
474 unsigned int CircularQueueVoid_Size(CircularQueueVoid* queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
475 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
476 if(NULL == queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
477 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
478 return 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
479 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
480 return queue->currentSize;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
481 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
482
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
483 unsigned int CircularQueueVoid_MaxSize(CircularQueueVoid* queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
484 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
485 if(NULL == queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
486 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
487 return 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
488 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
489 return queue->maxSize;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
490 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
491
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
492 void CircularQueueVoid_Clear(CircularQueueVoid* queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
493 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
494 if(NULL == queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
495 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
496 return;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
497 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
498
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
499 queue->currentSize = 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
500 queue->headIndex = 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
501 queue->tailIndex = 0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
502 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
503
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
504 void CircularQueueVoid_Print(CircularQueueVoid* queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
505 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
506 unsigned int i;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
507 unsigned int count;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
508 if(NULL == queue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
509 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
510 return;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
511 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
512 fprintf(stderr, "Queue: ");
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
513 for(count=0, i=queue->headIndex; count<queue->currentSize; count++, i++)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
514 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
515 if(i >= queue->maxSize)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
516 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
517 i=0;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
518 }
|
1
|
519 fprintf(stderr, "%x ", (unsigned int)queue->internalQueue[i]);
|
0
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
520 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
521 fprintf(stderr, "\n");
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
522 }
|
1
|
523
|
|
524 void* CircularQueueVoid_ValueAtIndex(CircularQueueVoid* queue, unsigned int the_index)
|
|
525 {
|
|
526 unsigned int i;
|
|
527 if(NULL == queue)
|
|
528 {
|
|
529 return NULL;
|
|
530 }
|
|
531 if(the_index >= queue->currentSize)
|
|
532 {
|
|
533 return NULL;
|
|
534 }
|
|
535 i = (queue->headIndex + the_index) % queue->currentSize;
|
|
536 // fprintf(stderr, "%d\n", queue->internalQueue[i]);
|
|
537 return queue->internalQueue[i];
|
|
538 }
|
0
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
539
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
540
|