src/linked_list.c

changeset 469
0458bff0b1cd
parent 468
75ae1dccd101
child 473
1bd4b8c28722
equal deleted inserted replaced
468:75ae1dccd101 469:0458bff0b1cd
210 char payload[]; 210 char payload[];
211 }; 211 };
212 212
213 #define CX_LL_LOC_PREV offsetof(cx_linked_list_node, prev) 213 #define CX_LL_LOC_PREV offsetof(cx_linked_list_node, prev)
214 #define CX_LL_LOC_NEXT offsetof(cx_linked_list_node, next) 214 #define CX_LL_LOC_NEXT offsetof(cx_linked_list_node, next)
215 #define CX_LL_LOC_DATA offsetof(cx_linked_list_node, payload)
215 216
216 typedef struct { 217 typedef struct {
217 cx_list_s base; 218 cx_list_s base;
218 cx_linked_list_node *begin; 219 cx_linked_list_node *begin;
219 cx_linked_list_node *end; 220 cx_linked_list_node *end;
378 cx_linked_list *ll = (cx_linked_list *) list; 379 cx_linked_list *ll = (cx_linked_list *) list;
379 cx_linked_list_node *last = ll->end; 380 cx_linked_list_node *last = ll->end;
380 return last == NULL ? NULL : *(void **) last->payload; 381 return last == NULL ? NULL : *(void **) last->payload;
381 } 382 }
382 383
384 static void cx_ll_sort(cx_list_s *list) {
385 cx_linked_list *ll = (cx_linked_list *) list;
386 cx_linked_list_sort((void **) &ll->begin, (void **) &ll->end,
387 CX_LL_LOC_PREV, CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
388 0, list->cmpfunc);
389 }
390
391 static void cx_pll_sort(cx_list_s *list) {
392 cx_linked_list *ll = (cx_linked_list *) list;
393 cx_linked_list_sort((void **) &ll->begin, (void **) &ll->end,
394 CX_LL_LOC_PREV, CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
395 1, list->cmpfunc);
396 }
397
383 static cx_list_class cx_linked_list_class = { 398 static cx_list_class cx_linked_list_class = {
384 cx_ll_add, 399 cx_ll_add,
385 cx_ll_insert, 400 cx_ll_insert,
386 cx_ll_remove, 401 cx_ll_remove,
387 cx_ll_at, 402 cx_ll_at,
388 cx_ll_find, 403 cx_ll_find,
389 cx_ll_last 404 cx_ll_last,
405 cx_ll_sort
390 }; 406 };
391 407
392 static cx_list_class cx_pointer_linked_list_class = { 408 static cx_list_class cx_pointer_linked_list_class = {
393 cx_pll_add, 409 cx_pll_add,
394 cx_pll_insert, 410 cx_pll_insert,
395 cx_ll_remove, 411 cx_ll_remove,
396 cx_pll_at, 412 cx_pll_at,
397 cx_pll_find, 413 cx_pll_find,
398 cx_pll_last 414 cx_pll_last,
415 cx_pll_sort
399 }; 416 };
400 417
401 CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t item_size) { 418 CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t item_size) {
402 cx_linked_list *list = cxMalloc(allocator, sizeof(cx_linked_list)); 419 cx_linked_list *list = cxMalloc(allocator, sizeof(cx_linked_list));
403 if (list == NULL) 420 if (list == NULL)

mercurial