src/array.c

Wed, 07 Aug 2019 21:44:35 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 07 Aug 2019 21:44:35 +0200
branch
feature/array
changeset 347
62660a4875c9
parent 346
1a9c112f4116
child 348
3b9b4f6e9fd6
permissions
-rw-r--r--

fixes build on FreeBSD

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

mercurial