src/linked_list.c

changeset 503
a89857072ace
parent 500
eb9e7bd40a8e
child 508
8aea65ae1eaf
equal deleted inserted replaced
502:33e7b6ebf403 503:a89857072ace
755 cx_pll_compare, 755 cx_pll_compare,
756 cx_ll_reverse, 756 cx_ll_reverse,
757 cx_pll_iterator, 757 cx_pll_iterator,
758 }; 758 };
759 759
760 static CxList *cx_ll_default_destructor(CxList *list) {
761 cx_linked_list *ll = (cx_linked_list *) list;
762
763 cx_linked_list_node *node = ll->begin;
764 while (node) {
765 void *next = node->next;
766 cxFree(list->allocator, node);
767 node = next;
768 }
769
770 cxFree(list->allocator, list);
771 return NULL;
772 }
773
760 CxList *cxLinkedListCreate( 774 CxList *cxLinkedListCreate(
761 CxAllocator *allocator, 775 CxAllocator *allocator,
762 CxListComparator comparator, 776 CxListComparator comparator,
763 size_t item_size 777 size_t item_size
764 ) { 778 ) {
765 cx_linked_list *list = cxMalloc(allocator, sizeof(cx_linked_list)); 779 cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list));
766 if (list == NULL) 780 if (list == NULL)
767 return NULL; 781 return NULL;
768 782
769 list->base.cl = &cx_linked_list_class; 783 list->base.cl = &cx_linked_list_class;
770 list->base.allocator = allocator; 784 list->base.allocator = allocator;
785 list->base.list_destructor = (cx_destructor_func) cx_ll_default_destructor;
771 list->base.cmpfunc = comparator; 786 list->base.cmpfunc = comparator;
772 list->base.itemsize = item_size; 787 list->base.itemsize = item_size;
773 list->base.capacity = SIZE_MAX; 788 list->base.capacity = SIZE_MAX;
774 list->base.size = 0;
775
776 list->begin = NULL;
777 list->end = NULL;
778 789
779 return (CxList *) list; 790 return (CxList *) list;
780 } 791 }
781 792
782 CxList *cxPointerLinkedListCreate( 793 CxList *cxPointerLinkedListCreate(
783 CxAllocator *allocator, 794 CxAllocator *allocator,
784 CxListComparator comparator 795 CxListComparator comparator
785 ) { 796 ) {
786 cx_linked_list *list = cxMalloc(allocator, sizeof(cx_linked_list)); 797 cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list));
787 if (list == NULL) 798 if (list == NULL)
788 return NULL; 799 return NULL;
789 800
790 list->base.cl = &cx_pointer_linked_list_class; 801 list->base.cl = &cx_pointer_linked_list_class;
791 list->base.allocator = allocator; 802 list->base.allocator = allocator;
803 list->base.list_destructor = (cx_destructor_func) cx_ll_default_destructor;
792 list->base.cmpfunc = comparator; 804 list->base.cmpfunc = comparator;
793 list->base.itemsize = sizeof(void *); 805 list->base.itemsize = sizeof(void *);
794 list->base.capacity = SIZE_MAX; 806 list->base.capacity = SIZE_MAX;
795 list->base.size = 0;
796
797 list->begin = NULL;
798 list->end = NULL;
799 807
800 return (CxList *) list; 808 return (CxList *) list;
801 } 809 }
802 810
803 CxList *cxLinkedListFromArray( 811 CxList *cxLinkedListFromArray(
809 ) { 817 ) {
810 CxList *list = cxLinkedListCreate(allocator, comparator, item_size); 818 CxList *list = cxLinkedListCreate(allocator, comparator, item_size);
811 if (list == NULL) return NULL; 819 if (list == NULL) return NULL;
812 for (size_t i = 0; i < num_items; i++) { 820 for (size_t i = 0; i < num_items; i++) {
813 if (0 != cxListAdd(list, ((const unsigned char *) array) + i * item_size)) { 821 if (0 != cxListAdd(list, ((const unsigned char *) array) + i * item_size)) {
814 cxLinkedListDestroy(list); 822 return cx_ll_default_destructor(list);
815 return NULL;
816 } 823 }
817 } 824 }
818 return list; 825 return list;
819 } 826 }
820
821 void cxLinkedListDestroy(CxList *list) {
822 cx_linked_list *ll = (cx_linked_list *) list;
823
824 cx_linked_list_node *node = ll->begin;
825 while (node) {
826 void *next = node->next;
827 cxFree(list->allocator, node);
828 node = next;
829 }
830
831 cxFree(list->allocator, list);
832 }

mercurial