Skip to content

MapReserve

Description

Reserve enough probe slots for at least n entries.

Parameters

Name Direction Description
m in,out Hash map.
n in Minimum number of entries expected.

Success

Returns true. The probe table now has capacity for at least n entries without triggering a rehash. Existing entries are preserved (a rehash into the larger table is performed if growth was needed). Map length is unchanged.

Failure

Returns false on allocation failure. The map is unchanged.

Usage example (Cross-references)

Usage examples (Cross-references)
    #define MapMustReserve(m, n)                                                                                           \
        do {                                                                                                               \
            if (!MapReserve((m), (n))) {                                                                                   \
                LOG_FATAL("MapMustReserve failed");                                                                        \
            }                                                                                                              \
        size             reserved_capacity;
    
        MapReserve(&map, 32);
        reserved_capacity = (size)MapCapacity(&map);
        // Force a growth-driven rehash. Existing entries must survive it,
        // length must be unchanged, and capacity must grow to at least 64.
        result = MapReserve(&map, 64);
        result = result && (MapCapacity(&map) >= 64);
        result = result && (MapPairCount(&map) == 8);
        bool result = (MapCapacity(&map) == 42);
    
        bool reserved = MapReserve(&map, 100);
        result        = result && reserved;
        result        = result && (MapCapacity(&map) >= 100);
        IntIntMap map = MapInitWithPolicy(const_hash, i32_compare, policy, &alloc);
    
        MapReserve(&map, 200);
    
        for (int k = 0; k < 60; k++)
        IntIntMap map = MapInitWithPolicy(const_hash, i32_compare, policy, &alloc);
    
        MapReserve(&map, 6);
        for (int k = 10; k <= 15; k++)
            MapInsertR(&map, k, k);
Last updated on