Skip to content

ListInsertL

Description

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

Parameters

Name Direction Description
l in,out List handle.
lval in Addressable element to insert. Must match the list’s element type.
idx in Position in [0, length].

Success

Returns true. A new node holding lval’s payload is linked at position idx; the list length grows by one. When the list has no copy_init handler, lval has been zeroed (moved-from); otherwise lval is unchanged.

Failure

Returns false on allocation failure (either the node header or the payload buffer). The list and lval are both unchanged.

Usage example (Cross-references)

Usage examples (Cross-references)
        int     i    = 90;
    
        ListInsertL(&list, a, 0);
        ListInsertR(&list, b, 1);
        ListInsert(&list, c, 1);
        reset_counters();
        ListPushBackR(&list, x);
        ListInsertL(&list, x, 1);
        ListPushArrL(&list, arr, 2);
    /// TAGS: List, Insert, API
    ///
    #define ListInsert(l, lval, idx) ListInsertL((l), (lval), (idx))
    
    ///
    /// TAGS: List, PushFront, LValue
    ///
    #define ListPushFrontL(l, lval) ListInsertL((l), (lval), 0)
    
    ///
    /// TAGS: List, PushBack, LValue
    ///
    #define ListPushBackL(l, lval) ListInsertL((l), (lval), (l)->length)
    
    ///
    #define ListMustInsertL(l, lval, idx)                                                                                  \
        do {                                                                                                               \
            if (!ListInsertL((l), (lval), (idx))) {                                                                        \
                LOG_FATAL("ListMustInsertL failed");                                                                       \
            }                                                                                                              \
Last updated on