src/array.c

Sat, 10 Aug 2019 09:47:59 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 10 Aug 2019 09:47:59 +0200
branch
feature/array
changeset 353
135ce35d5108
parent 348
3b9b4f6e9fd6
child 354
7fd13b9f8f60
permissions
-rw-r--r--

renames ucx_array_free() to ucx_array_destroy()

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@345 38
universe@345 39 #ifndef UCX_ARRAY_DISABLE_QSORT
universe@346 40 #ifdef __GLIBC__
universe@345 41 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 8)
universe@345 42 #define ucx_array_sort_impl qsort_r
universe@345 43 #endif /* glibc version >= 2.8 */
universe@346 44 #elif /* not __GLIBC__ */ defined(__APPLE__) || defined(__FreeBSD__)
universe@345 45 #define ucx_array_sort_impl ucx_qsort_r
universe@345 46 #define USE_UCX_QSORT_R
olaf@348 47 #elif /* not (__APPLE || __FreeBSD__) */ defined(__sun)
olaf@348 48 #if __STDC_VERSION__ >= 201112L
olaf@348 49 #define ucx_array_sort_impl qsort_s
olaf@348 50 #endif
olaf@348 51 #endif /* __GLIBC__, __APLE__, __FreeBSD__, __sun */
universe@345 52 #endif /* UCX_ARRAY_DISABLE_QSORT */
universe@345 53
universe@345 54 #ifndef ucx_array_sort_impl
universe@345 55 #define ucx_array_sort_impl ucx_mergesort
universe@345 56 #endif
universe@334 57
universe@334 58 UcxArray ucx_array_new(size_t capacity, size_t elemsize) {
universe@334 59 return ucx_array_new_a(capacity, elemsize, ucx_default_allocator());
universe@125 60 }
universe@125 61
universe@334 62 UcxArray ucx_array_new_a(size_t capacity, size_t elemsize,
universe@334 63 UcxAllocator* allocator) {
universe@334 64 UcxArray array;
universe@334 65
universe@336 66 array.allocator = allocator;
universe@336 67 array.elemsize = elemsize;
universe@336 68 array.size = 0;
universe@336 69 array.data = alcalloc(allocator, capacity, elemsize);
universe@336 70
universe@336 71 if (array.data) {
universe@336 72 array.capacity = capacity;
universe@336 73 } else {
universe@336 74 array.capacity = 0;
universe@336 75 }
universe@336 76
universe@334 77 return array;
universe@18 78 }
universe@18 79
universe@334 80 UcxArray ucx_array_clone(UcxArray array) {
universe@334 81 UcxArray clone;
universe@18 82
universe@336 83 clone.allocator = array.allocator;
universe@336 84 clone.elemsize = array.elemsize;
universe@336 85 clone.size = array.size;
universe@336 86 clone.data = alcalloc(array.allocator, array.capacity, array.elemsize);
universe@336 87
universe@336 88 if (clone.data) {
universe@336 89 clone.capacity = array.capacity;
universe@336 90 memcpy(clone.data, array.data, array.size*array.elemsize);
universe@336 91 } else {
universe@336 92 clone.capacity = clone.size = 0;
universe@336 93 }
universe@336 94
universe@334 95 return clone;
universe@18 96 }
universe@18 97
universe@334 98 int ucx_array_equals(UcxArray array1, UcxArray array2,
universe@334 99 cmp_func cmpfnc, void* data) {
universe@334 100
universe@336 101 if (array1.size != array2.size || array1.elemsize != array2.elemsize) {
universe@336 102 return 0;
universe@336 103 } else {
universe@336 104 if (array1.size == 0)
universe@336 105 return 1;
universe@336 106
universe@336 107 if (cmpfnc == NULL) {
universe@336 108 cmpfnc = ucx_cmp_mem;
universe@336 109 data = &array1.elemsize;
universe@336 110 }
universe@336 111
universe@336 112 for (size_t i = 0 ; i < array1.size ; i++) {
universe@336 113 int r = cmpfnc(
universe@336 114 ucx_array_at(array1, i),
universe@336 115 ucx_array_at(array2, i),
universe@336 116 data);
universe@336 117 if (r != 0)
universe@336 118 return 0;
universe@336 119 }
universe@336 120 return 1;
universe@336 121 }
universe@125 122 }
universe@125 123
universe@353 124 void ucx_array_destroy(UcxArray *array) {
universe@336 125 alfree(array->allocator, array->data);
universe@336 126 array->data = NULL;
universe@336 127 array->capacity = array->size = 0;
universe@8 128 }
universe@8 129
universe@334 130 int ucx_array_append(UcxArray *array, void *data) {
universe@336 131 if (array->size == array->capacity) {
universe@336 132 if (ucx_array_reserve(array, array->capacity*2)) {
universe@336 133 return 1;
universe@336 134 }
universe@336 135 }
universe@336 136
universe@336 137 void* dest = ucx_array_at(*array, array->size++);
universe@336 138 if (data) {
universe@336 139 memcpy(dest, data, array->elemsize);
universe@336 140 } else {
universe@336 141 memset(dest, 0, array->elemsize);
universe@336 142 }
universe@336 143
universe@336 144 return 0;
universe@211 145 }
universe@211 146
universe@334 147 int ucx_array_prepend(UcxArray *array, void *data) {
universe@336 148 if (array->size == array->capacity) {
universe@336 149 if (ucx_array_reserve(array, array->capacity*2)) {
universe@336 150 return 1;
universe@336 151 }
universe@336 152 }
universe@336 153
universe@336 154 array->size++;
universe@336 155
universe@336 156 if (array->size > 1) {
universe@336 157 void *dest = ucx_array_at(*array,1);
universe@336 158 memmove(dest, array->data, array->elemsize*array->size);
universe@336 159 }
universe@336 160
universe@336 161 if (data) {
universe@336 162 memcpy(array->data, data, array->elemsize);
universe@336 163 } else {
universe@336 164 memset(array->data, 0, array->elemsize);
universe@336 165 }
universe@336 166
universe@336 167 return 0;
universe@125 168 }
universe@125 169
universe@337 170 int ucx_array_set(UcxArray *array, size_t index, void *data) {
universe@337 171 if (index >= array->size) {
universe@337 172 if (ucx_array_reserve(array, index+1)) {
universe@337 173 return 1;
universe@337 174 }
universe@337 175 array->size = index+1;
universe@337 176 }
universe@337 177
universe@337 178 void *dest = ucx_array_at(*array, index);
universe@337 179 if (data) {
universe@337 180 memcpy(dest, data, array->elemsize);
universe@337 181 } else {
universe@337 182 memset(dest, 0, array->elemsize);
universe@337 183 }
universe@337 184
universe@337 185 return 0;
universe@337 186 }
universe@337 187
universe@334 188 int ucx_array_concat(UcxArray *array1, const UcxArray *array2) {
universe@336 189
universe@336 190 if (array1->elemsize != array2->elemsize)
universe@336 191 return 1;
universe@336 192
universe@336 193 size_t capacity = array1->capacity+array2->capacity;
universe@336 194
universe@336 195 if (array1->capacity < capacity) {
universe@336 196 if (ucx_array_reserve(array1, capacity)) {
universe@336 197 return 1;
universe@336 198 }
universe@336 199 }
universe@336 200
universe@336 201 void* dest = ucx_array_at(*array1, array1->size);
universe@336 202 memcpy(dest, array2->data, array2->size*array2->elemsize);
universe@336 203
universe@336 204 array1->size += array2->size;
universe@336 205
universe@336 206 return 0;
universe@7 207 }
universe@7 208
universe@334 209 void *ucx_array_at(UcxArray array, size_t index) {
universe@336 210 char* memory = array.data;
universe@336 211 char* loc = memory + index*array.elemsize;
universe@336 212 return loc;
universe@125 213 }
universe@125 214
universe@334 215 size_t ucx_array_find(UcxArray array, void *elem, cmp_func cmpfnc, void *data) {
universe@7 216
universe@336 217 if (cmpfnc == NULL) {
universe@336 218 cmpfnc = ucx_cmp_mem;
universe@336 219 data = &array.elemsize;
universe@336 220 }
universe@336 221
universe@336 222 if (array.size > 0) {
universe@336 223 for (size_t i = 0 ; i < array.size ; i++) {
universe@336 224 void* ptr = ucx_array_at(array, i);
universe@336 225 if (cmpfnc(ptr, elem, data) == 0) {
universe@336 226 return i;
universe@336 227 }
universe@336 228 }
universe@336 229 return array.size;
universe@336 230 } else {
universe@336 231 return 0;
universe@336 232 }
universe@7 233 }
universe@7 234
universe@334 235 int ucx_array_contains(UcxArray array, void *elem, cmp_func cmpfnc, void *data) {
universe@334 236 return ucx_array_find(array, elem, cmpfnc, data) != array.size;
universe@7 237 }
universe@7 238
universe@345 239 static void ucx_mergesort_merge(void *arrdata,size_t elemsize,
universe@345 240 cmp_func cmpfnc, void *data,
universe@336 241 size_t start, size_t mid, size_t end) {
universe@336 242
universe@345 243 char* array = arrdata;
universe@345 244
universe@336 245 size_t rightstart = mid + 1;
universe@336 246
universe@345 247 if (cmpfnc(array + mid*elemsize,
universe@345 248 array + rightstart*elemsize, data) <= 0) {
universe@336 249 /* already sorted */
universe@336 250 return;
universe@336 251 }
universe@336 252
universe@342 253 /* we need memory for one element */
universe@345 254 void *value = malloc(elemsize);
universe@336 255
universe@336 256 while (start <= mid && rightstart <= end) {
universe@345 257 if (cmpfnc(array + start*elemsize,
universe@345 258 array + rightstart*elemsize, data) <= 0) {
universe@336 259 start++;
universe@336 260 } else {
universe@342 261 /* save the value from the right */
universe@345 262 memcpy(value, array + rightstart*elemsize, elemsize);
universe@336 263
universe@342 264 /* shift all left elements one element to the right */
universe@336 265 size_t shiftcount = rightstart-start;
universe@345 266 void *startptr = array + start*elemsize;
universe@345 267 void *dest = array + (start+1)*elemsize;
universe@345 268 memmove(dest, startptr, shiftcount*elemsize);
universe@336 269
universe@342 270 /* bring the first value from the right to the left */
universe@345 271 memcpy(startptr, value, elemsize);
universe@336 272
universe@336 273 start++;
universe@336 274 mid++;
universe@336 275 rightstart++;
universe@336 276 }
universe@336 277 }
universe@336 278
universe@342 279 /* free the temporary memory */
universe@336 280 free(value);
universe@336 281 }
universe@336 282
universe@345 283 static void ucx_mergesort_impl(void *arrdata, size_t elemsize,
universe@345 284 cmp_func cmpfnc, void *data, size_t l, size_t r) {
universe@336 285 if (l < r) {
universe@336 286 size_t m = l + (r - l) / 2;
universe@336 287
universe@345 288 ucx_mergesort_impl(arrdata, elemsize, cmpfnc, data, l, m);
universe@345 289 ucx_mergesort_impl(arrdata, elemsize, cmpfnc, data, m + 1, r);
universe@345 290 ucx_mergesort_merge(arrdata, elemsize, cmpfnc, data, l, m, r);
universe@336 291 }
universe@336 292 }
universe@336 293
universe@345 294 static void ucx_mergesort(void *arrdata, size_t count, size_t elemsize,
universe@345 295 cmp_func cmpfnc, void *data) {
universe@345 296
universe@345 297 ucx_mergesort_impl(arrdata, elemsize, cmpfnc, data, 0, count-1);
universe@345 298 }
universe@345 299
universe@345 300 #ifdef USE_UCX_QSORT_R
universe@345 301 struct cmpfnc_swapargs_info {
universe@345 302 cmp_func func;
universe@345 303 void *data;
universe@345 304 };
universe@345 305
universe@345 306 static int cmp_func_swap_args(void *data, const void *x, const void *y) {
olaf@348 307 cmpfnc_swapargs_info* info = data;
universe@345 308 return info->func(x, y, info->data);
universe@345 309 }
universe@345 310
universe@345 311 static void ucx_qsort_r(void *array, size_t count, size_t elemsize,
universe@345 312 cmp_func cmpfnc, void *data) {
universe@345 313 struct cmpfnc_swapargs_info info;
universe@345 314 info.func = cmpfnc;
universe@345 315 info.data = data;
universe@345 316 qsort_r(array, count, elemsize, &info, cmp_func_swap_args);
universe@345 317 }
universe@345 318 #endif /* USE_UCX_QSORT_R */
universe@345 319
universe@345 320 void ucx_array_sort(UcxArray array, cmp_func cmpfnc, void *data) {
universe@345 321 ucx_array_sort_impl(array.data, array.size, array.elemsize, cmpfnc, data);
universe@7 322 }
universe@7 323
universe@334 324 void ucx_array_remove(UcxArray *array, size_t index) {
universe@336 325 array->size--;
universe@336 326 if (index < array->size) {
universe@336 327 void* dest = ucx_array_at(*array, index);
universe@336 328 void* src = ucx_array_at(*array, index+1);
universe@336 329 memmove(dest, src, (array->size - index)*array->elemsize);
universe@336 330 }
universe@123 331 }
universe@123 332
universe@334 333 void ucx_array_remove_fast(UcxArray *array, size_t index) {
universe@336 334 array->size--;
universe@336 335 if (index < array->size) {
universe@336 336 void* dest = ucx_array_at(*array, index);
universe@336 337 void* src = ucx_array_at(*array, array->size);
universe@336 338 memcpy(dest, src, array->elemsize);
universe@336 339 }
universe@7 340 }
universe@7 341
universe@334 342 int ucx_array_shrink(UcxArray* array) {
universe@336 343 void* newptr = alrealloc(array->allocator, array->data,
universe@336 344 array->size*array->elemsize);
universe@336 345 if (newptr) {
universe@336 346 array->data = newptr;
universe@336 347 array->capacity = array->size;
universe@336 348 return 0;
universe@336 349 } else {
universe@336 350 return 1;
universe@336 351 }
universe@123 352 }
universe@123 353
universe@334 354 int ucx_array_resize(UcxArray* array, size_t capacity) {
universe@336 355 if (array->capacity >= capacity) {
universe@336 356 void* newptr = alrealloc(array->allocator, array->data,
universe@336 357 capacity*array->elemsize);
universe@336 358 if (newptr) {
universe@336 359 array->data = newptr;
universe@336 360 array->capacity = capacity;
universe@336 361 if (array->size > array->capacity) {
universe@336 362 array->size = array->capacity;
universe@336 363 }
universe@336 364 return 0;
universe@336 365 } else {
universe@336 366 return 1;
universe@336 367 }
universe@336 368 } else {
universe@336 369 return ucx_array_reserve(array, capacity);
universe@336 370 }
universe@87 371 }
universe@87 372
universe@334 373 int ucx_array_reserve(UcxArray* array, size_t capacity) {
universe@336 374 if (array->capacity > capacity) {
universe@336 375 return 0;
universe@336 376 } else {
universe@336 377 void* newptr = alrealloc(array->allocator, array->data,
universe@336 378 capacity*array->elemsize);
universe@336 379 if (newptr) {
universe@336 380 array->data = newptr;
universe@336 381 array->capacity = capacity;
universe@336 382 return 0;
universe@336 383 } else {
universe@336 384 return 1;
universe@336 385 }
universe@336 386 }
universe@7 387 }

mercurial