0
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
1 /*
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
2 CircularQueue
|
2
|
3 Copyright (C) 2002 Eric Wing <ewing . public @ playcontrol.net>
|
0
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
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
21 #ifndef C_CIRCULAR_QUEUE_H
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
22 #define C_CIRCULAR_QUEUE_H
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
23
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
24 /* Set up for C function definitions, even when using C++ */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
25 #ifdef __cplusplus
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
26 extern "C" {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
27 #endif
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
28
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
29 /**
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
30 * @file
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
31 * This is a C-based Circular queue class.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
32 * This class provides very simple circular queue functionality,
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
33 * with an API similar to the C++ STL queue class.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
34 * Currently, a queue cannot be resized.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
35 * Because C doesn't do templates and I really don't want
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
36 * to write my own polymorphism, you must select the proper queue
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
37 * for your data types.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
38 * I currently provide an unisigned int version
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
39 * and a void* version. The void* version will let you use any type,
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
40 * but remember that you are responsible for casting and maintaining
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
41 * your own type safety. I have found the unsigned int version to be
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
42 * very useful because if you have a map somewhere that associates
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
43 * unique identifier numbers to data (e.g. OpenGL displaylists/textures),
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
44 * then you can just deal with the id numbers and don't have to deal with
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
45 * the casting and typesafety issues. I recommend you don't overlook the
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
46 * usefulness of this version.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
47 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
48 * @warning Do not mix the CircularQueues created from the different versions.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
49 * They are incompatible. Use only CircularQueueUnsignedInt objects with
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
50 * CircularQueueUnsignedInt_* functions, etc.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
51 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
52 * Example Usage:
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
53 * @code
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
54 * CircularQueueUnsignedInt* myqueue;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
55 * unsigned int ret_val;
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
56 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
57 * myqueue = CircularQueueUnsignedInt_CreateQueue(3);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
58 * if(NULL == myqueue)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
59 * {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
60 * fprintf(stderr, "Error, could not create queue\n");
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
61 * return 0;
|
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 * ret_val = CircularQueueUnsignedInt_PushBack(myqueue, 1);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
66 * ret_val = CircularQueueUnsignedInt_PushBack(myqueue, 2);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
67 * ret_val = CircularQueueUnsignedInt_PushBack(myqueue, 3);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
68 * if(0 == ret_val)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
69 * {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
70 * fprintf(stderr, "Error, Could not pushback\n");
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
71 * exit(1);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
72 * }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
73 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
74 * ret_val = CircularQueueUnsignedInt_PopBack(myqueue);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
75 * if(0 == ret_val)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
76 * {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
77 * fprintf(stderr, "Error, Could not popback\n");
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
78 * exit(2);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
79 * }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
80 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
81 * fprintf(stderr, "Testing queue, should have 1,2\n");
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
82 * CircularQueueUnsignedInt_Print(myqueue);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
83 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
84 * ret_val = CircularQueueUnsignedInt_PushFront(myqueue, 4);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
85 * if(0 == ret_val)
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
86 * {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
87 * fprintf(stderr, "Error, Could not pushfront\n");
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
88 * exit(1);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
89 * }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
90 * fprintf(stderr, "Testing queue, should have 4,1,2\n");
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
91 * CircularQueueUnsignedInt_Print(myqueue);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
92 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
93 * @endcode
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
94 */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
95
|
1
|
96 /* This is a trick I picked up from Lua. Doing the typedef separately
|
|
97 * (and I guess before the definition) instead of a single
|
|
98 * entry: typedef struct {...} YourName; seems to allow me
|
|
99 * to use forward declarations. Doing it the other way (like SDL)
|
|
100 * seems to prevent me from using forward declarions as I get conflicting
|
|
101 * definition errors. I don't really understand why though.
|
|
102 */
|
|
103 typedef struct CircularQueueUnsignedInt CircularQueueUnsignedInt;
|
0
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
104 /**
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
105 * This is the essentially the CircularQueue object.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
106 * This contains all the data associated with a CircularQueue instance.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
107 * This version is for unsigned int data types. In the future, I suppose
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
108 * I could add a void* data type at the very least and maybe some
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
109 * other data types.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
110 * This should be considered an opaque data type.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
111 */
|
1
|
112 struct CircularQueueUnsignedInt
|
0
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
113 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
114 unsigned int maxSize; /**< Max size of the queue. */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
115 unsigned int currentSize; /**< Current number of entries in the queue. */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
116 unsigned int headIndex; /**< The index of where the current head is. */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
117 unsigned int tailIndex; /**< The index of where the current tail is. */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
118 unsigned int* internalQueue; /**< The array representing the queue. */
|
1
|
119 };
|
0
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
120
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
121 /**
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
122 * This creates a new CircularQueue (for unsigned int) instance.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
123 * This will create a new circular queue instance which holds
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
124 * unsigned int's in its queue.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
125 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
126 * @note This implementation does not allow a circular queue to be resized.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
127 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
128 * @param max_size This specifies the maximum number of elements the circular
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
129 * can hold.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
130 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
131 * @return Returns a pointer to a CircularQueue which is the
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
132 * instance variable (if successful) or NULL on failure.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
133 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
134 * @see CircularQueueUnsignedInt_FreeQueue
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
135 */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
136 CircularQueueUnsignedInt* CircularQueueUnsignedInt_CreateQueue(unsigned int max_size);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
137
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
138 /**
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
139 * This destroys a CircularQueue instance.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
140 * This will destroy the memory associated with the circular queue instance.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
141 * Whenever you create a CircularQueue instance, you should always to
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
142 * remember to balance it with a FreeQueue.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
143 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
144 * @param queue The pointer to the CircularQueue instance.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
145 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
146 * @see CircularQueueUnsignedInt_CreateQueue
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
147 */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
148 void CircularQueueUnsignedInt_FreeQueue(CircularQueueUnsignedInt* queue);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
149
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
150 /**
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
151 * This pushes a new value into the back of the queue.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
152 * If the queue is full, the function will fail and return 0.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
153 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
154 * @param queue The pointer to the CircularQueue instance.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
155 * @param value The value you want to push into the queue.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
156 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
157 * @return Returns 1 on success, or 0 on failure.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
158 */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
159 unsigned int CircularQueueUnsignedInt_PushBack(CircularQueueUnsignedInt* queue, unsigned int value);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
160
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
161 /**
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
162 * This pushes a new value into the front of the queue.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
163 * If the queue is full, the function will fail and return 0.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
164 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
165 * @param queue The pointer to the CircularQueue instance.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
166 * @param value The value you want to push into the queue.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
167 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
168 * @return Returns 1 on success, or 0 on failure.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
169 */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
170 unsigned int CircularQueueUnsignedInt_PushFront(CircularQueueUnsignedInt* queue, unsigned int value);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
171
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
172 /**
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
173 * This removes the value at the front of the queue.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
174 * If the queue is empty, the function will return 0.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
175 * Note that this function does not return the value popped, but
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
176 * an error flag. If you need the value, you must call Front()
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
177 * to retrieve the value before popping it.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
178 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
179 * @param queue The pointer to the CircularQueue instance.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
180 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
181 * @return Returns 1 on success, or 0 on failure.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
182 * @see CircularQueueUnsignedInt_Front
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
183 */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
184 unsigned int CircularQueueUnsignedInt_PopFront(CircularQueueUnsignedInt* queue);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
185
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
186 /**
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
187 * This removes the value at the back of the queue.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
188 * If the queue is empty, the function will return 0.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
189 * Note that this function does not return the value popped, but
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
190 * an error flag. If you need the value, you must call Back()
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
191 * to retrieve the value before popping it.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
192 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
193 * @param queue The pointer to the CircularQueue instance.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
194 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
195 * @return Returns 1 on success, or 0 on failure.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
196 * @see CircularQueueUnsignedInt_Back
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
197 */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
198 unsigned int CircularQueueUnsignedInt_PopBack(CircularQueueUnsignedInt* queue);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
199
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
200 /**
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
201 * This gets the value at the front of the queue.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
202 * If the queue is empty, the value returned will be 0.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
203 * Because this 0 return value is ambiguous because it could also could
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
204 * be a legitimate value in the queue, if you need more robust error
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
205 * checking for if the queue is empty, you should get the size of the
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
206 * queue using the Size() function.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
207 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
208 * @param queue The pointer to the CircularQueue instance.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
209 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
210 * @return Returns the value stored at the queue or 0 if the queue is empty
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
211 * (or if there is an error).
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
212 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
213 * @see CircularQueueUnsignedInt_Size
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
214 */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
215 unsigned int CircularQueueUnsignedInt_Front(CircularQueueUnsignedInt* queue);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
216
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
217 /**
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
218 * This gets the value at the back of the queue.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
219 * If the queue is empty, the value returned will be 0.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
220 * Because this 0 return value is ambiguous because it could also could
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
221 * be a legitimate value in the queue, if you need more robust error
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
222 * checking for if the queue is empty, you should get the size of the
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
223 * queue using the Size() function.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
224 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
225 * @param queue The pointer to the CircularQueue instance.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
226 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
227 * @return Returns the value stored at the queue or 0 if the queue is empty
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
228 * (or 0 if there is an error).
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
229 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
230 * @see CircularQueueUnsignedInt_Size
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
231 */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
232 unsigned int CircularQueueUnsignedInt_Back(CircularQueueUnsignedInt* queue);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
233
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
234 /**
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
235 * This gets the current number of entries that are in the queue.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
236 * This is number is not to be confused with the MaxSize.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
237 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
238 * @param queue The pointer to the CircularQueue instance.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
239 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
240 * @return Returns the number of entries currently in queue, or 0 if
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
241 * there is an error.
|
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 unsigned int CircularQueueUnsignedInt_Size(CircularQueueUnsignedInt* queue);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
245
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
246 /**
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
247 * This gets the maximum number of entries that are allowed in the queue at
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
248 * a given time.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
249 * This is the number that you used in the CreateQueue function.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
250 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
251 * @param queue The pointer to the CircularQueue instance.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
252 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
253 * @return Returns the maximum number of entries allowed in the queue.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
254 */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
255 unsigned int CircularQueueUnsignedInt_MaxSize(CircularQueueUnsignedInt* queue);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
256 /**
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
257 * This empties the entire queue.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
258 * This will remove all entries that are in the queue.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
259 * This does not destroy any memory. Use FreeQueue() to actually destroy
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
260 * the queue.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
261 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
262 * @param queue The pointer to the CircularQueue instance.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
263 */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
264 void CircularQueueUnsignedInt_Clear(CircularQueueUnsignedInt* queue);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
265
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
266 /**
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
267 * This is a debugging function that will print all the elements in the
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
268 * queue to stderr.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
269 * This function is provided as convenience, but should not be considered
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
270 * as part of the standard API. Treat this function as deprecated
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
271 * as it's implementation may change or be removed entirely.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
272 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
273 * @param queue The pointer to the CircularQueue instance.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
274 */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
275 void CircularQueueUnsignedInt_Print(CircularQueueUnsignedInt* queue);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
276
|
1
|
277 /**
|
|
278 * This returns the element located at the specified index,
|
|
279 * where index=0 represents the head/front of the queue.
|
|
280 *
|
|
281 * @param queue The pointer to the CircularQueue instance.
|
|
282 * @param the_index The index of the element you want where 0 represents the
|
|
283 * head/front of the queue and Size-1 is the back.
|
|
284 *
|
|
285 * @return Returns the value located at the index on success, or 0 on failure.
|
|
286 * Be careful to not to confuse an error for a legitmate 0 value.
|
|
287 * Any index from 0 to Size-1 (where Size>0) will be a valid index.
|
|
288 *
|
|
289 * This example traverses through the whole queue and prints out each value.
|
|
290 * @code
|
|
291 * fprintf(stderr, "Queue: ");
|
|
292 * for(i=0;i<CircularQueueUnsignedInt_Size(xValueQueue);i++)
|
|
293 * {
|
|
294 * ret_val = CircularQueueUnsignedInt_ValueAtIndex(xValueQueue, i);
|
|
295 * fprintf(stderr, "%d ", ret_val);
|
|
296 *
|
|
297 * }
|
|
298 * fprintf(stderr, "\n");
|
5
|
299 * @endcode
|
1
|
300 *
|
|
301 * @note The implementation uses a modulo operation to compute the index, so
|
|
302 * this may not be the speediest operation in a tight loop.
|
|
303 * This implementation was not optimized for random access, though it still
|
|
304 * is technically O(1).
|
|
305 *
|
|
306 */
|
|
307 unsigned int CircularQueueUnsignedInt_ValueAtIndex(CircularQueueUnsignedInt* queue, unsigned int the_index);
|
0
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
308
|
1
|
309
|
|
310 /* This is a trick I picked up from Lua. Doing the typedef separately
|
|
311 * (and I guess before the definition) instead of a single
|
|
312 * entry: typedef struct {...} YourName; seems to allow me
|
|
313 * to use forward declarations. Doing it the other way (like SDL)
|
|
314 * seems to prevent me from using forward declarions as I get conflicting
|
|
315 * definition errors. I don't really understand why though.
|
|
316 */
|
|
317 typedef struct CircularQueueVoid CircularQueueVoid;
|
0
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
318 /**
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
319 * This is the essentially the CircularQueue object.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
320 * This contains all the data associated with a CircularQueue instance.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
321 * This version is for unsigned int data types. In the future, I suppose
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
322 * I could add a void* data type at the very least and maybe some
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
323 * other data types.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
324 * This should be considered an opaque data type.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
325 */
|
1
|
326 struct CircularQueueVoid
|
0
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
327 {
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
328 unsigned int maxSize; /**< Max size of the queue. */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
329 unsigned int currentSize; /**< Current number of entries in the queue. */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
330 unsigned int headIndex; /**< The index of where the current head is. */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
331 unsigned int tailIndex; /**< The index of where the current tail is. */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
332 void** internalQueue; /**< The array representing the queue. */
|
1
|
333 };
|
0
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
334
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
335 /**
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
336 * This creates a new CircularQueue (for void* ) instance.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
337 * This will create a new circular queue instance which holds
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
338 * unsigned int's in its queue.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
339 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
340 * @note This implementation does not allow a circular queue to be resized.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
341 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
342 * @param max_size This specifies the maximum number of elements the circular
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
343 * can hold.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
344 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
345 * @return Returns a pointer to a CircularQueue which is the
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
346 * instance variable (if successful) or NULL on failure.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
347 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
348 * @see CircularQueueVoid_FreeQueue
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
349 */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
350 CircularQueueVoid* CircularQueueVoid_CreateQueue(unsigned int max_size);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
351
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
352 /**
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
353 * This destroys a CircularQueue instance.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
354 * This will destroy the memory associated with the circular queue instance.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
355 * Whenever you create a CircularQueue instance, you should always to
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
356 * remember to balance it with a FreeQueue.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
357 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
358 * @param queue The pointer to the CircularQueue instance.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
359 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
360 * @see CircularQueueVoid_CreateQueue
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
361 */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
362 void CircularQueueVoid_FreeQueue(CircularQueueVoid* queue);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
363
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
364 /**
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
365 * This pushes a new value into the back of the queue.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
366 * If the queue is full, the function will fail and return 0.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
367 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
368 * @param queue The pointer to the CircularQueue instance.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
369 * @param value The value you want to push into the queue.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
370 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
371 * @return Returns 1 on success, or 0 on failure.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
372 */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
373 unsigned int CircularQueueVoid_PushBack(CircularQueueVoid* queue, void* value);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
374
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
375 /**
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
376 * This pushes a new value into the front of the queue.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
377 * If the queue is full, the function will fail and return 0.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
378 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
379 * @param queue The pointer to the CircularQueue instance.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
380 * @param value The value you want to push into the queue.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
381 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
382 * @return Returns 1 on success, or 0 on failure.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
383 */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
384 unsigned int CircularQueueVoid_PushFront(CircularQueueVoid* queue, void* value);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
385
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
386 /**
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
387 * This removes the value at the front of the queue.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
388 * If the queue is empty, the function will return 0.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
389 * Note that this function does not return the value popped, but
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
390 * an error flag. If you need the value, you must call Front()
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
391 * to retrieve the value before popping it.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
392 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
393 * @param queue The pointer to the CircularQueue instance.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
394 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
395 * @return Returns 1 on success, or 0 on failure.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
396 * @see CircularQueueVoid_Front
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
397 */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
398 unsigned int CircularQueueVoid_PopFront(CircularQueueVoid* queue);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
399
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
400 /**
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
401 * This removes the value at the back of the queue.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
402 * If the queue is empty, the function will return 0.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
403 * Note that this function does not return the value popped, but
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
404 * an error flag. If you need the value, you must call Back()
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
405 * to retrieve the value before popping it.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
406 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
407 * @param queue The pointer to the CircularQueue instance.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
408 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
409 * @return Returns 1 on success, or 0 on failure.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
410 * @see CircularQueueVoid_Back
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
411 */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
412 unsigned int CircularQueueVoid_PopBack(CircularQueueVoid* queue);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
413
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
414 /**
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
415 * This gets the value at the front of the queue.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
416 * If the queue is empty, the value returned will be 0.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
417 * Because this 0 return value is ambiguous because it could also could
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
418 * be a legitimate value in the queue, if you need more robust error
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
419 * checking for if the queue is empty, you should get the size of the
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
420 * queue using the Size() function.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
421 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
422 * @param queue The pointer to the CircularQueue instance.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
423 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
424 * @return Returns the value stored at the queue or 0 if the queue is empty
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
425 * (or if there is an error).
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
426 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
427 * @see CircularQueueVoid_Size
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
428 */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
429 void* CircularQueueVoid_Front(CircularQueueVoid* queue);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
430
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
431 /**
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
432 * This gets the value at the back of the queue.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
433 * If the queue is empty, the value returned will be 0.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
434 * Because this 0 return value is ambiguous because it could also could
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
435 * be a legitimate value in the queue, if you need more robust error
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
436 * checking for if the queue is empty, you should get the size of the
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
437 * queue using the Size() function.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
438 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
439 * @param queue The pointer to the CircularQueue instance.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
440 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
441 * @return Returns the value stored at the queue or 0 if the queue is empty
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
442 * (or 0 if there is an error).
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
443 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
444 * @see CircularQueueVoid_Size
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
445 */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
446 void* CircularQueueVoid_Back(CircularQueueVoid* queue);
|
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 * This gets the current number of entries that are in the queue.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
450 * This is number is not to be confused with the MaxSize.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
451 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
452 * @param queue The pointer to the CircularQueue instance.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
453 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
454 * @return Returns the number of entries currently in queue, or 0 if
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
455 * there is an error.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
456 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
457 */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
458 unsigned int CircularQueueVoid_Size(CircularQueueVoid* queue);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
459
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
460 /**
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
461 * This gets the maximum number of entries that are allowed in the queue at
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
462 * a given time.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
463 * This is the number that you used in the CreateQueue function.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
464 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
465 * @param queue The pointer to the CircularQueue instance.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
466 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
467 * @return Returns the maximum number of entries allowed in the queue.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
468 */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
469 unsigned int CircularQueueVoid_MaxSize(CircularQueueVoid* queue);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
470 /**
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
471 * This empties the entire queue.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
472 * This will remove all entries that are in the queue.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
473 * This does not destroy any memory. Use FreeQueue() to actually destroy
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
474 * the queue.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
475 *
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
476 * @param queue The pointer to the CircularQueue instance.
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
477 */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
478 void CircularQueueVoid_Clear(CircularQueueVoid* queue);
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
479
|
1
|
480 /**
|
|
481 * This is a debugging function that will print all the addresses
|
|
482 * of elements in the queue to stderr.
|
|
483 * This function is provided as convenience, but should not be considered
|
|
484 * as part of the standard API. Treat this function as deprecated
|
|
485 * as it's implementation may change or be removed entirely.
|
|
486 *
|
|
487 * @param queue The pointer to the CircularQueue instance.
|
|
488 */
|
0
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
489 void CircularQueueVoid_Print(CircularQueueVoid* queue);
|
1
|
490
|
|
491 /**
|
|
492 * This returns the element located at the specified index,
|
|
493 * where index=0 represents the head/front of the queue.
|
|
494 *
|
|
495 * @param queue The pointer to the CircularQueue instance.
|
|
496 * @param the_index The index of the element you want where 0 represents the
|
|
497 * head/front of the queue and Size-1 is the back.
|
|
498 *
|
|
499 * @return Returns the element located at the index on success, or NULL on failure.
|
|
500 * Be careful to not to confuse an error for a legitmate NULL value.
|
|
501 * Any index from 0 to Size-1 (where Size>0) will be a valid index.
|
|
502 *
|
|
503 * This example traverses through the whole queue and prints out each value.
|
|
504 * @code
|
|
505 * fprintf(stderr, "Queue: ");
|
|
506 * for(i=0;i<CircularQueueVoid_ValueAtIndex(xValueQueue);i++)
|
|
507 * {
|
|
508 * void* ret_val = CircularQueueUnsignedInt_ValueAtIndex(xValueQueue, i);
|
|
509 * fprintf(stderr, "%x ", ret_val);
|
|
510 *
|
|
511 * }
|
|
512 * fprintf(stderr, "\n");
|
|
513 *
|
|
514 * @note The implementation uses a modulo operation to compute the index, so
|
|
515 * this may not be the speediest operation in a tight loop.
|
|
516 * This implementation was not optimized for random access, though it still
|
|
517 * is technically O(1).
|
|
518 *
|
|
519 */
|
|
520 void* CircularQueueVoid_ValueAtIndex(CircularQueueVoid* queue, unsigned int the_index);
|
0
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
521
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
522
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
523
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
524
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
525 /* Ends C function definitions when using C++ */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
526 #ifdef __cplusplus
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
527 }
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
528 #endif
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
529
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
530
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
531
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
532 #endif /* C_CIRCULAR_QUEUE_H */
|
Eric Wing <ewing . public |-at-| gmail . com>
parents:
diff
changeset
|
533
|