src/array.c

Thu, 03 Oct 2019 10:55:39 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 03 Oct 2019 10:55:39 +0200
branch
feature/array
changeset 356
77efe51c6c9a
parent 355
d315a068235a
child 357
0f5732f0dc00
permissions
-rw-r--r--

changes UcxArray from value to pointer semantics

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@354 69 }
universe@354 70
universe@355 71 int ucx_array_util_set_a(UcxAllocator* alloc, void** array, size_t* capacity,
universe@355 72 size_t elmsize, size_t index, ...) {
universe@355 73
universe@355 74 if(!alloc || !capacity || !array) {
universe@355 75 errno = EINVAL;
universe@355 76 return 1;
universe@355 77 }
universe@355 78
universe@355 79 size_t newcapacity = *capacity;
universe@355 80 while(index >= newcapacity) {
universe@355 81 if(ucx_szmul(newcapacity, 2, &newcapacity)) {
universe@355 82 errno = EOVERFLOW;
universe@355 83 return 1;
universe@355 84 }
universe@355 85 }
universe@355 86
universe@355 87 size_t memlen, offset;
universe@355 88 if(ucx_szmul(newcapacity, elmsize, &memlen)) {
universe@355 89 errno = EOVERFLOW;
universe@355 90 return 1;
universe@355 91 }
universe@355 92 /* we don't need to check index*elmsize - it is smaller than memlen */
universe@355 93
universe@355 94
universe@355 95 void* newptr = alrealloc(alloc, *array, memlen);
universe@355 96 if(newptr == NULL) {
universe@355 97 errno = ENOMEM; /* we cannot assume that every allocator sets this */
universe@355 98 return 1;
universe@355 99 }
universe@355 100 *array = newptr;
universe@355 101 *capacity = newcapacity;
universe@355 102
universe@355 103
universe@355 104 char* dest = *array;
universe@355 105 dest += elmsize*index;
universe@355 106
universe@355 107 va_list ap;
universe@355 108 va_start(ap, index);
universe@355 109 int elem = va_arg(ap, int);
universe@355 110 memcpy(dest, &elem, elmsize);
universe@355 111 va_end(ap);
universe@355 112
universe@355 113 return 0;
universe@355 114 }
universe@355 115
universe@356 116 UcxArray* ucx_array_new(size_t capacity, size_t elemsize) {
universe@334 117 return ucx_array_new_a(capacity, elemsize, ucx_default_allocator());
universe@125 118 }
universe@125 119
universe@356 120 UcxArray* ucx_array_new_a(size_t capacity, size_t elemsize,
universe@334 121 UcxAllocator* allocator) {
universe@356 122 UcxArray* array = almalloc(allocator, sizeof(UcxArray));
universe@356 123 if(array) {
universe@356 124 ucx_array_init_a(array, capacity, elemsize, allocator);
universe@336 125 }
universe@334 126 return array;
universe@18 127 }
universe@18 128
universe@356 129 void ucx_array_init(UcxArray* array, size_t capacity, size_t elemsize) {
universe@356 130 ucx_array_init_a(array, capacity, elemsize, ucx_default_allocator());
universe@356 131 }
universe@356 132
universe@356 133 void ucx_array_init_a(UcxArray* array, size_t capacity, size_t elemsize,
universe@356 134 UcxAllocator* allocator) {
universe@18 135
universe@356 136 array->allocator = allocator;
universe@356 137 array->elemsize = elemsize;
universe@356 138 array->size = 0;
universe@356 139 array->data = alcalloc(allocator, capacity, elemsize);
universe@336 140
universe@356 141 if (array->data) {
universe@356 142 array->capacity = capacity;
universe@336 143 } else {
universe@356 144 array->capacity = 0;
universe@356 145 }
universe@356 146 }
universe@356 147
universe@356 148 int ucx_array_clone(UcxArray* dest, UcxArray const* src) {
universe@356 149 if (ucx_array_ensurecap(dest, src->capacity)) {
universe@356 150 return 1;
universe@336 151 }
universe@336 152
universe@356 153 dest->elemsize = src->elemsize;
universe@356 154 dest->size = src->size;
universe@356 155
universe@356 156 if (dest->data) {
universe@356 157 memcpy(dest->data, src->data, src->size*src->elemsize);
universe@356 158 }
universe@356 159
universe@356 160 return 0;
universe@18 161 }
universe@18 162
universe@356 163 int ucx_array_equals(UcxArray const *array1, UcxArray const *array2,
universe@334 164 cmp_func cmpfnc, void* data) {
universe@334 165
universe@356 166 if (array1->size != array2->size || array1->elemsize != array2->elemsize) {
universe@336 167 return 0;
universe@336 168 } else {
universe@356 169 if (array1->size == 0)
universe@336 170 return 1;
universe@336 171
universe@356 172 size_t elemsize;
universe@336 173 if (cmpfnc == NULL) {
universe@336 174 cmpfnc = ucx_cmp_mem;
universe@356 175 elemsize = array1->elemsize;
universe@356 176 data = &elemsize;
universe@336 177 }
universe@336 178
universe@356 179 for (size_t i = 0 ; i < array1->size ; i++) {
universe@336 180 int r = cmpfnc(
universe@336 181 ucx_array_at(array1, i),
universe@336 182 ucx_array_at(array2, i),
universe@336 183 data);
universe@336 184 if (r != 0)
universe@336 185 return 0;
universe@336 186 }
universe@336 187 return 1;
universe@336 188 }
universe@125 189 }
universe@125 190
universe@353 191 void ucx_array_destroy(UcxArray *array) {
universe@356 192 if(array->data)
universe@356 193 alfree(array->allocator, array->data);
universe@336 194 array->data = NULL;
universe@336 195 array->capacity = array->size = 0;
universe@8 196 }
universe@8 197
universe@356 198 void ucx_array_free(UcxArray *array) {
universe@356 199 ucx_array_destroy(array);
universe@356 200 alfree(array->allocator, array);
universe@356 201 }
universe@356 202
universe@354 203 int ucx_array_append_from(UcxArray *array, void *data, size_t count) {
universe@354 204 if (ucx_array_ensurecap(array, array->size + count))
universe@354 205 return 1;
universe@354 206
universe@356 207 void* dest = ucx_array_at(array, array->size);
universe@354 208 if (data) {
universe@354 209 memcpy(dest, data, array->elemsize*count);
universe@354 210 } else {
universe@354 211 memset(dest, 0, array->elemsize*count);
universe@354 212 }
universe@354 213 array->size += count;
universe@354 214
universe@354 215 return 0;
universe@354 216 }
universe@354 217
universe@354 218 int ucx_array_prepend_from(UcxArray *array, void *data, size_t count) {
universe@354 219 if (ucx_array_ensurecap(array, array->size + count))
universe@354 220 return 1;
universe@354 221
universe@354 222 if (array->size > 0) {
universe@356 223 void *dest = ucx_array_at(array, count);
universe@354 224 memmove(dest, array->data, array->elemsize*array->size);
universe@336 225 }
universe@336 226
universe@336 227 if (data) {
universe@354 228 memcpy(array->data, data, array->elemsize*count);
universe@336 229 } else {
universe@354 230 memset(array->data, 0, array->elemsize*count);
universe@354 231 }
universe@354 232 array->size += count;
universe@354 233
universe@354 234 return 0;
universe@354 235 }
universe@354 236
universe@354 237 int ucx_array_set_from(UcxArray *array, size_t index,
universe@354 238 void *data, size_t count) {
universe@354 239 if (ucx_array_ensurecap(array, index + count))
universe@354 240 return 1;
universe@354 241
universe@354 242 if (index+count > array->size) {
universe@354 243 array->size = index+count;
universe@354 244 }
universe@354 245
universe@356 246 void *dest = ucx_array_at(array, index);
universe@354 247 if (data) {
universe@354 248 memcpy(dest, data, array->elemsize*count);
universe@354 249 } else {
universe@354 250 memset(dest, 0, array->elemsize*count);
universe@336 251 }
universe@336 252
universe@336 253 return 0;
universe@211 254 }
universe@211 255
universe@354 256 int ucx_array_appendv(UcxArray *array, ...) {
universe@354 257 va_list ap;
universe@354 258 va_start(ap, array);
universe@354 259 int elem = va_arg(ap, int);
universe@354 260 int ret = ucx_array_append_from(array, &elem, 1);
universe@354 261 va_end(ap);
universe@354 262 return ret;
universe@125 263 }
universe@125 264
universe@354 265 int ucx_array_prependv(UcxArray *array, ...) {
universe@354 266 va_list ap;
universe@354 267 va_start(ap, array);
universe@354 268 int elem = va_arg(ap, int);
universe@354 269 int ret = ucx_array_prepend_from(array, &elem, 1);
universe@354 270 va_end(ap);
universe@354 271 return ret;
universe@354 272 }
universe@354 273
universe@354 274 int ucx_array_setv(UcxArray *array, size_t index, ...) {
universe@354 275 va_list ap;
universe@354 276 va_start(ap, index);
universe@354 277 int elem = va_arg(ap, int);
universe@354 278 int ret = ucx_array_set_from(array, index, &elem, 1);
universe@354 279 va_end(ap);
universe@354 280 return ret;
universe@337 281 }
universe@337 282
universe@334 283 int ucx_array_concat(UcxArray *array1, const UcxArray *array2) {
universe@336 284
universe@336 285 if (array1->elemsize != array2->elemsize)
universe@336 286 return 1;
universe@336 287
universe@336 288 size_t capacity = array1->capacity+array2->capacity;
universe@336 289
universe@336 290 if (array1->capacity < capacity) {
universe@336 291 if (ucx_array_reserve(array1, capacity)) {
universe@336 292 return 1;
universe@336 293 }
universe@336 294 }
universe@336 295
universe@356 296 void* dest = ucx_array_at(array1, array1->size);
universe@336 297 memcpy(dest, array2->data, array2->size*array2->elemsize);
universe@336 298
universe@336 299 array1->size += array2->size;
universe@336 300
universe@336 301 return 0;
universe@7 302 }
universe@7 303
universe@356 304 void *ucx_array_at(UcxArray const *array, size_t index) {
universe@356 305 char* memory = array->data;
universe@356 306 char* loc = memory + index*array->elemsize;
universe@336 307 return loc;
universe@125 308 }
universe@125 309
universe@356 310 size_t ucx_array_find(UcxArray const *array, void *elem,
universe@356 311 cmp_func cmpfnc, void *data) {
universe@7 312
universe@356 313 size_t elemsize;
universe@336 314 if (cmpfnc == NULL) {
universe@336 315 cmpfnc = ucx_cmp_mem;
universe@356 316 elemsize = array->elemsize;
universe@356 317 data = &elemsize;
universe@336 318 }
universe@336 319
universe@356 320 if (array->size > 0) {
universe@356 321 for (size_t i = 0 ; i < array->size ; i++) {
universe@336 322 void* ptr = ucx_array_at(array, i);
universe@336 323 if (cmpfnc(ptr, elem, data) == 0) {
universe@336 324 return i;
universe@336 325 }
universe@336 326 }
universe@356 327 return array->size;
universe@336 328 } else {
universe@336 329 return 0;
universe@336 330 }
universe@7 331 }
universe@7 332
universe@356 333 int ucx_array_contains(UcxArray const *array, void *elem,
universe@356 334 cmp_func cmpfnc, void *data) {
universe@356 335 return ucx_array_find(array, elem, cmpfnc, data) != array->size;
universe@7 336 }
universe@7 337
universe@345 338 static void ucx_mergesort_merge(void *arrdata,size_t elemsize,
universe@345 339 cmp_func cmpfnc, void *data,
universe@336 340 size_t start, size_t mid, size_t end) {
universe@336 341
universe@345 342 char* array = arrdata;
universe@345 343
universe@336 344 size_t rightstart = mid + 1;
universe@336 345
universe@345 346 if (cmpfnc(array + mid*elemsize,
universe@345 347 array + rightstart*elemsize, data) <= 0) {
universe@336 348 /* already sorted */
universe@336 349 return;
universe@336 350 }
universe@336 351
universe@342 352 /* we need memory for one element */
universe@345 353 void *value = malloc(elemsize);
universe@336 354
universe@336 355 while (start <= mid && rightstart <= end) {
universe@345 356 if (cmpfnc(array + start*elemsize,
universe@345 357 array + rightstart*elemsize, data) <= 0) {
universe@336 358 start++;
universe@336 359 } else {
universe@342 360 /* save the value from the right */
universe@345 361 memcpy(value, array + rightstart*elemsize, elemsize);
universe@336 362
universe@342 363 /* shift all left elements one element to the right */
universe@336 364 size_t shiftcount = rightstart-start;
universe@345 365 void *startptr = array + start*elemsize;
universe@345 366 void *dest = array + (start+1)*elemsize;
universe@345 367 memmove(dest, startptr, shiftcount*elemsize);
universe@336 368
universe@342 369 /* bring the first value from the right to the left */
universe@345 370 memcpy(startptr, value, elemsize);
universe@336 371
universe@336 372 start++;
universe@336 373 mid++;
universe@336 374 rightstart++;
universe@336 375 }
universe@336 376 }
universe@336 377
universe@342 378 /* free the temporary memory */
universe@336 379 free(value);
universe@336 380 }
universe@336 381
universe@345 382 static void ucx_mergesort_impl(void *arrdata, size_t elemsize,
universe@345 383 cmp_func cmpfnc, void *data, size_t l, size_t r) {
universe@336 384 if (l < r) {
universe@336 385 size_t m = l + (r - l) / 2;
universe@336 386
universe@345 387 ucx_mergesort_impl(arrdata, elemsize, cmpfnc, data, l, m);
universe@345 388 ucx_mergesort_impl(arrdata, elemsize, cmpfnc, data, m + 1, r);
universe@345 389 ucx_mergesort_merge(arrdata, elemsize, cmpfnc, data, l, m, r);
universe@336 390 }
universe@336 391 }
universe@336 392
universe@345 393 static void ucx_mergesort(void *arrdata, size_t count, size_t elemsize,
universe@345 394 cmp_func cmpfnc, void *data) {
universe@345 395
universe@345 396 ucx_mergesort_impl(arrdata, elemsize, cmpfnc, data, 0, count-1);
universe@345 397 }
universe@345 398
universe@345 399 #ifdef USE_UCX_QSORT_R
universe@345 400 struct cmpfnc_swapargs_info {
universe@345 401 cmp_func func;
universe@345 402 void *data;
universe@345 403 };
universe@345 404
universe@345 405 static int cmp_func_swap_args(void *data, const void *x, const void *y) {
olaf@348 406 cmpfnc_swapargs_info* info = data;
universe@345 407 return info->func(x, y, info->data);
universe@345 408 }
universe@345 409
universe@345 410 static void ucx_qsort_r(void *array, size_t count, size_t elemsize,
universe@345 411 cmp_func cmpfnc, void *data) {
universe@345 412 struct cmpfnc_swapargs_info info;
universe@345 413 info.func = cmpfnc;
universe@345 414 info.data = data;
universe@345 415 qsort_r(array, count, elemsize, &info, cmp_func_swap_args);
universe@345 416 }
universe@345 417 #endif /* USE_UCX_QSORT_R */
universe@345 418
universe@356 419 void ucx_array_sort(UcxArray* array, cmp_func cmpfnc, void *data) {
universe@356 420 ucx_array_sort_impl(array->data, array->size, array->elemsize,
universe@356 421 cmpfnc, data);
universe@7 422 }
universe@7 423
universe@334 424 void ucx_array_remove(UcxArray *array, size_t index) {
universe@336 425 array->size--;
universe@336 426 if (index < array->size) {
universe@356 427 void* dest = ucx_array_at(array, index);
universe@356 428 void* src = ucx_array_at(array, index+1);
universe@336 429 memmove(dest, src, (array->size - index)*array->elemsize);
universe@336 430 }
universe@123 431 }
universe@123 432
universe@334 433 void ucx_array_remove_fast(UcxArray *array, size_t index) {
universe@336 434 array->size--;
universe@336 435 if (index < array->size) {
universe@356 436 void* dest = ucx_array_at(array, index);
universe@356 437 void* src = ucx_array_at(array, array->size);
universe@336 438 memcpy(dest, src, array->elemsize);
universe@336 439 }
universe@7 440 }
universe@7 441
universe@334 442 int ucx_array_shrink(UcxArray* array) {
universe@336 443 void* newptr = alrealloc(array->allocator, array->data,
universe@336 444 array->size*array->elemsize);
universe@336 445 if (newptr) {
universe@336 446 array->data = newptr;
universe@336 447 array->capacity = array->size;
universe@336 448 return 0;
universe@336 449 } else {
universe@336 450 return 1;
universe@336 451 }
universe@123 452 }
universe@123 453
universe@334 454 int ucx_array_resize(UcxArray* array, size_t capacity) {
universe@336 455 if (array->capacity >= capacity) {
universe@336 456 void* newptr = alrealloc(array->allocator, array->data,
universe@336 457 capacity*array->elemsize);
universe@336 458 if (newptr) {
universe@336 459 array->data = newptr;
universe@336 460 array->capacity = capacity;
universe@336 461 if (array->size > array->capacity) {
universe@336 462 array->size = array->capacity;
universe@336 463 }
universe@336 464 return 0;
universe@336 465 } else {
universe@336 466 return 1;
universe@336 467 }
universe@336 468 } else {
universe@336 469 return ucx_array_reserve(array, capacity);
universe@336 470 }
universe@87 471 }
universe@87 472
universe@334 473 int ucx_array_reserve(UcxArray* array, size_t capacity) {
universe@336 474 if (array->capacity > capacity) {
universe@336 475 return 0;
universe@336 476 } else {
universe@336 477 void* newptr = alrealloc(array->allocator, array->data,
universe@336 478 capacity*array->elemsize);
universe@336 479 if (newptr) {
universe@336 480 array->data = newptr;
universe@336 481 array->capacity = capacity;
universe@336 482 return 0;
universe@336 483 } else {
universe@336 484 return 1;
universe@336 485 }
universe@336 486 }
universe@7 487 }

mercurial