src/array.c

Thu, 04 Jul 2019 22:32:03 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 04 Jul 2019 22:32:03 +0200
branch
feature/array
changeset 337
f695ae118460
parent 336
6d7aa8a1a3b3
child 342
8f0a3c00d1d2
permissions
-rw-r--r--

adds ucx_array_set()

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

mercurial