Mercurial > code
comparison array.c @ 22:ecdf21f1d0c6
Added a new function array_swap() that swap elements by index
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 14 Sep 2011 20:24:14 +0200 |
parents | e9a7b671d707 |
children | 216b6e6a539c |
comparison
equal
deleted
inserted
replaced
21:ae4128d16c92 | 22:ecdf21f1d0c6 |
---|---|
32 struct array *arr; | 32 struct array *arr; |
33 | 33 |
34 if (unit == 0 || !(arr = malloc(sizeof (struct array)))) | 34 if (unit == 0 || !(arr = malloc(sizeof (struct array)))) |
35 return NULL; | 35 return NULL; |
36 | 36 |
37 arr->tmp = NULL; | |
38 arr->length = 0; | |
39 arr->flags = flags; | |
37 arr->bsize = (bsize == 0) ? ARRAY_DEFAULT_BSIZE : bsize; | 40 arr->bsize = (bsize == 0) ? ARRAY_DEFAULT_BSIZE : bsize; |
38 arr->flags = flags; | |
39 arr->unit = unit; | 41 arr->unit = unit; |
40 arr->size = SIZE(bsize); | 42 arr->size = SIZE(arr->bsize); |
41 arr->length = 0; | |
42 | 43 |
43 if (!(arr->data = malloc(arr->size))) { | 44 if (!(arr->data = malloc(arr->size))) { |
44 free(arr); | 45 free(arr); |
45 return NULL; | 46 return NULL; |
46 } | 47 } |
161 SIZE(arr->length - index)); | 162 SIZE(arr->length - index)); |
162 memset(arr->data + SIZE(--arr->length), 0, arr->unit); | 163 memset(arr->data + SIZE(--arr->length), 0, arr->unit); |
163 | 164 |
164 return data; | 165 return data; |
165 } | 166 } |
167 | |
168 /* | |
169 * Swap the two elements referenced by index `i1' and `i2'. This function needs | |
170 * to allocate data to swap elements thus if the functions fails it returns -1 | |
171 * otherwise 0 is returned. | |
172 */ | |
173 | |
174 int | |
175 array_swap(struct array *arr, int i1, int i2) | |
176 { | |
177 /* Out of bounds */ | |
178 if (i1 > arr->length - 1|| i1 < 0 || i2 > arr->length - 1|| i2 < 0) | |
179 return -1; | |
180 | |
181 /* | |
182 * Only allocate at this time, the user may do not want to use this | |
183 * function. | |
184 */ | |
185 | |
186 if (!arr->tmp && !(arr->tmp = malloc(arr->unit))) | |
187 return -1; | |
188 | |
189 memcpy(arr->tmp, arr->data + SIZE(i1), arr->unit); | |
190 memcpy(arr->data + SIZE(i1), arr->data + SIZE(i2), arr->unit); | |
191 memcpy(arr->data + SIZE(i2), arr->tmp, arr->unit); | |
192 | |
193 return 0; | |
194 } | |
195 | |
166 | 196 |
167 /* | 197 /* |
168 * Apply the function `fn' on each object and give the optional `udata' | 198 * Apply the function `fn' on each object and give the optional `udata' |
169 * argument to the function too. | 199 * argument to the function too. |
170 */ | 200 */ |
223 void | 253 void |
224 array_free(struct array *arr) | 254 array_free(struct array *arr) |
225 { | 255 { |
226 array_clear(arr); | 256 array_clear(arr); |
227 | 257 |
228 free(arr->data); | 258 if (arr->data) |
259 free(arr->data); | |
260 if (arr->tmp) | |
261 free(arr->tmp); | |
262 | |
229 free(arr); | 263 free(arr); |
230 } | 264 } |
231 | 265 |
232 /* | 266 /* |
233 * Increate the array storage when it is full. If the buffer is fixed size | 267 * Increate the array storage when it is full. If the buffer is fixed size |