Skip to content

VecInsertR

Description

Insert a single element at the given index, preserving order of trailing elements. R-value form: the source is treated as a temporary value and is never zeroed.

Parameters

Name Direction Description
v in,out Vector handle.
rval in Value to insert. Must be convertible to the vector’s element type.
idx in Position in [0, length].

Usage example (from documentation)

  if (!VecInsertR(&v, 42, 0)) { /* recover */ }

Success

Returns true. The value of rval is written at idx, the vector length grows by one, and trailing elements have shifted one slot right.

Failure

Returns false on allocation failure. The vector is unchanged.

Usage example (Cross-references)

Usage examples (Cross-references)
    
        // Insert item2 at index 0
        VecInsertR(&vec, item2, 0);
    
        // Insert item3 in the middle
    
        // Insert item3 in the middle
        VecInsertR(&vec, item3, 1);
    
        // Check vector length
    
        // Test inserting at end index
        VecInsertR(&vec, 20, VecLen(&vec));
        result = result && (VecLen(&vec) == 2 && VecAt(&vec, 1) == 20);
    
        // Insert at index 0 (empty vector)
        VecInsertR(&vec, 10, 0);
    
        // Check first element
    
        // Insert at the end
        VecInsertR(&vec, 30, 1);
    
        // Check elements
    
        // Insert in the middle
        VecInsertR(&vec, 20, 1);
    
        // Check all elements
    
        // Test R-value insert at index
        VecInsertR(&vec, LVAL(50), 1);
    
        // Check that the element was inserted
    /// TAGS: Vec, PushBack, RValue
    ///
    #define VecPushBackR(v, val) VecInsertR((v), (val), (v)->length)
    
    ///
    /// TAGS: Vec, PushFront, RValue
    ///
    #define VecPushFrontR(v, val) VecInsertR((v), (val), 0)
    
    ///
    #define VecMustInsertR(v, rval, idx)                                                                                   \
        do {                                                                                                               \
            if (!VecInsertR((v), (rval), (idx))) {                                                                         \
                LOG_FATAL("VecMustInsertR failed");                                                                        \
            }                                                                                                              \
    ///
    #define StrInsertL(str, lval, idx) VecInsertL((str), (lval), (idx))
    #define StrInsertR(str, rval, idx) VecInsertR((str), (rval), (idx))
    #define StrInsert(str, val, idx)   StrInsertL((str), (val), (idx))
Last updated on