Skip to content

MapRetainIf

Description

Retain only entries that satisfy a predicate.

Parameters

Name Direction Description
m in,out Map.
predicate_fn in Callback returning true for entries to keep.
ctx_ptr in,out Optional user context passed to the predicate.

Success

Returns the count of removed entries (may be 0). Every entry for which the predicate returned false has been removed, its slot turned into a tombstone, and key_copy_deinit / value_copy_deinit (if configured) invoked. Map length shrinks by the returned count.

Failure

Function cannot fail. A NULL predicate_fn is a caller bug and aborts via LOG_FATAL.

Usage example (Cross-references)

Usage examples (Cross-references)
    
        if (!predicate) {
            LOG_FATAL("MapRetainIf requires a predicate");
        }
    
    static bool test_map_retain_if_without_predicate_fails(void) {
        WriteFmt("Testing MapRetainIf without predicate\n");
    
        typedef Map(int, int) IntIntMap;
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        MapRetainIf(&map, NULL, NULL);
    
        MapDeinit(&map);
        MapInsertR(&map, 4, 40);
    
        bool result = (MapRetainIf(&map, retain_values_above_threshold, &threshold) == 2);
        result      = result && !MapContainsKey(&map, 1);
        result      = result && !MapContainsKey(&map, 2);
Last updated on