src/array.c

branch
feature/array
changeset 354
7fd13b9f8f60
parent 353
135ce35d5108
child 355
d315a068235a
equal deleted inserted replaced
353:135ce35d5108 354:7fd13b9f8f60
53 53
54 #ifndef ucx_array_sort_impl 54 #ifndef ucx_array_sort_impl
55 #define ucx_array_sort_impl ucx_mergesort 55 #define ucx_array_sort_impl ucx_mergesort
56 #endif 56 #endif
57 57
58 static int ucx_array_ensurecap(UcxArray *array, size_t reqcap) {
59 size_t required_capacity = array->capacity;
60 while (reqcap > required_capacity) {
61 if (required_capacity * 2 < required_capacity)
62 return 1;
63 required_capacity <<= 1;
64 }
65 if (ucx_array_reserve(array, required_capacity)) {
66 return 1;
67 }
68 }
69
58 UcxArray ucx_array_new(size_t capacity, size_t elemsize) { 70 UcxArray ucx_array_new(size_t capacity, size_t elemsize) {
59 return ucx_array_new_a(capacity, elemsize, ucx_default_allocator()); 71 return ucx_array_new_a(capacity, elemsize, ucx_default_allocator());
60 } 72 }
61 73
62 UcxArray ucx_array_new_a(size_t capacity, size_t elemsize, 74 UcxArray ucx_array_new_a(size_t capacity, size_t elemsize,
125 alfree(array->allocator, array->data); 137 alfree(array->allocator, array->data);
126 array->data = NULL; 138 array->data = NULL;
127 array->capacity = array->size = 0; 139 array->capacity = array->size = 0;
128 } 140 }
129 141
130 int ucx_array_append(UcxArray *array, void *data) { 142 int ucx_array_append_from(UcxArray *array, void *data, size_t count) {
131 if (array->size == array->capacity) { 143 if (ucx_array_ensurecap(array, array->size + count))
132 if (ucx_array_reserve(array, array->capacity*2)) { 144 return 1;
133 return 1; 145
134 } 146 void* dest = ucx_array_at(*array, array->size);
135 }
136
137 void* dest = ucx_array_at(*array, array->size++);
138 if (data) { 147 if (data) {
139 memcpy(dest, data, array->elemsize); 148 memcpy(dest, data, array->elemsize*count);
140 } else { 149 } else {
141 memset(dest, 0, array->elemsize); 150 memset(dest, 0, array->elemsize*count);
142 } 151 }
152 array->size += count;
143 153
144 return 0; 154 return 0;
145 } 155 }
146 156
147 int ucx_array_prepend(UcxArray *array, void *data) { 157 int ucx_array_prepend_from(UcxArray *array, void *data, size_t count) {
148 if (array->size == array->capacity) { 158 if (ucx_array_ensurecap(array, array->size + count))
149 if (ucx_array_reserve(array, array->capacity*2)) { 159 return 1;
150 return 1; 160
151 } 161 if (array->size > 0) {
152 } 162 void *dest = ucx_array_at(*array, count);
153
154 array->size++;
155
156 if (array->size > 1) {
157 void *dest = ucx_array_at(*array,1);
158 memmove(dest, array->data, array->elemsize*array->size); 163 memmove(dest, array->data, array->elemsize*array->size);
159 } 164 }
160 165
161 if (data) { 166 if (data) {
162 memcpy(array->data, data, array->elemsize); 167 memcpy(array->data, data, array->elemsize*count);
163 } else { 168 } else {
164 memset(array->data, 0, array->elemsize); 169 memset(array->data, 0, array->elemsize*count);
165 } 170 }
171 array->size += count;
166 172
167 return 0; 173 return 0;
168 } 174 }
169 175
170 int ucx_array_set(UcxArray *array, size_t index, void *data) { 176 int ucx_array_set_from(UcxArray *array, size_t index,
171 if (index >= array->size) { 177 void *data, size_t count) {
172 if (ucx_array_reserve(array, index+1)) { 178 if (ucx_array_ensurecap(array, index + count))
173 return 1; 179 return 1;
174 } 180
175 array->size = index+1; 181 if (index+count > array->size) {
182 array->size = index+count;
176 } 183 }
177 184
178 void *dest = ucx_array_at(*array, index); 185 void *dest = ucx_array_at(*array, index);
179 if (data) { 186 if (data) {
180 memcpy(dest, data, array->elemsize); 187 memcpy(dest, data, array->elemsize*count);
181 } else { 188 } else {
182 memset(dest, 0, array->elemsize); 189 memset(dest, 0, array->elemsize*count);
183 } 190 }
184 191
185 return 0; 192 return 0;
193 }
194
195 int ucx_array_appendv(UcxArray *array, ...) {
196 va_list ap;
197 va_start(ap, array);
198 int elem = va_arg(ap, int);
199 int ret = ucx_array_append_from(array, &elem, 1);
200 va_end(ap);
201 return ret;
202 }
203
204 int ucx_array_prependv(UcxArray *array, ...) {
205 va_list ap;
206 va_start(ap, array);
207 int elem = va_arg(ap, int);
208 int ret = ucx_array_prepend_from(array, &elem, 1);
209 va_end(ap);
210 return ret;
211 }
212
213 int ucx_array_setv(UcxArray *array, size_t index, ...) {
214 va_list ap;
215 va_start(ap, index);
216 int elem = va_arg(ap, int);
217 int ret = ucx_array_set_from(array, index, &elem, 1);
218 va_end(ap);
219 return ret;
186 } 220 }
187 221
188 int ucx_array_concat(UcxArray *array1, const UcxArray *array2) { 222 int ucx_array_concat(UcxArray *array1, const UcxArray *array2) {
189 223
190 if (array1->elemsize != array2->elemsize) 224 if (array1->elemsize != array2->elemsize)

mercurial