Skip to content

MapCompact

Description

Rebuild the map using the current policy and current pair count. This removes tombstones and re-packs the probe table.

Parameters

Name Direction Description
m in,out Map.

Success

Returns true. Tombstones have been removed and every live entry has been re-hashed into a fresh probe table sized for the current length. Map length is preserved; probe distances may decrease.

Failure

Returns false on allocation failure for the new probe table. The map and existing entries are unchanged.

Usage example (Cross-references)

Usage examples (Cross-references)
    #define MapMustCompact(m)                                                                                              \
        do {                                                                                                               \
            if (!MapCompact((m))) {                                                                                        \
                LOG_FATAL("MapMustCompact failed");                                                                        \
            }                                                                                                              \
    
        bool result = (MapTombstones(&first) == 1);
        MapCompact(&first);
    
        result = result && (MapTombstones(&first) == 0);
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        bool result = MapCompact(&map); // empty map: must succeed via fast path
    
        result = result && (MapPairCount(&map) == 0);
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        bool result = MapCompact(&map);
        MapInsertR(&map, 7, 70);
        result = result && MapGetFirstPtr(&map, 7) && (*MapGetFirstPtr(&map, 7) == 70);
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        bool result = MapCompact(&map);
        MapInsertR(&map, 8, 80);
        MapInsertR(&map, 9, 90);
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        bool result = MapCompact(&map);
        MapInsertR(&map, 11, 110);
        result = result && MapGetFirstPtr(&map, 11) && (*MapGetFirstPtr(&map, 11) == 110);
    
        fa.fail_now = true;              // force the new-table allocation to fail
        bool failed = !MapCompact(&map); // must return false
        fa.fail_now = false;
        bool result = (MapPairCount(&map) == 6);
    
        result = result && MapCompact(&map);
        result = result && (MapCapacity(&map) == 16);
Last updated on