Skip to content

MapInit

Description

Initialize a map with required key hash and compare callbacks plus a user-owned allocator. Uses linear probing. Allocator argument is optional inside a Scope block.

Usage example (Cross-references)

Usage examples (Cross-references)
    
        IntGraph graph  = GraphInit(&alloc);
        CountMap counts = MapInit(node_id_hash, node_id_compare, &alloc);
    
        GraphNodeId a = GraphAddNodeR(&graph, 1);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Map(Float, u64) counts = MapInit(float_hash, float_compare, &alloc);
    
        Float k1 = FloatFromStr("3.14", &alloc.base);
        Allocator       *base  = ALLOCATOR_OF(&alloc);
    
        Map(BitVec, u64) counts = MapInit(bitvec_hash, bitvec_compare, &alloc);
    
        BitVec k1 = BitVecInit(base);
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        Map(Int, u64) counts = MapInit(int_hash, int_compare, &alloc);
    
        Int k1 = IntFrom(100u, &alloc.base);
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        bool result = MapEmpty(&map);
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc     = DefaultAllocatorInit();
        IntIntMap        map       = MapInit(i32_hash, i32_compare, &alloc);
        int              threshold = 30;
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
        size             reserved_capacity;
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        for (int i = 0; i < 24; i++) {
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        // Fresh MapInit installs the linear policy by value and starts with
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
        bool             result;
        int              i;
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        MapInsertR(&map, 1, 10);
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        enum {
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        MapInsertR(&map, 1, 10);
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        // Miss on an empty (capacity 0) map: returns false, nothing inserted.
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
        int              key   = 42;
        int              value = 84;
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
        int              key   = 42;
        int              value = 84;
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
        int              key   = 11;
        int              value = 110;
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
        int              value = 110;
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc  = DefaultAllocatorInit();
        IntIntMap        map    = MapInit(i32_hash, i32_compare, &alloc);
        bool             result = true;
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
        int             *value_ptr;
        bool             result;
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        int *value_ptr = MapEnsurePtr(&map, 3, 30);
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
        int              k1    = 1;
        int              v1    = 10;
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        MapInsertR(&map, 1, 10);
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        bool result = MapCompact(&map); // empty map: must succeed via fast path
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        bool result = MapCompact(&map);
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        bool result = MapCompact(&map);
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        bool result = MapCompact(&map);
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        MapInsertR(&map, 1, 10);
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        for (int k = 0; k < 10; k++)
        DefaultAllocator inner = DefaultAllocatorInit();
        FailAlloc        fa    = fail_alloc_init(&inner);
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &fa.base);
    
        for (int k = 0; k < 6; k++)
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc  = DefaultAllocatorInit();
        IntIntMap        map    = MapInit(i32_hash, i32_compare, &alloc);
        bool             result = true;
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        MapInsertR(&map, 7, 70);
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        for (int k = 0; k < 7; k++)
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        for (int k = 0; k < 5; k++)
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        for (int k = 0; k < 6; k++)
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        bool result = MapPairCount(&map) == 0 && MapCapacity(&map) == 0 && MapTombstones(&map) == 0 &&
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        MapInsertR(&map, 1, 10);
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        GENERIC_MAP(&map)->__magic ^= 0x1;
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        MapContainsPair(&map, 1, 10);
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        MapRemovePair(&map, 1, 10);
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        MapRemoveIf(&map, NULL, NULL);
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        MapRetainIf(&map, NULL, NULL);
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        MapInsertR(&map, 1, 10);
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        MapInsertR(&map, 1, 10);          // capacity becomes 8
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        bool result = MapInsertR(&map, 7, 70); // first ops clear the validated bit
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        MapSetOnlyR(&map, 1, 10);
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        MapInsertR(&map, 1, 10);
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        MapInsertR(&map, 5, 50);
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        bool result = (MapRetainIf(&map, always_retain, NULL) == 0);
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        size removed = MapRemoveIf(&map, always_true_predicate, NULL);
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        MapInsertR(&map, 1, 10);
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        MapInsertR(&map, 5, 50);
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        MapInsertR(&map, 5, 50);
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        for (int i = 0; i < 12; i++)
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(const_hash, i32_compare, &alloc);
    
        // Four keys colliding into one bucket form a single probe chain.
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc     = DefaultAllocatorInit();
        IntIntMap        map       = MapInit(i32_hash, i32_compare, &alloc);
        int              key_sum   = 0;
        int              value_sum = 0;
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc          = DefaultAllocatorInit();
        IntIntMap        map            = MapInit(i32_hash, i32_compare, &alloc);
        int              unique_key_sum = 0;
        int              all_value_sum  = 0;
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc     = DefaultAllocatorInit();
        IntIntMap        map       = MapInit(i32_hash, i32_compare, &alloc);
        int              key_sum   = 0;
        int              value_sum = 0;
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        MapInsertR(&map, 1, 10);
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        int n = 0;
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        MapSetOnlyR(&map, 11, 110);
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc = DefaultAllocatorInit();
        IntIntMap        map   = MapInit(i32_hash, i32_compare, &alloc);
    
        MapSetOnlyR(&map, 11, 110);
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc     = DefaultAllocatorInit();
        IntIntMap        map       = MapInit(i32_hash, i32_compare, &alloc);
        MapValueCursor   cursor    = MapValueCursorInvalid();
        int              value_sum = 0;
        typedef Map(int, int) IntIntMap;
        DefaultAllocator alloc  = DefaultAllocatorInit();
        IntIntMap        map    = MapInit(i32_hash, i32_compare, &alloc);
        MapValueCursor   cursor = MapValueCursorInvalid();
Last updated on