comparison array.c @ 142:e3cf5ac9a5aa

[p]array_insert now returns -1 or index of added element
author David Demelier <markand@malikania.fr>
date Tue, 08 May 2012 15:02:17 +0200
parents c6d9eb5702e8
children 535f12e0a5af
comparison
equal deleted inserted replaced
141:5ed54050ae31 142:e3cf5ac9a5aa
84 } 84 }
85 85
86 /* 86 /*
87 * Add to the head of array. NOTE: this may be very slow when adding a lot 87 * Add to the head of array. NOTE: this may be very slow when adding a lot
88 * of object (about 100000). If you need to add a lot of data please consider 88 * of object (about 100000). If you need to add a lot of data please consider
89 * using linked list instead. 89 * using linked list instead. Returns -1 on failure or 0 on success.
90 */ 90 */
91 91
92 int 92 int
93 array_push(struct array *arr, const void *data) 93 array_push(struct array *arr, const void *data)
94 { 94 {
101 return 0; 101 return 0;
102 } 102 }
103 103
104 /* 104 /*
105 * Insert the data at the specified index. The function returns -1 on 105 * Insert the data at the specified index. The function returns -1 on
106 * allocation failure or when the index is outof bounds otherwise 0 is returned. 106 * allocation failure or the position of the added element.
107 */ 107 */
108 108
109 int 109 int
110 array_insert(struct array *arr, const void *data, int index) 110 array_insert(struct array *arr, const void *data, int index)
111 { 111 {
117 /* Good place */ 117 /* Good place */
118 memmove((char *)arr->data + OFFSET(index + 1), 118 memmove((char *)arr->data + OFFSET(index + 1),
119 (char *)arr->data + OFFSET(index), OFFSET(arr->length++ - index)); 119 (char *)arr->data + OFFSET(index), OFFSET(arr->length++ - index));
120 memcpy((char *)arr->data + OFFSET(index), data, arr->unit); 120 memcpy((char *)arr->data + OFFSET(index), data, arr->unit);
121 121
122 return 0; 122 return index;
123 } 123 }
124 124
125 /* 125 /*
126 * Append the data to the end of array. 126 * Append the data to the end of array. Returns -1 on failure or the position
127 * of the added element.
127 */ 128 */
128 129
129 int 130 int
130 array_append(struct array *arr, const void *data) 131 array_append(struct array *arr, const void *data)
131 { 132 {
132 if (grow(arr) < 0) 133 if (grow(arr) < 0)
133 return -1; 134 return -1;
134 135
135 memcpy((char *)arr->data + OFFSET(arr->length++), data, arr->unit); 136 memcpy((char *)arr->data + OFFSET(arr->length++), data, arr->unit);
136 137
137 return 0; 138 return (arr->length - 1);
138 } 139 }
139 140
140 /* 141 /*
141 * Remove the array's head. 142 * Remove the array's head.
142 */ 143 */
265 * Apply the function `fn' on each object and give the optional `udata' 266 * Apply the function `fn' on each object and give the optional `udata'
266 * argument to the function too. 267 * argument to the function too.
267 */ 268 */
268 269
269 void 270 void
270 array_map(const struct array *arr, array_map_fn fn, void *udata) 271 array_map(const struct array *arr, array_map_t fn, void *udata)
271 { 272 {
272 int i; 273 int i;
273 274
274 for (i = 0; i < arr->length; ++i) 275 for (i = 0; i < arr->length; ++i)
275 fn((char *)arr->data + OFFSET(i), udata); 276 fn((char *)arr->data + OFFSET(i), udata);
280 * returns 1, 1 is returned and dst points to the correct object, dst should 281 * returns 1, 1 is returned and dst points to the correct object, dst should
281 * be a pointer to a pointer of object, like (int **) for a array of int. 282 * be a pointer to a pointer of object, like (int **) for a array of int.
282 */ 283 */
283 284
284 int 285 int
285 array_find(const struct array *arr, array_cmp_fn fn, void *dst, void *u) 286 array_find(const struct array *arr, array_cmp_t fn, void *dst, void *u)
286 { 287 {
287 int st, i; 288 int st, i;
288 289
289 for (i = st = 0; i < arr->length && st != 1; ++i) 290 for (i = st = 0; i < arr->length && st != 1; ++i)
290 st = fn((char *)arr->data + OFFSET(i), u); 291 st = fn((char *)arr->data + OFFSET(i), u);