Skip to content

VecSwapItems

Description

Swap the elements at two given indices in place.

Parameters

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

Success

Returns to the caller. The values at idx1 and idx2 have been exchanged byte-for-byte. The vector length and all other elements are unchanged.

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)
                    size_t i = extract_u32(data, offset, data_size) % VecLen(vec);
                    size_t j = extract_u32(data, offset, data_size) % VecLen(vec);
                    VecSwapItems(vec, i, j);
                }
                break;
                    size_t i = extract_u32(data, offset, data_size) % VecLen(vec);
                    size_t j = extract_u32(data, offset, data_size) % VecLen(vec);
                    VecSwapItems(vec, i, j);
                }
                break;
                uint64_t len = VecLen(vec);
                if (len > 1 && idx1 < len && idx2 < len) {
                    VecSwapItems(vec, idx1, idx2);
                }
                break;
    
    bool test_vec_swap_items(void) {
        WriteFmt("Testing VecSwapItems\n");
    
        // Create a vector of integers
    
        // Swap first and last elements
        VecSwapItems(&vec, 0, 4);
    
        // Check that the elements were swapped
    
        // Swap two elements in the middle
        VecSwapItems(&vec, 1, 3);
    
        // Check that the elements were swapped
    /// TAGS: Str, Memory, Swap
    ///
    #define StrSwapCharAt(str, idx1, idx2) VecSwapItems((str), (idx1), (idx2))
    
    ///
Last updated on