Mercurial > code
comparison array.c @ 26:4fd9ecbbb143
Code cosmetic
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 18 Sep 2011 14:09:37 +0200 |
parents | e09000fc013a |
children | 904a373aa120 |
comparison
equal
deleted
inserted
replaced
25:e09000fc013a | 26:4fd9ecbbb143 |
---|---|
53 } | 53 } |
54 | 54 |
55 /* | 55 /* |
56 * Add to the head of array. NOTE: this may be very slow when adding a lot | 56 * Add to the head of array. NOTE: this may be very slow when adding a lot |
57 * of object (about 100000). If you need to add a lot of data please consider | 57 * of object (about 100000). If you need to add a lot of data please consider |
58 * using linked list instead or use array_append and then use reverse foreach | 58 * using linked list instead. |
59 * functions. | |
60 */ | 59 */ |
61 | 60 |
62 int | 61 int |
63 array_push(struct array *arr, const void *data) | 62 array_push(struct array *arr, const void *data) |
64 { | 63 { |
70 | 69 |
71 return 0; | 70 return 0; |
72 } | 71 } |
73 | 72 |
74 /* | 73 /* |
75 * Insert the data to the specified index. The function returns -1 on | 74 * Insert the data at the specified index. The function returns -1 on |
76 * allocation failure if the array need to grow or when the index is out | 75 * allocation failure or when the index is outof bounds otherwise 0 is returned. |
77 * of bounds otherwise 0 is returned. | |
78 */ | 76 */ |
79 | 77 |
80 int | 78 int |
81 array_insert(struct array *arr, const void *data, int index) | 79 array_insert(struct array *arr, const void *data, int index) |
82 { | 80 { |
83 if (index > arr->length - 1 || index < 0 || array_grow(arr) < 0) | 81 if (index > arr->length - 1 || index < 0 || array_grow(arr) < 0) |
84 return -1; | 82 return -1; |
85 | 83 |
86 memmove((char *) arr->data + SIZE(index + 1), (char *) arr->data + SIZE(index), | 84 memmove((char *) arr->data + SIZE(index + 1), |
87 SIZE(arr->length++ - index)); | 85 (char *) arr->data + SIZE(index), SIZE(arr->length++ - index)); |
88 memcpy((char *) arr->data + SIZE(index), data, arr->unit); | 86 memcpy((char *) arr->data + SIZE(index), data, arr->unit); |
89 | 87 |
90 return 0; | 88 return 0; |
91 } | 89 } |
92 | 90 |
157 | 155 |
158 if (arr->length == 0 || index < 0 || index > arr->length - 1) | 156 if (arr->length == 0 || index < 0 || index > arr->length - 1) |
159 return NULL; | 157 return NULL; |
160 | 158 |
161 data = (char *) arr->data + SIZE(index); | 159 data = (char *) arr->data + SIZE(index); |
162 memmove((char *) arr->data + SIZE(index), (char *) arr->data + SIZE(index + 1), | 160 memmove((char *) arr->data + SIZE(index), |
163 SIZE(arr->length - index)); | 161 (char *) arr->data + SIZE(index + 1), SIZE(arr->length - index)); |
164 memset((char *) arr->data + SIZE(--arr->length), 0, arr->unit); | 162 memset((char *) arr->data + SIZE(--arr->length), 0, arr->unit); |
165 | 163 |
166 return data; | 164 return data; |
167 } | 165 } |
168 | 166 |
174 | 172 |
175 int | 173 int |
176 array_swap(struct array *arr, int i1, int i2) | 174 array_swap(struct array *arr, int i1, int i2) |
177 { | 175 { |
178 /* Out of bounds */ | 176 /* Out of bounds */ |
179 if (i1 > arr->length - 1|| i1 < 0 || i2 > arr->length - 1|| i2 < 0) | 177 if (i1 > arr->length - 1|| i1 < 0 || i2 > arr->length - 1 || i2 < 0) |
180 return -1; | 178 return -1; |
181 | 179 |
182 /* | 180 /* |
183 * Only allocate at this time, the user may do not want to use this | 181 * Only allocate at this time, the user may do not want to use this |
184 * function. | 182 * function. |
186 | 184 |
187 if (!arr->tmp && !(arr->tmp = malloc(arr->unit))) | 185 if (!arr->tmp && !(arr->tmp = malloc(arr->unit))) |
188 return -1; | 186 return -1; |
189 | 187 |
190 memcpy((char *) arr->tmp, (char *) arr->data + SIZE(i1), arr->unit); | 188 memcpy((char *) arr->tmp, (char *) arr->data + SIZE(i1), arr->unit); |
191 memcpy((char *) arr->data + SIZE(i1), (char *) arr->data + SIZE(i2), arr->unit); | 189 memcpy((char *) arr->data + SIZE(i1), (char *) arr->data + SIZE(i2), |
190 arr->unit); | |
192 memcpy((char *) arr->data + SIZE(i2), (char *) arr->tmp, arr->unit); | 191 memcpy((char *) arr->data + SIZE(i2), (char *) arr->tmp, arr->unit); |
193 | 192 |
194 return 0; | 193 return 0; |
195 } | 194 } |
196 | 195 |
280 SIZE(arr->bsize)))) | 279 SIZE(arr->bsize)))) |
281 return -1; | 280 return -1; |
282 | 281 |
283 arr->size += SIZE(arr->bsize); | 282 arr->size += SIZE(arr->bsize); |
284 } else | 283 } else |
285 return ((arr->size / arr->unit) <= (size_t) arr->length) ? -1 : 0; | 284 return (arr->size / arr->unit <= (size_t) arr->length) ? -1 : 0; |
286 | 285 |
287 return 0; | 286 return 0; |
288 } | 287 } |