src/array.c

Wed, 07 Aug 2019 19:43:50 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 07 Aug 2019 19:43:50 +0200
branch
feature/array
changeset 343
c09da4ee177f
parent 342
8f0a3c00d1d2
child 345
6089eb30a51a
permissions
-rw-r--r--

adjusts the documentation for ucx_array_sort() to the current plans

103
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 93
diff changeset
1 /*
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 93
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 93
diff changeset
3 *
334
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
4 * Copyright 2019 Mike Becker, Olaf Wintermann All rights reserved.
103
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 93
diff changeset
5 *
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 93
diff changeset
6 * Redistribution and use in source and binary forms, with or without
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 93
diff changeset
7 * modification, are permitted provided that the following conditions are met:
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 93
diff changeset
8 *
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 93
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 93
diff changeset
10 * notice, this list of conditions and the following disclaimer.
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 93
diff changeset
11 *
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 93
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 93
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 93
diff changeset
14 * documentation and/or other materials provided with the distribution.
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 93
diff changeset
15 *
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 93
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 93
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 93
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 93
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 93
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 93
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 93
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 93
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 93
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 93
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 93
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 93
diff changeset
27 */
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 93
diff changeset
28
334
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
29 #include "ucx/array.h"
336
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
30 #include "ucx/utils.h"
4
dbc31727b38e added dlist specification
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31
336
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
32 #include <string.h>
125
fca8efb122de changed suffix for allocator aware functions + added allocator aware functions for UcxList
Mike Becker <universe@uap-core.de>
parents: 123
diff changeset
33
334
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
34 UcxArray ucx_array_new(size_t capacity, size_t elemsize) {
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
35 return ucx_array_new_a(capacity, elemsize, ucx_default_allocator());
18
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
36 }
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
37
334
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
38 UcxArray ucx_array_new_a(size_t capacity, size_t elemsize,
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
39 UcxAllocator* allocator) {
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
40 UcxArray array;
18
69636f81db31 added clone and equals to lists
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
41
336
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
42 array.allocator = allocator;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
43 array.elemsize = elemsize;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
44 array.size = 0;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
45 array.data = alcalloc(allocator, capacity, elemsize);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
46
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
47 if (array.data) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
48 array.capacity = capacity;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
49 } else {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
50 array.capacity = 0;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
51 }
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
52
334
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
53 return array;
125
fca8efb122de changed suffix for allocator aware functions + added allocator aware functions for UcxList
Mike Becker <universe@uap-core.de>
parents: 123
diff changeset
54 }
fca8efb122de changed suffix for allocator aware functions + added allocator aware functions for UcxList
Mike Becker <universe@uap-core.de>
parents: 123
diff changeset
55
334
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
56 UcxArray ucx_array_clone(UcxArray array) {
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
57 UcxArray clone;
7
68091406d1cf added dlist to makefile, implemented first functions, discarded some fails
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
58
336
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
59 clone.allocator = array.allocator;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
60 clone.elemsize = array.elemsize;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
61 clone.size = array.size;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
62 clone.data = alcalloc(array.allocator, array.capacity, array.elemsize);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
63
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
64 if (clone.data) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
65 clone.capacity = array.capacity;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
66 memcpy(clone.data, array.data, array.size*array.elemsize);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
67 } else {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
68 clone.capacity = clone.size = 0;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
69 }
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
70
334
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
71 return clone;
7
68091406d1cf added dlist to makefile, implemented first functions, discarded some fails
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
72 }
68091406d1cf added dlist to makefile, implemented first functions, discarded some fails
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
73
334
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
74 int ucx_array_equals(UcxArray array1, UcxArray array2,
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
75 cmp_func cmpfnc, void* data) {
7
68091406d1cf added dlist to makefile, implemented first functions, discarded some fails
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
76
336
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
77 if (array1.size != array2.size || array1.elemsize != array2.elemsize) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
78 return 0;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
79 } else {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
80 if (array1.size == 0)
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
81 return 1;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
82
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
83 if (cmpfnc == NULL) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
84 cmpfnc = ucx_cmp_mem;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
85 data = &array1.elemsize;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
86 }
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
87
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
88 for (size_t i = 0 ; i < array1.size ; i++) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
89 int r = cmpfnc(
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
90 ucx_array_at(array1, i),
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
91 ucx_array_at(array2, i),
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
92 data);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
93 if (r != 0)
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
94 return 0;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
95 }
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
96 return 1;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
97 }
7
68091406d1cf added dlist to makefile, implemented first functions, discarded some fails
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
98 }
68091406d1cf added dlist to makefile, implemented first functions, discarded some fails
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
99
334
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
100 void ucx_array_free(UcxArray *array) {
336
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
101 alfree(array->allocator, array->data);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
102 array->data = NULL;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
103 array->capacity = array->size = 0;
123
7fb0f74517c5 changed signature of sstrncat + some documentation for UcxList + new features for UcxList
Mike Becker <universe@uap-core.de>
parents: 122
diff changeset
104 }
7fb0f74517c5 changed signature of sstrncat + some documentation for UcxList + new features for UcxList
Mike Becker <universe@uap-core.de>
parents: 122
diff changeset
105
334
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
106 int ucx_array_append(UcxArray *array, void *data) {
336
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
107 if (array->size == array->capacity) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
108 if (ucx_array_reserve(array, array->capacity*2)) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
109 return 1;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
110 }
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
111 }
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
112
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
113 void* dest = ucx_array_at(*array, array->size++);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
114 if (data) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
115 memcpy(dest, data, array->elemsize);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
116 } else {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
117 memset(dest, 0, array->elemsize);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
118 }
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
119
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
120 return 0;
334
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
121 }
8
9cd2b2460db0 completed dlist
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
122
334
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
123 int ucx_array_prepend(UcxArray *array, void *data) {
336
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
124 if (array->size == array->capacity) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
125 if (ucx_array_reserve(array, array->capacity*2)) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
126 return 1;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
127 }
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
128 }
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
129
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
130 array->size++;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
131
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
132 if (array->size > 1) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
133 void *dest = ucx_array_at(*array,1);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
134 memmove(dest, array->data, array->elemsize*array->size);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
135 }
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
136
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
137 if (data) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
138 memcpy(array->data, data, array->elemsize);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
139 } else {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
140 memset(array->data, 0, array->elemsize);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
141 }
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
142
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
143 return 0;
7
68091406d1cf added dlist to makefile, implemented first functions, discarded some fails
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
144 }
68091406d1cf added dlist to makefile, implemented first functions, discarded some fails
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
145
337
f695ae118460 adds ucx_array_set()
Mike Becker <universe@uap-core.de>
parents: 336
diff changeset
146 int ucx_array_set(UcxArray *array, size_t index, void *data) {
f695ae118460 adds ucx_array_set()
Mike Becker <universe@uap-core.de>
parents: 336
diff changeset
147 if (index >= array->size) {
f695ae118460 adds ucx_array_set()
Mike Becker <universe@uap-core.de>
parents: 336
diff changeset
148 if (ucx_array_reserve(array, index+1)) {
f695ae118460 adds ucx_array_set()
Mike Becker <universe@uap-core.de>
parents: 336
diff changeset
149 return 1;
f695ae118460 adds ucx_array_set()
Mike Becker <universe@uap-core.de>
parents: 336
diff changeset
150 }
f695ae118460 adds ucx_array_set()
Mike Becker <universe@uap-core.de>
parents: 336
diff changeset
151 array->size = index+1;
f695ae118460 adds ucx_array_set()
Mike Becker <universe@uap-core.de>
parents: 336
diff changeset
152 }
f695ae118460 adds ucx_array_set()
Mike Becker <universe@uap-core.de>
parents: 336
diff changeset
153
f695ae118460 adds ucx_array_set()
Mike Becker <universe@uap-core.de>
parents: 336
diff changeset
154 void *dest = ucx_array_at(*array, index);
f695ae118460 adds ucx_array_set()
Mike Becker <universe@uap-core.de>
parents: 336
diff changeset
155 if (data) {
f695ae118460 adds ucx_array_set()
Mike Becker <universe@uap-core.de>
parents: 336
diff changeset
156 memcpy(dest, data, array->elemsize);
f695ae118460 adds ucx_array_set()
Mike Becker <universe@uap-core.de>
parents: 336
diff changeset
157 } else {
f695ae118460 adds ucx_array_set()
Mike Becker <universe@uap-core.de>
parents: 336
diff changeset
158 memset(dest, 0, array->elemsize);
f695ae118460 adds ucx_array_set()
Mike Becker <universe@uap-core.de>
parents: 336
diff changeset
159 }
f695ae118460 adds ucx_array_set()
Mike Becker <universe@uap-core.de>
parents: 336
diff changeset
160
f695ae118460 adds ucx_array_set()
Mike Becker <universe@uap-core.de>
parents: 336
diff changeset
161 return 0;
f695ae118460 adds ucx_array_set()
Mike Becker <universe@uap-core.de>
parents: 336
diff changeset
162 }
f695ae118460 adds ucx_array_set()
Mike Becker <universe@uap-core.de>
parents: 336
diff changeset
163
334
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
164 int ucx_array_concat(UcxArray *array1, const UcxArray *array2) {
336
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
165
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
166 if (array1->elemsize != array2->elemsize)
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
167 return 1;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
168
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
169 size_t capacity = array1->capacity+array2->capacity;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
170
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
171 if (array1->capacity < capacity) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
172 if (ucx_array_reserve(array1, capacity)) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
173 return 1;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
174 }
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
175 }
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
176
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
177 void* dest = ucx_array_at(*array1, array1->size);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
178 memcpy(dest, array2->data, array2->size*array2->elemsize);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
179
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
180 array1->size += array2->size;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
181
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
182 return 0;
123
7fb0f74517c5 changed signature of sstrncat + some documentation for UcxList + new features for UcxList
Mike Becker <universe@uap-core.de>
parents: 122
diff changeset
183 }
7fb0f74517c5 changed signature of sstrncat + some documentation for UcxList + new features for UcxList
Mike Becker <universe@uap-core.de>
parents: 122
diff changeset
184
334
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
185 void *ucx_array_at(UcxArray array, size_t index) {
336
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
186 char* memory = array.data;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
187 char* loc = memory + index*array.elemsize;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
188 return loc;
7
68091406d1cf added dlist to makefile, implemented first functions, discarded some fails
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
189 }
68091406d1cf added dlist to makefile, implemented first functions, discarded some fails
Mike Becker <universe@uap-core.de>
parents: 4
diff changeset
190
334
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
191 size_t ucx_array_find(UcxArray array, void *elem, cmp_func cmpfnc, void *data) {
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
192
336
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
193 if (cmpfnc == NULL) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
194 cmpfnc = ucx_cmp_mem;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
195 data = &array.elemsize;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
196 }
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
197
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
198 if (array.size > 0) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
199 for (size_t i = 0 ; i < array.size ; i++) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
200 void* ptr = ucx_array_at(array, i);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
201 if (cmpfnc(ptr, elem, data) == 0) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
202 return i;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
203 }
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
204 }
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
205 return array.size;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
206 } else {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
207 return 0;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
208 }
334
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
209 }
35
fdabd1240b69 added mkdir for build directory to makefile + added qsort for list and dlist
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
210
334
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
211 int ucx_array_contains(UcxArray array, void *elem, cmp_func cmpfnc, void *data) {
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
212 return ucx_array_find(array, elem, cmpfnc, data) != array.size;
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
213 }
35
fdabd1240b69 added mkdir for build directory to makefile + added qsort for list and dlist
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
214
336
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
215 static void ucx_array_merge(UcxArray *array, cmp_func cmpfnc, void *data,
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
216 size_t start, size_t mid, size_t end) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
217
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
218 size_t rightstart = mid + 1;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
219
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
220 if (cmpfnc(ucx_array_at(*array, mid),
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
221 ucx_array_at(*array, rightstart), data) <= 0) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
222 /* already sorted */
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
223 return;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
224 }
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
225
342
8f0a3c00d1d2 removes cumbersome array accessor macros
Mike Becker <universe@uap-core.de>
parents: 337
diff changeset
226 /* we need memory for one element */
336
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
227 void *value = malloc(array->elemsize);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
228
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
229 while (start <= mid && rightstart <= end) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
230 if (cmpfnc(ucx_array_at(*array, start),
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
231 ucx_array_at(*array, rightstart), data) <= 0) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
232 start++;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
233 } else {
342
8f0a3c00d1d2 removes cumbersome array accessor macros
Mike Becker <universe@uap-core.de>
parents: 337
diff changeset
234 /* save the value from the right */
336
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
235 memcpy(value, ucx_array_at(*array, rightstart), array->elemsize);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
236
342
8f0a3c00d1d2 removes cumbersome array accessor macros
Mike Becker <universe@uap-core.de>
parents: 337
diff changeset
237 /* shift all left elements one element to the right */
336
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
238 size_t shiftcount = rightstart-start;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
239 void *startptr = ucx_array_at(*array, start);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
240 void *dest = ucx_array_at(*array, start+1);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
241 memmove(dest, startptr, shiftcount*array->elemsize);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
242
342
8f0a3c00d1d2 removes cumbersome array accessor macros
Mike Becker <universe@uap-core.de>
parents: 337
diff changeset
243 /* bring the first value from the right to the left */
336
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
244 memcpy(startptr, value, array->elemsize);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
245
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
246 start++;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
247 mid++;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
248 rightstart++;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
249 }
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
250 }
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
251
342
8f0a3c00d1d2 removes cumbersome array accessor macros
Mike Becker <universe@uap-core.de>
parents: 337
diff changeset
252 /* free the temporary memory */
336
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
253 free(value);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
254 }
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
255
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
256 static void ucx_array_mergesort(UcxArray *array, cmp_func cmpfnc, void *data,
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
257 size_t l, size_t r) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
258 if (l < r) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
259 size_t m = l + (r - l) / 2;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
260
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
261 ucx_array_mergesort(array, cmpfnc, data, l, m);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
262 ucx_array_mergesort(array, cmpfnc, data, m + 1, r);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
263 ucx_array_merge(array, cmpfnc, data, l, m, r);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
264 }
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
265 }
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
266
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
267 void ucx_array_sort(UcxArray array, cmp_func cmpfnc, void *data) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
268 ucx_array_mergesort(&array, cmpfnc, data, 0, array.size-1);
35
fdabd1240b69 added mkdir for build directory to makefile + added qsort for list and dlist
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
269 }
fdabd1240b69 added mkdir for build directory to makefile + added qsort for list and dlist
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
270
334
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
271 void ucx_array_remove(UcxArray *array, size_t index) {
336
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
272 array->size--;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
273 if (index < array->size) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
274 void* dest = ucx_array_at(*array, index);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
275 void* src = ucx_array_at(*array, index+1);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
276 memmove(dest, src, (array->size - index)*array->elemsize);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
277 }
334
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
278 }
37
ec296899d12f replaced qsort with natural merge sort
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
279
334
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
280 void ucx_array_remove_fast(UcxArray *array, size_t index) {
336
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
281 array->size--;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
282 if (index < array->size) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
283 void* dest = ucx_array_at(*array, index);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
284 void* src = ucx_array_at(*array, array->size);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
285 memcpy(dest, src, array->elemsize);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
286 }
35
fdabd1240b69 added mkdir for build directory to makefile + added qsort for list and dlist
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
287 }
fdabd1240b69 added mkdir for build directory to makefile + added qsort for list and dlist
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
288
334
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
289 int ucx_array_shrink(UcxArray* array) {
336
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
290 void* newptr = alrealloc(array->allocator, array->data,
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
291 array->size*array->elemsize);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
292 if (newptr) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
293 array->data = newptr;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
294 array->capacity = array->size;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
295 return 0;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
296 } else {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
297 return 1;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
298 }
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 8
diff changeset
299 }
22
76cdd8209f1f added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
300
334
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
301 int ucx_array_resize(UcxArray* array, size_t capacity) {
336
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
302 if (array->capacity >= capacity) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
303 void* newptr = alrealloc(array->allocator, array->data,
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
304 capacity*array->elemsize);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
305 if (newptr) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
306 array->data = newptr;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
307 array->capacity = capacity;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
308 if (array->size > array->capacity) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
309 array->size = array->capacity;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
310 }
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
311 return 0;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
312 } else {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
313 return 1;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
314 }
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
315 } else {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
316 return ucx_array_reserve(array, capacity);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
317 }
125
fca8efb122de changed suffix for allocator aware functions + added allocator aware functions for UcxList
Mike Becker <universe@uap-core.de>
parents: 123
diff changeset
318 }
334
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
319
bc81faa9afda adds array interface and tests
Mike Becker <universe@uap-core.de>
parents: 323
diff changeset
320 int ucx_array_reserve(UcxArray* array, size_t capacity) {
336
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
321 if (array->capacity > capacity) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
322 return 0;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
323 } else {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
324 void* newptr = alrealloc(array->allocator, array->data,
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
325 capacity*array->elemsize);
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
326 if (newptr) {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
327 array->data = newptr;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
328 array->capacity = capacity;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
329 return 0;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
330 } else {
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
331 return 1;
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
332 }
6d7aa8a1a3b3 implements ucx_array_sort()
Mike Becker <universe@uap-core.de>
parents: 334
diff changeset
333 }
22
76cdd8209f1f added ucx_dlist_remove and tests + fixed makefile error
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
334 }

mercurial