2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2019 Mike Becker, Olaf Wintermann All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
29 #define _GNU_SOURCE /* we want to use qsort_r(), if available */
30 #define __STDC_WANT_LIB_EXT1__ 1 /* use qsort_s, if available */
33 #include "ucx/array.h"
34 #include "ucx/utils.h"
40 #ifndef UCX_ARRAY_DISABLE_QSORT
42 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 8)
43 #define ucx_array_sort_impl qsort_r
44 #endif /* glibc version >= 2.8 */
45 #elif /* not __GLIBC__ */ defined(__APPLE__) || defined(__FreeBSD__)
46 #define ucx_array_sort_impl ucx_qsort_r
47 #define USE_UCX_QSORT_R
48 #elif /* not (__APPLE || __FreeBSD__) */ defined(__sun)
49 #if __STDC_VERSION__ >= 201112L
50 #define ucx_array_sort_impl qsort_s
52 #endif /* __GLIBC__, __APLE__, __FreeBSD__, __sun */
53 #endif /* UCX_ARRAY_DISABLE_QSORT */
55 #ifndef ucx_array_sort_impl
56 #define ucx_array_sort_impl ucx_mergesort
59 static int ucx_array_ensurecap(UcxArray *array, size_t reqcap) {
60 size_t required_capacity = array->capacity;
61 while (reqcap > required_capacity) {
62 if (required_capacity * 2 < required_capacity)
64 required_capacity <<= 1;
66 if (ucx_array_reserve(array, required_capacity)) {
72 int ucx_array_util_set_a(UcxAllocator* alloc, void** array, size_t* capacity,
73 size_t elmsize, size_t index, void* data) {
75 if(!alloc || !capacity || !array) {
80 size_t newcapacity = *capacity;
81 while(index >= newcapacity) {
82 if(ucx_szmul(newcapacity, 2, &newcapacity)) {
88 size_t memlen, offset;
89 if(ucx_szmul(newcapacity, elmsize, &memlen)) {
93 /* we don't need to check index*elmsize - it is smaller than memlen */
96 void* newptr = alrealloc(alloc, *array, memlen);
98 errno = ENOMEM; /* we cannot assume that every allocator sets this */
102 *capacity = newcapacity;
106 dest += elmsize*index;
107 memcpy(dest, data, elmsize);
112 int ucx_array_util_setptr_a(UcxAllocator* alloc, void** array, size_t* capacity,
113 size_t index, void* data) {
115 return ucx_array_util_set_a(alloc, array, capacity, sizeof(void*),
119 UcxArray* ucx_array_new(size_t capacity, size_t elemsize) {
120 return ucx_array_new_a(capacity, elemsize, ucx_default_allocator());
123 UcxArray* ucx_array_new_a(size_t capacity, size_t elemsize,
124 UcxAllocator* allocator) {
125 UcxArray* array = almalloc(allocator, sizeof(UcxArray));
127 ucx_array_init_a(array, capacity, elemsize, allocator);
132 void ucx_array_init(UcxArray* array, size_t capacity, size_t elemsize) {
133 ucx_array_init_a(array, capacity, elemsize, ucx_default_allocator());
136 void ucx_array_init_a(UcxArray* array, size_t capacity, size_t elemsize,
137 UcxAllocator* allocator) {
139 array->allocator = allocator;
140 array->elemsize = elemsize;
142 array->data = alcalloc(allocator, capacity, elemsize);
145 array->capacity = capacity;
151 int ucx_array_clone(UcxArray* dest, UcxArray const* src) {
152 if (ucx_array_ensurecap(dest, src->capacity)) {
156 dest->elemsize = src->elemsize;
157 dest->size = src->size;
160 memcpy(dest->data, src->data, src->size*src->elemsize);
166 int ucx_array_equals(UcxArray const *array1, UcxArray const *array2,
167 cmp_func cmpfnc, void* data) {
169 if (array1->size != array2->size || array1->elemsize != array2->elemsize) {
172 if (array1->size == 0)
176 if (cmpfnc == NULL) {
177 cmpfnc = ucx_cmp_mem;
178 elemsize = array1->elemsize;
182 for (size_t i = 0 ; i < array1->size ; i++) {
184 ucx_array_at(array1, i),
185 ucx_array_at(array2, i),
194 void ucx_array_destroy(UcxArray *array) {
196 alfree(array->allocator, array->data);
198 array->capacity = array->size = 0;
201 void ucx_array_free(UcxArray *array) {
202 ucx_array_destroy(array);
203 alfree(array->allocator, array);
206 int ucx_array_append_from(UcxArray *array, void *data, size_t count) {
207 if (ucx_array_ensurecap(array, array->size + count))
210 void* dest = ucx_array_at(array, array->size);
212 memcpy(dest, data, array->elemsize*count);
214 memset(dest, 0, array->elemsize*count);
216 array->size += count;
221 int ucx_array_prepend_from(UcxArray *array, void *data, size_t count) {
222 if (ucx_array_ensurecap(array, array->size + count))
225 if (array->size > 0) {
226 void *dest = ucx_array_at(array, count);
227 memmove(dest, array->data, array->elemsize*array->size);
231 memcpy(array->data, data, array->elemsize*count);
233 memset(array->data, 0, array->elemsize*count);
235 array->size += count;
240 int ucx_array_set_from(UcxArray *array, size_t index,
241 void *data, size_t count) {
242 if (ucx_array_ensurecap(array, index + count))
245 if (index+count > array->size) {
246 array->size = index+count;
249 void *dest = ucx_array_at(array, index);
251 memcpy(dest, data, array->elemsize*count);
253 memset(dest, 0, array->elemsize*count);
259 int ucx_array_concat(UcxArray *array1, const UcxArray *array2) {
261 if (array1->elemsize != array2->elemsize)
264 size_t capacity = array1->capacity+array2->capacity;
266 if (array1->capacity < capacity) {
267 if (ucx_array_reserve(array1, capacity)) {
272 void* dest = ucx_array_at(array1, array1->size);
273 memcpy(dest, array2->data, array2->size*array2->elemsize);
275 array1->size += array2->size;
280 void *ucx_array_at(UcxArray const *array, size_t index) {
281 char* memory = array->data;
282 char* loc = memory + index*array->elemsize;
286 size_t ucx_array_find(UcxArray const *array, void *elem,
287 cmp_func cmpfnc, void *data) {
290 if (cmpfnc == NULL) {
291 cmpfnc = ucx_cmp_mem;
292 elemsize = array->elemsize;
296 if (array->size > 0) {
297 for (size_t i = 0 ; i < array->size ; i++) {
298 void* ptr = ucx_array_at(array, i);
299 if (cmpfnc(ptr, elem, data) == 0) {
309 int ucx_array_contains(UcxArray const *array, void *elem,
310 cmp_func cmpfnc, void *data) {
311 return ucx_array_find(array, elem, cmpfnc, data) != array->size;
314 static void ucx_mergesort_merge(void *arrdata,size_t elemsize,
315 cmp_func cmpfnc, void *data,
316 size_t start, size_t mid, size_t end) {
318 char* array = arrdata;
320 size_t rightstart = mid + 1;
322 if (cmpfnc(array + mid*elemsize,
323 array + rightstart*elemsize, data) <= 0) {
328 /* we need memory for one element */
329 void *value = malloc(elemsize);
331 while (start <= mid && rightstart <= end) {
332 if (cmpfnc(array + start*elemsize,
333 array + rightstart*elemsize, data) <= 0) {
336 /* save the value from the right */
337 memcpy(value, array + rightstart*elemsize, elemsize);
339 /* shift all left elements one element to the right */
340 size_t shiftcount = rightstart-start;
341 void *startptr = array + start*elemsize;
342 void *dest = array + (start+1)*elemsize;
343 memmove(dest, startptr, shiftcount*elemsize);
345 /* bring the first value from the right to the left */
346 memcpy(startptr, value, elemsize);
354 /* free the temporary memory */
358 static void ucx_mergesort_impl(void *arrdata, size_t elemsize,
359 cmp_func cmpfnc, void *data, size_t l, size_t r) {
361 size_t m = l + (r - l) / 2;
363 ucx_mergesort_impl(arrdata, elemsize, cmpfnc, data, l, m);
364 ucx_mergesort_impl(arrdata, elemsize, cmpfnc, data, m + 1, r);
365 ucx_mergesort_merge(arrdata, elemsize, cmpfnc, data, l, m, r);
369 static void ucx_mergesort(void *arrdata, size_t count, size_t elemsize,
370 cmp_func cmpfnc, void *data) {
372 ucx_mergesort_impl(arrdata, elemsize, cmpfnc, data, 0, count-1);
375 #ifdef USE_UCX_QSORT_R
376 struct cmpfnc_swapargs_info {
381 static int cmp_func_swap_args(void *data, const void *x, const void *y) {
382 struct cmpfnc_swapargs_info* info = data;
383 return info->func(x, y, info->data);
386 static void ucx_qsort_r(void *array, size_t count, size_t elemsize,
387 cmp_func cmpfnc, void *data) {
388 struct cmpfnc_swapargs_info info;
391 qsort_r(array, count, elemsize, &info, cmp_func_swap_args);
393 #endif /* USE_UCX_QSORT_R */
395 void ucx_array_sort(UcxArray* array, cmp_func cmpfnc, void *data) {
396 ucx_array_sort_impl(array->data, array->size, array->elemsize,
400 void ucx_array_remove(UcxArray *array, size_t index) {
402 if (index < array->size) {
403 void* dest = ucx_array_at(array, index);
404 void* src = ucx_array_at(array, index+1);
405 memmove(dest, src, (array->size - index)*array->elemsize);
409 void ucx_array_remove_fast(UcxArray *array, size_t index) {
411 if (index < array->size) {
412 void* dest = ucx_array_at(array, index);
413 void* src = ucx_array_at(array, array->size);
414 memcpy(dest, src, array->elemsize);
418 int ucx_array_shrink(UcxArray* array) {
419 void* newptr = alrealloc(array->allocator, array->data,
420 array->size*array->elemsize);
422 array->data = newptr;
423 array->capacity = array->size;
430 int ucx_array_resize(UcxArray* array, size_t capacity) {
431 if (array->capacity >= capacity) {
432 void* newptr = alrealloc(array->allocator, array->data,
433 capacity*array->elemsize);
435 array->data = newptr;
436 array->capacity = capacity;
437 if (array->size > array->capacity) {
438 array->size = array->capacity;
445 return ucx_array_reserve(array, capacity);
449 int ucx_array_reserve(UcxArray* array, size_t capacity) {
450 if (array->capacity > capacity) {
453 void* newptr = alrealloc(array->allocator, array->data,
454 capacity*array->elemsize);
456 array->data = newptr;
457 array->capacity = capacity;
465 int ucx_array_grow(UcxArray* array, size_t count) {
466 return ucx_array_reserve(array, array->size+count);