ucx
UAP Common Extensions
Loading...
Searching...
No Matches
array_list.h
Go to the documentation of this file.
1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2021 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 */
37#ifndef UCX_ARRAY_LIST_H
38#define UCX_ARRAY_LIST_H
39
40#include "list.h"
41
42#ifdef __cplusplus
43extern "C" {
44#endif
45
51extern const unsigned cx_array_swap_sbo_size;
52
79#define CX_ARRAY_DECLARE_SIZED(type, name, size_type) \
80 type * name; \
81 size_type name##_size; \
82 size_type name##_capacity
83
108#define CX_ARRAY_DECLARE(type, name) CX_ARRAY_DECLARE_SIZED(type, name, size_t)
109
133#define cx_array_initialize(array, capacity) \
134 array##_capacity = capacity; \
135 array##_size = 0; \
136 array = malloc(sizeof(array[0]) * capacity)
137
160#define cx_array_initialize_a(allocator, array, capacity) \
161 array##_capacity = capacity; \
162 array##_size = 0; \
163 array = cxMalloc(allocator, sizeof(array[0]) * capacity)
164
189 void *(*realloc)(
190 void *array,
191 size_t capacity,
192 size_t elem_size,
193 struct cx_array_reallocator_s *alloc
194 );
195
199 void *ptr1;
203 void *ptr2;
207 size_t int1;
211 size_t int2;
212};
213
218
224
245 const struct cx_allocator_s *allocator,
246 const void *stackmem
247);
248
279cx_attr_nonnull_arg(1, 2, 3)
282 void **array,
283 void *size,
284 void *capacity,
285 unsigned width,
286 size_t elem_size,
287 size_t elem_count,
288 CxArrayReallocator *reallocator
289);
290
323cx_attr_nonnull_arg(1, 2, 3, 6)
326 void **target,
327 void *size,
328 void *capacity,
329 unsigned width,
330 size_t index,
331 const void *src,
332 size_t elem_size,
333 size_t elem_count,
334 CxArrayReallocator *reallocator
335);
336
351#define cx_array_simple_copy_a(reallocator, array, index, src, count) \
352 cx_array_copy((void**)&(array), &(array##_size), &(array##_capacity), \
353 sizeof(array##_size), index, src, sizeof((array)[0]), count, \
354 reallocator)
355
369#define cx_array_simple_copy(array, index, src, count) \
370 cx_array_simple_copy_a(NULL, array, index, src, count)
371
384#define cx_array_simple_reserve_a(reallocator, array, count) \
385 cx_array_reserve((void**)&(array), &(array##_size), &(array##_capacity), \
386 sizeof(array##_size), sizeof((array)[0]), count, \
387 reallocator)
388
400#define cx_array_simple_reserve(array, count) \
401 cx_array_simple_reserve_a(NULL, array, count)
402
426#define cx_array_add(target, size, capacity, elem_size, elem, reallocator) \
427 cx_array_copy((void**)(target), size, capacity, sizeof(*(size)), \
428 *(size), elem, elem_size, 1, reallocator)
429
442#define cx_array_simple_add_a(reallocator, array, elem) \
443 cx_array_simple_copy_a(reallocator, array, array##_size, &(elem), 1)
444
456#define cx_array_simple_add(array, elem) \
457 cx_array_simple_add_a(cx_array_default_reallocator, array, elem)
458
482cx_attr_nonnull_arg(1, 2, 3, 5)
485 void **target,
486 size_t *size,
487 size_t *capacity,
488 cx_compare_func cmp_func,
489 const void *src,
490 size_t elem_size,
491 size_t elem_count,
492 CxArrayReallocator *reallocator
493);
494
518#define cx_array_add_sorted(target, size, capacity, elem_size, elem, cmp_func, reallocator) \
519 cx_array_insert_sorted((void**)(target), size, capacity, cmp_func, elem, elem_size, 1, reallocator)
520
534#define cx_array_simple_add_sorted_a(reallocator, array, elem, cmp_func) \
535 cx_array_add_sorted(&array, &(array##_size), &(array##_capacity), \
536 sizeof((array)[0]), &(elem), cmp_func, reallocator)
537
550#define cx_array_simple_add_sorted(array, elem, cmp_func) \
551 cx_array_simple_add_sorted_a(NULL, array, elem, cmp_func)
552
567#define cx_array_simple_insert_sorted_a(reallocator, array, src, n, cmp_func) \
568 cx_array_insert_sorted((void**)(&array), &(array##_size), &(array##_capacity), \
569 cmp_func, src, sizeof((array)[0]), n, reallocator)
570
584#define cx_array_simple_insert_sorted(array, src, n, cmp_func) \
585 cx_array_simple_insert_sorted_a(NULL, array, src, n, cmp_func)
586
612 const void *arr,
613 size_t size,
614 size_t elem_size,
615 const void *elem,
616 cx_compare_func cmp_func
617);
618
638 const void *arr,
639 size_t size,
640 size_t elem_size,
641 const void *elem,
642 cx_compare_func cmp_func
643);
644
670 const void *arr,
671 size_t size,
672 size_t elem_size,
673 const void *elem,
674 cx_compare_func cmp_func
675);
676
688 void *arr,
689 size_t elem_size,
690 size_t idx1,
691 size_t idx2
692);
693
715 const CxAllocator *allocator,
716 cx_compare_func comparator,
717 size_t elem_size,
718 size_t initial_capacity
719);
720
736#define cxArrayListCreateSimple(elem_size, initial_capacity) \
737 cxArrayListCreate(NULL, NULL, elem_size, initial_capacity)
738
739#ifdef __cplusplus
740} // extern "C"
741#endif
742
743#endif // UCX_ARRAY_LIST_H
size_t cx_array_binary_search_inf(const void *arr, size_t size, size_t elem_size, const void *elem, cx_compare_func cmp_func)
Searches the largest lower bound in a sorted array.
int cx_array_insert_sorted(void **target, size_t *size, size_t *capacity, cx_compare_func cmp_func, const void *src, size_t elem_size, size_t elem_count, CxArrayReallocator *reallocator)
Inserts a sorted array into another sorted array.
const unsigned cx_array_swap_sbo_size
The maximum item size in an array list that fits into stack buffer when swapped.
CxArrayReallocator cx_array_reallocator(const struct cx_allocator_s *allocator, const void *stackmem)
Creates a new array reallocator.
int cx_array_reserve(void **array, void *size, void *capacity, unsigned width, size_t elem_size, size_t elem_count, CxArrayReallocator *reallocator)
Reserves memory for additional elements.
size_t cx_array_binary_search_sup(const void *arr, size_t size, size_t elem_size, const void *elem, cx_compare_func cmp_func)
Searches the smallest upper bound in a sorted array.
CxArrayReallocator * cx_array_default_reallocator
A default stdlib-based array reallocator.
size_t cx_array_binary_search(const void *arr, size_t size, size_t elem_size, const void *elem, cx_compare_func cmp_func)
Searches an item in a sorted array.
int cx_array_copy(void **target, void *size, void *capacity, unsigned width, size_t index, const void *src, size_t elem_size, size_t elem_count, CxArrayReallocator *reallocator)
Copies elements from one array to another.
CxList * cxArrayListCreate(const CxAllocator *allocator, cx_compare_func comparator, size_t elem_size, size_t initial_capacity)
Allocates an array list for storing elements with elem_size bytes each.
void cx_array_swap(void *arr, size_t elem_size, size_t idx1, size_t idx2)
Swaps two array elements.
#define cx_attr_export
Only used for building Windows DLLs.
Definition common.h:285
#define cx_attr_allocsize(...)
Specifies the parameters from which the allocation size is calculated.
Definition common.h:178
#define cx_attr_nonnull
All pointer arguments must be non-NULL.
Definition common.h:136
#define cx_attr_nodiscard
Warn about discarded return value.
Definition common.h:265
#define cx_attr_malloc
The attributed function always returns freshly allocated memory.
Definition common.h:151
#define cx_attr_nonnull_arg(...)
The specified pointer arguments must be non-NULL.
Definition common.h:141
#define cx_attr_dealloc(...)
Not supported in clang.
Definition common.h:167
int(* cx_compare_func)(const void *left, const void *right)
A comparator function comparing two arbitrary values.
Definition compare.h:60
Interface for list implementations.
void cxListFree(CxList *list)
Deallocates the memory of the specified list structure.
Structure holding the data for an allocator.
Definition allocator.h:84
Defines a reallocation mechanism for arrays.
Definition array_list.h:170
void * ptr1
Custom data pointer.
Definition array_list.h:199
size_t int1
Custom data integer.
Definition array_list.h:207
size_t int2
Custom data integer.
Definition array_list.h:211
void * ptr2
Custom data pointer.
Definition array_list.h:203
void *(* realloc)(void *array, size_t capacity, size_t elem_size, struct cx_array_reallocator_s *alloc)
Reallocates space for the given array.
Definition array_list.h:189
Structure for holding the base data of a list.
Definition list.h:54