Skip to content

VecInsertL

Description

Insert a single element at the given index, preserving order of trailing elements. L-value form: takes ownership of lval on success when the vector has no copy_init handler configured (source is zeroed). On failure the source is left untouched.

Parameters

Name Direction Description
v in,out Vector handle.
lval in Addressable element to insert. Must match the vector’s element type.
idx in Position in [0, length]. Existing elements at and after this index shift one slot to the right.

Usage example (from documentation)

  typedef Vec(int) IntVec;
  IntVec v = VecInit();
  int x = 42;
  if (!VecInsertL(&v, x, 0)) { /* recover */ }

Success

Returns true. The element value of lval is written at idx, the vector length grows by one, and trailing elements have shifted one slot right. When the vector has no copy_init handler, lval has been zeroed (moved-from); otherwise lval is unchanged.

Failure

Returns false on allocation failure. The vector and lval are both unchanged; the caller may retry or propagate the failure.

Usage example (Cross-references)

Usage examples (Cross-references)
        // Test VecInsertL
        int val3 = 30;
        VecInsertL(&vec, val3, 1);
    
        // Check vector length
    // Test VecInsertL zero-on-take behavior with complex structures
    bool test_lvalue_zero_on_take_insert(void) {
        WriteFmt("Testing VecInsertL zero-on-take with complex structures\n");
    
        // Create a test item
    
        // Now insert our test item at position 0 using L-value semantics
        VecInsertL(&vec, item, 0);
    
        // Check that the item was zeroed
        // Test L-value insert at index
        int insert_value = 75;
        VecInsertL(&vec, insert_value, 2);
    
        // Check that the element was inserted
        // Test VecInsertL
        int val3 = 30;
        VecInsertL(&vec, val3, 1);
        result = result && (val3 == 0); // Should be zeroed
    /// TAGS: Vec, Insert, LValue
    ///
    #define VecInsert(v, lval, idx) VecInsertL((v), (lval), (idx))
    
    ///
    /// TAGS: Vec, PushBack, LValue
    ///
    #define VecPushBackL(v, val) VecInsertL((v), (val), (v)->length)
    
    ///
    /// TAGS: Vec, PushFront, LValue
    ///
    #define VecPushFrontL(v, val) VecInsertL((v), (val), 0)
    
    ///
    #define VecMustInsertL(v, lval, idx)                                                                                   \
        do {                                                                                                               \
            if (!VecInsertL((v), (lval), (idx))) {                                                                         \
                LOG_FATAL("VecMustInsertL failed");                                                                        \
            }                                                                                                              \
    /// TAGS: Str, Insert, Char
    ///
    #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