src/array.c

Mon, 30 Dec 2019 09:52:07 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 30 Dec 2019 09:52:07 +0100
branch
feature/array
changeset 387
7e0f19fe23ff
parent 357
0f5732f0dc00
child 369
28a8ccc442b0
permissions
-rw-r--r--

closes array branch towards ucx 2.1 release

universe@103 1 /*
universe@103 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@103 3 *
universe@334 4 * Copyright 2019 Mike Becker, Olaf Wintermann All rights reserved.
universe@103 5 *
universe@103 6 * Redistribution and use in source and binary forms, with or without
universe@103 7 * modification, are permitted provided that the following conditions are met:
universe@103 8 *
universe@103 9 * 1. Redistributions of source code must retain the above copyright
universe@103 10 * notice, this list of conditions and the following disclaimer.
universe@103 11 *
universe@103 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@103 13 * notice, this list of conditions and the following disclaimer in the
universe@103 14 * documentation and/or other materials provided with the distribution.
universe@103 15 *
universe@103 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@103 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@103 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@103 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@103 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@103 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@103 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@103 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@103 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@103 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@103 26 * POSSIBILITY OF SUCH DAMAGE.
universe@103 27 */
universe@103 28
universe@345 29 #define _GNU_SOURCE /* we want to use qsort_r(), if available */
olaf@348 30 #define __STDC_WANT_LIB_EXT1__ 1 /* use qsort_s, if available */
olaf@348 31
universe@345 32
universe@334 33 #include "ucx/array.h"
universe@336 34 #include "ucx/utils.h"
universe@4 35
universe@336 36 #include <string.h>
universe@345 37 #include <stdlib.h>
universe@355 38 #include <errno.h>
universe@345 39
universe@345 40 #ifndef UCX_ARRAY_DISABLE_QSORT
universe@346 41 #ifdef __GLIBC__
universe@345 42 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 8)
universe@345 43 #define ucx_array_sort_impl qsort_r
universe@345 44 #endif /* glibc version >= 2.8 */
universe@346 45 #elif /* not __GLIBC__ */ defined(__APPLE__) || defined(__FreeBSD__)
universe@345 46 #define ucx_array_sort_impl ucx_qsort_r
universe@345 47 #define USE_UCX_QSORT_R
olaf@348 48 #elif /* not (__APPLE || __FreeBSD__) */ defined(__sun)
olaf@348 49 #if __STDC_VERSION__ >= 201112L
olaf@348 50 #define ucx_array_sort_impl qsort_s
olaf@348 51 #endif
olaf@348 52 #endif /* __GLIBC__, __APLE__, __FreeBSD__, __sun */
universe@345 53 #endif /* UCX_ARRAY_DISABLE_QSORT */
universe@345 54
universe@345 55 #ifndef ucx_array_sort_impl
universe@345 56 #define ucx_array_sort_impl ucx_mergesort
universe@345 57 #endif
universe@334 58
universe@354 59 static int ucx_array_ensurecap(UcxArray *array, size_t reqcap) {
universe@354 60 size_t required_capacity = array->capacity;
universe@354 61 while (reqcap > required_capacity) {
universe@354 62 if (required_capacity * 2 < required_capacity)
universe@354 63 return 1;
universe@354 64 required_capacity <<= 1;
universe@354 65 }
universe@354 66 if (ucx_array_reserve(array, required_capacity)) {
universe@354 67 return 1;
universe@354 68 }
universe@357 69 return 0;
universe@354 70 }
universe@354 71
universe@355 72 int ucx_array_util_set_a(UcxAllocator* alloc, void** array, size_t* capacity,
universe@355 73 size_t elmsize, size_t index, ...) {
universe@355 74
universe@355 75 if(!alloc || !capacity || !array) {
universe@355 76 errno = EINVAL;
universe@355 77 return 1;
universe@355 78 }
universe@355 79
universe@355 80 size_t newcapacity = *capacity;
universe@355 81 while(index >= newcapacity) {
universe@355 82 if(ucx_szmul(newcapacity, 2, &newcapacity)) {
universe@355 83 errno = EOVERFLOW;
universe@355 84 return 1;
universe@355 85 }
universe@355 86 }
universe@355 87
universe@355 88 size_t memlen, offset;
universe@355 89 if(ucx_szmul(newcapacity, elmsize, &memlen)) {
universe@355 90 errno = EOVERFLOW;
universe@355 91 return 1;
universe@355 92 }
universe@355 93 /* we don't need to check index*elmsize - it is smaller than memlen */
universe@355 94
universe@355 95
universe@355 96 void* newptr = alrealloc(alloc, *array, memlen);
universe@355 97 if(newptr == NULL) {
universe@355 98 errno = ENOMEM; /* we cannot assume that every allocator sets this */
universe@355 99 return 1;
universe@355 100 }
universe@355 101 *array = newptr;
universe@355 102 *capacity = newcapacity;
universe@355 103
universe@355 104
universe@355 105 char* dest = *array;
universe@355 106 dest += elmsize*index;
universe@355 107
universe@355 108 va_list ap;
universe@355 109 va_start(ap, index);
universe@355 110 int elem = va_arg(ap, int);
universe@355 111 memcpy(dest, &elem, elmsize);
universe@355 112 va_end(ap);
universe@355 113
universe@355 114 return 0;
universe@355 115 }
universe@355 116
universe@356 117 UcxArray* ucx_array_new(size_t capacity, size_t elemsize) {
universe@334 118 return ucx_array_new_a(capacity, elemsize, ucx_default_allocator());
universe@125 119 }
universe@125 120
universe@356 121 UcxArray* ucx_array_new_a(size_t capacity, size_t elemsize,
universe@334 122 UcxAllocator* allocator) {
universe@356 123 UcxArray* array = almalloc(allocator, sizeof(UcxArray));
universe@356 124 if(array) {
universe@356 125 ucx_array_init_a(array, capacity, elemsize, allocator);
universe@336 126 }
universe@334 127 return array;
universe@18 128 }
universe@18 129
universe@356 130 void ucx_array_init(UcxArray* array, size_t capacity, size_t elemsize) {
universe@356 131 ucx_array_init_a(array, capacity, elemsize, ucx_default_allocator());
universe@356 132 }
universe@356 133
universe@356 134 void ucx_array_init_a(UcxArray* array, size_t capacity, size_t elemsize,
universe@356 135 UcxAllocator* allocator) {
universe@18 136
universe@356 137 array->allocator = allocator;
universe@356 138 array->elemsize = elemsize;
universe@356 139 array->size = 0;
universe@356 140 array->data = alcalloc(allocator, capacity, elemsize);
universe@336 141
universe@356 142 if (array->data) {
universe@356 143 array->capacity = capacity;
universe@336 144 } else {
universe@356 145 array->capacity = 0;
universe@356 146 }
universe@356 147 }
universe@356 148
universe@356 149 int ucx_array_clone(UcxArray* dest, UcxArray const* src) {
universe@356 150 if (ucx_array_ensurecap(dest, src->capacity)) {
universe@356 151 return 1;
universe@336 152 }
universe@336 153
universe@356 154 dest->elemsize = src->elemsize;
universe@356 155 dest->size = src->size;
universe@356 156
universe@356 157 if (dest->data) {
universe@356 158 memcpy(dest->data, src->data, src->size*src->elemsize);
universe@356 159 }
universe@356 160
universe@356 161 return 0;
universe@18 162 }
universe@18 163
universe@356 164 int ucx_array_equals(UcxArray const *array1, UcxArray const *array2,
universe@334 165 cmp_func cmpfnc, void* data) {
universe@334 166
universe@356 167 if (array1->size != array2->size || array1->elemsize != array2->elemsize) {
universe@336 168 return 0;
universe@336 169 } else {
universe@356 170 if (array1->size == 0)
universe@336 171 return 1;
universe@336 172
universe@356 173 size_t elemsize;
universe@336 174 if (cmpfnc == NULL) {
universe@336 175 cmpfnc = ucx_cmp_mem;
universe@356 176 elemsize = array1->elemsize;
universe@356 177 data = &elemsize;
universe@336 178 }
universe@336 179
universe@356 180 for (size_t i = 0 ; i < array1->size ; i++) {
universe@336 181 int r = cmpfnc(
universe@336 182 ucx_array_at(array1, i),
universe@336 183 ucx_array_at(array2, i),
universe@336 184 data);
universe@336 185 if (r != 0)
universe@336 186 return 0;
universe@336 187 }
universe@336 188 return 1;
universe@336 189 }
universe@125 190 }
universe@125 191
universe@353 192 void ucx_array_destroy(UcxArray *array) {
universe@356 193 if(array->data)
universe@356 194 alfree(array->allocator, array->data);
universe@336 195 array->data = NULL;
universe@336 196 array->capacity = array->size = 0;
universe@8 197 }
universe@8 198
universe@356 199 void ucx_array_free(UcxArray *array) {
universe@356 200 ucx_array_destroy(array);
universe@356 201 alfree(array->allocator, array);
universe@356 202 }
universe@356 203
universe@354 204 int ucx_array_append_from(UcxArray *array, void *data, size_t count) {
universe@354 205 if (ucx_array_ensurecap(array, array->size + count))
universe@354 206 return 1;
universe@354 207
universe@356 208 void* dest = ucx_array_at(array, array->size);
universe@354 209 if (data) {
universe@354 210 memcpy(dest, data, array->elemsize*count);
universe@354 211 } else {
universe@354 212 memset(dest, 0, array->elemsize*count);
universe@354 213 }
universe@354 214 array->size += count;
universe@354 215
universe@354 216 return 0;
universe@354 217 }
universe@354 218
universe@354 219 int ucx_array_prepend_from(UcxArray *array, void *data, size_t count) {
universe@354 220 if (ucx_array_ensurecap(array, array->size + count))
universe@354 221 return 1;
universe@354 222
universe@354 223 if (array->size > 0) {
universe@356 224 void *dest = ucx_array_at(array, count);
universe@354 225 memmove(dest, array->data, array->elemsize*array->size);
universe@336 226 }
universe@336 227
universe@336 228 if (data) {
universe@354 229 memcpy(array->data, data, array->elemsize*count);
universe@336 230 } else {
universe@354 231 memset(array->data, 0, array->elemsize*count);
universe@354 232 }
universe@354 233 array->size += count;
universe@354 234
universe@354 235 return 0;
universe@354 236 }
universe@354 237
universe@354 238 int ucx_array_set_from(UcxArray *array, size_t index,
universe@354 239 void *data, size_t count) {
universe@354 240 if (ucx_array_ensurecap(array, index + count))
universe@354 241 return 1;
universe@354 242
universe@354 243 if (index+count > array->size) {
universe@354 244 array->size = index+count;
universe@354 245 }
universe@354 246
universe@356 247 void *dest = ucx_array_at(array, index);
universe@354 248 if (data) {
universe@354 249 memcpy(dest, data, array->elemsize*count);
universe@354 250 } else {
universe@354 251 memset(dest, 0, array->elemsize*count);
universe@336 252 }
universe@336 253
universe@336 254 return 0;
universe@211 255 }
universe@211 256
universe@354 257 int ucx_array_appendv(UcxArray *array, ...) {
universe@354 258 va_list ap;
universe@354 259 va_start(ap, array);
universe@354 260 int elem = va_arg(ap, int);
universe@354 261 int ret = ucx_array_append_from(array, &elem, 1);
universe@354 262 va_end(ap);
universe@354 263 return ret;
universe@125 264 }
universe@125 265
universe@354 266 int ucx_array_prependv(UcxArray *array, ...) {
universe@354 267 va_list ap;
universe@354 268 va_start(ap, array);
universe@354 269 int elem = va_arg(ap, int);
universe@354 270 int ret = ucx_array_prepend_from(array, &elem, 1);
universe@354 271 va_end(ap);
universe@354 272 return ret;
universe@354 273 }
universe@354 274
universe@354 275 int ucx_array_setv(UcxArray *array, size_t index, ...) {
universe@354 276 va_list ap;
universe@354 277 va_start(ap, index);
universe@354 278 int elem = va_arg(ap, int);
universe@354 279 int ret = ucx_array_set_from(array, index, &elem, 1);
universe@354 280 va_end(ap);
universe@354 281 return ret;
universe@337 282 }
universe@337 283
universe@334 284 int ucx_array_concat(UcxArray *array1, const UcxArray *array2) {
universe@336 285
universe@336 286 if (array1->elemsize != array2->elemsize)
universe@336 287 return 1;
universe@336 288
universe@336 289 size_t capacity = array1->capacity+array2->capacity;
universe@336 290
universe@336 291 if (array1->capacity < capacity) {
universe@336 292 if (ucx_array_reserve(array1, capacity)) {
universe@336 293 return 1;
universe@336 294 }
universe@336 295 }
universe@336 296
universe@356 297 void* dest = ucx_array_at(array1, array1->size);
universe@336 298 memcpy(dest, array2->data, array2->size*array2->elemsize);
universe@336 299
universe@336 300 array1->size += array2->size;
universe@336 301
universe@336 302 return 0;
universe@7 303 }
universe@7 304
universe@356 305 void *ucx_array_at(UcxArray const *array, size_t index) {
universe@356 306 char* memory = array->data;
universe@356 307 char* loc = memory + index*array->elemsize;
universe@336 308 return loc;
universe@125 309 }
universe@125 310
universe@356 311 size_t ucx_array_find(UcxArray const *array, void *elem,
universe@356 312 cmp_func cmpfnc, void *data) {
universe@7 313
universe@356 314 size_t elemsize;
universe@336 315 if (cmpfnc == NULL) {
universe@336 316 cmpfnc = ucx_cmp_mem;
universe@356 317 elemsize = array->elemsize;
universe@356 318 data = &elemsize;
universe@336 319 }
universe@336 320
universe@356 321 if (array->size > 0) {
universe@356 322 for (size_t i = 0 ; i < array->size ; i++) {
universe@336 323 void* ptr = ucx_array_at(array, i);
universe@336 324 if (cmpfnc(ptr, elem, data) == 0) {
universe@336 325 return i;
universe@336 326 }
universe@336 327 }
universe@356 328 return array->size;
universe@336 329 } else {
universe@336 330 return 0;
universe@336 331 }
universe@7 332 }
universe@7 333
universe@356 334 int ucx_array_contains(UcxArray const *array, void *elem,
universe@356 335 cmp_func cmpfnc, void *data) {
universe@356 336 return ucx_array_find(array, elem, cmpfnc, data) != array->size;
universe@7 337 }
universe@7 338
universe@345 339 static void ucx_mergesort_merge(void *arrdata,size_t elemsize,
universe@345 340 cmp_func cmpfnc, void *data,
universe@336 341 size_t start, size_t mid, size_t end) {
universe@336 342
universe@345 343 char* array = arrdata;
universe@345 344
universe@336 345 size_t rightstart = mid + 1;
universe@336 346
universe@345 347 if (cmpfnc(array + mid*elemsize,
universe@345 348 array + rightstart*elemsize, data) <= 0) {
universe@336 349 /* already sorted */
universe@336 350 return;
universe@336 351 }
universe@336 352
universe@342 353 /* we need memory for one element */
universe@345 354 void *value = malloc(elemsize);
universe@336 355
universe@336 356 while (start <= mid && rightstart <= end) {
universe@345 357 if (cmpfnc(array + start*elemsize,
universe@345 358 array + rightstart*elemsize, data) <= 0) {
universe@336 359 start++;
universe@336 360 } else {
universe@342 361 /* save the value from the right */
universe@345 362 memcpy(value, array + rightstart*elemsize, elemsize);
universe@336 363
universe@342 364 /* shift all left elements one element to the right */
universe@336 365 size_t shiftcount = rightstart-start;
universe@345 366 void *startptr = array + start*elemsize;
universe@345 367 void *dest = array + (start+1)*elemsize;
universe@345 368 memmove(dest, startptr, shiftcount*elemsize);
universe@336 369
universe@342 370 /* bring the first value from the right to the left */
universe@345 371 memcpy(startptr, value, elemsize);
universe@336 372
universe@336 373 start++;
universe@336 374 mid++;
universe@336 375 rightstart++;
universe@336 376 }
universe@336 377 }
universe@336 378
universe@342 379 /* free the temporary memory */
universe@336 380 free(value);
universe@336 381 }
universe@336 382
universe@345 383 static void ucx_mergesort_impl(void *arrdata, size_t elemsize,
universe@345 384 cmp_func cmpfnc, void *data, size_t l, size_t r) {
universe@336 385 if (l < r) {
universe@336 386 size_t m = l + (r - l) / 2;
universe@336 387
universe@345 388 ucx_mergesort_impl(arrdata, elemsize, cmpfnc, data, l, m);
universe@345 389 ucx_mergesort_impl(arrdata, elemsize, cmpfnc, data, m + 1, r);
universe@345 390 ucx_mergesort_merge(arrdata, elemsize, cmpfnc, data, l, m, r);
universe@336 391 }
universe@336 392 }
universe@336 393
universe@345 394 static void ucx_mergesort(void *arrdata, size_t count, size_t elemsize,
universe@345 395 cmp_func cmpfnc, void *data) {
universe@345 396
universe@345 397 ucx_mergesort_impl(arrdata, elemsize, cmpfnc, data, 0, count-1);
universe@345 398 }
universe@345 399
universe@345 400 #ifdef USE_UCX_QSORT_R
universe@345 401 struct cmpfnc_swapargs_info {
universe@345 402 cmp_func func;
universe@345 403 void *data;
universe@345 404 };
universe@345 405
universe@345 406 static int cmp_func_swap_args(void *data, const void *x, const void *y) {
olaf@348 407 cmpfnc_swapargs_info* info = data;
universe@345 408 return info->func(x, y, info->data);
universe@345 409 }
universe@345 410
universe@345 411 static void ucx_qsort_r(void *array, size_t count, size_t elemsize,
universe@345 412 cmp_func cmpfnc, void *data) {
universe@345 413 struct cmpfnc_swapargs_info info;
universe@345 414 info.func = cmpfnc;
universe@345 415 info.data = data;
universe@345 416 qsort_r(array, count, elemsize, &info, cmp_func_swap_args);
universe@345 417 }
universe@345 418 #endif /* USE_UCX_QSORT_R */
universe@345 419
universe@356 420 void ucx_array_sort(UcxArray* array, cmp_func cmpfnc, void *data) {
universe@356 421 ucx_array_sort_impl(array->data, array->size, array->elemsize,
universe@356 422 cmpfnc, data);
universe@7 423 }
universe@7 424
universe@334 425 void ucx_array_remove(UcxArray *array, size_t index) {
universe@336 426 array->size--;
universe@336 427 if (index < array->size) {
universe@356 428 void* dest = ucx_array_at(array, index);
universe@356 429 void* src = ucx_array_at(array, index+1);
universe@336 430 memmove(dest, src, (array->size - index)*array->elemsize);
universe@336 431 }
universe@123 432 }
universe@123 433
universe@334 434 void ucx_array_remove_fast(UcxArray *array, size_t index) {
universe@336 435 array->size--;
universe@336 436 if (index < array->size) {
universe@356 437 void* dest = ucx_array_at(array, index);
universe@356 438 void* src = ucx_array_at(array, array->size);
universe@336 439 memcpy(dest, src, array->elemsize);
universe@336 440 }
universe@7 441 }
universe@7 442
universe@334 443 int ucx_array_shrink(UcxArray* array) {
universe@336 444 void* newptr = alrealloc(array->allocator, array->data,
universe@336 445 array->size*array->elemsize);
universe@336 446 if (newptr) {
universe@336 447 array->data = newptr;
universe@336 448 array->capacity = array->size;
universe@336 449 return 0;
universe@336 450 } else {
universe@336 451 return 1;
universe@336 452 }
universe@123 453 }
universe@123 454
universe@334 455 int ucx_array_resize(UcxArray* array, size_t capacity) {
universe@336 456 if (array->capacity >= capacity) {
universe@336 457 void* newptr = alrealloc(array->allocator, array->data,
universe@336 458 capacity*array->elemsize);
universe@336 459 if (newptr) {
universe@336 460 array->data = newptr;
universe@336 461 array->capacity = capacity;
universe@336 462 if (array->size > array->capacity) {
universe@336 463 array->size = array->capacity;
universe@336 464 }
universe@336 465 return 0;
universe@336 466 } else {
universe@336 467 return 1;
universe@336 468 }
universe@336 469 } else {
universe@336 470 return ucx_array_reserve(array, capacity);
universe@336 471 }
universe@87 472 }
universe@87 473
universe@334 474 int ucx_array_reserve(UcxArray* array, size_t capacity) {
universe@336 475 if (array->capacity > capacity) {
universe@336 476 return 0;
universe@336 477 } else {
universe@336 478 void* newptr = alrealloc(array->allocator, array->data,
universe@336 479 capacity*array->elemsize);
universe@336 480 if (newptr) {
universe@336 481 array->data = newptr;
universe@336 482 array->capacity = capacity;
universe@336 483 return 0;
universe@336 484 } else {
universe@336 485 return 1;
universe@336 486 }
universe@336 487 }
universe@7 488 }

mercurial