src/linked_list.c

changeset 1065
6eb7b54975ee
parent 962
cd418898af5c
equal deleted inserted replaced
1064:f3b04cd60776 1065:6eb7b54975ee
654 654
655 // find position efficiently 655 // find position efficiently
656 cx_linked_list_node *node = index == 0 ? NULL : cx_ll_node_at((cx_linked_list *) list, index - 1); 656 cx_linked_list_node *node = index == 0 ? NULL : cx_ll_node_at((cx_linked_list *) list, index - 1);
657 657
658 // perform first insert 658 // perform first insert
659 if (0 != cx_ll_insert_at(list, node, array)) { 659 if (0 != cx_ll_insert_at(list, node, array)) return 1;
660 return 1;
661 }
662 660
663 // is there more? 661 // is there more?
664 if (n == 1) return 1; 662 if (n == 1) return 1;
665 663
666 // we now know exactly where we are 664 // we now know exactly where we are
668 666
669 // we can add the remaining nodes and immediately advance to the inserted node 667 // we can add the remaining nodes and immediately advance to the inserted node
670 const char *source = array; 668 const char *source = array;
671 for (size_t i = 1; i < n; i++) { 669 for (size_t i = 1; i < n; i++) {
672 source += list->collection.elem_size; 670 source += list->collection.elem_size;
673 if (0 != cx_ll_insert_at(list, node, source)) { 671 if (0 != cx_ll_insert_at(list, node, source)) return i;
674 return i;
675 }
676 node = node->next; 672 node = node->next;
677 } 673 }
678 return n; 674 return n;
679 } 675 }
680 676

mercurial