Skip to content

GENERIC_LIST

Description

Cast any list to a generic list

Usage example (Cross-references)

Usage examples (Cross-references)
        bool result =
            (a == 0) && (b == 20) && (c == 0) && (d == 0) && (e == 50) && (f == 0) && (g == 0) && (h == 80) && (i == 0);
        result = result && list_matches(GENERIC_LIST(&list), (const int[]) {60, 80, 40, 10, 30, 20, 50, 90, 70}, 9);
    
        ListDeinit(&list);
        ListPushArrL(&list, arr, 3);
    
        bool result = list_matches(GENERIC_LIST(&list), (const int[]) {1, 2, 3}, 3);
        result      = result && (arr[0] == 0) && (arr[1] == 0) && (arr[2] == 0);
        ListPushArrL(&list, arr, 0);
    
        bool result = list_matches(GENERIC_LIST(&list), (const int[]) {1}, 1);
        result      = result && (arr[0] == 4) && (arr[1] == 5) && (arr[2] == 6);
        result      = result && (x == 7);
        result      = result && (arr[0] == 8) && (arr[1] == 9);
        result      = result && list_matches(GENERIC_LIST(&list), (const int[]) {1007, 1007, 1008, 1009}, 4);
    
        ListDeinit(&list);
    
        bool result = (g_copy_init_count == 2);
        result      = result && list_matches(GENERIC_LIST(&dest), (const int[]) {1003, 1004}, 2);
        result      = result && (ListLen(&src) == 0) && (ListHead(&src) == NULL) && (ListTail(&src) == NULL);
        result      = result && (ListCopyInit(&src) == tracked_copy_init) && (ListCopyDeinit(&src) == tracked_copy_deinit);
        ListPushBackR(&src, 5);
        result = result && (g_copy_init_count == 3);
        result = result && list_matches(GENERIC_LIST(&src), (const int[]) {1005}, 1);
    
        ListDeinit(&dest);
        ListMerge(&dest_a, &src_a);
    
        bool result = list_matches(GENERIC_LIST(&dest_l), (const int[]) {1, 2, 3, 4}, 4);
        result      = result && (ListLen(&src_l) == 0) && (ListHead(&src_l) == NULL) && (ListTail(&src_l) == NULL);
        result      = result && list_matches(GENERIC_LIST(&dest_r), (const int[]) {1, 2, 3, 4}, 4);
        bool result = list_matches(GENERIC_LIST(&dest_l), (const int[]) {1, 2, 3, 4}, 4);
        result      = result && (ListLen(&src_l) == 0) && (ListHead(&src_l) == NULL) && (ListTail(&src_l) == NULL);
        result      = result && list_matches(GENERIC_LIST(&dest_r), (const int[]) {1, 2, 3, 4}, 4);
        result      = result && list_matches(GENERIC_LIST(&src_r), (const int[]) {3, 4}, 2);
        result      = result && list_matches(GENERIC_LIST(&dest_a), (const int[]) {5, 6}, 2);
        result      = result && (ListLen(&src_l) == 0) && (ListHead(&src_l) == NULL) && (ListTail(&src_l) == NULL);
        result      = result && list_matches(GENERIC_LIST(&dest_r), (const int[]) {1, 2, 3, 4}, 4);
        result      = result && list_matches(GENERIC_LIST(&src_r), (const int[]) {3, 4}, 2);
        result      = result && list_matches(GENERIC_LIST(&dest_a), (const int[]) {5, 6}, 2);
        result      = result && (ListLen(&src_a) == 0) && (ListHead(&src_a) == NULL) && (ListTail(&src_a) == NULL);
        result      = result && list_matches(GENERIC_LIST(&dest_r), (const int[]) {1, 2, 3, 4}, 4);
        result      = result && list_matches(GENERIC_LIST(&src_r), (const int[]) {3, 4}, 2);
        result      = result && list_matches(GENERIC_LIST(&dest_a), (const int[]) {5, 6}, 2);
        result      = result && (ListLen(&src_a) == 0) && (ListHead(&src_a) == NULL) && (ListTail(&src_a) == NULL);
        bool result = (g_copy_init_count == 2);
        result      = result && (g_copy_deinit_count == 0);
        result      = result && list_matches(GENERIC_LIST(&deep_dest), (const int[]) {1011, 1012}, 2);
        result      = result && list_matches(GENERIC_LIST(&shallow_src), (const int[]) {11, 12}, 2);
        result      = result && (ListLen(&empty_dest) == 0) && (ListLen(&empty_src) == 0);
        result      = result && (g_copy_deinit_count == 0);
        result      = result && list_matches(GENERIC_LIST(&deep_dest), (const int[]) {1011, 1012}, 2);
        result      = result && list_matches(GENERIC_LIST(&shallow_src), (const int[]) {11, 12}, 2);
        result      = result && (ListLen(&empty_dest) == 0) && (ListLen(&empty_src) == 0);
        result      = result && (reverse_values[0] == 51) && (reverse_values[1] == 41) && (reverse_values[2] == 31) &&
                 (reverse_values[3] == 21) && (reverse_values[4] == 11);
        result = result && list_matches(GENERIC_LIST(&list), (const int[]) {10, 20, 30, 40, 50}, 5);
    
        ListDeinit(&list);
        bool result = (forward_range_sum == 90);
        result      = result && (reverse_range_sum == 180);
        result      = result && list_matches(GENERIC_LIST(&list), (const int[]) {10, 20, 30, 40, 50}, 5);
    
        ListDeinit(&list);
    
        bool result = (count == 0);
        result      = result && list_matches(GENERIC_LIST(&list), (const int[]) {10, 20, 30, 40, 50}, 5);
    
        ListDeinit(&list);
        result      = result && (reverse_values[0] == 54) && (reverse_values[1] == 43) && (reverse_values[2] == 32) &&
                 (reverse_values[3] == 21) && (reverse_values[4] == 10);
        result = result && list_matches(GENERIC_LIST(&list), (const int[]) {10, 20, 30, 40, 50}, 5);
    
        ListDeinit(&list);
        result      = result && (forward_values[0] == 10) && (forward_values[1] == 30) && (forward_values[2] == 50);
        result      = result && (reverse_values[0] == 54) && (reverse_values[1] == 32) && (reverse_values[2] == 10);
        result      = result && list_matches(GENERIC_LIST(&list), (const int[]) {10, 20, 30, 40, 50}, 5);
    
        ListDeinit(&list);
    
        ListPushBackR(&list, 9);
        result = result && list_matches(GENERIC_LIST(&list), (const int[]) {9}, 1);
    
        ListDeinit(&list);
    
        ListSort(&list, compare_ints);
        bool result = list_matches(GENERIC_LIST(&list), (const int[]) {1, 2, 2, 3, 4}, 5);
    
        ListReverse(&list);
    
        ListReverse(&list);
        result = result && list_matches(GENERIC_LIST(&list), (const int[]) {4, 3, 2, 2, 1}, 5);
    
        ListDeinit(&list);
    
        bool result = (ListLen(&empty) == 0) && (ListHead(&empty) == NULL) && (ListTail(&empty) == NULL);
        result      = result && list_matches(GENERIC_LIST(&singleton), (const int[]) {42}, 1);
    
        ListDeinit(&empty);
        ListPushBackR(&list, 9);
        result = result && (g_copy_init_count == 3);
        result = result && list_matches(GENERIC_LIST(&list), (const int[]) {9}, 1);
    
        ListDeinit(&list);
    
        ListPushBackR(&list, 30);
        result = result && list_matches(GENERIC_LIST(&list), (const int[]) {30}, 1);
    
        ListDeinit(&list);
    
        ListSwapItems(&list, 1, 3);
        result = result && list_matches(GENERIC_LIST(&list), (const int[]) {10, 40, 30, 20}, 4);
        ListSwapItems(&list, 2, 2);
        result = result && list_matches(GENERIC_LIST(&list), (const int[]) {10, 40, 30, 20}, 4);
        result = result && list_matches(GENERIC_LIST(&list), (const int[]) {10, 40, 30, 20}, 4);
        ListSwapItems(&list, 2, 2);
        result = result && list_matches(GENERIC_LIST(&list), (const int[]) {10, 40, 30, 20}, 4);
    
        ListDeinit(&list);
        ListRemove(&list, &removed, 1);
        bool result = (removed == 20);
        result      = result && list_matches(GENERIC_LIST(&list), (const int[]) {10, 30, 40}, 3);
    
        ListPopFront(&list, &removed);
        ListPopFront(&list, &removed);
        result = result && (removed == 10);
        result = result && list_matches(GENERIC_LIST(&list), (const int[]) {30, 40}, 2);
    
        ListPopBack(&list, &removed);
        ListPopBack(&list, &removed);
        result = result && (removed == 40);
        result = result && list_matches(GENERIC_LIST(&list), (const int[]) {30}, 1);
        result = result && ListHead(&list) && ListNodeData(ListHead(&list)) && (*ListNodeData(ListHead(&list)) == 30);
        result = result && ListTail(&list) && ListNodeData(ListTail(&list)) && (*ListNodeData(ListTail(&list)) == 30);
        ListRemoveRange(&list, removed, 2, 2);
        bool result = (removed[0] == 3) && (removed[1] == 4);
        result      = result && list_matches(GENERIC_LIST(&list), (const int[]) {1, 2, 5, 6}, 4);
    
        ListDelete(&list, 1);
    
        ListDelete(&list, 1);
        result = result && list_matches(GENERIC_LIST(&list), (const int[]) {1, 5, 6}, 3);
    
        ListDeleteLast(&list);
    
        ListDeleteLast(&list);
        result = result && list_matches(GENERIC_LIST(&list), (const int[]) {1, 5}, 2);
    
        ListDeleteRange(&list, 0, 2);
        ListRemoveRange(&list, prefix, 0, 2);
        bool result = (prefix[0] == 1) && (prefix[1] == 2);
        result      = result && list_matches(GENERIC_LIST(&list), (const int[]) {3, 4, 5, 6}, 4);
    
        ListRemoveRange(&list, suffix, 2, 2);
        ListRemoveRange(&list, suffix, 2, 2);
        result = result && (suffix[0] == 5) && (suffix[1] == 6);
        result = result && list_matches(GENERIC_LIST(&list), (const int[]) {3, 4}, 2);
        result = result && ListHead(&list) && ListNodeData(ListHead(&list)) && (*ListNodeData(ListHead(&list)) == 3);
        result = result && ListTail(&list) && ListNodeData(ListTail(&list)) && (*ListNodeData(ListTail(&list)) == 4);
        bool result = (g_copy_init_count == 3);
        result      = result && (g_copy_deinit_count == 0);
        result      = result && list_matches(GENERIC_LIST(&list), (const int[]) {7, 8, 9}, 3);
    
        ListRemove(&list, &removed, 0);
        result = result && (removed == 7);
        result = result && (g_copy_deinit_count == 0);
        result = result && list_matches(GENERIC_LIST(&list), (const int[]) {8, 9}, 2);
    
        ListDelete(&list, 0);
        ListDelete(&list, 0);
        result = result && (g_copy_deinit_count == 1);
        result = result && list_matches(GENERIC_LIST(&list), (const int[]) {9}, 1);
    
        ListClear(&list);
        result      = result && (g_copy_deinit_count == 0);
        result      = result && (removed[0] == 5) && (removed[1] == 6);
        result      = result && list_matches(GENERIC_LIST(&list), (const int[]) {4}, 1);
    
        ListDeinit(&list);
        GenericListNode node = {0};
        List(int) list       = ListInit(get_test_alloc());
        GenericList *g       = GENERIC_LIST(&list);
    
        // intentional bypass: ListHead/ListTail/ListLen are read-only; plant
        GenericListNode node  = {.next = NULL, .prev = NULL, .data = &value};
        List(int) list        = ListInit(get_test_alloc());
        GenericList *g        = GENERIC_LIST(&list);
    
        // intentional bypass: ListHead/ListTail/ListLen are read-only; plant
        GenericListNode node  = {.next = NULL, .prev = (GenericListNode *)1, .data = &value};
        List(int) list        = ListInit(get_test_alloc());
        GenericList *g        = GENERIC_LIST(&list);
    
        // intentional bypass: ListHead/ListTail/ListLen are read-only; plant a
        GenericListNode node  = {.next = (GenericListNode *)1, .prev = NULL, .data = &value};
        List(int) list        = ListInit(get_test_alloc());
        GenericList *g        = GENERIC_LIST(&list);
    
        // intentional bypass: ListHead/ListTail/ListLen are read-only; plant a
    /// TAGS: List, Deinit, Lifecycle
    ///
    #define ListDeinit(v) deinit_list(GENERIC_LIST(v), sizeof(LIST_DATA_TYPE(v)))
    
    #endif // MISRA_STD_CONTAINER_LIST_INIT_H
    /// TAGS: List, Ops, Clear
    ///
    #define ListClear(l) clear_list(GENERIC_LIST(l), sizeof(LIST_DATA_TYPE(l)))
    
    ///
    /// TAGS: List, Ops, Sort
    ///
    #define ListSort(l, compare) list_sort(GENERIC_LIST(l), sizeof(LIST_DATA_TYPE(l)), (compare))
    
    ///
    /// TAGS: List, Ops, Reverse
    ///
    #define ListReverse(l) reverse_list(GENERIC_LIST(l), sizeof(LIST_DATA_TYPE(l)))
    
    #endif // MISRA_STD_CONTAINER_LIST_OPS_H
    /// TAGS: List, Access, Swap
    ///
    #define ListSwapItems(l, idx1, idx2) swap_list(GENERIC_LIST(l), sizeof(LIST_DATA_TYPE(l)), (idx1), (idx2))
    
    ///
    /// TAGS: List, Access, API
    ///
    #define ListPtrAt(l, idx) ((LIST_DATA_TYPE(l) *)item_ptr_at_list(GENERIC_LIST(l), sizeof(LIST_DATA_TYPE(l)), (idx)))
    
    #ifdef __cplusplus
    /// TAGS: List, Find, Search, Compare
    ///
    #define ListFind(l, item_ptr, compare) find_idx_list(GENERIC_LIST(l), (item_ptr), sizeof(LIST_DATA_TYPE(l)), (compare))
    
    ///
    /// TAGS: List, Node, Access
    ///
    #define ListNodePtrAt(l, idx) ((LIST_NODE_TYPE(l) *)(node_at_list(GENERIC_LIST(l), sizeof(LIST_DATA_TYPE(l)), (idx))))
    
    ///
    /// TAGS: List, Node, Access
    ///
    #define ListNodeAt(l, idx) (*((LIST_NODE_TYPE(l) *)(node_at_list(GENERIC_LIST(l), sizeof(LIST_DATA_TYPE(l)), (idx)))))
    
    ///
    /// TAGS: List, Remove
    ///
    #define ListRemove(l, val, idx) remove_range_list(GENERIC_LIST(l), (val), sizeof(LIST_DATA_TYPE(l)), (idx), 1)
    
    ///
    ///
    #define ListRemoveRange(l, rd, start, count)                                                                           \
        remove_range_list(GENERIC_LIST(l), (rd), sizeof(LIST_DATA_TYPE(l)), (start), (count))
    
    ///
                             idx = ((idx != UNPL(resolved_idx)) ? idx : (UNPL(resolved_idx) + 1)))                          \
                        if ((UNPL(node) = get_node_for_list_iteration(                                                       \
                                 GENERIC_LIST(UNPL(pl)), UNPL(cursor), UNPL(cursor_idx), idx)) &&                           \
                            ((UNPL(resolved_idx) = idx), 1) && UNPL(node)->data)                                             \
                            for (bool UNPL(_once) = true; UNPL(_once); UNPL(_once) = false)                                  \
                             idx = ((idx != UNPL(resolved_idx)) ? idx : (UNPL(resolved_idx) + 1)))                          \
                        if ((UNPL(node) = get_node_for_list_iteration(                                                       \
                                 GENERIC_LIST(UNPL(pl)), UNPL(cursor), UNPL(cursor_idx), idx)) &&                           \
                            ((UNPL(resolved_idx) = idx), 1) && UNPL(node)->data)                                             \
                            for (bool UNPL(_once) = true; UNPL(_once); UNPL(_once) = false)                                  \
                                                                                            : UNPL(pl)->length)))            \
                        if ((UNPL(node) = get_node_for_list_iteration(                                                       \
                                 GENERIC_LIST(UNPL(pl)), UNPL(cursor), UNPL(cursor_idx), idx)) &&                           \
                            ((UNPL(resolved_idx) = idx), 1) && UNPL(node)->data)                                             \
                            for (bool UNPL(_once) = true; UNPL(_once); UNPL(_once) = false)                                  \
                                                                                            : UNPL(pl)->length)))            \
                        if ((UNPL(node) = get_node_for_list_iteration(                                                       \
                                 GENERIC_LIST(UNPL(pl)), UNPL(cursor), UNPL(cursor_idx), idx)) &&                           \
                            ((UNPL(resolved_idx) = idx), 1) && UNPL(node)->data)                                             \
                            for (bool UNPL(_once) = true; UNPL(_once); UNPL(_once) = false)                                  \
        (ValidateList(l),                                                                                                  \
         CHECK_TYPE_EQUIVALENCE(TYPE_OF(lval), LIST_DATA_TYPE(l)),                                                         \
         list_insert_one_l(GENERIC_LIST(l), &LVAL_AS(LIST_DATA_TYPE(l), lval), &(lval), sizeof(LIST_DATA_TYPE(l)), (idx)))
    
    ///
        (ValidateList(l),                                                                                                  \
         CHECK_TYPE_CONVERTIBLE(LIST_DATA_TYPE(l), rval),                                                                  \
         list_insert_one_r(GENERIC_LIST(l), &LVAL_AS(LIST_DATA_TYPE(l), rval), sizeof(LIST_DATA_TYPE(l)), (idx)))
    
    ///
        (ValidateList(l),                                                                                                  \
         CHECK_TYPE_EQUIVALENCE(TYPE_OF(*(arr)), LIST_DATA_TYPE(l)),                                                       \
         list_insert_range_l(GENERIC_LIST(l), (void *)(arr), sizeof(LIST_DATA_TYPE(l)), (count)))
    
    ///
        (ValidateList(l),                                                                                                  \
         CHECK_TYPE_CONVERTIBLE(const LIST_DATA_TYPE(l) *, arr),                                                           \
         list_insert_range_r(GENERIC_LIST(l), (const void *)(arr), sizeof(LIST_DATA_TYPE(l)), (count)))
    
    ///
         ValidateList(l2),                                                                                                 \
         CHECK_TYPE_EQUIVALENCE(LIST_DATA_TYPE(l2), LIST_DATA_TYPE(l)),                                                    \
         list_merge_l(GENERIC_LIST(l), GENERIC_LIST(l2), sizeof(LIST_DATA_TYPE(l))))
    
    ///
         ValidateList(l2),                                                                                                 \
         CHECK_TYPE_EQUIVALENCE(LIST_DATA_TYPE(l2), LIST_DATA_TYPE(l)),                                                    \
         list_merge_r(GENERIC_LIST(l), GENERIC_LIST(l2), sizeof(LIST_DATA_TYPE(l))))
    
    ///
    /// TAGS: List, Validate, API
    ///
    #define ValidateList(l) validate_list((const GenericList *)GENERIC_LIST(l))
    
    #endif // MISRA_STD_CONTAINER_LIST_TYPE_H
Last updated on