Mercurial > code
diff array.c @ 118:3c51b1f2974e
Same for array
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 28 Feb 2012 21:15:08 +0100 |
parents | 4efd3873a457 |
children | d6a78dea70c9 |
line wrap: on
line diff
--- a/array.c Tue Feb 28 20:57:48 2012 +0100 +++ b/array.c Tue Feb 28 21:15:08 2012 +0100 @@ -51,7 +51,7 @@ * l -> optional array block size of type int * m -> malloc function that must matches void * (*malloc)(size_t) * r -> realloc function that must matches void * (*realloc)(void *, size_t) - * t -> type of array of type enum array_type + * t -> type of array of type int */ void @@ -106,9 +106,12 @@ int array_insert(struct array *arr, const void *data, int index) { - if (index > arr->length - 1 || index < 0 || array_grow(arr) < 0) - return -1; + if (index < 0) + return array_push(arr, data); + if (index >= arr->length) + return array_append(arr, data); + /* Good place */ memmove((char *)arr->data + OFFSET(index + 1), (char *)arr->data + OFFSET(index), OFFSET(arr->length++ - index)); memcpy((char *)arr->data + OFFSET(index), data, arr->unit);