VecInsertL

Table of Contents

VecInsertL

Description

Insert an l-value into vector of it’s type. Insertion index must not exceed vector length. This preserves the ordering of elements. Best to be used with sorted vectors, if the sorted property is to be preserved.

Info

If copy_init is set, then vector will create it’s own copy of items.

Note

Ownership of item is transferred to vector if no copy_init method is set. This is to prevent multiple ownership of same object, once inserted into vector. Object won’t be usable after this call if copy_init is not set.

Parameters

NameDirectionDescription
vin,outVector to insert item into
lvalinl-value to be inserted
idxinIndex to insert item at.

Usage example (from documentation)

  // the data
  int x = 10;
  int y = 20;

  // vector
  Vec(int) integers = VecInit();

  // insert items
  VecInsertL(&integers, &x, 0); // x inserted at position 0
  VecInsertL(&integers, &y, 0); // x shifted one position and y is inserted
  VecInsertL(&integers, LVAL(101), 1); // x shifted one position and 101 is inserted at index 1

Success

return

Failure

Does not return

Usage example (Cross-references)

    /// FAILURE : Does not return
    ///
    #define VecInsert(v, lval, idx) VecInsertL((v), (lval), (idx))
    
    ///
    /// FAILURE : Does not return
    ///
    #define VecPushBackL(v, val) VecInsertL((v), (val), (v)->length)
    
    ///
    /// FAILURE : Does not return
    ///
    #define VecPushFrontL(v, val) VecInsertL((v), (val), 0)
    
    ///
    // 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 memset to 0
    // Test VecInsertL
    int val3 = 30;
    VecInsertL(&vec, val3, 1);
    
    // Check vector length
    // Test VecInsertL memset behavior with complex structures
    bool test_lvalue_memset_insert(void) {
    printf("Testing VecInsertL memset 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 memset to 0

Share :