src/linked_list.c

changeset 438
cd3069757010
parent 437
9d4971ea0625
child 439
9a5adedd6de6
equal deleted inserted replaced
437:9d4971ea0625 438:cd3069757010
32 32
33 /* LOW LEVEL LINKED LIST FUNCTIONS */ 33 /* LOW LEVEL LINKED LIST FUNCTIONS */
34 34
35 #define CX_LL_PTR(cur, off) ((void**)(((char*)cur)+off)) 35 #define CX_LL_PTR(cur, off) ((void**)(((char*)cur)+off))
36 36
37 void *cx_linked_list_at(void *start, size_t start_index, ptrdiff_t loc_advance, size_t index) {
38 size_t i = start_index;
39 void* cur = start;
40 while (i != index && cur != NULL) {
41 cur = *CX_LL_PTR(cur, loc_advance);
42 i < index ? i++ : i--;
43 }
44 return cur;
45 }
46
37 void *cx_linked_list_last(void **begin, void **end, ptrdiff_t loc_next) { 47 void *cx_linked_list_last(void **begin, void **end, ptrdiff_t loc_next) {
38 if (end != NULL) { 48 if (end != NULL) {
39 return *end; 49 return *end;
40 } else { 50 } else {
41 if (begin == NULL || *begin == NULL) 51 if (begin == NULL || *begin == NULL)
95 void *end; 105 void *end;
96 ptrdiff_t loc_prev; 106 ptrdiff_t loc_prev;
97 ptrdiff_t loc_next; 107 ptrdiff_t loc_next;
98 } cx_linked_list; 108 } cx_linked_list;
99 109
100 int cx_ll_add(cx_list_s *list, void *elem) { 110 static int cx_ll_add(cx_list_s *list, void *elem) {
101 cx_linked_list *ll = (cx_linked_list *) list; 111 cx_linked_list *ll = (cx_linked_list *) list;
102 112
103 struct cx_linked_list_node *node = cxMalloc(list->allocator, 113 struct cx_linked_list_node *node = cxMalloc(list->allocator,
104 sizeof(struct cx_linked_list_node) + list->itemsize); 114 sizeof(struct cx_linked_list_node) + list->itemsize);
105 if (node == NULL) 115 if (node == NULL)
120 } else { 130 } else {
121 return ret; 131 return ret;
122 } 132 }
123 } 133 }
124 134
125 int cx_ll_insert(cx_list_s *list, size_t index, void *elem) { 135 static int cx_ll_insert(cx_list_s *list, size_t index, void *elem) {
126 cx_linked_list *ll = (cx_linked_list *) list; 136 cx_linked_list *ll = (cx_linked_list *) list;
127 // TODO: implement using low level API 137 // TODO: implement using low level API
128 return 1; 138 return 1;
129 } 139 }
130 140
131 void *cx_ll_remove(cx_list_s *list, size_t index) { 141 static int cx_ll_remove(cx_list_s *list, size_t index) {
142 if (index >= list->size) {
143 return 1;
144 }
132 cx_linked_list *ll = (cx_linked_list *) list; 145 cx_linked_list *ll = (cx_linked_list *) list;
133 // TODO: implement using low level API 146 // TODO: implement using low level API
134 return NULL; 147 return 0;
135 } 148 }
136 149
137 size_t cx_ll_find(cx_list_s *list, void *elem) { 150 static size_t cx_ll_find(cx_list_s *list, void *elem) {
138 CxListComparator cmp = list->cmpfunc; 151 CxListComparator cmp = list->cmpfunc;
139 cx_linked_list *ll = (cx_linked_list *) list; 152 cx_linked_list *ll = (cx_linked_list *) list;
140 153
141 size_t index; 154 size_t index;
142 struct cx_linked_list_node* node = ll->begin; 155 struct cx_linked_list_node* node = ll->begin;
148 node = node->next; 161 node = node->next;
149 } 162 }
150 return index; 163 return index;
151 } 164 }
152 165
153 void *cx_ll_last(cx_list_s *list) { 166 static void *cx_ll_last(cx_list_s *list) {
154 cx_linked_list *linkedList = (cx_linked_list *) list; 167 cx_linked_list *linkedList = (cx_linked_list *) list;
155 struct cx_linked_list_node *last = cx_linked_list_last( 168 struct cx_linked_list_node *last = cx_linked_list_last(
156 NULL, &linkedList->end, offsetof(struct cx_linked_list_node, next)); 169 NULL, &linkedList->end, offsetof(struct cx_linked_list_node, next));
157 return &last->payload; 170 return &last->payload;
158 } 171 }

mercurial