src/array.c

Thu, 04 Jul 2019 22:23:15 +0200

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

implements ucx_array_sort()

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2019 Mike Becker, Olaf Wintermann All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  */
    29 #include "ucx/array.h"
    30 #include "ucx/utils.h"
    32 #include <string.h>
    34 UcxArray ucx_array_new(size_t capacity, size_t elemsize) {
    35     return ucx_array_new_a(capacity, elemsize, ucx_default_allocator());
    36 }
    38 UcxArray ucx_array_new_a(size_t capacity, size_t elemsize,
    39         UcxAllocator* allocator) {
    40     UcxArray array;
    42     array.allocator = allocator;
    43     array.elemsize = elemsize;
    44     array.size = 0;
    45     array.data = alcalloc(allocator, capacity, elemsize);
    47     if (array.data) {
    48         array.capacity = capacity;
    49     } else {
    50         array.capacity = 0;
    51     }
    53     return array;
    54 }
    56 UcxArray ucx_array_clone(UcxArray array) {
    57     UcxArray clone;
    59     clone.allocator = array.allocator;
    60     clone.elemsize = array.elemsize;
    61     clone.size = array.size;
    62     clone.data = alcalloc(array.allocator, array.capacity, array.elemsize);
    64     if (clone.data) {
    65         clone.capacity = array.capacity;
    66         memcpy(clone.data, array.data, array.size*array.elemsize);
    67     } else {
    68         clone.capacity = clone.size = 0;
    69     }
    71     return clone;
    72 }
    74 int ucx_array_equals(UcxArray array1, UcxArray array2,
    75         cmp_func cmpfnc, void* data) {
    77     if (array1.size != array2.size || array1.elemsize != array2.elemsize) {
    78         return 0;
    79     } else {
    80         if (array1.size == 0)
    81             return 1;
    83         if (cmpfnc == NULL) {
    84             cmpfnc = ucx_cmp_mem;
    85             data = &array1.elemsize;
    86         }
    88         for (size_t i = 0 ; i < array1.size ; i++) {
    89             int r = cmpfnc(
    90                     ucx_array_at(array1, i),
    91                     ucx_array_at(array2, i),
    92                     data);
    93             if (r != 0)
    94                 return 0;
    95         }
    96         return 1;
    97     }
    98 }
   100 void ucx_array_free(UcxArray *array) {
   101     alfree(array->allocator, array->data);
   102     array->data = NULL;
   103     array->capacity = array->size = 0;
   104 }
   106 int ucx_array_append(UcxArray *array, void *data) {
   107     if (array->size == array->capacity) {
   108         if (ucx_array_reserve(array, array->capacity*2)) {
   109             return 1;
   110         }
   111     }
   113     void* dest = ucx_array_at(*array, array->size++);
   114     if (data) {
   115         memcpy(dest, data, array->elemsize);
   116     } else {
   117         memset(dest, 0, array->elemsize);
   118     }
   120     return 0;
   121 }
   123 int ucx_array_prepend(UcxArray *array, void *data) {
   124     if (array->size == array->capacity) {
   125         if (ucx_array_reserve(array, array->capacity*2)) {
   126             return 1;
   127         }
   128     }
   130     array->size++;
   132     if (array->size > 1) {
   133         void *dest = ucx_array_at(*array,1);
   134         memmove(dest, array->data, array->elemsize*array->size);
   135     }
   137     if (data) {
   138         memcpy(array->data, data, array->elemsize);
   139     } else {
   140         memset(array->data, 0, array->elemsize);
   141     }
   143     return 0;
   144 }
   146 int ucx_array_concat(UcxArray *array1, const UcxArray *array2) {
   148     if (array1->elemsize != array2->elemsize)
   149         return 1;
   151     size_t capacity = array1->capacity+array2->capacity;
   153     if (array1->capacity < capacity) {
   154         if (ucx_array_reserve(array1, capacity)) {
   155             return 1;
   156         }
   157     }
   159     void* dest = ucx_array_at(*array1, array1->size);
   160     memcpy(dest, array2->data, array2->size*array2->elemsize);
   162     array1->size += array2->size;
   164     return 0;
   165 }
   167 void *ucx_array_at(UcxArray array, size_t index) {
   168     char* memory = array.data;
   169     char* loc = memory + index*array.elemsize;
   170     return loc;
   171 }
   173 size_t ucx_array_find(UcxArray array, void *elem, cmp_func cmpfnc, void *data) {
   175     if (cmpfnc == NULL) {
   176         cmpfnc = ucx_cmp_mem;
   177         data = &array.elemsize;
   178     }
   180     if (array.size > 0) {
   181         for (size_t i = 0 ; i < array.size ; i++) {
   182             void* ptr = ucx_array_at(array, i);
   183             if (cmpfnc(ptr, elem, data) == 0) {
   184                 return i;
   185             }
   186         }
   187         return array.size;
   188     } else {
   189         return 0;
   190     }
   191 }
   193 int ucx_array_contains(UcxArray array, void *elem, cmp_func cmpfnc, void *data) {
   194     return ucx_array_find(array, elem, cmpfnc, data) != array.size;
   195 }
   197 static void ucx_array_merge(UcxArray *array, cmp_func cmpfnc, void *data,
   198         size_t start, size_t mid, size_t end) { 
   200     size_t rightstart = mid + 1; 
   202     if (cmpfnc(ucx_array_at(*array, mid),
   203             ucx_array_at(*array, rightstart), data) <= 0) {
   204         /* already sorted */
   205         return;
   206     }
   208     // we need memory for one element
   209     void *value = malloc(array->elemsize);
   211     while (start <= mid && rightstart <= end) { 
   212         if (cmpfnc(ucx_array_at(*array, start),
   213                 ucx_array_at(*array, rightstart), data) <= 0) { 
   214             start++; 
   215         } else {
   216             // save the value from the right
   217             memcpy(value, ucx_array_at(*array, rightstart), array->elemsize);
   219             // shift all left elements one element to the right
   220             size_t shiftcount = rightstart-start;
   221             void *startptr = ucx_array_at(*array, start);
   222             void *dest = ucx_array_at(*array, start+1);
   223             memmove(dest, startptr, shiftcount*array->elemsize);
   225             // bring the first value from the right to the left
   226             memcpy(startptr, value, array->elemsize);
   228             start++; 
   229             mid++; 
   230             rightstart++; 
   231         }
   232     }
   234     // free the temporary memory
   235     free(value);
   236 } 
   238 static void ucx_array_mergesort(UcxArray *array, cmp_func cmpfnc, void *data,
   239         size_t l, size_t r) { 
   240     if (l < r) {
   241         size_t m = l + (r - l) / 2; 
   243         ucx_array_mergesort(array, cmpfnc, data, l, m); 
   244         ucx_array_mergesort(array, cmpfnc, data, m + 1, r); 
   245         ucx_array_merge(array, cmpfnc, data, l, m, r);
   246     } 
   247 }
   249 void ucx_array_sort(UcxArray array, cmp_func cmpfnc, void *data) {   
   250     ucx_array_mergesort(&array, cmpfnc, data, 0, array.size-1);
   251 }
   253 void ucx_array_remove(UcxArray *array, size_t index) {
   254     array->size--;
   255     if (index < array->size) {
   256         void* dest = ucx_array_at(*array, index);
   257         void* src = ucx_array_at(*array, index+1);
   258         memmove(dest, src, (array->size - index)*array->elemsize);
   259     }
   260 }
   262 void ucx_array_remove_fast(UcxArray *array, size_t index) {
   263     array->size--;
   264     if (index < array->size) {       
   265         void* dest = ucx_array_at(*array, index);
   266         void* src = ucx_array_at(*array, array->size);
   267         memcpy(dest, src, array->elemsize);
   268     }
   269 }
   271 int ucx_array_shrink(UcxArray* array) {
   272     void* newptr = alrealloc(array->allocator, array->data,
   273                 array->size*array->elemsize);
   274     if (newptr) {
   275         array->data = newptr;
   276         array->capacity = array->size;
   277         return 0;
   278     } else {
   279         return 1;
   280     }
   281 }
   283 int ucx_array_resize(UcxArray* array, size_t capacity) {
   284     if (array->capacity >= capacity) {
   285         void* newptr = alrealloc(array->allocator, array->data,
   286                 capacity*array->elemsize);
   287         if (newptr) {
   288             array->data = newptr;
   289             array->capacity = capacity;
   290             if (array->size > array->capacity) {
   291                 array->size = array->capacity;
   292             }
   293             return 0;
   294         } else {
   295             return 1;
   296         }
   297     } else {
   298         return ucx_array_reserve(array, capacity);
   299     }
   300 }
   302 int ucx_array_reserve(UcxArray* array, size_t capacity) {
   303     if (array->capacity > capacity) {
   304         return 0;
   305     } else {
   306         void* newptr = alrealloc(array->allocator, array->data,
   307                 capacity*array->elemsize);
   308         if (newptr) {
   309             array->data = newptr;
   310             array->capacity = capacity;
   311             return 0;
   312         } else {
   313             return 1;
   314         }
   315     }
   316 }

mercurial