src/array_list.c

changeset 655
7340c4255f1f
parent 654
c9d008861178
child 660
4738a9065907
     1.1 --- a/src/array_list.c	Wed Feb 08 20:26:26 2023 +0100
     1.2 +++ b/src/array_list.c	Wed Feb 15 16:48:11 2023 +0100
     1.3 @@ -395,6 +395,21 @@
     1.4      }
     1.5  }
     1.6  
     1.7 +static void cx_arl_iter_prev(void *it) {
     1.8 +    struct cx_iterator_base_s *itbase = it;
     1.9 +    struct cx_mut_iterator_s *iter = it;
    1.10 +    cx_array_list *const list = iter->src_handle;
    1.11 +    if (itbase->remove) {
    1.12 +        itbase->remove = false;
    1.13 +        cx_arl_remove(iter->src_handle, iter->index);
    1.14 +    }
    1.15 +    iter->index--;
    1.16 +    if (iter->index < list->base.size) {
    1.17 +        iter->elem_handle = ((char *) list->data)
    1.18 +                            + iter->index * list->base.itemsize;
    1.19 +    }
    1.20 +}
    1.21 +
    1.22  static bool cx_arl_iter_flag_rm(void *it) {
    1.23      struct cx_iterator_base_s *iter = it;
    1.24      if (iter->mutating) {
    1.25 @@ -407,7 +422,8 @@
    1.26  
    1.27  static struct cx_iterator_s cx_arl_iterator(
    1.28          struct cx_list_s const *list,
    1.29 -        size_t index
    1.30 +        size_t index,
    1.31 +        bool backwards
    1.32  ) {
    1.33      struct cx_iterator_s iter;
    1.34  
    1.35 @@ -416,7 +432,7 @@
    1.36      iter.elem_handle = cx_arl_at(list, index);
    1.37      iter.base.valid = cx_arl_iter_valid;
    1.38      iter.base.current = cx_arl_iter_current;
    1.39 -    iter.base.next = cx_arl_iter_next;
    1.40 +    iter.base.next = backwards ? cx_arl_iter_prev : cx_arl_iter_next;
    1.41      iter.base.flag_removal = cx_arl_iter_flag_rm;
    1.42      iter.base.remove = false;
    1.43      iter.base.mutating = false;

mercurial