src/linked_list.c

changeset 488
9138acaa494b
parent 487
4bd19279778c
child 489
af6be1e123aa
equal deleted inserted replaced
487:4bd19279778c 488:9138acaa494b
611 cx_linked_list_sort((void **) &ll->begin, (void **) &ll->end, 611 cx_linked_list_sort((void **) &ll->begin, (void **) &ll->end,
612 CX_LL_LOC_PREV, CX_LL_LOC_NEXT, CX_LL_LOC_DATA, 612 CX_LL_LOC_PREV, CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
613 true, list->cmpfunc); 613 true, list->cmpfunc);
614 } 614 }
615 615
616 static int cx_ll_compare(
617 cx_list_s *list,
618 cx_list_s *other
619 ) {
620 cx_linked_list *left = (cx_linked_list *) list;
621 cx_linked_list *right = (cx_linked_list *) other;
622 return cx_linked_list_compare(left->begin, right->begin,
623 CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
624 false, list->cmpfunc);
625 }
626
627 static int cx_pll_compare(
628 cx_list_s *list,
629 cx_list_s *other
630 ) {
631 cx_linked_list *left = (cx_linked_list *) list;
632 cx_linked_list *right = (cx_linked_list *) other;
633 return cx_linked_list_compare(left->begin, right->begin,
634 CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
635 true, list->cmpfunc);
636 }
637
616 static cx_list_class cx_linked_list_class = { 638 static cx_list_class cx_linked_list_class = {
617 cx_ll_add, 639 cx_ll_add,
618 cx_ll_insert, 640 cx_ll_insert,
619 cx_ll_remove, 641 cx_ll_remove,
620 cx_ll_at, 642 cx_ll_at,
621 cx_ll_find, 643 cx_ll_find,
622 cx_ll_sort 644 cx_ll_sort,
645 cx_ll_compare
623 }; 646 };
624 647
625 static cx_list_class cx_pointer_linked_list_class = { 648 static cx_list_class cx_pointer_linked_list_class = {
626 cx_pll_add, 649 cx_pll_add,
627 cx_pll_insert, 650 cx_pll_insert,
628 cx_ll_remove, 651 cx_ll_remove,
629 cx_pll_at, 652 cx_pll_at,
630 cx_pll_find, 653 cx_pll_find,
631 cx_pll_sort 654 cx_pll_sort,
655 cx_pll_compare
632 }; 656 };
633 657
634 CxList cxLinkedListCreate( 658 CxList cxLinkedListCreate(
635 CxAllocator allocator, 659 CxAllocator allocator,
636 CxListComparator comparator, 660 CxListComparator comparator,
672 list->end = NULL; 696 list->end = NULL;
673 697
674 return (CxList) list; 698 return (CxList) list;
675 } 699 }
676 700
701 CxList cxLinkedListFromArray(
702 CxAllocator allocator,
703 CxListComparator comparator,
704 size_t item_size,
705 size_t num_items,
706 const void *array
707 ) {
708 CxList list = cxLinkedListCreate(allocator, comparator, item_size);
709 if (list == NULL) return NULL;
710 for (size_t i = 0; i < num_items; i++) {
711 if (0 != cxListAdd(list, ((const unsigned char *) array) + i * item_size)) {
712 cxLinkedListDestroy(list);
713 return NULL;
714 }
715 }
716 return list;
717 }
718
677 void cxLinkedListDestroy(CxList list) { 719 void cxLinkedListDestroy(CxList list) {
678 cx_linked_list *ll = (cx_linked_list *) list; 720 cx_linked_list *ll = (cx_linked_list *) list;
679 721
680 cx_linked_list_node *node = ll->begin; 722 cx_linked_list_node *node = ll->begin;
681 while (node) { 723 while (node) {

mercurial