25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
26 * POSSIBILITY OF SUCH DAMAGE. |
26 * POSSIBILITY OF SUCH DAMAGE. |
27 */ |
27 */ |
28 |
28 |
29 #include "cx/linked_list.h" |
29 #include "cx/linked_list.h" |
30 #include "cx/utils.h" |
|
31 #include "cx/compare.h" |
30 #include "cx/compare.h" |
32 #include <string.h> |
31 #include <string.h> |
33 #include <assert.h> |
32 #include <assert.h> |
34 |
33 |
35 // LOW LEVEL LINKED LIST FUNCTIONS |
34 // LOW LEVEL LINKED LIST FUNCTIONS |
445 n++; |
444 n++; |
446 } |
445 } |
447 |
446 |
448 // Update pointer |
447 // Update pointer |
449 if (loc_prev >= 0) ll_prev(sorted[0]) = NULL; |
448 if (loc_prev >= 0) ll_prev(sorted[0]) = NULL; |
450 cx_for_n (i, length - 1) { |
449 for (size_t i = 0 ; i < length - 1; i++) { |
451 cx_linked_list_link(sorted[i], sorted[i + 1], loc_prev, loc_next); |
450 cx_linked_list_link(sorted[i], sorted[i + 1], loc_prev, loc_next); |
452 } |
451 } |
453 ll_next(sorted[length - 1]) = NULL; |
452 ll_next(sorted[length - 1]) = NULL; |
454 |
453 |
455 *begin = sorted[0]; |
454 *begin = sorted[0]; |
770 list->collection.size -= removed; |
769 list->collection.size -= removed; |
771 |
770 |
772 // copy or destroy the removed chain |
771 // copy or destroy the removed chain |
773 if (targetbuf == NULL) { |
772 if (targetbuf == NULL) { |
774 cx_linked_list_node *n = node; |
773 cx_linked_list_node *n = node; |
775 cx_for_n(i, removed) { |
774 for (size_t i = 0; i < removed; i++) { |
776 // element destruction |
775 // element destruction |
777 cx_invoke_destructor(list, n->payload); |
776 cx_invoke_destructor(list, n->payload); |
778 |
777 |
779 // free the node and advance |
778 // free the node and advance |
780 void *next = n->next; |
779 void *next = n->next; |
782 n = next; |
781 n = next; |
783 } |
782 } |
784 } else { |
783 } else { |
785 char *dest = targetbuf; |
784 char *dest = targetbuf; |
786 cx_linked_list_node *n = node; |
785 cx_linked_list_node *n = node; |
787 cx_for_n(i, removed) { |
786 for (size_t i = 0; i < removed; i++) { |
788 // copy payload |
787 // copy payload |
789 memcpy(dest, n->payload, list->collection.elem_size); |
788 memcpy(dest, n->payload, list->collection.elem_size); |
790 |
789 |
791 // advance target buffer |
790 // advance target buffer |
792 dest += list->collection.elem_size; |
791 dest += list->collection.elem_size; |