Skip to content

ListFind

ListFind

Description

Find the first item equal to the searched value.

item_ptr must point to a value comparable with list elements. Use &LVAL(expr) when searching with a temporary expression.

Parameters

Name Direction Description
l in List to search.
item_ptr in Pointer to searched value.
compare in Comparator returning 0 for equality.

Success

Index of first matching item.

Failure

SIZE_MAX

Usage example (Cross-references)

Usage examples (Cross-references)
    
    static bool test_list_find_contains(void) {
        WriteFmt("Testing ListFind and ListContains\n");
    
        typedef List(int) IntList;
        int  needle  = 20;
        int  missing = 99;
        bool result  = (ListFind(&list, &needle, compare_ints) == SIZE_MAX);
        result       = result && !ListContains(&list, &needle, compare_ints);
        ListPushBackR(&list, 20);
    
        result = result && (ListFind(&list, &needle, compare_ints) == 1);
        result = result && ListContains(&list, &needle, compare_ints);
        result = result && !ListContains(&list, &missing, compare_ints);
        result = result && ListContains(&list, &needle, compare_ints);
        result = result && !ListContains(&list, &missing, compare_ints);
        result = result && (ListFind(&list, &missing, compare_ints) == SIZE_MAX);
    
        ListDeinit(&list);
    /// TAGS: List, Contains, Search, Compare
    ///
    #define ListContains(l, item_ptr, compare) (ListFind((l), (item_ptr), (compare)) != SIZE_MAX)
    
    ///
Last updated on