cdcontainers  0.1.1
Library of data containers and collections for C programming language.
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.
30 #ifndef CDCONTAINERS_INCLUDE_CDCONTAINERS_VECTOR_H
31 #define CDCONTAINERS_INCLUDE_CDCONTAINERS_VECTOR_H
32 
33 #include <cdcontainers/common.h>
34 #include <cdcontainers/status.h>
35 
36 #include <assert.h>
37 #include <stdarg.h>
38 #include <stdbool.h>
39 
50 struct cdc_array {
51  size_t size;
52  size_t capacity;
53  void **buffer;
55 };
56 // Base
68 enum cdc_stat cdc_array_ctor(struct cdc_array **v, struct cdc_data_info *info);
69 
87 enum cdc_stat cdc_array_ctorl(struct cdc_array **v, struct cdc_data_info *info,
88  ...);
89 
97 enum cdc_stat cdc_array_ctorv(struct cdc_array **v, struct cdc_data_info *info,
98  va_list args);
99 
104 void cdc_array_dtor(struct cdc_array *v);
107 // Element access
118 static inline void *cdc_array_get(struct cdc_array *v, size_t index)
119 {
120  assert(v != NULL);
121  assert(index < v->size);
122 
123  return v->buffer[index];
124 }
125 
135 enum cdc_stat cdc_array_at(struct cdc_array *v, size_t index, void **elem);
136 
142 static inline void *cdc_array_front(struct cdc_array *v)
143 {
144  assert(v != NULL);
145  assert(v->size > 0);
146 
147  return v->buffer[0];
148 }
149 
155 static inline void *cdc_array_back(struct cdc_array *v)
156 {
157  assert(v != NULL);
158  assert(v->size > 0);
159 
160  return v->buffer[v->size - 1];
161 }
162 
168 static inline void **cdc_array_data(struct cdc_array *v)
169 {
170  assert(v != NULL);
171 
172  return v->buffer;
173 }
176 // Capacity
190 enum cdc_stat cdc_array_reserve(struct cdc_array *v, size_t capacity);
191 
197 static inline bool cdc_array_empty(struct cdc_array *v)
198 {
199  assert(v != NULL);
200 
201  return v->size == 0;
202 }
203 
209 static inline size_t cdc_array_size(struct cdc_array *v)
210 {
211  assert(v != NULL);
212 
213  return v->size;
214 }
215 
221 static inline size_t cdc_array_capacity(struct cdc_array *v)
222 {
223  assert(v != NULL);
224 
225  return v->capacity;
226 }
227 
237 // Modifiers
249 static inline void cdc_array_set(struct cdc_array *v, size_t index, void *value)
250 {
251  assert(v != NULL);
252  assert(index < v->size);
253 
254  v->buffer[index] = value;
255 }
256 
267 enum cdc_stat cdc_array_insert(struct cdc_array *v, size_t index, void *value);
268 
274 void cdc_array_erase(struct cdc_array *v, size_t index);
275 
280 void cdc_array_clear(struct cdc_array *v);
281 
289 enum cdc_stat cdc_array_push_back(struct cdc_array *v, void *value);
290 
295 static inline void cdc_array_pop_back(struct cdc_array *v)
296 {
297  assert(v != NULL);
298  assert(v->size > 0);
299 
300  cdc_array_erase(v, v->size - 1);
301 }
302 
311 enum cdc_stat cdc_array_append(struct cdc_array *v, void **data, size_t len);
312 
321  struct cdc_array *other);
322 
328 void cdc_array_swap(struct cdc_array *a, struct cdc_array *b);
331 // Short names
332 #ifdef CDC_USE_SHORT_NAMES
333 typedef struct cdc_array array_t;
334 
335 // Base
336 #define array_ctor(...) cdc_array_ctor(__VA_ARGS__)
337 #define array_ctorl(...) cdc_array_ctorl(__VA_ARGS__)
338 #define array_ctorv(...) cdc_array_ctorv(__VA_ARGS__)
339 #define array_dtor(...) cdc_array_dtor(__VA_ARGS__)
340 
341 // Element access
342 #define array_get(...) cdc_array_get(__VA_ARGS__)
343 #define array_at(...) cdc_array_at(__VA_ARGS__)
344 #define array_front(...) cdc_array_front(__VA_ARGS__)
345 #define array_back(...) cdc_array_back(__VA_ARGS__)
346 #define array_data(...) cdc_array_data(__VA_ARGS__)
347 
348 // Capacity
349 #define array_reserve(...) cdc_array_reserve(__VA_ARGS__)
350 #define array_empty(...) cdc_array_empty(__VA_ARGS__)
351 #define array_size(...) cdc_array_size(__VA_ARGS__)
352 #define array_capacity(...) cdc_array_capacity(__VA_ARGS__)
353 #define array_cap_exp(...) cdc_array_cap_exp(__VA_ARGS__)
354 #define array_shrink_to_fit(...) cdc_array_shrink_to_fit(__VA_ARGS__)
355 
356 // Modifiers
357 #define array_set(...) cdc_array_set(__VA_ARGS__)
358 #define array_insert(...) cdc_array_insert(__VA_ARGS__)
359 #define array_erase(...) cdc_array_erase(__VA_ARGS__)
360 #define array_clear(...) cdc_array_clear(__VA_ARGS__)
361 #define array_push_back(...) cdc_array_push_back(__VA_ARGS__)
362 #define array_pop_back(...) cdc_array_pop_back(__VA_ARGS__)
363 #define array_append(...) cdc_array_append(__VA_ARGS__)
364 #define array_append_move(...) cdc_array_append_move(__VA_ARGS__)
365 #define array_swap(...) cdc_array_swap(__VA_ARGS__)
366 #endif
367 
368 #endif // CDSTRUCTURES_INCLUDE_CDCONTAINERS_VECTOR_H
size_t size
Definition: array.h:51
enum cdc_stat cdc_array_ctor(struct cdc_array **v, struct cdc_data_info *info)
Constructs an empty array.
enum cdc_stat cdc_array_ctorl(struct cdc_array **v, struct cdc_data_info *info,...)
Constructs an array, initialized by an variable number of pointers. The last pointer must be CDC_END...
enum cdc_stat cdc_array_reserve(struct cdc_array *v, size_t capacity)
Attempts to allocate memory for at least size elements. If you know in advance how large the array wi...
enum cdc_stat cdc_array_insert(struct cdc_array *v, size_t index, void *value)
Inserts value at |index| position in the array. If index is 0, the value is prepended to the array...
void cdc_array_erase(struct cdc_array *v, size_t index)
Removes an element at index position in the arrray.
void cdc_array_swap(struct cdc_array *a, struct cdc_array *b)
Swaps arrays. This operation is very fast and never fails.
static void cdc_array_set(struct cdc_array *v, size_t index, void *value)
Sets an element at index position to the value. The function is not called to free memory...
Definition: array.h:249
The cdc_array is service struct.
Definition: array.h:50
static void cdc_array_pop_back(struct cdc_array *v)
Removes a last element in the array.
Definition: array.h:295
void cdc_array_clear(struct cdc_array *v)
Removes all the elements from the array.
static void * cdc_array_front(struct cdc_array *v)
Returns a first element in the array.
Definition: array.h:142
enum cdc_stat cdc_array_append_move(struct cdc_array *v, struct cdc_array *other)
Appends one array to the end of other array.
enum cdc_stat cdc_array_at(struct cdc_array *v, size_t index, void **elem)
Writes to pointer an element from specified position in the array. Bounds checking is performed...
static size_t cdc_array_capacity(struct cdc_array *v)
Returns the number of elements that the container has currently allocated space for.
Definition: array.h:221
void ** buffer
Definition: array.h:53
cdc_stat
Definition: status.h:24
enum cdc_stat cdc_array_append(struct cdc_array *v, void **data, size_t len)
Appends elements at the end of array.
enum cdc_stat cdc_array_shrink_to_fit(struct cdc_array *v)
Requests the container to reduce its capacity to fit its size.
enum cdc_stat cdc_array_ctorv(struct cdc_array **v, struct cdc_data_info *info, va_list args)
Constructs an array, initialized by args. The last pointer must be CDC_END.
static void ** cdc_array_data(struct cdc_array *v)
Returns a pointer to the data stored in the array.
Definition: array.h:168
struct cdc_data_info * dinfo
Definition: array.h:54
The cdc_data_info struct used to initialize contaners.
Definition: common.h:71
static size_t cdc_array_size(struct cdc_array *v)
Returns the number of elements in the array.
Definition: array.h:209
void cdc_array_dtor(struct cdc_array *v)
Destroys the array.
enum cdc_stat cdc_array_push_back(struct cdc_array *v, void *value)
Inserts an element at the end of the array.
size_t capacity
Definition: array.h:52
static bool cdc_array_empty(struct cdc_array *v)
Checks if the array has no elements.
Definition: array.h:197
static void * cdc_array_back(struct cdc_array *v)
Returns a last element in the array.
Definition: array.h:155
static void * cdc_array_get(struct cdc_array *v, size_t index)
Returns an element at index position in the array.
Definition: array.h:118