src/array_list.c

changeset 655
7340c4255f1f
parent 654
c9d008861178
child 660
4738a9065907
equal deleted inserted replaced
654:c9d008861178 655:7340c4255f1f
393 ((char *) iter->elem_handle) 393 ((char *) iter->elem_handle)
394 + ((struct cx_list_s const *) iter->src_handle)->itemsize; 394 + ((struct cx_list_s const *) iter->src_handle)->itemsize;
395 } 395 }
396 } 396 }
397 397
398 static void cx_arl_iter_prev(void *it) {
399 struct cx_iterator_base_s *itbase = it;
400 struct cx_mut_iterator_s *iter = it;
401 cx_array_list *const list = iter->src_handle;
402 if (itbase->remove) {
403 itbase->remove = false;
404 cx_arl_remove(iter->src_handle, iter->index);
405 }
406 iter->index--;
407 if (iter->index < list->base.size) {
408 iter->elem_handle = ((char *) list->data)
409 + iter->index * list->base.itemsize;
410 }
411 }
412
398 static bool cx_arl_iter_flag_rm(void *it) { 413 static bool cx_arl_iter_flag_rm(void *it) {
399 struct cx_iterator_base_s *iter = it; 414 struct cx_iterator_base_s *iter = it;
400 if (iter->mutating) { 415 if (iter->mutating) {
401 iter->remove = true; 416 iter->remove = true;
402 return true; 417 return true;
405 } 420 }
406 } 421 }
407 422
408 static struct cx_iterator_s cx_arl_iterator( 423 static struct cx_iterator_s cx_arl_iterator(
409 struct cx_list_s const *list, 424 struct cx_list_s const *list,
410 size_t index 425 size_t index,
426 bool backwards
411 ) { 427 ) {
412 struct cx_iterator_s iter; 428 struct cx_iterator_s iter;
413 429
414 iter.index = index; 430 iter.index = index;
415 iter.src_handle = list; 431 iter.src_handle = list;
416 iter.elem_handle = cx_arl_at(list, index); 432 iter.elem_handle = cx_arl_at(list, index);
417 iter.base.valid = cx_arl_iter_valid; 433 iter.base.valid = cx_arl_iter_valid;
418 iter.base.current = cx_arl_iter_current; 434 iter.base.current = cx_arl_iter_current;
419 iter.base.next = cx_arl_iter_next; 435 iter.base.next = backwards ? cx_arl_iter_prev : cx_arl_iter_next;
420 iter.base.flag_removal = cx_arl_iter_flag_rm; 436 iter.base.flag_removal = cx_arl_iter_flag_rm;
421 iter.base.remove = false; 437 iter.base.remove = false;
422 iter.base.mutating = false; 438 iter.base.mutating = false;
423 439
424 return iter; 440 return iter;

mercurial