Skip to content

ListLen

Description

Number of items in list.

Parameters

Name Direction Description
l in List to query.

Success

Length of list.

Failure

Function cannot fail.

Usage example (Cross-references)

Usage examples (Cross-references)
    
            case LIST_INT_POP_BACK : {
                if (ListLen(list) > 0) {
                    i32 popped;
                    ListPopBack(list, &popped);
    
            case LIST_INT_POP_FRONT : {
                if (ListLen(list) > 0) {
                    i32 popped;
                    ListPopFront(list, &popped);
                i32      value = (i32)extract_u32(data, offset, data_size);
    
                if (idx <= ListLen(list)) {
                    ListInsertR(list, value, idx);
                }
            case LIST_INT_REMOVE : {
                uint16_t idx = extract_u16(data, offset, data_size);
                if (idx < ListLen(list)) {
                    i32 removed;
                    ListRemove(list, &removed, idx);
            case LIST_INT_DELETE : {
                uint16_t idx = extract_u16(data, offset, data_size);
                if (idx < ListLen(list)) {
                    ListDelete(list, idx);
                }
            case LIST_INT_AT : {
                uint16_t idx = extract_u16(data, offset, data_size);
                if (idx < ListLen(list)) {
                    volatile i32 value = ListAt(list, idx);
                    (void)value; // Prevent optimization
    
            case LIST_INT_LEN : {
                volatile uint64_t len = ListLen(list);
                (void)len;
                break;
    
            case LIST_INT_FIRST : {
                if (ListLen(list) > 0) {
                    volatile i32 first = ListFirst(list);
                    (void)first;
    
            case LIST_INT_LAST : {
                if (ListLen(list) > 0) {
                    volatile i32 last = ListLast(list);
                    (void)last;
                uint16_t idx2 = extract_u16(data, offset, data_size);
    
                if (ListLen(list) > 1 && idx1 < ListLen(list) && idx2 < ListLen(list)) {
                    ListSwapItems(list, idx1, idx2);
                }
                count          = count % 16;
    
                if (ListLen(list) > 0 && start < ListLen(list) && count > 0 && start + count <= ListLen(list)) {
                    i32 removed_items[16];
                    ListRemoveRange(list, removed_items, start, count);
                count          = count % 16;
    
                if (ListLen(list) > 0 && start < ListLen(list) && count > 0 && start + count <= ListLen(list)) {
                    ListDeleteRange(list, start, count);
                }
            case LIST_INT_PTR_AT : {
                uint16_t idx = extract_u16(data, offset, data_size);
                if (idx < ListLen(list)) {
                    volatile i32 *ptr = ListPtrAt(list, idx);
                    if (ptr) {
            // Foreach operations
            case LIST_INT_FOREACH : {
                if (ListLen(list) > 0) {
                    int sum = 0;
                    ListForeach(list, item) {
    
            case LIST_INT_FOREACH_IDX : {
                if (ListLen(list) > 0) {
                    int sum = 0;
                    ListForeachIdx(list, item, idx) {
    
            case LIST_INT_FOREACH_PTR : {
                if (ListLen(list) > 0) {
                    int sum = 0;
                    ListForeachPtr(list, item_ptr) {
    
            case LIST_INT_FOREACH_PTR_IDX : {
                if (ListLen(list) > 0) {
                    int sum = 0;
                    ListForeachPtrIdx(list, item_ptr, idx) {
    
            case LIST_INT_FOREACH_REVERSE : {
                if (ListLen(list) > 0) {
                    int sum = 0;
                    ListForeachReverse(list, item) {
    
            case LIST_INT_FOREACH_REVERSE_IDX : {
                if (ListLen(list) > 0) {
                    int sum = 0;
                    ListForeachReverseIdx(list, item, idx) {
    
            case LIST_INT_FOREACH_PTR_REVERSE : {
                if (ListLen(list) > 0) {
                    int sum = 0;
                    ListForeachPtrReverse(list, item_ptr) {
    
            case LIST_INT_FOREACH_PTR_REVERSE_IDX : {
                if (ListLen(list) > 0) {
                    int sum = 0;
                    ListForeachPtrReverseIdx(list, item_ptr, idx) {
    
            case LIST_INT_FOREACH_IN_RANGE : {
                if (ListLen(list) > 0 && *offset + 8 <= data_size) {
                    size_t start = extract_u32(data, offset, data_size) % ListLen(list);
                    size_t end   = extract_u32(data, offset, data_size) % (ListLen(list) + 1);
            case LIST_INT_FOREACH_IN_RANGE : {
                if (ListLen(list) > 0 && *offset + 8 <= data_size) {
                    size_t start = extract_u32(data, offset, data_size) % ListLen(list);
                    size_t end   = extract_u32(data, offset, data_size) % (ListLen(list) + 1);
                    if (start < end) {
                if (ListLen(list) > 0 && *offset + 8 <= data_size) {
                    size_t start = extract_u32(data, offset, data_size) % ListLen(list);
                    size_t end   = extract_u32(data, offset, data_size) % (ListLen(list) + 1);
                    if (start < end) {
                        int sum = 0;
    
            case LIST_INT_FOREACH_PTR_IN_RANGE : {
                if (ListLen(list) > 0 && *offset + 8 <= data_size) {
                    size_t start = extract_u32(data, offset, data_size) % ListLen(list);
                    size_t end   = extract_u32(data, offset, data_size) % (ListLen(list) + 1);
            case LIST_INT_FOREACH_PTR_IN_RANGE : {
                if (ListLen(list) > 0 && *offset + 8 <= data_size) {
                    size_t start = extract_u32(data, offset, data_size) % ListLen(list);
                    size_t end   = extract_u32(data, offset, data_size) % (ListLen(list) + 1);
                    if (start < end) {
                if (ListLen(list) > 0 && *offset + 8 <= data_size) {
                    size_t start = extract_u32(data, offset, data_size) % ListLen(list);
                    size_t end   = extract_u32(data, offset, data_size) % (ListLen(list) + 1);
                    if (start < end) {
                        int sum = 0;
    
            case LIST_INT_FOREACH_REVERSE_IN_RANGE : {
                if (ListLen(list) > 0 && *offset + 8 <= data_size) {
                    size_t start = extract_u32(data, offset, data_size) % ListLen(list);
                    size_t end   = extract_u32(data, offset, data_size) % (ListLen(list) + 1);
            case LIST_INT_FOREACH_REVERSE_IN_RANGE : {
                if (ListLen(list) > 0 && *offset + 8 <= data_size) {
                    size_t start = extract_u32(data, offset, data_size) % ListLen(list);
                    size_t end   = extract_u32(data, offset, data_size) % (ListLen(list) + 1);
                    if (start < end) {
                if (ListLen(list) > 0 && *offset + 8 <= data_size) {
                    size_t start = extract_u32(data, offset, data_size) % ListLen(list);
                    size_t end   = extract_u32(data, offset, data_size) % (ListLen(list) + 1);
                    if (start < end) {
                        int sum = 0;
    
            case LIST_INT_FOREACH_PTR_REVERSE_IN_RANGE : {
                if (ListLen(list) > 0 && *offset + 8 <= data_size) {
                    size_t start = extract_u32(data, offset, data_size) % ListLen(list);
                    size_t end   = extract_u32(data, offset, data_size) % (ListLen(list) + 1);
            case LIST_INT_FOREACH_PTR_REVERSE_IN_RANGE : {
                if (ListLen(list) > 0 && *offset + 8 <= data_size) {
                    size_t start = extract_u32(data, offset, data_size) % ListLen(list);
                    size_t end   = extract_u32(data, offset, data_size) % (ListLen(list) + 1);
                    if (start < end) {
                if (ListLen(list) > 0 && *offset + 8 <= data_size) {
                    size_t start = extract_u32(data, offset, data_size) % ListLen(list);
                    size_t end   = extract_u32(data, offset, data_size) % (ListLen(list) + 1);
                    if (start < end) {
                        int sum = 0;
        ValidateList(&list_d);
    
        bool result = (ListCopyInit(&list_a) == NULL) && (ListCopyDeinit(&list_a) == NULL) && (ListLen(&list_a) == 0);
        result = result && (ListCopyInit(&list_b) == NULL) && (ListCopyDeinit(&list_b) == NULL) && (ListLen(&list_b) == 0);
        result = result && (ListCopyInit(&list_c) == tracked_copy_init) && (ListCopyDeinit(&list_c) == tracked_copy_deinit);
    
        bool result = (ListCopyInit(&list_a) == NULL) && (ListCopyDeinit(&list_a) == NULL) && (ListLen(&list_a) == 0);
        result = result && (ListCopyInit(&list_b) == NULL) && (ListCopyDeinit(&list_b) == NULL) && (ListLen(&list_b) == 0);
        result = result && (ListCopyInit(&list_c) == tracked_copy_init) && (ListCopyDeinit(&list_c) == tracked_copy_deinit);
        result = result && (ListCopyInit(&list_d) == tracked_copy_init) && (ListCopyDeinit(&list_d) == tracked_copy_deinit);
    
        result = result && (g_copy_deinit_count == 2);
        result = result && (ListHead(&list) == NULL) && (ListTail(&list) == NULL) && (ListLen(&list) == 0);
        // Verifying ListDeinit clears the hook fields.
        result = result && (ListCopyInit(&list) == NULL) && (ListCopyDeinit(&list) == NULL);
    
    static bool list_matches(GenericList *list, const int *expected, size count) {
        if (ListLen(list) != count) {
            return false;
        }
        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);
    
        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(&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);
    
        ListDeinit(&dest_l);
        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);
    
        ListDeinit(&deep_dest);
    
    static bool list_matches(GenericList *list, const int *expected, size count) {
        if (ListLen(list) != count) {
            return false;
        }
    
    static bool list_matches(GenericList *list, const int *expected, size count) {
        if (ListLen(list) != count) {
            return false;
        }
        ListClear(&list);
    
        bool result = (ListLen(&list) == 0) && (ListHead(&list) == NULL) && (ListTail(&list) == NULL);
    
        ListPushBackR(&list, 9);
        ListReverse(&singleton);
    
        bool result = (ListLen(&empty) == 0) && (ListHead(&empty) == NULL) && (ListTail(&empty) == NULL);
        result      = result && list_matches(GENERIC_LIST(&singleton), (const int[]) {42}, 1);
        ListClear(&list);
        result = result && (g_copy_deinit_count == 2);
        result = result && (ListLen(&list) == 0) && (ListHead(&list) == NULL) && (ListTail(&list) == NULL);
    
        ListPushBackR(&list, 9);
    
    static bool list_matches(GenericList *list, const int *expected, size count) {
        if (ListLen(list) != count) {
            return false;
        }
    
    static bool test_list_len_empty(void) {
        WriteFmt("Testing ListLen and ListEmpty\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntList list = ListInit(&alloc);
    
        bool result = (ListLen(&list) == 0);
        result      = result && ListEmpty(&list);
        ListPushBackR(&list, 20);
    
        result = result && (ListLen(&list) == 2);
        result = result && !ListEmpty(&list);
    
        ListClear(&list);
        result = result && (ListLen(&list) == 0);
        result = result && ListEmpty(&list);
        result = result && (ListHead(&list) == NULL) && (ListTail(&list) == NULL);
    
    static bool list_matches(GenericList *list, const int *expected, size count) {
        if (ListLen(list) != count) {
            return false;
        }
    
        ListDeleteRange(&list, 0, 2);
        result = result && (ListLen(&list) == 0) && (ListHead(&list) == NULL) && (ListTail(&list) == NULL);
    
        ListDeinit(&list);
    
        bool result = (removed[0] == 7) && (removed[1] == 8) && (removed[2] == 9);
        result      = result && (ListLen(&list) == 0) && (ListHead(&list) == NULL) && (ListTail(&list) == NULL);
    
        ListDeinit(&list);
    
        ListRemoveRange(&list, NULL, 1, 0);
        ListRemoveRange(&list, NULL, ListLen(&list), 0);
        ListRemoveRange(&list, NULL, 9999, 0);
        bool result = (g_copy_init_count == 3);
        ListClear(&list);
        result = result && (g_copy_deinit_count == 2);
        result = result && (ListLen(&list) == 0) && (ListHead(&list) == NULL) && (ListTail(&list) == NULL);
    
        ListDeinit(&list);
        // directly to confirm ListInit planted the right value.
        bool result = (ListHead(&list) == NULL) && (ListTail(&list) == NULL) && (ListCopyInit(&list) == NULL) &&
                      (ListCopyDeinit(&list) == NULL) && (ListLen(&list) == 0) && MAGIC_MATCHES(list.__magic, LIST_MAGIC);
    
        ListDeinit(&list);
    /// TAGS: List, Empty, Query
    ///
    #define ListEmpty(l) (ListLen(l) == 0)
    
    ///
Last updated on