/static-map

A generic hashmap backed by static memory

Primary LanguageZigMIT LicenseMIT

StaticMap

A generic HashMap in zig similar to std.ArrayHashMap().

Use

zig fetch --save git+https://github.com/travisstaloch/static-map
// build.zig:
const static_map = b.dependency("static-map", .{});
exe.root_module.addImport("static-map", static_map.module("static-map"));
// main.zig
test "usage" {
    // const static_map = @import("static-map");
    // init()
    const Map = static_map.StaticStringMap(u8, 16);
    var map = Map.init();
    // put()/get()/contains()
    try map.put("abc", 1);
    try testing.expect(map.contains("abc"));
    try testing.expectEqual(1, map.get("abc"));
    // getOrPut()
    const gop = map.getOrPut("abc");
    try testing.expectEqual(.existing, gop.status);
    gop.value_ptr.* = 2;
    try testing.expectEqual(2, map.get("abc"));
    // putAssumeCapacity()
    map.putAssumeCapacity("def", 3);
    try testing.expectEqual(3, map.get("def"));
    // putNoClobber()
    map.putNoClobber("ghi", 4);
    try testing.expectEqual(4, map.get("ghi"));
    // getPtr()
    map.getPtr("def").?.* = 5;
    try testing.expectEqual(5, map.get("def"));
    // count()
    try testing.expectEqual(3, map.count());
    // iterator()
    var iter = map.iterator();
    var count: u8 = 0;
    while (iter.next()) |kv| : (count += 1) {
        try std.io.null_writer.print("{s}: {}", .{ kv.key, kv.value });
    }
    try testing.expectEqual(map.count(), count);
}

Tests

src/tests.zig

zig build test

Bench

this is a plot of data generated by src/bench.zig. you can generate some data by running ./bench.sh

ReleaseFast

Screenshot-4

Debug

Screenshot-2

Prior Art

https://github.com/Vexu/comptime_hash_map