VecInsertL
- Macro
- August 22, 2025
Table of Contents
VecInsertL
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
Name | Direction | Description |
---|---|---|
v | in,out | Vector to insert item into |
lval | in | l-value to be inserted |
idx | in | Index 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)
- In
Insert.h:100
:
/// FAILURE : Does not return
///
#define VecInsert(v, lval, idx) VecInsertL((v), (lval), (idx))
///
- In
Insert.h:579
:
/// FAILURE : Does not return
///
#define VecPushBackL(v, val) VecInsertL((v), (val), (v)->length)
///
- In
Insert.h:616
:
/// FAILURE : Does not return
///
#define VecPushFrontL(v, val) VecInsertL((v), (val), 0)
///
- In
Vec.Insert.c:339
:
// Test L-value insert at index
int insert_value = 75;
VecInsertL(&vec, insert_value, 2);
// Check that the element was inserted
- In
Vec.Insert.c:410
:
// 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