comparison array.c @ 124:5917096facb9

Use enum and add #ifdef __cplusplus
author David Demelier <markand@malikania.fr>
date Wed, 07 Mar 2012 09:50:41 +0100
parents 59745a235d16
children c6d9eb5702e8
comparison
equal deleted inserted replaced
123:59745a235d16 124:5917096facb9
22 22
23 #include "array.h" 23 #include "array.h"
24 24
25 #define OFFSET(x) (arr->unit * (x)) 25 #define OFFSET(x) (arr->unit * (x))
26 26
27 static int array_grow(struct array *); 27 static int grow(struct array *);
28 28
29 int 29 int
30 array_init(struct array *arr, size_t unit) 30 array_init(struct array *arr, size_t unit)
31 { 31 {
32 if (unit == 0) 32 if (unit == 0)
87 */ 87 */
88 88
89 int 89 int
90 array_push(struct array *arr, const void *data) 90 array_push(struct array *arr, const void *data)
91 { 91 {
92 if (array_grow(arr) < 0) 92 if (grow(arr) < 0)
93 return -1; 93 return -1;
94 94
95 memmove((char *)arr->data + arr->unit, arr->data, OFFSET(arr->length++)); 95 memmove((char *)arr->data + arr->unit, arr->data, OFFSET(arr->length++));
96 memcpy((char *)arr->data, data, arr->unit); 96 memcpy((char *)arr->data, data, arr->unit);
97 97
124 */ 124 */
125 125
126 int 126 int
127 array_append(struct array *arr, const void *data) 127 array_append(struct array *arr, const void *data)
128 { 128 {
129 if (array_grow(arr) < 0) 129 if (grow(arr) < 0)
130 return -1; 130 return -1;
131 131
132 memcpy((char *)arr->data + OFFSET(arr->length++), data, arr->unit); 132 memcpy((char *)arr->data + OFFSET(arr->length++), data, arr->unit);
133 133
134 return 0; 134 return 0;
318 * it returns -1 on full buffer otherwise 0 is returned if allocation 318 * it returns -1 on full buffer otherwise 0 is returned if allocation
319 * succeeded. 319 * succeeded.
320 */ 320 */
321 321
322 static int 322 static int
323 array_grow(struct array *arr) 323 grow(struct array *arr)
324 { 324 {
325 if ((arr->size / arr->unit) > (size_t) arr->length) 325 if ((arr->size / arr->unit) > (size_t) arr->length)
326 return 0; 326 return 0;
327 327
328 if (arr->type == ARRAY_AUTO) { 328 if (arr->type == ARRAY_AUTO) {