Skip to content

ListSwapItems

Description

Swap the payloads of two list nodes in place.

Parameters

Name Direction Description
l in,out List handle.
idx1 in First index in [0, length).
idx2 in Second index in [0, length).

Success

Returns to the caller. The data payloads at idx1 and idx2 have been exchanged byte-for-byte. Node identity and node order are unchanged - only the values they carry move.

Failure

Function cannot fail. Either index being out of range is a caller bug and aborts via LOG_FATAL.

Usage example (Cross-references)

Usage examples (Cross-references)
    
                if (ListLen(list) > 1 && idx1 < ListLen(list) && idx2 < ListLen(list)) {
                    ListSwapItems(list, idx1, idx2);
                }
                break;
        result      = result && (ListLast(&list) == 40);
    
        ListSwapItems(&list, 1, 3);
        result = result && list_matches(GENERIC_LIST(&list), (const int[]) {10, 40, 30, 20}, 4);
        ListSwapItems(&list, 2, 2);
        ListSwapItems(&list, 1, 3);
        result = result && list_matches(GENERIC_LIST(&list), (const int[]) {10, 40, 30, 20}, 4);
        ListSwapItems(&list, 2, 2);
        result = result && list_matches(GENERIC_LIST(&list), (const int[]) {10, 40, 30, 20}, 4);
    
    static bool test_list_swap_items_out_of_range_fails(void) {
        WriteFmt("Testing ListSwapItems out of range\n");
    
        List(int) list = ListInit(get_test_alloc());
        List(int) list = ListInit(get_test_alloc());
        ListPushBackR(&list, 10);
        ListSwapItems(&list, 0, 1);
    
        return false;
Last updated on