src/linked_list.c

changeset 435
0fe204d50f54
parent 428
da66264af8ad
child 436
ca9ce2297c29
     1.1 --- a/src/linked_list.c	Sun Sep 26 18:01:51 2021 +0200
     1.2 +++ b/src/linked_list.c	Sun Sep 26 18:31:24 2021 +0200
     1.3 @@ -90,27 +90,27 @@
     1.4  };
     1.5  
     1.6  typedef struct {
     1.7 +    cx_list_s base;
     1.8      void *begin;
     1.9      void *end;
    1.10      ptrdiff_t loc_prev;
    1.11      ptrdiff_t loc_next;
    1.12  } cx_linked_list;
    1.13  
    1.14 -int cx_ll_add(cx_list *list, void *elem) {
    1.15 -    cx_linked_list *listdata = (cx_linked_list *) list->listdata;
    1.16 -    CxAllocator allocator = list->allocator;
    1.17 +int cx_ll_add(cx_list_s *list, void *elem) {
    1.18 +    cx_linked_list *linkedlist = (cx_linked_list *) list;
    1.19  
    1.20 -    struct cx_linked_list_node *node = cxMalloc(allocator,
    1.21 +    struct cx_linked_list_node *node = cxMalloc(list->allocator,
    1.22                                                  sizeof(struct cx_linked_list_node) + list->itemsize);
    1.23      if (node == NULL)
    1.24          return 1;
    1.25  
    1.26 -    node->next = NULL;
    1.27 +    node->next = node->prev = NULL;
    1.28      memcpy(node->payload, elem, list->itemsize);
    1.29  
    1.30      int ret = cx_linked_list_add(
    1.31 -            &listdata->begin,
    1.32 -            &listdata->end,
    1.33 +            &linkedlist->begin,
    1.34 +            &linkedlist->end,
    1.35              offsetof(struct cx_linked_list_node, prev),
    1.36              offsetof(struct cx_linked_list_node, next),
    1.37              node
    1.38 @@ -123,28 +123,28 @@
    1.39      }
    1.40  }
    1.41  
    1.42 -int cx_ll_insert(cx_list *list, size_t index, void *elem) {
    1.43 -    cx_linked_list *listdata = (cx_linked_list *) list->listdata;
    1.44 +int cx_ll_insert(cx_list_s *list, size_t index, void *elem) {
    1.45 +    cx_linked_list *linkedList = (cx_linked_list *) list;
    1.46      // TODO: implement using low level API
    1.47      return 1;
    1.48  }
    1.49  
    1.50 -void *cx_ll_remove(cx_list *list, size_t index) {
    1.51 -    cx_linked_list *listdata = (cx_linked_list *) list->listdata;
    1.52 +void *cx_ll_remove(cx_list_s *list, size_t index) {
    1.53 +    cx_linked_list *linkedList = (cx_linked_list *) list;
    1.54      // TODO: implement using low level API
    1.55      return NULL;
    1.56  }
    1.57  
    1.58 -size_t cx_ll_find(cx_list *list, void *elem) {
    1.59 -    cx_linked_list *listdata = (cx_linked_list *) list->listdata;
    1.60 +size_t cx_ll_find(cx_list_s *list, void *elem) {
    1.61 +    cx_linked_list *linkedList = (cx_linked_list *) list;
    1.62      // TODO: implement using low level API
    1.63      return 0;
    1.64  }
    1.65  
    1.66 -void *cx_ll_last(cx_list *list) {
    1.67 -    cx_linked_list *listdata = (cx_linked_list *) list->listdata;
    1.68 +void *cx_ll_last(cx_list_s *list) {
    1.69 +    cx_linked_list *linkedList = (cx_linked_list *) list;
    1.70      struct cx_linked_list_node *last = cx_linked_list_last(
    1.71 -            NULL, &listdata->end, offsetof(struct cx_linked_list_node, next));
    1.72 +            NULL, &linkedList->end, offsetof(struct cx_linked_list_node, next));
    1.73      return &last->payload;
    1.74  }
    1.75  
    1.76 @@ -157,47 +157,47 @@
    1.77  };
    1.78  
    1.79  CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t item_size) {
    1.80 -    CxList list = cxMalloc(allocator, sizeof(cx_list_s) + sizeof(cx_linked_list));
    1.81 +    cx_linked_list* list = cxMalloc(allocator, sizeof(cx_linked_list));
    1.82      if (list == NULL)
    1.83          return NULL;
    1.84  
    1.85 -    list->cl = &cx_linked_list_class;
    1.86 -    list->data.allocator = allocator;
    1.87 -    list->data.cmpfunc = comparator;
    1.88 -    list->data.itemsize = item_size;
    1.89 -    list->data.capacity = SIZE_MAX;
    1.90 +    list->base.cl = &cx_linked_list_class;
    1.91 +    list->base.allocator = allocator;
    1.92 +    list->base.cmpfunc = comparator;
    1.93 +    list->base.itemsize = item_size;
    1.94 +    list->base.capacity = SIZE_MAX;
    1.95  
    1.96 -    cx_linked_list *ll = (cx_linked_list *) list->data.listdata;
    1.97 -    ll->begin = NULL;
    1.98 -    ll->loc_prev = offsetof(struct cx_linked_list_node, prev);
    1.99 -    ll->loc_next = offsetof(struct cx_linked_list_node, next);
   1.100 -    cxLinkedListRecalculateSize(list);
   1.101 +    list->begin = NULL;
   1.102 +    list->loc_prev = offsetof(struct cx_linked_list_node, prev);
   1.103 +    list->loc_next = offsetof(struct cx_linked_list_node, next);
   1.104  
   1.105 -    return list;
   1.106 +    CxList result = (CxList) list;
   1.107 +    cxLinkedListRecalculateSize(result);
   1.108 +    return result;
   1.109  }
   1.110  
   1.111  void cxLinkedListDestroy(CxList list) {
   1.112      // TODO: free contents
   1.113 -    cxFree(list->data.allocator, list);
   1.114 +    cxFree(list->allocator, list);
   1.115  }
   1.116  
   1.117  size_t cxLinkedListRecalculateSize(CxList list) {
   1.118 -    cx_linked_list *ll = (cx_linked_list *) list->data.listdata;
   1.119 +    cx_linked_list *ll = (cx_linked_list *) list;
   1.120  
   1.121      if (ll->begin == NULL) {
   1.122 -        list->data.size = 0;
   1.123 +        ll->base.size = 0;
   1.124          ll->end = NULL;
   1.125          return 0;
   1.126      } else {
   1.127          void *cur = ll->begin;
   1.128          void *last;
   1.129 -        size_t size;
   1.130 +        size_t size = 0;
   1.131          do {
   1.132              last = cur;
   1.133              size++;
   1.134          } while ((cur = *CX_LL_PTR(cur, ll->loc_next)) != NULL);
   1.135          ll->end = last;
   1.136 -        list->data.size = size;
   1.137 +        ll->base.size = size;
   1.138          return size;
   1.139      }
   1.140  }

mercurial