Mercurial > code
comparison parray.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 | 97c9c20535e2 |
children | 535f12e0a5af |
comparison
equal
deleted
inserted
replaced
141:5ed54050ae31 | 142:e3cf5ac9a5aa |
---|---|
80 } | 80 } |
81 | 81 |
82 /* | 82 /* |
83 * Add to the head of array. NOTE: this may be very slow when adding a lot | 83 * Add to the head of array. NOTE: this may be very slow when adding a lot |
84 * of object (about 100000). If you need to add a lot of data please consider | 84 * of object (about 100000). If you need to add a lot of data please consider |
85 * using linked list instead. | 85 * using linked list instead. Returns -1 on failure or 0 on success. |
86 */ | 86 */ |
87 | 87 |
88 int | 88 int |
89 parray_push(struct parray *arr, void *data) | 89 parray_push(struct parray *arr, void *data) |
90 { | 90 { |
100 return 0; | 100 return 0; |
101 } | 101 } |
102 | 102 |
103 /* | 103 /* |
104 * Insert the data at the specified index. The function returns -1 on | 104 * Insert the data at the specified index. The function returns -1 on |
105 * allocation failure. | 105 * allocation failure or the position of the added element. |
106 */ | 106 */ |
107 | 107 |
108 int | 108 int |
109 parray_insert(struct parray *arr, void *data, int index) | 109 parray_insert(struct parray *arr, void *data, int index) |
110 { | 110 { |
119 arr->data[index] = data; | 119 arr->data[index] = data; |
120 | 120 |
121 if (arr->flags & PARRAY_NULLEND) | 121 if (arr->flags & PARRAY_NULLEND) |
122 arr->data[arr->length] = NULL; | 122 arr->data[arr->length] = NULL; |
123 | 123 |
124 return 0; | 124 return index; |
125 } | 125 } |
126 | 126 |
127 /* | 127 /* |
128 * Append the data to the end of array. | 128 * Append the data to the end of array. Returns -1 on failure or the position |
129 * of the added element. | |
129 */ | 130 */ |
130 | 131 |
131 int | 132 int |
132 parray_append(struct parray *arr, void *data) | 133 parray_append(struct parray *arr, void *data) |
133 { | 134 { |
137 arr->data[arr->length++] = data; | 138 arr->data[arr->length++] = data; |
138 | 139 |
139 if (arr->flags & PARRAY_NULLEND) | 140 if (arr->flags & PARRAY_NULLEND) |
140 arr->data[arr->length] = NULL; | 141 arr->data[arr->length] = NULL; |
141 | 142 |
142 return 0; | 143 return (arr->length - 1); |
143 } | 144 } |
144 | 145 |
145 /* | 146 /* |
146 * Remove the array's head. | 147 * Remove the array's head. |
147 */ | 148 */ |
251 * Apply the function `fn' on each object and give the optional `udata' | 252 * Apply the function `fn' on each object and give the optional `udata' |
252 * argument to the function too. | 253 * argument to the function too. |
253 */ | 254 */ |
254 | 255 |
255 void | 256 void |
256 parray_map(const struct parray *arr, parray_map_fn fn, void *udata) | 257 parray_map(const struct parray *arr, parray_map_t fn, void *udata) |
257 { | 258 { |
258 int i; | 259 int i; |
259 | 260 |
260 for (i = 0; i < arr->length; ++i) | 261 for (i = 0; i < arr->length; ++i) |
261 fn(arr->data[i], udata); | 262 fn(arr->data[i], udata); |
267 * data points to the array data at the correct index. If the comparison | 268 * data points to the array data at the correct index. If the comparison |
268 * function nevers returns 1, array_find returns -1. | 269 * function nevers returns 1, array_find returns -1. |
269 */ | 270 */ |
270 | 271 |
271 int | 272 int |
272 parray_find(const struct parray *arr, parray_cmp_fn fn, void *ptr, void *u) | 273 parray_find(const struct parray *arr, parray_cmp_t fn, void *ptr, void *u) |
273 { | 274 { |
274 int st, i; | 275 int st, i; |
275 | 276 |
276 for (i = st = 0; i < arr->length && st != 1; ++i) | 277 for (i = st = 0; i < arr->length && st != 1; ++i) |
277 st = fn(arr->data[i], u); | 278 st = fn(arr->data[i], u); |