src/linked_list.c

changeset 629
6c81ee4f11ad
parent 628
1e2be40f0cb5
child 630
ac5e7f789048
equal deleted inserted replaced
628:1e2be40f0cb5 629:6c81ee4f11ad
538 void const *elem 538 void const *elem
539 ) { 539 ) {
540 return cx_ll_insert(list, list->size, elem); 540 return cx_ll_insert(list, list->size, elem);
541 } 541 }
542 542
543 static size_t cx_ll_add_array(
544 struct cx_list_s *list,
545 void const *array,
546 size_t n
547 ) {
548 // TODO: redirect to cx_ll_insert_array
549 cx_for_n (i, n) {
550 if (cx_ll_add(list, ((char const *) array) + i * list->itemsize)) {
551 return i;
552 }
553 }
554 return n;
555 }
556
543 static int cx_pll_insert( 557 static int cx_pll_insert(
544 struct cx_list_s *list, 558 struct cx_list_s *list,
545 size_t index, 559 size_t index,
546 void const *elem 560 void const *elem
547 ) { 561 ) {
725 } 739 }
726 740
727 static cx_list_class cx_linked_list_class = { 741 static cx_list_class cx_linked_list_class = {
728 cx_ll_destructor, 742 cx_ll_destructor,
729 cx_ll_add, 743 cx_ll_add,
744 cx_ll_add_array,
730 cx_ll_insert, 745 cx_ll_insert,
731 cx_ll_insert_iter, 746 cx_ll_insert_iter,
732 cx_ll_remove, 747 cx_ll_remove,
733 cx_ll_at, 748 cx_ll_at,
734 cx_ll_find, 749 cx_ll_find,
739 }; 754 };
740 755
741 static cx_list_class cx_pointer_linked_list_class = { 756 static cx_list_class cx_pointer_linked_list_class = {
742 cx_ll_destructor, 757 cx_ll_destructor,
743 cx_pll_add, 758 cx_pll_add,
759 cx_ll_add_array,
744 cx_pll_insert, 760 cx_pll_insert,
745 cx_pll_insert_iter, 761 cx_pll_insert_iter,
746 cx_ll_remove, 762 cx_ll_remove,
747 cx_pll_at, 763 cx_pll_at,
748 cx_ll_find, 764 cx_ll_find,
784 list->base.itemsize = sizeof(void *); 800 list->base.itemsize = sizeof(void *);
785 list->base.capacity = SIZE_MAX; 801 list->base.capacity = SIZE_MAX;
786 802
787 return (CxList *) list; 803 return (CxList *) list;
788 } 804 }
789
790 CxList *cxLinkedListFromArray(
791 CxAllocator const *allocator,
792 CxListComparator comparator,
793 size_t item_size,
794 size_t num_items,
795 void const *array
796 ) {
797 CxList *list = cxLinkedListCreate(allocator, comparator, item_size);
798 if (list == NULL) return NULL;
799 cx_for_n (i, num_items) {
800 if (0 != cxListAdd(list, ((const unsigned char *) array) + i * item_size)) {
801 cx_ll_destructor(list);
802 cxFree(allocator, list);
803 return NULL;
804 }
805 }
806 return list;
807 }

mercurial