Skip to content
MapRehashWithPolicy

MapRehashWithPolicy

Description

Remap using a specific probing policy.

Parameters

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

Success

Returns true. The map is rebuilt with the new probing policy (copied into the map by value), sized to fit at least n entries plus the current length. Every live entry has been re-hashed under the new policy; tombstones are gone.

Failure

Returns false on allocation failure during the new probe table build. The map and its existing policy are unchanged.

Usage example (Cross-references)

Usage examples (Cross-references)
    #define MapMustRehashWithPolicy(m, n, policy_value)                                                                    \
        do {                                                                                                               \
            if (!MapRehashWithPolicy((m), (n), (policy_value))) {                                                          \
                LOG_FATAL("MapMustRehashWithPolicy failed");                                                               \
            }                                                                                                              \
        MapSetOnlyR(&map, "yellow", "banana");
        MapSetOnlyR(&map, "green", "pear");
        MapRehashWithPolicy(&map, MapPairCount(&map), MapPolicyQuadratic);
    
        bool result = (MapPolicy(&map).first_index == MapPolicyQuadratic.first_index) &&
        }
    
        MapRehashWithPolicy(&map, MapPairCount(&map), MapPolicyQuadratic);
    
        bool result = (MapPolicy(&map).first_index == MapPolicyQuadratic.first_index) &&
    // installing the policy, and nothing aborts -> deadend FAILS = mutant killed.
    static bool test_rehash_rejects_invalid_policy(void) {
        WriteFmt("Testing MapRehashWithPolicy validates the new policy up front\n");
    
        typedef Map(int, int) IntIntMap;
    
        MapPolicy bad = make_validate_only_reject_policy();
        MapRehashWithPolicy(&map, 1, bad); // real: LOG_FATAL on the stuck-probe check
    
        MapDeinit(&map);
    
        MapPolicy tight  = make_tight_policy();
        bool      result = MapRehashWithPolicy(&map, 0, tight);
    
        result = result && (MapPairCount(&map) == 3);
    // [n, length), the real code must reject it (length is required).
    static bool test_rehash_rejects_insufficient_with_small_n(void) {
        WriteFmt("Testing MapRehashWithPolicy rejects under-sized capacity for n<length\n");
    
        typedef Map(int, int) IntIntMap;
    
        MapPolicy sab = make_sabotage_policy();
        MapRehashWithPolicy(&map, 2, sab); // must LOG_FATAL "insufficient capacity"
    
        MapDeinit(&map);
            MapInsertR(&map, keys[i], keys[i] + 1);
    
        MapRehashWithPolicy(&map, 8, policy);
    
        bool result = (MapPairCount(&map) == 5);
    
        MapRemoveFirst(&map, 10);
        MapRehashWithPolicy(&map, 5, policy);
    
        bool result = ok && (MapValuePtrFromCursor(&map, cursor) == NULL);
Last updated on