1.1 --- a/src/array.c Tue Sep 24 20:16:00 2019 +0200 1.2 +++ b/src/array.c Thu Oct 03 10:55:39 2019 +0200 1.3 @@ -113,61 +113,70 @@ 1.4 return 0; 1.5 } 1.6 1.7 -UcxArray ucx_array_new(size_t capacity, size_t elemsize) { 1.8 +UcxArray* ucx_array_new(size_t capacity, size_t elemsize) { 1.9 return ucx_array_new_a(capacity, elemsize, ucx_default_allocator()); 1.10 } 1.11 1.12 -UcxArray ucx_array_new_a(size_t capacity, size_t elemsize, 1.13 +UcxArray* ucx_array_new_a(size_t capacity, size_t elemsize, 1.14 UcxAllocator* allocator) { 1.15 - UcxArray array; 1.16 - 1.17 - array.allocator = allocator; 1.18 - array.elemsize = elemsize; 1.19 - array.size = 0; 1.20 - array.data = alcalloc(allocator, capacity, elemsize); 1.21 - 1.22 - if (array.data) { 1.23 - array.capacity = capacity; 1.24 - } else { 1.25 - array.capacity = 0; 1.26 + UcxArray* array = almalloc(allocator, sizeof(UcxArray)); 1.27 + if(array) { 1.28 + ucx_array_init_a(array, capacity, elemsize, allocator); 1.29 } 1.30 - 1.31 return array; 1.32 } 1.33 1.34 -UcxArray ucx_array_clone(UcxArray array) { 1.35 - UcxArray clone; 1.36 +void ucx_array_init(UcxArray* array, size_t capacity, size_t elemsize) { 1.37 + ucx_array_init_a(array, capacity, elemsize, ucx_default_allocator()); 1.38 +} 1.39 + 1.40 +void ucx_array_init_a(UcxArray* array, size_t capacity, size_t elemsize, 1.41 + UcxAllocator* allocator) { 1.42 1.43 - clone.allocator = array.allocator; 1.44 - clone.elemsize = array.elemsize; 1.45 - clone.size = array.size; 1.46 - clone.data = alcalloc(array.allocator, array.capacity, array.elemsize); 1.47 + array->allocator = allocator; 1.48 + array->elemsize = elemsize; 1.49 + array->size = 0; 1.50 + array->data = alcalloc(allocator, capacity, elemsize); 1.51 1.52 - if (clone.data) { 1.53 - clone.capacity = array.capacity; 1.54 - memcpy(clone.data, array.data, array.size*array.elemsize); 1.55 + if (array->data) { 1.56 + array->capacity = capacity; 1.57 } else { 1.58 - clone.capacity = clone.size = 0; 1.59 + array->capacity = 0; 1.60 + } 1.61 +} 1.62 + 1.63 +int ucx_array_clone(UcxArray* dest, UcxArray const* src) { 1.64 + if (ucx_array_ensurecap(dest, src->capacity)) { 1.65 + return 1; 1.66 } 1.67 1.68 - return clone; 1.69 + dest->elemsize = src->elemsize; 1.70 + dest->size = src->size; 1.71 + 1.72 + if (dest->data) { 1.73 + memcpy(dest->data, src->data, src->size*src->elemsize); 1.74 + } 1.75 + 1.76 + return 0; 1.77 } 1.78 1.79 -int ucx_array_equals(UcxArray array1, UcxArray array2, 1.80 +int ucx_array_equals(UcxArray const *array1, UcxArray const *array2, 1.81 cmp_func cmpfnc, void* data) { 1.82 1.83 - if (array1.size != array2.size || array1.elemsize != array2.elemsize) { 1.84 + if (array1->size != array2->size || array1->elemsize != array2->elemsize) { 1.85 return 0; 1.86 } else { 1.87 - if (array1.size == 0) 1.88 + if (array1->size == 0) 1.89 return 1; 1.90 1.91 + size_t elemsize; 1.92 if (cmpfnc == NULL) { 1.93 cmpfnc = ucx_cmp_mem; 1.94 - data = &array1.elemsize; 1.95 + elemsize = array1->elemsize; 1.96 + data = &elemsize; 1.97 } 1.98 1.99 - for (size_t i = 0 ; i < array1.size ; i++) { 1.100 + for (size_t i = 0 ; i < array1->size ; i++) { 1.101 int r = cmpfnc( 1.102 ucx_array_at(array1, i), 1.103 ucx_array_at(array2, i), 1.104 @@ -180,16 +189,22 @@ 1.105 } 1.106 1.107 void ucx_array_destroy(UcxArray *array) { 1.108 - alfree(array->allocator, array->data); 1.109 + if(array->data) 1.110 + alfree(array->allocator, array->data); 1.111 array->data = NULL; 1.112 array->capacity = array->size = 0; 1.113 } 1.114 1.115 +void ucx_array_free(UcxArray *array) { 1.116 + ucx_array_destroy(array); 1.117 + alfree(array->allocator, array); 1.118 +} 1.119 + 1.120 int ucx_array_append_from(UcxArray *array, void *data, size_t count) { 1.121 if (ucx_array_ensurecap(array, array->size + count)) 1.122 return 1; 1.123 1.124 - void* dest = ucx_array_at(*array, array->size); 1.125 + void* dest = ucx_array_at(array, array->size); 1.126 if (data) { 1.127 memcpy(dest, data, array->elemsize*count); 1.128 } else { 1.129 @@ -205,7 +220,7 @@ 1.130 return 1; 1.131 1.132 if (array->size > 0) { 1.133 - void *dest = ucx_array_at(*array, count); 1.134 + void *dest = ucx_array_at(array, count); 1.135 memmove(dest, array->data, array->elemsize*array->size); 1.136 } 1.137 1.138 @@ -228,7 +243,7 @@ 1.139 array->size = index+count; 1.140 } 1.141 1.142 - void *dest = ucx_array_at(*array, index); 1.143 + void *dest = ucx_array_at(array, index); 1.144 if (data) { 1.145 memcpy(dest, data, array->elemsize*count); 1.146 } else { 1.147 @@ -278,7 +293,7 @@ 1.148 } 1.149 } 1.150 1.151 - void* dest = ucx_array_at(*array1, array1->size); 1.152 + void* dest = ucx_array_at(array1, array1->size); 1.153 memcpy(dest, array2->data, array2->size*array2->elemsize); 1.154 1.155 array1->size += array2->size; 1.156 @@ -286,34 +301,38 @@ 1.157 return 0; 1.158 } 1.159 1.160 -void *ucx_array_at(UcxArray array, size_t index) { 1.161 - char* memory = array.data; 1.162 - char* loc = memory + index*array.elemsize; 1.163 +void *ucx_array_at(UcxArray const *array, size_t index) { 1.164 + char* memory = array->data; 1.165 + char* loc = memory + index*array->elemsize; 1.166 return loc; 1.167 } 1.168 1.169 -size_t ucx_array_find(UcxArray array, void *elem, cmp_func cmpfnc, void *data) { 1.170 +size_t ucx_array_find(UcxArray const *array, void *elem, 1.171 + cmp_func cmpfnc, void *data) { 1.172 1.173 + size_t elemsize; 1.174 if (cmpfnc == NULL) { 1.175 cmpfnc = ucx_cmp_mem; 1.176 - data = &array.elemsize; 1.177 + elemsize = array->elemsize; 1.178 + data = &elemsize; 1.179 } 1.180 1.181 - if (array.size > 0) { 1.182 - for (size_t i = 0 ; i < array.size ; i++) { 1.183 + if (array->size > 0) { 1.184 + for (size_t i = 0 ; i < array->size ; i++) { 1.185 void* ptr = ucx_array_at(array, i); 1.186 if (cmpfnc(ptr, elem, data) == 0) { 1.187 return i; 1.188 } 1.189 } 1.190 - return array.size; 1.191 + return array->size; 1.192 } else { 1.193 return 0; 1.194 } 1.195 } 1.196 1.197 -int ucx_array_contains(UcxArray array, void *elem, cmp_func cmpfnc, void *data) { 1.198 - return ucx_array_find(array, elem, cmpfnc, data) != array.size; 1.199 +int ucx_array_contains(UcxArray const *array, void *elem, 1.200 + cmp_func cmpfnc, void *data) { 1.201 + return ucx_array_find(array, elem, cmpfnc, data) != array->size; 1.202 } 1.203 1.204 static void ucx_mergesort_merge(void *arrdata,size_t elemsize, 1.205 @@ -397,15 +416,16 @@ 1.206 } 1.207 #endif /* USE_UCX_QSORT_R */ 1.208 1.209 -void ucx_array_sort(UcxArray array, cmp_func cmpfnc, void *data) { 1.210 - ucx_array_sort_impl(array.data, array.size, array.elemsize, cmpfnc, data); 1.211 +void ucx_array_sort(UcxArray* array, cmp_func cmpfnc, void *data) { 1.212 + ucx_array_sort_impl(array->data, array->size, array->elemsize, 1.213 + cmpfnc, data); 1.214 } 1.215 1.216 void ucx_array_remove(UcxArray *array, size_t index) { 1.217 array->size--; 1.218 if (index < array->size) { 1.219 - void* dest = ucx_array_at(*array, index); 1.220 - void* src = ucx_array_at(*array, index+1); 1.221 + void* dest = ucx_array_at(array, index); 1.222 + void* src = ucx_array_at(array, index+1); 1.223 memmove(dest, src, (array->size - index)*array->elemsize); 1.224 } 1.225 } 1.226 @@ -413,8 +433,8 @@ 1.227 void ucx_array_remove_fast(UcxArray *array, size_t index) { 1.228 array->size--; 1.229 if (index < array->size) { 1.230 - void* dest = ucx_array_at(*array, index); 1.231 - void* src = ucx_array_at(*array, array->size); 1.232 + void* dest = ucx_array_at(array, index); 1.233 + void* src = ucx_array_at(array, array->size); 1.234 memcpy(dest, src, array->elemsize); 1.235 } 1.236 }