VecInsertR

Table of Contents

VecInsertR

Description

Insert an r-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. In worst case this would to to O(n)

Parameters

NameDirectionDescription
vin,outVector to insert item into
rvalinr-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
  VecInsert(&integers, &x, 0); // x inserted at position 0
  VecInsert(&integers, &y, 0); // x shifted one position and y is inserted
  VecInsert(&integers, ((int[]){5}), 1); // x shifted one position and 5 is inserted at index 1

Success

return

Failure

Does not return

Usage example (Cross-references)

    /// FAILURE : Returns `NULL` otherwise.
    ///
    #define StrInsertCharAt(str, chr, idx) VecInsertR((str), (chr), (idx))
    
    ///
    /// FAILURE : Does not return
    ///
    #define VecPushBackR(v, val) VecInsertR((v), (val), (v)->length)
    
    ///
    /// FAILURE : Does not return
    ///
    #define VecPushFrontR(v, val) VecInsertR((v), (val), 0)
    
    ///
    
    // 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
    
    // 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, vec.length);
    result = result && (vec.length == 2 && VecAt(&vec, 1) == 20);

Share :