Mercurial > code
comparison 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 |
comparison
equal
deleted
inserted
replaced
117:bbe86bdb55da | 118:3c51b1f2974e |
---|---|
49 /* | 49 /* |
50 * Valid options that can be set for an array : | 50 * Valid options that can be set for an array : |
51 * l -> optional array block size of type int | 51 * l -> optional array block size of type int |
52 * m -> malloc function that must matches void * (*malloc)(size_t) | 52 * m -> malloc function that must matches void * (*malloc)(size_t) |
53 * r -> realloc function that must matches void * (*realloc)(void *, size_t) | 53 * r -> realloc function that must matches void * (*realloc)(void *, size_t) |
54 * t -> type of array of type enum array_type | 54 * t -> type of array of type int |
55 */ | 55 */ |
56 | 56 |
57 void | 57 void |
58 array_set(struct array *arr, const char *fmt, ...) | 58 array_set(struct array *arr, const char *fmt, ...) |
59 { | 59 { |
104 */ | 104 */ |
105 | 105 |
106 int | 106 int |
107 array_insert(struct array *arr, const void *data, int index) | 107 array_insert(struct array *arr, const void *data, int index) |
108 { | 108 { |
109 if (index > arr->length - 1 || index < 0 || array_grow(arr) < 0) | 109 if (index < 0) |
110 return -1; | 110 return array_push(arr, data); |
111 | 111 if (index >= arr->length) |
112 return array_append(arr, data); | |
113 | |
114 /* Good place */ | |
112 memmove((char *)arr->data + OFFSET(index + 1), | 115 memmove((char *)arr->data + OFFSET(index + 1), |
113 (char *)arr->data + OFFSET(index), OFFSET(arr->length++ - index)); | 116 (char *)arr->data + OFFSET(index), OFFSET(arr->length++ - index)); |
114 memcpy((char *)arr->data + OFFSET(index), data, arr->unit); | 117 memcpy((char *)arr->data + OFFSET(index), data, arr->unit); |
115 | 118 |
116 return 0; | 119 return 0; |