src/linked_list.c

changeset 466
28bc3e10ac28
parent 457
8f7d3fe9ca40
child 467
95e42a963520
     1.1 --- a/src/linked_list.c	Tue Oct 05 12:25:23 2021 +0200
     1.2 +++ b/src/linked_list.c	Tue Oct 05 13:03:45 2021 +0200
     1.3 @@ -172,6 +172,14 @@
     1.4      return cx_ll_insert(list, list->size, elem);
     1.5  }
     1.6  
     1.7 +static int cx_pll_insert(cx_list_s *list, size_t index, void *elem) {
     1.8 +    return cx_ll_insert(list, index, &elem);
     1.9 +}
    1.10 +
    1.11 +static int cx_pll_add(cx_list_s *list, void *elem) {
    1.12 +    return cx_ll_insert(list, list->size, &elem);
    1.13 +}
    1.14 +
    1.15  static int cx_ll_remove(cx_list_s *list, size_t index) {
    1.16      cx_linked_list *ll = (cx_linked_list *) list;
    1.17      cx_linked_list_node *node = cx_ll_node_at(ll, index);
    1.18 @@ -205,7 +213,13 @@
    1.19  static void *cx_ll_at(cx_list_s *list, size_t index) {
    1.20      cx_linked_list *ll = (cx_linked_list *) list;
    1.21      cx_linked_list_node *node = cx_ll_node_at(ll, index);
    1.22 -    return node == NULL ? NULL : &node->payload;
    1.23 +    return node == NULL ? NULL : node->payload;
    1.24 +}
    1.25 +
    1.26 +static void *cx_pll_at(cx_list_s *list, size_t index) {
    1.27 +    cx_linked_list *ll = (cx_linked_list *) list;
    1.28 +    cx_linked_list_node *node = cx_ll_node_at(ll, index);
    1.29 +    return node == NULL ? NULL : *(void**)node->payload;
    1.30  }
    1.31  
    1.32  static size_t cx_ll_find(cx_list_s *list, void *elem) {
    1.33 @@ -224,10 +238,32 @@
    1.34      return index;
    1.35  }
    1.36  
    1.37 +static size_t cx_pll_find(cx_list_s *list, void *elem) {
    1.38 +    CxListComparator cmp = list->cmpfunc;
    1.39 +    cx_linked_list *ll = (cx_linked_list *) list;
    1.40 +
    1.41 +    size_t index;
    1.42 +    cx_linked_list_node *node = ll->begin;
    1.43 +    for (index = 0; index < list->size; index++) {
    1.44 +        void *current = *(void**)node->payload;
    1.45 +        if (cmp(current, elem) == 0) {
    1.46 +            return index;
    1.47 +        }
    1.48 +        node = node->next;
    1.49 +    }
    1.50 +    return index;
    1.51 +}
    1.52 +
    1.53  static void *cx_ll_last(cx_list_s *list) {
    1.54      cx_linked_list *ll = (cx_linked_list *) list;
    1.55      cx_linked_list_node *last = ll->end;
    1.56 -    return last == NULL ? NULL : &last->payload;
    1.57 +    return last == NULL ? NULL : last->payload;
    1.58 +}
    1.59 +
    1.60 +static void *cx_pll_last(cx_list_s *list) {
    1.61 +    cx_linked_list *ll = (cx_linked_list *) list;
    1.62 +    cx_linked_list_node *last = ll->end;
    1.63 +    return last == NULL ? NULL : *(void**)last->payload;
    1.64  }
    1.65  
    1.66  static cx_list_class cx_linked_list_class = {
    1.67 @@ -239,6 +275,15 @@
    1.68          cx_ll_last
    1.69  };
    1.70  
    1.71 +static cx_list_class cx_pointer_linked_list_class = {
    1.72 +        cx_pll_add,
    1.73 +        cx_pll_insert,
    1.74 +        cx_ll_remove,
    1.75 +        cx_pll_at,
    1.76 +        cx_pll_find,
    1.77 +        cx_pll_last
    1.78 +};
    1.79 +
    1.80  CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t item_size) {
    1.81      cx_linked_list *list = cxMalloc(allocator, sizeof(cx_linked_list));
    1.82      if (list == NULL)
    1.83 @@ -257,6 +302,24 @@
    1.84      return (CxList) list;
    1.85  }
    1.86  
    1.87 +CxList cxPointerLinkedListCreate(CxAllocator allocator, CxListComparator comparator) {
    1.88 +    cx_linked_list *list = cxMalloc(allocator, sizeof(cx_linked_list));
    1.89 +    if (list == NULL)
    1.90 +        return NULL;
    1.91 +
    1.92 +    list->base.cl = &cx_pointer_linked_list_class;
    1.93 +    list->base.allocator = allocator;
    1.94 +    list->base.cmpfunc = comparator;
    1.95 +    list->base.itemsize = sizeof(void*);
    1.96 +    list->base.capacity = SIZE_MAX;
    1.97 +    list->base.size = 0;
    1.98 +
    1.99 +    list->begin = NULL;
   1.100 +    list->end = NULL;
   1.101 +
   1.102 +    return (CxList) list;
   1.103 +}
   1.104 +
   1.105  void cxLinkedListDestroy(CxList list) {
   1.106      cx_linked_list *ll = (cx_linked_list *) list;
   1.107  

mercurial