src/array.c

branch
feature/array
changeset 354
7fd13b9f8f60
parent 353
135ce35d5108
child 355
d315a068235a
     1.1 --- a/src/array.c	Sat Aug 10 09:47:59 2019 +0200
     1.2 +++ b/src/array.c	Sat Aug 10 11:12:49 2019 +0200
     1.3 @@ -55,6 +55,18 @@
     1.4  #define ucx_array_sort_impl ucx_mergesort
     1.5  #endif
     1.6  
     1.7 +static int ucx_array_ensurecap(UcxArray *array, size_t reqcap) {
     1.8 +    size_t required_capacity = array->capacity;
     1.9 +    while (reqcap > required_capacity) {
    1.10 +        if (required_capacity * 2 < required_capacity)
    1.11 +            return 1;
    1.12 +        required_capacity <<= 1;
    1.13 +    }
    1.14 +    if (ucx_array_reserve(array, required_capacity)) {
    1.15 +        return 1;
    1.16 +    }
    1.17 +}
    1.18 +
    1.19  UcxArray ucx_array_new(size_t capacity, size_t elemsize) {
    1.20      return ucx_array_new_a(capacity, elemsize, ucx_default_allocator());
    1.21  }
    1.22 @@ -127,62 +139,84 @@
    1.23      array->capacity = array->size = 0;
    1.24  }
    1.25  
    1.26 -int ucx_array_append(UcxArray *array, void *data) {
    1.27 -    if (array->size == array->capacity) {
    1.28 -        if (ucx_array_reserve(array, array->capacity*2)) {
    1.29 -            return 1;
    1.30 -        }
    1.31 +int ucx_array_append_from(UcxArray *array, void *data, size_t count) {
    1.32 +    if (ucx_array_ensurecap(array, array->size + count))
    1.33 +        return 1;
    1.34 +    
    1.35 +    void* dest = ucx_array_at(*array, array->size);
    1.36 +    if (data) {
    1.37 +        memcpy(dest, data, array->elemsize*count);
    1.38 +    } else {
    1.39 +        memset(dest, 0, array->elemsize*count);
    1.40 +    }
    1.41 +    array->size += count;
    1.42 +    
    1.43 +    return 0;
    1.44 +}
    1.45 +
    1.46 +int ucx_array_prepend_from(UcxArray *array, void *data, size_t count) {
    1.47 +    if (ucx_array_ensurecap(array, array->size + count))
    1.48 +        return 1;
    1.49 +    
    1.50 +    if (array->size > 0) {
    1.51 +        void *dest = ucx_array_at(*array, count);
    1.52 +        memmove(dest, array->data, array->elemsize*array->size);
    1.53      }
    1.54      
    1.55 -    void* dest = ucx_array_at(*array, array->size++);
    1.56      if (data) {
    1.57 -        memcpy(dest, data, array->elemsize);
    1.58 +        memcpy(array->data, data, array->elemsize*count);
    1.59      } else {
    1.60 -        memset(dest, 0, array->elemsize);
    1.61 +        memset(array->data, 0, array->elemsize*count);
    1.62 +    }
    1.63 +    array->size += count;
    1.64 +        
    1.65 +    return 0;
    1.66 +}
    1.67 +
    1.68 +int ucx_array_set_from(UcxArray *array, size_t index,
    1.69 +        void *data, size_t count) {
    1.70 +    if (ucx_array_ensurecap(array, index + count))
    1.71 +        return 1;
    1.72 +    
    1.73 +    if (index+count > array->size) {
    1.74 +        array->size = index+count;
    1.75 +    }
    1.76 +    
    1.77 +    void *dest = ucx_array_at(*array, index);
    1.78 +    if (data) {
    1.79 +        memcpy(dest, data, array->elemsize*count);
    1.80 +    } else {
    1.81 +        memset(dest, 0, array->elemsize*count);
    1.82      }
    1.83      
    1.84      return 0;
    1.85  }
    1.86  
    1.87 -int ucx_array_prepend(UcxArray *array, void *data) {
    1.88 -    if (array->size == array->capacity) {
    1.89 -        if (ucx_array_reserve(array, array->capacity*2)) {
    1.90 -            return 1;
    1.91 -        }
    1.92 -    }
    1.93 -    
    1.94 -    array->size++;
    1.95 -    
    1.96 -    if (array->size > 1) {
    1.97 -        void *dest = ucx_array_at(*array,1);
    1.98 -        memmove(dest, array->data, array->elemsize*array->size);
    1.99 -    }
   1.100 -    
   1.101 -    if (data) {
   1.102 -        memcpy(array->data, data, array->elemsize);
   1.103 -    } else {
   1.104 -        memset(array->data, 0, array->elemsize);
   1.105 -    }
   1.106 -        
   1.107 -    return 0;
   1.108 +int ucx_array_appendv(UcxArray *array, ...) {
   1.109 +    va_list ap;
   1.110 +    va_start(ap, array);
   1.111 +    int elem = va_arg(ap, int);
   1.112 +    int ret = ucx_array_append_from(array, &elem, 1);
   1.113 +    va_end(ap);
   1.114 +    return ret;
   1.115  }
   1.116  
   1.117 -int ucx_array_set(UcxArray *array, size_t index, void *data) {
   1.118 -    if (index >= array->size) {
   1.119 -        if (ucx_array_reserve(array, index+1)) {
   1.120 -            return 1;
   1.121 -        }
   1.122 -        array->size = index+1;
   1.123 -    }
   1.124 -    
   1.125 -    void *dest = ucx_array_at(*array, index);
   1.126 -    if (data) {
   1.127 -        memcpy(dest, data, array->elemsize);
   1.128 -    } else {
   1.129 -        memset(dest, 0, array->elemsize);
   1.130 -    }
   1.131 -    
   1.132 -    return 0;
   1.133 +int ucx_array_prependv(UcxArray *array, ...) {
   1.134 +    va_list ap;
   1.135 +    va_start(ap, array);
   1.136 +    int elem = va_arg(ap, int);
   1.137 +    int ret = ucx_array_prepend_from(array, &elem, 1);
   1.138 +    va_end(ap);
   1.139 +    return ret;
   1.140 +}
   1.141 +
   1.142 +int ucx_array_setv(UcxArray *array, size_t index, ...) {
   1.143 +    va_list ap;
   1.144 +    va_start(ap, index);
   1.145 +    int elem = va_arg(ap, int);
   1.146 +    int ret = ucx_array_set_from(array, index, &elem, 1);
   1.147 +    va_end(ap);
   1.148 +    return ret;
   1.149  }
   1.150  
   1.151  int ucx_array_concat(UcxArray *array1, const UcxArray *array2) {

mercurial