Vector 0.0.2
Loading...
Searching...
No Matches
vector.h
Go to the documentation of this file.
1
7#ifndef _VECTOR_H_
8#define _VECTOR_H_
9
10#include <stdbool.h> /* bool, true, false */
11#include <stddef.h> /* size_t */
12#include <sys/types.h> /* ssize_t */
13
20#define TMP_REF(type, value) (type[1]){value}
21
29typedef struct vector_t vector_t;
30
34typedef struct alloc_opts_t
35{
36 size_t size;
39 void *data;
43}
45
51typedef struct vector_opts_t
52{
55 /* required: */
56 size_t element_size;
58 /* optional: */
59 size_t initial_cap;
60}
62
74
78#define VECTOR_DEFAULT_ARGS \
79 .initial_cap = 10
80
97typedef bool (*predicate_t) (const void *const element, void *const param);
98
108typedef ssize_t (*compare_t) (const void *const value, const void *const element, void *const param);
109
120typedef int (*foreach_t) (const void *const element, void *const param);
121
133typedef int (*aggregate_t) (const void *const element, void *const acc, void *const param);
134
145typedef int (*transform_t) (void *const element, void *const param);
153#define alloc_opts(...) (alloc_opts_t){__VA_ARGS__}
154
162#define vector_create(...) \
163 vector_create_( \
164 &(vector_opts_t) { \
165 VECTOR_DEFAULT_ARGS,\
166 __VA_ARGS__ \
167 }\
168 )\
169
180vector_t *vector_create_(const vector_opts_t *const opts);
181
182
190void vector_destroy(vector_t *const vector);
191
192
200vector_t *vector_clone(const vector_t *const vector);
201
202
215vector_status_t vector_resize(vector_t **const vector, const size_t capacity, const vector_status_t error);
216
232void* vector_get_ext_header(const vector_t *const vector);
233
234
241size_t vector_ext_header_size(const vector_t *const vector);
242
243
250size_t vector_data_offset(const vector_t *const vector);
251
264alloc_opts_t vector_alloc_opts(const vector_t *const vector);
265
266
273size_t vector_element_size(const vector_t *const vector);
274
275
282size_t vector_capacity(const vector_t *const vector);
283
284
291size_t vector_capacity_bytes(const vector_t *const vector);
292
311void *vector_linear_find(const vector_t *const vector,
312 const size_t limit,
313 const predicate_t predicate,
314 void *const param);
315
316
328void *vector_binary_find(const vector_t *const vector,
329 const void *const value,
330 const size_t limit,
331 const compare_t cmp,
332 void *const param);
333
334
346ssize_t vector_binary_find_index(const vector_t *const vector,
347 const void *const value,
348 const size_t limit,
349 const compare_t cmp,
350 void *const param);
351
366char *vector_data(const vector_t *const vector);
367
368
376void *vector_get(const vector_t *const vector, const size_t index);
377
378
386void vector_set(vector_t *const vector, const size_t index, const void *const value);
387
388
395void vector_set_zero(vector_t *const vector, const size_t index);
396
397
408void vector_copy(const vector_t *const vector,
409 char *dest,
410 const size_t offset,
411 const size_t length);
412
413
423void vector_move(const vector_t *const vector,
424 char *dest,
425 const size_t offset,
426 const size_t length);
427
428
443void vector_part_copy(const vector_t *const vector,
444 char *dest,
445 const size_t offset,
446 const size_t length,
447 const size_t part_offset,
448 const size_t part_length);
449
450
463void vector_spread(vector_t *const vector, const size_t index, const size_t amount);
464
465
477void vector_shift(vector_t *const vector,
478 const size_t offset,
479 const size_t length,
480 const ssize_t shift);
481
482
491void vector_swap(vector_t *const vector,
492 const size_t index_a,
493 const size_t index_b);
494
495
506int vector_foreach(const vector_t *const vector,
507 const size_t limit,
508 const foreach_t func,
509 void *const param);
510
511
524int vector_aggregate(const vector_t *const vector,
525 const size_t limit,
526 const aggregate_t func,
527 void *const acc,
528 void *const param);
529
530
541int vector_transform(vector_t *const vector,
542 const size_t limit,
543 const transform_t func,
544 void *const param);
545
563void *vector_alloc(const size_t alloc_size, void *const param);
564
565
575void *vector_realloc(void *ptr, const size_t alloc_size, void *const param);
576
577
584void vector_free(void *ptr, void *const param);
585
599size_t calc_aligned_size(const size_t size, const size_t alignment);
600
606ssize_t cmp_lex_asc(const void *const value, const void *const element, void *const param);
607
608
614ssize_t cmp_lex_dsc(const void *const value, const void *const element, void *const param);
615
619#endif/*_VECTOR_H_*/
void * vector_realloc(void *ptr, const size_t alloc_size, void *const param)
Reallocates already allocated memory chunk in order to change allocation size.
void * vector_alloc(const size_t alloc_size, void *const param)
Allocates memory chunk of alloc_size.
void vector_free(void *ptr, void *const param)
Free allocation that was previously allocated.
int(* foreach_t)(const void *const element, void *const param)
Callback determines an operation for vector_foreach.
Definition vector.h:120
int(* transform_t)(void *const element, void *const param)
Callback determines an operation for vector_transform.
Definition vector.h:145
bool(* predicate_t)(const void *const element, void *const param)
Predicate, tells if traversed element matches user's criteria.
Definition vector.h:97
int(* aggregate_t)(const void *const element, void *const acc, void *const param)
Callback determines an operation for vector_aggregate.
Definition vector.h:133
ssize_t(* compare_t)(const void *const value, const void *const element, void *const param)
Compare, used to define traversal order.
Definition vector.h:108
int vector_transform(vector_t *const vector, const size_t limit, const transform_t func, void *const param)
Perform mutable transformation on each element of the vector.
Definition vector.c:414
int vector_aggregate(const vector_t *const vector, const size_t limit, const aggregate_t func, void *const acc, void *const param)
Perform immutable accamulating action on each element of the vector.
Definition vector.c:398
void vector_shift(vector_t *const vector, const size_t offset, const size_t length, const ssize_t shift)
Shift range of elements.
Definition vector.c:358
void vector_part_copy(const vector_t *const vector, char *dest, const size_t offset, const size_t length, const size_t part_offset, const size_t part_length)
Partial copying.
Definition vector.c:316
void vector_spread(vector_t *const vector, const size_t index, const size_t amount)
Duplicates existing element across range.
Definition vector.c:334
void * vector_get(const vector_t *const vector, const size_t index)
Returns pointer for the element at index.
Definition vector.c:271
int vector_foreach(const vector_t *const vector, const size_t limit, const foreach_t func, void *const param)
Perform immutable action on each element of the vector.
Definition vector.c:382
void vector_move(const vector_t *const vector, char *dest, const size_t offset, const size_t length)
Moves range of the vector elements to another location.
Definition vector.c:306
void vector_set(vector_t *const vector, const size_t index, const void *const value)
Sets element at given index to a value.
Definition vector.c:280
void vector_copy(const vector_t *const vector, char *dest, const size_t offset, const size_t length)
Copy element range to other location.
Definition vector.c:296
char * vector_data(const vector_t *const vector)
Gives a pointer to a location where elements' data begins.
Definition vector.c:264
void vector_swap(vector_t *const vector, const size_t index_a, const size_t index_b)
Swaps values of elements designated by indicies.
Definition vector.c:370
void vector_set_zero(vector_t *const vector, const size_t index)
Sets element at given index to a zero value.
Definition vector.c:288
size_t vector_data_offset(const vector_t *const vector)
Compute offset from vector_t::memory to first element.
Definition vector.c:177
size_t vector_ext_header_size(const vector_t *const vector)
Retrieves extended header size.
Definition vector.c:170
void * vector_get_ext_header(const vector_t *const vector)
Provides a location where user can put a header for the derived class.
Definition vector.c:162
vector_status_t vector_resize(vector_t **const vector, const size_t capacity, const vector_status_t error)
Performs allocation resize.
Definition vector.c:141
vector_t * vector_clone(const vector_t *const vector)
Duplicates a vector.
Definition vector.c:119
void vector_destroy(vector_t *const vector)
Deallocates vector.
Definition vector.c:112
vector_t * vector_create_(const vector_opts_t *const opts)
Vector contructor.
Definition vector.c:82
size_t vector_capacity_bytes(const vector_t *const vector)
Reports current capacity of the vector in bytes.
Definition vector.c:208
size_t vector_element_size(const vector_t *const vector)
Reports current element size.
Definition vector.c:194
size_t vector_capacity(const vector_t *const vector)
Reports current capacity of the vector.
Definition vector.c:201
alloc_opts_t vector_alloc_opts(const vector_t *const vector)
Access allocator options.
Definition vector.c:184
void * vector_linear_find(const vector_t *const vector, const size_t limit, const predicate_t predicate, void *const param)
Simple linear search for unordered data.
Definition vector.c:215
void * vector_binary_find(const vector_t *const vector, const void *const value, const size_t limit, const compare_t cmp, void *const param)
Run binary search on the vector.
Definition vector.c:234
ssize_t vector_binary_find_index(const vector_t *const vector, const void *const value, const size_t limit, const compare_t cmp, void *const param)
Run binary search on the vector.
Definition vector.c:249
ssize_t cmp_lex_asc(const void *const value, const void *const element, void *const param)
Performs comparison in lexicographical ascending order.
Definition vector.c:451
size_t calc_aligned_size(const size_t size, const size_t alignment)
Function calculates size of the element while respecting requirement for alignment.
Definition vector.c:445
ssize_t cmp_lex_dsc(const void *const value, const void *const element, void *const param)
Performs comparison in lexicographical descending order.
Definition vector.c:457
Allocator options.
Definition vector.h:35
size_t size
Size of the allocator data.
Definition vector.h:36
void * data
User defined allocator structure.
Definition vector.h:39
Vector options.
Definition vector.h:52
size_t element_size
Size of the underling element type.
Definition vector.h:56
alloc_opts_t alloc_opts
optional allocator
Definition vector.h:53
size_t initial_cap
Amount of elements that will be preallocated.
Definition vector.h:59
size_t ext_header_size
Size of the extention header.
Definition vector.h:54
Vector control structure type.
Definition vector.c:24
vector_status_t
Status enum that indicates errors of operations that may fail.
Definition vector.h:68
@ VECTOR_SUCCESS
Definition vector.h:69
@ VECTOR_ALLOC_ERROR
Definition vector.h:70
@ VECTOR_STATUS_LAST
Definition vector.h:71