src/array_list.c

changeset 664
af5bf4603a5d
parent 662
d0d95740071b
child 666
b5dd654deb3b
equal deleted inserted replaced
663:d50b5dc1e058 664:af5bf4603a5d
261 261
262 static int cx_arl_remove( 262 static int cx_arl_remove(
263 struct cx_list_s *list, 263 struct cx_list_s *list,
264 size_t index 264 size_t index
265 ) { 265 ) {
266 cx_array_list *arl = (cx_array_list *) list;
267
266 // out-of-bounds check 268 // out-of-bounds check
267 if (index >= list->size) { 269 if (index >= list->size) {
268 return 1; 270 return 1;
271 }
272
273 // content destruction
274 if (list->content_destructor_type != CX_DESTRUCTOR_NONE) {
275 char *ptr = arl->data;
276 ptr += index * list->itemsize;
277 cx_list_invoke_destructor(list, ptr);
269 } 278 }
270 279
271 // short-circuit removal of last element 280 // short-circuit removal of last element
272 if (index == list->size - 1) { 281 if (index == list->size - 1) {
273 list->size--; 282 list->size--;
274 return 0; 283 return 0;
275 } 284 }
276 285
277 // just move the elements starting at index to the left 286 // just move the elements starting at index to the left
278 cx_array_list *arl = (cx_array_list *) list;
279 int result = cx_array_copy( 287 int result = cx_array_copy(
280 &arl->data, 288 &arl->data,
281 &list->size, 289 &list->size,
282 &list->capacity, 290 &list->capacity,
283 index, 291 index,
291 list->size--; 299 list->size--;
292 } 300 }
293 return result; 301 return result;
294 } 302 }
295 303
304 static void cx_arl_clear(struct cx_list_s *list) {
305 if (list->size == 0) return;
306
307 cx_array_list *arl = (cx_array_list *) list;
308 char *ptr = arl->data;
309
310 switch (list->content_destructor_type) {
311 case CX_DESTRUCTOR_SIMPLE: {
312 for (size_t i = 0; i < list->size; i++) {
313 list->simple_destructor(ptr);
314 ptr += list->itemsize;
315 }
316 break;
317 }
318 case CX_DESTRUCTOR_ADVANCED: {
319 for (size_t i = 0; i < list->size; i++) {
320 list->advanced_destructor.func(list->advanced_destructor.data,
321 ptr);
322 ptr += list->itemsize;
323 }
324 break;
325 }
326 case CX_DESTRUCTOR_NONE:
327 break; // nothing
328 }
329 }
330
296 static int cx_arl_swap( 331 static int cx_arl_swap(
297 struct cx_list_s *list, 332 struct cx_list_s *list,
298 size_t i, 333 size_t i,
299 size_t j 334 size_t j
300 ) { 335 ) {
449 cx_arl_destructor, 484 cx_arl_destructor,
450 cx_arl_insert_element, 485 cx_arl_insert_element,
451 cx_arl_insert_array, 486 cx_arl_insert_array,
452 cx_arl_insert_iter, 487 cx_arl_insert_iter,
453 cx_arl_remove, 488 cx_arl_remove,
489 cx_arl_clear,
454 cx_arl_swap, 490 cx_arl_swap,
455 cx_arl_at, 491 cx_arl_at,
456 cx_arl_find, 492 cx_arl_find,
457 cx_arl_sort, 493 cx_arl_sort,
458 cx_arl_compare, 494 cx_arl_compare,

mercurial