change inheritance model for lists

Sun, 26 Sep 2021 18:31:24 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 26 Sep 2021 18:31:24 +0200
changeset 435
0fe204d50f54
parent 434
38ee262e8b94
child 436
ca9ce2297c29

change inheritance model for lists

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
test/test_list.c file | annotate | diff | comparison | revisions
     1.1 --- a/src/cx/list.h	Sun Sep 26 18:01:51 2021 +0200
     1.2 +++ b/src/cx/list.h	Sun Sep 26 18:31:24 2021 +0200
     1.3 @@ -38,31 +38,28 @@
     1.4  
     1.5  typedef int(*CxListComparator)(void const *left, void const *right);
     1.6  
     1.7 +typedef struct cx_list_s cx_list_s;
     1.8 +
     1.9  typedef struct {
    1.10 +    int (*add)(cx_list_s *list, void *elem);
    1.11 +
    1.12 +    int (*insert)(cx_list_s *list, size_t index, void *elem);
    1.13 +
    1.14 +    void *(*remove)(cx_list_s *list, size_t index);
    1.15 +
    1.16 +    size_t (*find)(cx_list_s *list, void *elem);
    1.17 +
    1.18 +    void *(*last)(cx_list_s *list);
    1.19 +} cx_list_class;
    1.20 +
    1.21 +struct cx_list_s {
    1.22 +    cx_list_class *cl;
    1.23      CxAllocator allocator;
    1.24      CxListComparator cmpfunc;
    1.25      size_t itemsize;
    1.26      size_t size;
    1.27      size_t capacity;
    1.28 -    char listdata[];
    1.29 -} cx_list;
    1.30 -
    1.31 -typedef struct {
    1.32 -    int (*add)(cx_list *list, void *elem);
    1.33 -
    1.34 -    int (*insert)(cx_list *list, size_t index, void *elem);
    1.35 -
    1.36 -    void *(*remove)(cx_list *list, size_t index);
    1.37 -
    1.38 -    size_t (*find)(cx_list *list, void *elem);
    1.39 -
    1.40 -    void *(*last)(cx_list *list);
    1.41 -} cx_list_class;
    1.42 -
    1.43 -typedef struct {
    1.44 -    cx_list_class *cl;
    1.45 -    cx_list data;
    1.46 -} cx_list_s;
    1.47 +};
    1.48  
    1.49  typedef cx_list_s *CxList;
    1.50  
     2.1 --- a/src/linked_list.c	Sun Sep 26 18:01:51 2021 +0200
     2.2 +++ b/src/linked_list.c	Sun Sep 26 18:31:24 2021 +0200
     2.3 @@ -90,27 +90,27 @@
     2.4  };
     2.5  
     2.6  typedef struct {
     2.7 +    cx_list_s base;
     2.8      void *begin;
     2.9      void *end;
    2.10      ptrdiff_t loc_prev;
    2.11      ptrdiff_t loc_next;
    2.12  } cx_linked_list;
    2.13  
    2.14 -int cx_ll_add(cx_list *list, void *elem) {
    2.15 -    cx_linked_list *listdata = (cx_linked_list *) list->listdata;
    2.16 -    CxAllocator allocator = list->allocator;
    2.17 +int cx_ll_add(cx_list_s *list, void *elem) {
    2.18 +    cx_linked_list *linkedlist = (cx_linked_list *) list;
    2.19  
    2.20 -    struct cx_linked_list_node *node = cxMalloc(allocator,
    2.21 +    struct cx_linked_list_node *node = cxMalloc(list->allocator,
    2.22                                                  sizeof(struct cx_linked_list_node) + list->itemsize);
    2.23      if (node == NULL)
    2.24          return 1;
    2.25  
    2.26 -    node->next = NULL;
    2.27 +    node->next = node->prev = NULL;
    2.28      memcpy(node->payload, elem, list->itemsize);
    2.29  
    2.30      int ret = cx_linked_list_add(
    2.31 -            &listdata->begin,
    2.32 -            &listdata->end,
    2.33 +            &linkedlist->begin,
    2.34 +            &linkedlist->end,
    2.35              offsetof(struct cx_linked_list_node, prev),
    2.36              offsetof(struct cx_linked_list_node, next),
    2.37              node
    2.38 @@ -123,28 +123,28 @@
    2.39      }
    2.40  }
    2.41  
    2.42 -int cx_ll_insert(cx_list *list, size_t index, void *elem) {
    2.43 -    cx_linked_list *listdata = (cx_linked_list *) list->listdata;
    2.44 +int cx_ll_insert(cx_list_s *list, size_t index, void *elem) {
    2.45 +    cx_linked_list *linkedList = (cx_linked_list *) list;
    2.46      // TODO: implement using low level API
    2.47      return 1;
    2.48  }
    2.49  
    2.50 -void *cx_ll_remove(cx_list *list, size_t index) {
    2.51 -    cx_linked_list *listdata = (cx_linked_list *) list->listdata;
    2.52 +void *cx_ll_remove(cx_list_s *list, size_t index) {
    2.53 +    cx_linked_list *linkedList = (cx_linked_list *) list;
    2.54      // TODO: implement using low level API
    2.55      return NULL;
    2.56  }
    2.57  
    2.58 -size_t cx_ll_find(cx_list *list, void *elem) {
    2.59 -    cx_linked_list *listdata = (cx_linked_list *) list->listdata;
    2.60 +size_t cx_ll_find(cx_list_s *list, void *elem) {
    2.61 +    cx_linked_list *linkedList = (cx_linked_list *) list;
    2.62      // TODO: implement using low level API
    2.63      return 0;
    2.64  }
    2.65  
    2.66 -void *cx_ll_last(cx_list *list) {
    2.67 -    cx_linked_list *listdata = (cx_linked_list *) list->listdata;
    2.68 +void *cx_ll_last(cx_list_s *list) {
    2.69 +    cx_linked_list *linkedList = (cx_linked_list *) list;
    2.70      struct cx_linked_list_node *last = cx_linked_list_last(
    2.71 -            NULL, &listdata->end, offsetof(struct cx_linked_list_node, next));
    2.72 +            NULL, &linkedList->end, offsetof(struct cx_linked_list_node, next));
    2.73      return &last->payload;
    2.74  }
    2.75  
    2.76 @@ -157,47 +157,47 @@
    2.77  };
    2.78  
    2.79  CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t item_size) {
    2.80 -    CxList list = cxMalloc(allocator, sizeof(cx_list_s) + sizeof(cx_linked_list));
    2.81 +    cx_linked_list* list = cxMalloc(allocator, sizeof(cx_linked_list));
    2.82      if (list == NULL)
    2.83          return NULL;
    2.84  
    2.85 -    list->cl = &cx_linked_list_class;
    2.86 -    list->data.allocator = allocator;
    2.87 -    list->data.cmpfunc = comparator;
    2.88 -    list->data.itemsize = item_size;
    2.89 -    list->data.capacity = SIZE_MAX;
    2.90 +    list->base.cl = &cx_linked_list_class;
    2.91 +    list->base.allocator = allocator;
    2.92 +    list->base.cmpfunc = comparator;
    2.93 +    list->base.itemsize = item_size;
    2.94 +    list->base.capacity = SIZE_MAX;
    2.95  
    2.96 -    cx_linked_list *ll = (cx_linked_list *) list->data.listdata;
    2.97 -    ll->begin = NULL;
    2.98 -    ll->loc_prev = offsetof(struct cx_linked_list_node, prev);
    2.99 -    ll->loc_next = offsetof(struct cx_linked_list_node, next);
   2.100 -    cxLinkedListRecalculateSize(list);
   2.101 +    list->begin = NULL;
   2.102 +    list->loc_prev = offsetof(struct cx_linked_list_node, prev);
   2.103 +    list->loc_next = offsetof(struct cx_linked_list_node, next);
   2.104  
   2.105 -    return list;
   2.106 +    CxList result = (CxList) list;
   2.107 +    cxLinkedListRecalculateSize(result);
   2.108 +    return result;
   2.109  }
   2.110  
   2.111  void cxLinkedListDestroy(CxList list) {
   2.112      // TODO: free contents
   2.113 -    cxFree(list->data.allocator, list);
   2.114 +    cxFree(list->allocator, list);
   2.115  }
   2.116  
   2.117  size_t cxLinkedListRecalculateSize(CxList list) {
   2.118 -    cx_linked_list *ll = (cx_linked_list *) list->data.listdata;
   2.119 +    cx_linked_list *ll = (cx_linked_list *) list;
   2.120  
   2.121      if (ll->begin == NULL) {
   2.122 -        list->data.size = 0;
   2.123 +        ll->base.size = 0;
   2.124          ll->end = NULL;
   2.125          return 0;
   2.126      } else {
   2.127          void *cur = ll->begin;
   2.128          void *last;
   2.129 -        size_t size;
   2.130 +        size_t size = 0;
   2.131          do {
   2.132              last = cur;
   2.133              size++;
   2.134          } while ((cur = *CX_LL_PTR(cur, ll->loc_next)) != NULL);
   2.135          ll->end = last;
   2.136 -        list->data.size = size;
   2.137 +        ll->base.size = size;
   2.138          return size;
   2.139      }
   2.140  }
     3.1 --- a/src/list.c	Sun Sep 26 18:01:51 2021 +0200
     3.2 +++ b/src/list.c	Sun Sep 26 18:31:24 2021 +0200
     3.3 @@ -29,21 +29,21 @@
     3.4  #include "cx/list.h"
     3.5  
     3.6  int cxListAdd(CxList list, void *elem) {
     3.7 -    return list->cl->add(&list->data, elem);
     3.8 +    return list->cl->add(list, elem);
     3.9  }
    3.10  
    3.11  int cxListInsert(CxList list, size_t index, void *elem) {
    3.12 -    return list->cl->insert(&list->data, index, elem);
    3.13 +    return list->cl->insert(list, index, elem);
    3.14  }
    3.15  
    3.16  void *cxListRemove(CxList list, size_t index) {
    3.17 -    return list->cl->remove(&list->data, index);
    3.18 +    return list->cl->remove(list, index);
    3.19  }
    3.20  
    3.21  size_t cxListFind(CxList list, void *elem) {
    3.22 -    return list->cl->find(&list->data, elem);
    3.23 +    return list->cl->find(list, elem);
    3.24  }
    3.25  
    3.26  void *cxListLast(CxList list) {
    3.27 -    return list->cl->last(&list->data);
    3.28 +    return list->cl->last(list);
    3.29  }
     4.1 --- a/test/test_list.c	Sun Sep 26 18:01:51 2021 +0200
     4.2 +++ b/test/test_list.c	Sun Sep 26 18:31:24 2021 +0200
     4.3 @@ -40,20 +40,22 @@
     4.4  
     4.5      CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int));
     4.6  
     4.7 -    CU_ASSERT_EQUAL(list->data.size, 0)
     4.8 -    CU_ASSERT_EQUAL(list->data.capacity, (size_t) -1)
     4.9 -    CU_ASSERT_PTR_EQUAL(list->data.allocator, cxTestingAllocator)
    4.10 -    CU_ASSERT_EQUAL(list->data.itemsize, sizeof(int))
    4.11 -    CU_ASSERT_PTR_EQUAL(list->data.cmpfunc, cmp_int)
    4.12 +    CU_ASSERT_EQUAL(list->size, 0)
    4.13 +    CU_ASSERT_EQUAL(list->capacity, (size_t) -1)
    4.14 +    CU_ASSERT_PTR_EQUAL(list->allocator, cxTestingAllocator)
    4.15 +    CU_ASSERT_EQUAL(list->itemsize, sizeof(int))
    4.16 +    CU_ASSERT_PTR_EQUAL(list->cmpfunc, cmp_int)
    4.17  
    4.18 -    struct node {
    4.19 +    // assume this structure for a linked list
    4.20 +    struct ll_check {
    4.21 +        cx_list_s base;
    4.22          void *begin;
    4.23          void *end;
    4.24          ptrdiff_t ploc;
    4.25          ptrdiff_t nloc;
    4.26      };
    4.27  
    4.28 -    struct node *actual = (struct node *) list->data.listdata;
    4.29 +    struct ll_check *actual = (struct ll_check *) list;
    4.30      CU_ASSERT_PTR_NULL(actual->begin)
    4.31      CU_ASSERT_PTR_NULL(actual->end)
    4.32      CU_ASSERT_EQUAL(0, actual->ploc)

mercurial