universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: ucx: /home/mike/workspace/c/ucx/src/ucx/array.h Source File universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390:
universe@390:
universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390:
universe@390:
ucx universe@390:
universe@390:
UAP Common Extensions
universe@390:
universe@390:
universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390:
universe@390:
universe@390: universe@390: universe@390:
universe@390: universe@390:
universe@390: universe@390: universe@390:
universe@390:
universe@390:
universe@390:
array.h
universe@390:
universe@390:
universe@390: Go to the documentation of this file.
1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3  *
4  * Copyright 2019 Mike Becker, Olaf Wintermann All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
36 #ifndef UCX_ARRAY_H
37 #define UCX_ARRAY_H
38 
39 #include "ucx.h"
40 #include "allocator.h"
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
49 typedef struct {
53  size_t capacity;
57  size_t size;
61  size_t elemsize;
65  void* data;
70 } UcxArray;
71 
89 #define ucx_array_util_set(array, capacity, elmsize, idx, data) \
90  ucx_array_util_set_a(ucx_default_allocator(), (void**)(array), capacity, \
91  elmsize, idx, data)
92 
112 int ucx_array_util_set_a(UcxAllocator* alloc, void** array, size_t* capacity,
113  size_t elmsize, size_t idx, void* data);
114 
131 #define ucx_array_util_setptr(array, capacity, idx, ptr) \
132  ucx_array_util_setptr_a(ucx_default_allocator(), (void**)(array), \
133  capacity, idx, ptr)
134 
153 int ucx_array_util_setptr_a(UcxAllocator* alloc, void** array, size_t* capacity,
154  size_t idx, void* ptr);
155 
156 
163 UcxArray* ucx_array_new(size_t capacity, size_t elemsize);
164 
173 UcxArray* ucx_array_new_a(size_t capacity, size_t elemsize,
174  UcxAllocator* allocator);
175 
184 void ucx_array_init(UcxArray* array, size_t capacity, size_t elemsize);
185 
195 void ucx_array_init_a(UcxArray* array, size_t capacity, size_t elemsize,
196  UcxAllocator* allocator);
197 
211 int ucx_array_clone(UcxArray* dest, UcxArray const* src);
212 
213 
230 int ucx_array_equals(UcxArray const *array1, UcxArray const *array2,
231  cmp_func cmpfnc, void* data);
232 
242 void ucx_array_destroy(UcxArray *array);
243 
249 void ucx_array_free(UcxArray *array);
250 
267 int ucx_array_append_from(UcxArray *array, void *data, size_t count);
268 
269 
286 int ucx_array_prepend_from(UcxArray *array, void *data, size_t count);
287 
288 
304 int ucx_array_set_from(UcxArray *array, size_t index, void *data, size_t count);
305 
320 int ucx_array_concat(UcxArray *array1, const UcxArray *array2);
321 
330 void *ucx_array_at(UcxArray const* array, size_t index);
331 
349 size_t ucx_array_find(UcxArray const *array, void *elem,
350  cmp_func cmpfnc, void *data);
351 
364 int ucx_array_contains(UcxArray const *array, void *elem,
365  cmp_func cmpfnc, void *data);
366 
381 void ucx_array_sort(UcxArray* array, cmp_func cmpfnc, void *data);
382 
393 void ucx_array_remove(UcxArray *array, size_t index);
394 
405 void ucx_array_remove_fast(UcxArray *array, size_t index);
406 
415 int ucx_array_shrink(UcxArray* array);
416 
427 int ucx_array_resize(UcxArray* array, size_t capacity);
428 
439 int ucx_array_reserve(UcxArray* array, size_t capacity);
440 
452 int ucx_array_grow(UcxArray* array, size_t count);
453 
454 
455 #ifdef __cplusplus
456 }
457 #endif
458 
459 #endif /* UCX_ARRAY_H */
460 
int ucx_array_append_from(UcxArray *array, void *data, size_t count)
Inserts elements at the end of the array.
Definition: array.c:206
universe@390:
int ucx_array_prepend_from(UcxArray *array, void *data, size_t count)
Inserts elements at the beginning of the array.
Definition: array.c:221
universe@390:
int ucx_array_grow(UcxArray *array, size_t count)
Resizes the capacity, if the specified number of elements would not fit.
Definition: array.c:465
universe@390:
int(* cmp_func)(const void *, const void *, void *)
Function pointer to a compare function.
Definition: ucx.h:84
universe@390:
int ucx_array_shrink(UcxArray *array)
Shrinks the memory to exactly fit the contents.
Definition: array.c:418
universe@390:
int ucx_array_concat(UcxArray *array1, const UcxArray *array2)
Concatenates two arrays.
Definition: array.c:259
universe@390:
Main UCX Header providing most common definitions.
universe@390:
size_t capacity
The current capacity of the array.
Definition: array.h:53
universe@390:
int ucx_array_util_set_a(UcxAllocator *alloc, void **array, size_t *capacity, size_t elmsize, size_t idx, void *data)
Sets an element in an arbitrary user defined array.
Definition: array.c:72
universe@390:
int ucx_array_equals(UcxArray const *array1, UcxArray const *array2, cmp_func cmpfnc, void *data)
Compares two UCX arrays element-wise by using a compare function.
Definition: array.c:166
universe@390:
void ucx_array_init_a(UcxArray *array, size_t capacity, size_t elemsize, UcxAllocator *allocator)
Initializes a UCX array structure using the specified allocator.
Definition: array.c:136
universe@390:
void ucx_array_sort(UcxArray *array, cmp_func cmpfnc, void *data)
Sorts a UcxArray with the best available sort algorithm.
Definition: array.c:395
universe@390:
void * data
A pointer to the data.
Definition: array.h:65
universe@390:
UcxArray * ucx_array_new(size_t capacity, size_t elemsize)
Creates a new UCX array with the given capacity and element size.
Definition: array.c:119
universe@390:
void ucx_array_remove(UcxArray *array, size_t index)
Removes an element from the array.
Definition: array.c:400
universe@390:
UcxAllocator * allocator
The allocator used for the data.
Definition: array.h:69
universe@390:
int ucx_array_set_from(UcxArray *array, size_t index, void *data, size_t count)
Sets elements starting at the specified index.
Definition: array.c:240
universe@390:
void ucx_array_remove_fast(UcxArray *array, size_t index)
Removes an element from the array.
Definition: array.c:409
universe@390:
UCX allocator data structure containing memory management functions.
Definition: allocator.h:88
universe@390:
int ucx_array_contains(UcxArray const *array, void *elem, cmp_func cmpfnc, void *data)
Checks, if an array contains a specific element.
Definition: array.c:309
universe@390:
size_t ucx_array_find(UcxArray const *array, void *elem, cmp_func cmpfnc, void *data)
Returns the index of an element containing the specified data.
Definition: array.c:286
universe@390:
UCX array type.
Definition: array.h:49
universe@390:
size_t size
The actual number of elements in the array.
Definition: array.h:57
universe@390:
void ucx_array_destroy(UcxArray *array)
Destroys the array.
Definition: array.c:194
universe@390:
int ucx_array_util_setptr_a(UcxAllocator *alloc, void **array, size_t *capacity, size_t idx, void *ptr)
Stores a pointer in an arbitrary user defined array.
Definition: array.c:112
universe@390:
Allocator for custom memory management.
universe@390:
void ucx_array_free(UcxArray *array)
Destroys and frees the array.
Definition: array.c:201
universe@390:
int ucx_array_clone(UcxArray *dest, UcxArray const *src)
Creates an shallow copy of an array.
Definition: array.c:151
universe@390:
void * ucx_array_at(UcxArray const *array, size_t index)
Returns a pointer to the array element at the specified index.
Definition: array.c:280
universe@390:
int ucx_array_resize(UcxArray *array, size_t capacity)
Sets the capacity of the array.
Definition: array.c:430
universe@390:
void ucx_array_init(UcxArray *array, size_t capacity, size_t elemsize)
Initializes a UCX array structure with the given capacity and element size.
Definition: array.c:132
universe@390:
size_t elemsize
The size of an individual element in bytes.
Definition: array.h:61
universe@390:
int ucx_array_reserve(UcxArray *array, size_t capacity)
Resizes the array only, if the capacity is insufficient.
Definition: array.c:449
universe@390:
UcxArray * ucx_array_new_a(size_t capacity, size_t elemsize, UcxAllocator *allocator)
Creates a new UCX array using the specified allocator.
Definition: array.c:123
universe@390:
universe@390: universe@390: universe@390: universe@390: