Skip to content

MapSetOnlyR

Description

Set the value for in_key, replacing any existing entries. R-value form.

Success

Returns true. Same state effects as MapSetOnlyL minus the source-zeroing step; both sources are left untouched.

Failure

Returns false on allocation failure. The map is unchanged.

Usage example (Cross-references)

Usage examples (Cross-references)
    #define MapMustSetOnlyR(m, in_key, in_value)                                                                           \
        do {                                                                                                               \
            if (!MapSetOnlyR((m), (in_key), (in_value))) {                                                                 \
                LOG_FATAL("MapMustSetOnlyR failed");                                                                       \
            }                                                                                                              \
        int red_count = 0;
    
        MapSetOnlyR(&map, "red", "apple");
        MapInsertR(&map, "red", "cherry");
        MapSetOnlyR(&map, "yellow", "banana");
        MapSetOnlyR(&map, "red", "apple");
        MapInsertR(&map, "red", "cherry");
        MapSetOnlyR(&map, "yellow", "banana");
        MapSetOnlyR(&map, "green", "pear");
        MapRehashWithPolicy(&map, MapPairCount(&map), MapPolicyQuadratic);
        MapInsertR(&map, "red", "cherry");
        MapSetOnlyR(&map, "yellow", "banana");
        MapSetOnlyR(&map, "green", "pear");
        MapRehashWithPolicy(&map, MapPairCount(&map), MapPolicyQuadratic);
                      (MapPairCount(&map) == 0) && MapEmpty(&map) && !MapContainsKey(&map, 1) && !MapContainsKey(&map, 2);
    
        MapSetOnlyR(&map, 7, 70);
        result = result && (MapPairCount(&map) == 1) && (MapValueCountForKey(&map, 7) == 1);
        result = result && MapGetFirstPtr(&map, 7) && (*MapGetFirstPtr(&map, 7) == 70);
    
        for (int i = 0; i < 24; i++) {
            MapSetOnlyR(&map, i, i * 10);
        }
    
        for (int i = 0; i < 6; i++) {
            MapSetOnlyR(&map, i, i + 100);
        }
    
        for (i = 0; i < 8; i++)
            MapSetOnlyR(&map, i, i * 100);
    
        // Force a growth-driven rehash. Existing entries must survive it,
        MapInsertR(&map, 1, 11);
        MapInsertR(&map, 2, 20);
        MapSetOnlyR(&map, 2, 200);
        MapSetOnlyR(&map, 3, 30);
        MapInsertR(&map, 2, 20);
        MapSetOnlyR(&map, 2, 200);
        MapSetOnlyR(&map, 3, 30);
    
        bool result = MapPairCount(&map) == 4;
        bool result = (MapValueCountForKey(&map, 5) == 3);
    
        MapSetOnlyR(&map, 5, 500);
    
        result = result && (MapValueCountForKey(&map, 5) == 1);
        bool result = (MapTombstones(&map) == 0);
    
        MapSetOnlyR(&map, 7, 700); // remove-all (tomb=1) then raw reinsert into the tombstone
    
        result = result && (MapTombstones(&map) == 0);
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        MapSetOnlyR(&map, 1, 10);
        MapInsertR(&map, 1, 11);
        MapSetOnlyR(&map, 2, 20);
        MapSetOnlyR(&map, 1, 10);
        MapInsertR(&map, 1, 11);
        MapSetOnlyR(&map, 2, 20);
    
        u64 length_before = MapPairCount(&map);
    
        for (int i = 0; i < 12; i++)
            MapSetOnlyR(&map, i, i + 100);
    
        u64 length_before = MapPairCount(&map);
        result      = result && (MapPairCount(&map) == length_before - 1);
    
        MapSetOnlyR(&map, 5, 205);
        result = result && MapContainsKey(&map, 5);
        result = result && MapGetFirstPtr(&map, 5) && (*MapGetFirstPtr(&map, 5) == 205);
    
        for (int i = 1; i <= 4; i++) {
            MapSetOnlyR(&map, i, i * 10);
        }
        MapInsertR(&map, 2, 25);
    
        for (int i = 1; i <= 4; i++)
            MapSetOnlyR(&map, i, i * 10);
        MapInsertR(&map, 2, 25);
        IntIntMap        map   = MapInitWithValueCompare(i32_hash, i32_compare, i32_compare, &alloc);
    
        MapSetOnlyR(&map, 7, 70);
        MapInsertR(&map, 7, 71);
        MapSetOnlyR(&map, 9, 90);
        MapSetOnlyR(&map, 7, 70);
        MapInsertR(&map, 7, 71);
        MapSetOnlyR(&map, 9, 90);
    
        bool result = MapContainsKey(&map, 7);
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        MapSetOnlyR(&map, 11, 110);
        MapInsertR(&map, 11, 111);
        IntIntMap        map   = MapInitWithValueCompare(i32_hash, i32_compare, i32_compare, &alloc);
    
        MapSetOnlyR(&map, 11, 110);
        MapInsertR(&map, 11, 111);
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        MapSetOnlyR(&map, 11, 110);
        MapInsertR(&map, 11, 111);
Last updated on