simplify list class - fixes #236

Wed, 25 Jan 2023 19:19:29 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 25 Jan 2023 19:19:29 +0100
changeset 640
55cc3b373c5e
parent 639
309e8b08c60e
child 641
d402fead3386

simplify list class - fixes #236

src/array_list.c file | annotate | diff | comparison | revisions
src/cx/list.h file | annotate | diff | comparison | revisions
src/linked_list.c file | annotate | diff | comparison | revisions
src/list.c file | annotate | diff | comparison | revisions
     1.1 --- a/src/array_list.c	Mon Jan 23 20:34:18 2023 +0100
     1.2 +++ b/src/array_list.c	Wed Jan 25 19:19:29 2023 +0100
     1.3 @@ -168,23 +168,6 @@
     1.4      cxFree(list->allocator, arl->data);
     1.5  }
     1.6  
     1.7 -static int cx_arl_add(
     1.8 -        struct cx_list_s *list,
     1.9 -        void const *elem
    1.10 -) {
    1.11 -    cx_array_list *arl = (cx_array_list *) list;
    1.12 -    return cx_array_copy(
    1.13 -            &arl->data,
    1.14 -            &list->size,
    1.15 -            &list->capacity,
    1.16 -            list->size,
    1.17 -            elem,
    1.18 -            list->itemsize,
    1.19 -            1,
    1.20 -            &arl->reallocator
    1.21 -    );
    1.22 -}
    1.23 -
    1.24  static size_t cx_arl_insert_array(
    1.25          struct cx_list_s *list,
    1.26          size_t index,
    1.27 @@ -241,48 +224,6 @@
    1.28      }
    1.29  }
    1.30  
    1.31 -static size_t cx_arl_add_array(
    1.32 -        struct cx_list_s *list,
    1.33 -        void const *array,
    1.34 -        size_t n
    1.35 -) {
    1.36 -    return cx_arl_insert_array(list, list->size, array, n);
    1.37 -}
    1.38 -
    1.39 -static int cx_arl_insert(
    1.40 -        struct cx_list_s *list,
    1.41 -        size_t index,
    1.42 -        void const *elem
    1.43 -) {
    1.44 -    if (index > list->size) {
    1.45 -        return 1;
    1.46 -    } else if (index == list->size) {
    1.47 -        return cx_arl_add(list, elem);
    1.48 -    } else {
    1.49 -        cx_array_list *arl = (cx_array_list *) list;
    1.50 -
    1.51 -        // move elements starting at index to the right
    1.52 -        if (cx_array_copy(
    1.53 -                &arl->data,
    1.54 -                &list->size,
    1.55 -                &list->capacity,
    1.56 -                index + 1,
    1.57 -                ((char *) arl->data) + index * list->itemsize,
    1.58 -                list->itemsize,
    1.59 -                list->size - index,
    1.60 -                &arl->reallocator
    1.61 -        )) {
    1.62 -            return 1;
    1.63 -        }
    1.64 -
    1.65 -        // place the element
    1.66 -        memcpy(((char *) arl->data) + index * list->itemsize,
    1.67 -               elem, list->itemsize);
    1.68 -
    1.69 -        return 0;
    1.70 -    }
    1.71 -}
    1.72 -
    1.73  static int cx_arl_insert_iter(
    1.74          struct cx_mut_iterator_s *iter,
    1.75          void const *elem,
    1.76 @@ -290,10 +231,11 @@
    1.77  ) {
    1.78      struct cx_list_s *list = iter->src_handle;
    1.79      if (iter->index < list->size) {
    1.80 -        int result = cx_arl_insert(
    1.81 +        int result = 1 != cx_arl_insert_array(
    1.82                  list,
    1.83                  iter->index + 1 - prepend,
    1.84 -                elem
    1.85 +                elem,
    1.86 +                1
    1.87          );
    1.88          if (result == 0 && prepend != 0) {
    1.89              iter->index++;
    1.90 @@ -301,7 +243,7 @@
    1.91          }
    1.92          return result;
    1.93      } else {
    1.94 -        int result = cx_arl_add(list, elem);
    1.95 +        int result = 1 != cx_arl_insert_array(list, list->size, elem, 1);
    1.96          iter->index = list->size;
    1.97          return result;
    1.98      }
    1.99 @@ -463,24 +405,8 @@
   1.100      return iter;
   1.101  }
   1.102  
   1.103 -static struct cx_mut_iterator_s cx_arl_mut_iterator(
   1.104 -        struct cx_list_s *list,
   1.105 -        size_t index
   1.106 -) {
   1.107 -    CxIterator it = cx_arl_iterator(list, index);
   1.108 -    it.base.mutating = true;
   1.109 -
   1.110 -    // we know the iterators share the same memory layout
   1.111 -    CxMutIterator iter;
   1.112 -    memcpy(&iter, &it, sizeof(CxMutIterator));
   1.113 -    return iter;
   1.114 -}
   1.115 -
   1.116  static cx_list_class cx_array_list_class = {
   1.117          cx_arl_destructor,
   1.118 -        cx_arl_add,
   1.119 -        cx_arl_add_array,
   1.120 -        cx_arl_insert,
   1.121          cx_arl_insert_array,
   1.122          cx_arl_insert_iter,
   1.123          cx_arl_remove,
   1.124 @@ -490,7 +416,6 @@
   1.125          cx_arl_compare,
   1.126          cx_arl_reverse,
   1.127          cx_arl_iterator,
   1.128 -        cx_arl_mut_iterator,
   1.129  };
   1.130  
   1.131  CxList *cxArrayListCreate(
     2.1 --- a/src/cx/list.h	Mon Jan 23 20:34:18 2023 +0100
     2.2 +++ b/src/cx/list.h	Wed Jan 25 19:19:29 2023 +0100
     2.3 @@ -122,33 +122,8 @@
     2.4      void (*destructor)(struct cx_list_s *list);
     2.5  
     2.6      /**
     2.7 -     * Member function for adding an element.
     2.8 -     */
     2.9 -    int (*add)(
    2.10 -            struct cx_list_s *list,
    2.11 -            void const *elem
    2.12 -    );
    2.13 -
    2.14 -    /**
    2.15 -     * Member function for adding multiple elements.
    2.16 -     */
    2.17 -    size_t (*add_array)(
    2.18 -            struct cx_list_s *list,
    2.19 -            void const *array,
    2.20 -            size_t n
    2.21 -    );
    2.22 -
    2.23 -    /**
    2.24 -     * Member function for inserting an element.
    2.25 -     */
    2.26 -    int (*insert)(
    2.27 -            struct cx_list_s *list,
    2.28 -            size_t index,
    2.29 -            void const *elem
    2.30 -    );
    2.31 -
    2.32 -    /**
    2.33       * Member function for inserting multiple elements.
    2.34 +     * Implementors SHOULD see to performant implementations for corner cases.
    2.35       */
    2.36      size_t (*insert_array)(
    2.37              struct cx_list_s *list,
    2.38 @@ -209,20 +184,12 @@
    2.39      void (*reverse)(struct cx_list_s *list);
    2.40  
    2.41      /**
    2.42 -     * Returns an iterator pointing to the specified index.
    2.43 +     * Member function for returning an iterator pointing to the specified index.
    2.44       */
    2.45      struct cx_iterator_s (*iterator)(
    2.46              struct cx_list_s const *list,
    2.47              size_t index
    2.48      );
    2.49 -
    2.50 -    /**
    2.51 -     * Returns a mutating iterator pointing to the specified index.
    2.52 -     */
    2.53 -    struct cx_mut_iterator_s (*mut_iterator)(
    2.54 -            struct cx_list_s *list,
    2.55 -            size_t index
    2.56 -    );
    2.57  };
    2.58  
    2.59  /**
    2.60 @@ -243,7 +210,7 @@
    2.61          CxList *list,
    2.62          void const *elem
    2.63  ) {
    2.64 -    return list->cl->add(list, elem);
    2.65 +    return list->cl->insert_array(list, list->size, elem, 1) != 1;
    2.66  }
    2.67  
    2.68  /**
    2.69 @@ -265,7 +232,7 @@
    2.70          void const *array,
    2.71          size_t n
    2.72  ) {
    2.73 -    return list->cl->add_array(list, array, n);
    2.74 +    return list->cl->insert_array(list, list->size, array, n);
    2.75  }
    2.76  
    2.77  /**
    2.78 @@ -287,7 +254,7 @@
    2.79          size_t index,
    2.80          void const *elem
    2.81  ) {
    2.82 -    return list->cl->insert(list, index, elem);
    2.83 +    return list->cl->insert_array(list, index, elem, 1) != 1;
    2.84  }
    2.85  
    2.86  /**
    2.87 @@ -422,12 +389,10 @@
    2.88   * @return a new iterator
    2.89   */
    2.90  __attribute__((__nonnull__, __warn_unused_result__))
    2.91 -static inline CxMutIterator cxListMutIterator(
    2.92 +CxMutIterator cxListMutIterator(
    2.93          CxList *list,
    2.94          size_t index
    2.95 -) {
    2.96 -    return list->cl->mut_iterator(list, index);
    2.97 -}
    2.98 +);
    2.99  
   2.100  /**
   2.101   * Returns an iterator pointing to the first item of the list.
   2.102 @@ -456,7 +421,7 @@
   2.103   */
   2.104  __attribute__((__nonnull__, __warn_unused_result__))
   2.105  static inline CxMutIterator cxListBeginMut(CxList *list) {
   2.106 -    return list->cl->mut_iterator(list, 0);
   2.107 +    return cxListMutIterator(list, 0);
   2.108  }
   2.109  
   2.110  /**
     3.1 --- a/src/linked_list.c	Mon Jan 23 20:34:18 2023 +0100
     3.2 +++ b/src/linked_list.c	Wed Jan 25 19:19:29 2023 +0100
     3.3 @@ -546,29 +546,6 @@
     3.4      return n;
     3.5  }
     3.6  
     3.7 -static int cx_ll_insert(
     3.8 -        struct cx_list_s *list,
     3.9 -        size_t index,
    3.10 -        void const *elem
    3.11 -) {
    3.12 -    return cx_ll_insert_array(list, index, elem, 1) != 1;
    3.13 -}
    3.14 -
    3.15 -static int cx_ll_add(
    3.16 -        struct cx_list_s *list,
    3.17 -        void const *elem
    3.18 -) {
    3.19 -    return cx_ll_insert(list, list->size, elem);
    3.20 -}
    3.21 -
    3.22 -static size_t cx_ll_add_array(
    3.23 -        struct cx_list_s *list,
    3.24 -        void const *array,
    3.25 -        size_t n
    3.26 -) {
    3.27 -    return cx_ll_insert_array(list, list->size, array, n);
    3.28 -}
    3.29 -
    3.30  static int cx_ll_remove(
    3.31          struct cx_list_s *list,
    3.32          size_t index
    3.33 @@ -691,19 +668,6 @@
    3.34      return iter;
    3.35  }
    3.36  
    3.37 -static CxMutIterator cx_ll_mut_iterator(
    3.38 -        struct cx_list_s *list,
    3.39 -        size_t index
    3.40 -) {
    3.41 -    CxIterator it = cx_ll_iterator(list, index);
    3.42 -    it.base.mutating = true;
    3.43 -
    3.44 -    // we know the iterators share the same memory layout
    3.45 -    CxMutIterator iter;
    3.46 -    memcpy(&iter, &it, sizeof(CxMutIterator));
    3.47 -    return iter;
    3.48 -}
    3.49 -
    3.50  static int cx_ll_insert_iter(
    3.51          CxMutIterator *iter,
    3.52          void const *elem,
    3.53 @@ -718,7 +682,7 @@
    3.54          iter->index += prepend * (0 == result);
    3.55          return result;
    3.56      } else {
    3.57 -        int result = cx_ll_insert(list, list->size, elem);
    3.58 +        int result = cx_ll_insert_array(list, list->size, elem, 1) != 1;
    3.59          iter->index = list->size;
    3.60          return result;
    3.61      }
    3.62 @@ -738,9 +702,6 @@
    3.63  
    3.64  static cx_list_class cx_linked_list_class = {
    3.65          cx_ll_destructor,
    3.66 -        cx_ll_add,
    3.67 -        cx_ll_add_array,
    3.68 -        cx_ll_insert,
    3.69          cx_ll_insert_array,
    3.70          cx_ll_insert_iter,
    3.71          cx_ll_remove,
    3.72 @@ -750,7 +711,6 @@
    3.73          cx_ll_compare,
    3.74          cx_ll_reverse,
    3.75          cx_ll_iterator,
    3.76 -        cx_ll_mut_iterator,
    3.77  };
    3.78  
    3.79  CxList *cxLinkedListCreate(
     4.1 --- a/src/list.c	Mon Jan 23 20:34:18 2023 +0100
     4.2 +++ b/src/list.c	Wed Jan 25 19:19:29 2023 +0100
     4.3 @@ -28,6 +28,8 @@
     4.4  
     4.5  #include "cx/list.h"
     4.6  
     4.7 +#include <string.h>
     4.8 +
     4.9  void cxListDestroy(CxList *list) {
    4.10      switch (list->content_destructor_type) {
    4.11          case CX_DESTRUCTOR_SIMPLE: {
    4.12 @@ -80,3 +82,16 @@
    4.13          }
    4.14      }
    4.15  }
    4.16 +
    4.17 +CxMutIterator cxListMutIterator(
    4.18 +        CxList *list,
    4.19 +        size_t index
    4.20 +) {
    4.21 +    CxIterator it = list->cl->iterator(list, index);
    4.22 +    it.base.mutating = true;
    4.23 +
    4.24 +    // we know the iterators share the same memory layout
    4.25 +    CxMutIterator iter;
    4.26 +    memcpy(&iter, &it, sizeof(CxMutIterator));
    4.27 +    return iter;
    4.28 +}

mercurial