cdcontainers  0.1.1
Library of data containers and collections for C programming language.
circular-array.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.
27 #ifndef CDCONTAINERS_INCLUDE_CDCONTAINERS_CIRCULAR_ARRAY_H
28 #define CDCONTAINERS_INCLUDE_CDCONTAINERS_CIRCULAR_ARRAY_H
29 
30 #include <cdcontainers/common.h>
31 #include <cdcontainers/status.h>
32 
33 #include <assert.h>
34 #include <stdarg.h>
35 #include <stdbool.h>
36 #include <stdlib.h>
37 
49  void **buffer;
50  size_t head;
51  size_t tail;
52  size_t size;
53  size_t capacity;
55 };
56 
57 //Base
70  struct cdc_data_info *info);
71 
90  struct cdc_data_info *info, ...);
91 
100  struct cdc_data_info *info,
101  va_list args);
102 
110 // Element access
121 static inline void *cdc_circular_array_get(struct cdc_circular_array *d,
122  size_t index)
123 {
124  assert(d != NULL);
125  assert(index < d->size);
126 
127  size_t real_index = (d->head + index) & (d->capacity - 1);
128  return d->buffer[real_index];
129 }
130 
140 enum cdc_stat cdc_circular_array_at(struct cdc_circular_array *d, size_t index,
141  void **elem);
142 
148 static inline void *cdc_circular_array_front(struct cdc_circular_array *d)
149 {
150  assert(d != NULL);
151  assert(d->size > 0);
152 
153  return d->buffer[d->head];
154 }
155 
161 static inline void *cdc_circular_array_back(struct cdc_circular_array *d)
162 {
163  assert(d != NULL);
164  assert(d->size > 0);
165 
166  size_t real_index = (d->tail - 1 + d->capacity) & (d->capacity - 1);
167  return d->buffer[real_index];
168 }
171 // Capacity
181 static inline bool cdc_circular_array_empty(struct cdc_circular_array *d)
182 {
183  assert(d != NULL);
184 
185  return d->size == 0;
186 }
187 
193 static inline size_t cdc_circular_array_size(struct cdc_circular_array *d)
194 {
195  assert(d != NULL);
196 
197  return d->size;
198 }
201 // Modifiers
213 static inline void cdc_circular_array_set(struct cdc_circular_array *d,
214  size_t index, void *value)
215 {
216  assert(d != NULL);
217  assert(index < d->size);
218 
219  size_t real_index = (d->head + index) & (d->capacity - 1);
220  d->buffer[real_index] = value;
221 }
222 
234  size_t index, void *value);
235 
241 void cdc_circular_array_erase(struct cdc_circular_array *d, size_t index);
242 
248 
257  void *value);
258 
264 
273  void *value);
274 
280 
288  struct cdc_circular_array *b);
291 // Short names
292 #ifdef CDC_USE_SHORT_NAMES
293 typedef struct cdc_circular_array circular_array_t;
294 
295 // Base
296 #define circular_array_ctor(...) cdc_circular_array_ctor(__VA_ARGS__)
297 #define circular_array_ctorl(...) cdc_circular_array_ctorl(__VA_ARGS__)
298 #define circular_array_ctorv(...) cdc_circular_array_ctorv(__VA_ARGS__)
299 #define circular_array_dtor(...) cdc_circular_array_dtor(__VA_ARGS__)
300 
301 // Element access
302 #define circular_array_get(...) cdc_circular_array_get(__VA_ARGS__)
303 #define circular_array_at(...) cdc_circular_array_at(__VA_ARGS__)
304 #define circular_array_front(...) cdc_circular_array_front(__VA_ARGS__)
305 #define circular_array_back(...) cdc_circular_array_back(__VA_ARGS__)
306 
307 // Capacity
308 #define circular_array_empty(...) cdc_circular_array_empty(__VA_ARGS__)
309 #define circular_array_size(...) cdc_circular_array_size(__VA_ARGS__)
310 
311 // Modifiers
312 #define circular_array_set(...) cdc_circular_array_set(__VA_ARGS__)
313 #define circular_array_insert(...) cdc_circular_array_insert(__VA_ARGS__)
314 #define circular_array_erase(...) cdc_circular_array_erase(__VA_ARGS__)
315 #define circular_array_clear(...) cdc_circular_array_clear(__VA_ARGS__)
316 #define circular_array_push_back(...) cdc_circular_array_push_back(__VA_ARGS__)
317 #define circular_array_pop_back(...) cdc_circular_array_pop_back(__VA_ARGS__)
318 #define circular_array_push_front(...) \
319  cdc_circular_array_push_front(__VA_ARGS__)
320 #define circular_array_pop_front(...) cdc_circular_array_pop_back(__VA_ARGS__)
321 #define circular_array_swap(...) cdc_circular_array_swap(__VA_ARGS__)
322 #endif
323 
324 #endif // CDCONTAINERS_INCLUDE_CDCONTAINERS_CIRCULAR_ARRAY_H
void cdc_circular_array_clear(struct cdc_circular_array *d)
Removes all the elements from the circular array.
size_t head
Definition: circular-array.h:50
enum cdc_stat cdc_circular_array_insert(struct cdc_circular_array *d, size_t index, void *value)
Inserts value at |index| position in the circular array. If index is 0, the value is prepended to the...
static void * cdc_circular_array_front(struct cdc_circular_array *d)
Returns a first element in the circular array.
Definition: circular-array.h:148
static size_t cdc_circular_array_size(struct cdc_circular_array *d)
Returns the number of elements in the circular array.
Definition: circular-array.h:193
void cdc_circular_array_swap(struct cdc_circular_array *a, struct cdc_circular_array *b)
Swaps circular arrays a and b. This operation is very fast and never fails.
size_t tail
Definition: circular-array.h:51
enum cdc_stat cdc_circular_array_ctorv(struct cdc_circular_array **d, struct cdc_data_info *info, va_list args)
Constructs a circular array, initialized by args. The last pointer must be CDC_END.
enum cdc_stat cdc_circular_array_ctorl(struct cdc_circular_array **d, struct cdc_data_info *info,...)
Constructs a circular array, initialized by an variable number of pointers. The last pointer must be ...
void ** buffer
Definition: circular-array.h:49
static void * cdc_circular_array_get(struct cdc_circular_array *d, size_t index)
Returns an element at index position in the circular array.
Definition: circular-array.h:121
size_t size
Definition: circular-array.h:52
static bool cdc_circular_array_empty(struct cdc_circular_array *d)
Checks if the circular array has no elements.
Definition: circular-array.h:181
enum cdc_stat cdc_circular_array_push_front(struct cdc_circular_array *d, void *value)
Inserts an element at the beginning of the circular array.
static void * cdc_circular_array_back(struct cdc_circular_array *d)
Returns a last element in the circular array.
Definition: circular-array.h:161
enum cdc_stat cdc_circular_array_push_back(struct cdc_circular_array *d, void *value)
Inserts an element at the end of the circular array.
static void cdc_circular_array_set(struct cdc_circular_array *d, size_t index, void *value)
Sets an element at index position to the value. The function is not called to free memory...
Definition: circular-array.h:213
cdc_stat
Definition: status.h:24
void cdc_circular_array_pop_front(struct cdc_circular_array *d)
Removes a first element in the circular array.
void cdc_circular_array_pop_back(struct cdc_circular_array *d)
Removes a last element in the circular array.
struct cdc_data_info * dinfo
Definition: circular-array.h:54
size_t capacity
Definition: circular-array.h:53
The cdc_circular_array is service struct.
Definition: circular-array.h:48
void cdc_circular_array_dtor(struct cdc_circular_array *d)
Destroys the circular array.
void cdc_circular_array_erase(struct cdc_circular_array *d, size_t index)
Removes an element at index position in the circular arrray.
The cdc_data_info struct used to initialize contaners.
Definition: common.h:71
enum cdc_stat cdc_circular_array_at(struct cdc_circular_array *d, size_t index, void **elem)
Writes to pointer an element from specified position in the circular array. Bounds checking is perfor...
enum cdc_stat cdc_circular_array_ctor(struct cdc_circular_array **d, struct cdc_data_info *info)
Constructs an empty circular array.