cdcontainers  0.1.1
Library of data containers and collections for C programming language.
queue.h
Go to the documentation of this file.
1 // The MIT License (MIT)
2 // Copyright (c) 2017 Maksim Andrianov
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to
6 // deal in the Software without restriction, including without limitation the
7 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 // sell copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 // IN THE SOFTWARE.
26 #ifndef CDCONTAINERS_INCLUDE_CDCONTAINERS_QUEUE_H
27 #define CDCONTAINERS_INCLUDE_CDCONTAINERS_QUEUE_H
28 
29 #include <cdcontainers/common.h>
30 #include <cdcontainers/status.h>
32 
33 #include <assert.h>
34 #include <stdarg.h>
35 #include <stdbool.h>
36 #include <stdlib.h>
37 
43 struct cdc_queue {
44  void *container;
45  const struct cdc_sequence_table *table;
46 };
47 
57  struct cdc_queue **q, struct cdc_data_info *info);
58 
69  struct cdc_queue **q, struct cdc_data_info *info,
70  ...);
71 
82  struct cdc_queue **q, struct cdc_data_info *info,
83  va_list args);
84 
89 void cdc_queue_dtor(struct cdc_queue *q);
90 
91 // Element access
98 static inline void *cdc_queue_front(struct cdc_queue *q)
99 {
100  assert(q != NULL);
101 
102  return q->table->front(q->container);
103 }
104 
111 static inline void *cdc_queue_back(struct cdc_queue *q)
112 {
113  assert(q != NULL);
114 
115  return q->table->back(q->container);
116 }
117 
118 // Capacity
124 static inline bool cdc_queue_empty(struct cdc_queue *q)
125 {
126  assert(q != NULL);
127 
128  return q->table->empty(q->container);
129 }
130 
136 static inline size_t cdc_queue_size(struct cdc_queue *q)
137 {
138  assert(q != NULL);
139 
140  return q->table->size(q->container);
141 }
142 
143 // Modifiers
151 static inline enum cdc_stat cdc_queue_push(struct cdc_queue *q, void *elem)
152 {
153  assert(q != NULL);
154 
155  return q->table->push_back(q->container, elem);
156 }
157 
163 static void cdc_queue_pop(struct cdc_queue *q)
164 {
165  assert(q != NULL);
166 
167  q->table->pop_front(q->container);
168 }
169 
175 void cdc_queue_swap(struct cdc_queue *a, struct cdc_queue *b);
176 
177 // Short names
178 #ifdef CDC_USE_SHORT_NAMES
179 typedef struct cdc_queue queue_t;
180 
181 #define queue_ctor(...) cdc_queue_ctor(__VA_ARGS__)
182 #define queue_ctorl(...) cdc_queue_ctorl(__VA_ARGS__)
183 #define queue_ctorv(...) cdc_queue_ctorv(__VA_ARGS__)
184 #define queue_dtor(...) cdc_queue_dtor(__VA_ARGS__)
185 
186 // Element access
187 #define queue_front(...) cdc_queue_front(__VA_ARGS__)
188 #define queue_back(...) cdc_queue_back(__VA_ARGS__)
189 
190 // Capacity
191 #define queue_empty(...) cdc_queue_empty(__VA_ARGS__)
192 #define queue_size(...) cdc_queue_size(__VA_ARGS__)
193 
194 // Modifiers
195 #define queue_push(...) cdc_queue_push(__VA_ARGS__)
196 #define queue_pop(...) cdc_queue_pop(__VA_ARGS__)
197 #define queue_swap(...) cdc_queue_swap(__VA_ARGS__)
198 #endif
199 
200 #endif // CDCONTAINERS_INCLUDE_CDCONTAINERS_QUEUE_H
static size_t cdc_queue_size(struct cdc_queue *q)
Returns the number of items in the queue.
Definition: queue.h:136
void * container
Definition: queue.h:44
const struct cdc_sequence_table * table
Definition: queue.h:45
The cdc_queue struct.
Definition: queue.h:43
void *(* front)(void *cntr)
Definition: isequence.h:45
static void * cdc_queue_back(struct cdc_queue *q)
Returns pointer to the last element in the queue. This function assumes that the queue isn&#39;t empty...
Definition: queue.h:111
static void * cdc_queue_front(struct cdc_queue *q)
Returns pointer to the first element in the queue. This function assumes that the queue isn&#39;t empty...
Definition: queue.h:98
enum cdc_stat cdc_queue_ctorl(const struct cdc_sequence_table *table, struct cdc_queue **q, struct cdc_data_info *info,...)
Constructs a queue, initialized by an arbitrary number of pointers. The last item must be NULL...
void *(* back)(void *cntr)
Definition: isequence.h:46
The cdc_sequence_table is a sequence container interface.
bool(* empty)(void *cntr)
Definition: isequence.h:47
cdc_stat
Definition: status.h:24
static void cdc_queue_pop(struct cdc_queue *q)
Removes the head item in the queue. This function assumes that the queue isn&#39;t empty.
Definition: queue.h:163
size_t(* size)(void *cntr)
Definition: isequence.h:48
void cdc_queue_swap(struct cdc_queue *a, struct cdc_queue *b)
Swaps queues a and b. This operation is very fast and never fails.
enum cdc_stat(* push_back)(void *cntr, void *elem)
Definition: isequence.h:49
void(* pop_front)(void *cntr)
Definition: isequence.h:52
The cdc_data_info struct used to initialize contaners.
Definition: common.h:71
enum cdc_stat cdc_queue_ctor(const struct cdc_sequence_table *table, struct cdc_queue **q, struct cdc_data_info *info)
Constructs an empty queue.
static bool cdc_queue_empty(struct cdc_queue *q)
Returns true if the queue has size 0; otherwise returns false.
Definition: queue.h:124
enum cdc_stat cdc_queue_ctorv(const struct cdc_sequence_table *table, struct cdc_queue **q, struct cdc_data_info *info, va_list args)
Constructs a queue, initialized by args The last item must be NULL.
void cdc_queue_dtor(struct cdc_queue *q)
Destroys the queue.
static enum cdc_stat cdc_queue_push(struct cdc_queue *q, void *elem)
Adds value elem to the tail of the queue.
Definition: queue.h:151
The cdc_sequence_table struct.
Definition: isequence.h:41