src/array.c

branch
feature/array
changeset 354
7fd13b9f8f60
parent 353
135ce35d5108
child 355
d315a068235a
--- a/src/array.c	Sat Aug 10 09:47:59 2019 +0200
+++ b/src/array.c	Sat Aug 10 11:12:49 2019 +0200
@@ -55,6 +55,18 @@
 #define ucx_array_sort_impl ucx_mergesort
 #endif
 
+static int ucx_array_ensurecap(UcxArray *array, size_t reqcap) {
+    size_t required_capacity = array->capacity;
+    while (reqcap > required_capacity) {
+        if (required_capacity * 2 < required_capacity)
+            return 1;
+        required_capacity <<= 1;
+    }
+    if (ucx_array_reserve(array, required_capacity)) {
+        return 1;
+    }
+}
+
 UcxArray ucx_array_new(size_t capacity, size_t elemsize) {
     return ucx_array_new_a(capacity, elemsize, ucx_default_allocator());
 }
@@ -127,62 +139,84 @@
     array->capacity = array->size = 0;
 }
 
-int ucx_array_append(UcxArray *array, void *data) {
-    if (array->size == array->capacity) {
-        if (ucx_array_reserve(array, array->capacity*2)) {
-            return 1;
-        }
+int ucx_array_append_from(UcxArray *array, void *data, size_t count) {
+    if (ucx_array_ensurecap(array, array->size + count))
+        return 1;
+    
+    void* dest = ucx_array_at(*array, array->size);
+    if (data) {
+        memcpy(dest, data, array->elemsize*count);
+    } else {
+        memset(dest, 0, array->elemsize*count);
+    }
+    array->size += count;
+    
+    return 0;
+}
+
+int ucx_array_prepend_from(UcxArray *array, void *data, size_t count) {
+    if (ucx_array_ensurecap(array, array->size + count))
+        return 1;
+    
+    if (array->size > 0) {
+        void *dest = ucx_array_at(*array, count);
+        memmove(dest, array->data, array->elemsize*array->size);
     }
     
-    void* dest = ucx_array_at(*array, array->size++);
     if (data) {
-        memcpy(dest, data, array->elemsize);
+        memcpy(array->data, data, array->elemsize*count);
     } else {
-        memset(dest, 0, array->elemsize);
+        memset(array->data, 0, array->elemsize*count);
+    }
+    array->size += count;
+        
+    return 0;
+}
+
+int ucx_array_set_from(UcxArray *array, size_t index,
+        void *data, size_t count) {
+    if (ucx_array_ensurecap(array, index + count))
+        return 1;
+    
+    if (index+count > array->size) {
+        array->size = index+count;
+    }
+    
+    void *dest = ucx_array_at(*array, index);
+    if (data) {
+        memcpy(dest, data, array->elemsize*count);
+    } else {
+        memset(dest, 0, array->elemsize*count);
     }
     
     return 0;
 }
 
-int ucx_array_prepend(UcxArray *array, void *data) {
-    if (array->size == array->capacity) {
-        if (ucx_array_reserve(array, array->capacity*2)) {
-            return 1;
-        }
-    }
-    
-    array->size++;
-    
-    if (array->size > 1) {
-        void *dest = ucx_array_at(*array,1);
-        memmove(dest, array->data, array->elemsize*array->size);
-    }
-    
-    if (data) {
-        memcpy(array->data, data, array->elemsize);
-    } else {
-        memset(array->data, 0, array->elemsize);
-    }
-        
-    return 0;
+int ucx_array_appendv(UcxArray *array, ...) {
+    va_list ap;
+    va_start(ap, array);
+    int elem = va_arg(ap, int);
+    int ret = ucx_array_append_from(array, &elem, 1);
+    va_end(ap);
+    return ret;
 }
 
-int ucx_array_set(UcxArray *array, size_t index, void *data) {
-    if (index >= array->size) {
-        if (ucx_array_reserve(array, index+1)) {
-            return 1;
-        }
-        array->size = index+1;
-    }
-    
-    void *dest = ucx_array_at(*array, index);
-    if (data) {
-        memcpy(dest, data, array->elemsize);
-    } else {
-        memset(dest, 0, array->elemsize);
-    }
-    
-    return 0;
+int ucx_array_prependv(UcxArray *array, ...) {
+    va_list ap;
+    va_start(ap, array);
+    int elem = va_arg(ap, int);
+    int ret = ucx_array_prepend_from(array, &elem, 1);
+    va_end(ap);
+    return ret;
+}
+
+int ucx_array_setv(UcxArray *array, size_t index, ...) {
+    va_list ap;
+    va_start(ap, index);
+    int elem = va_arg(ap, int);
+    int ret = ucx_array_set_from(array, index, &elem, 1);
+    va_end(ap);
+    return ret;
 }
 
 int ucx_array_concat(UcxArray *array1, const UcxArray *array2) {

mercurial