std.AutoHashMap 是 Zig 标准库中最常用的键值映射容器,它会根据键的类型自动生成哈希与等值比较函数。对于字符串切片等引用类型,也可以使用对应的 std.StringHashMap。

常用操作包括:

  • init(allocator):创建哈希表;
  • put(key, value):插入或更新键值对;
  • get(key):查询对应的值,不存在返回 null;
  • contains(key):判断键是否存在;
  • getOrPut(key):查找现有条目,不存在则就地创建条目并返回指针;
  • remove(key):根据键删除对应条目;
  • iterator():获取遍历所有键值对的迭代器;
  • deinit():释放哈希表占用的内存。

//! Demonstrate hash map operations with std.AutoHashMap in Zig.

const std = @import("std");

pub fn main(init: std.process.Init) !void {
    const gpa = init.gpa;

    // std.AutoHashMap automatically provides hash and equality implementations for the key type.
    var map = std.AutoHashMap(u32, []const u8).init(gpa);
    defer map.deinit();

    // Insert key-value pairs
    try map.put(1, "Zig");
    try map.put(2, "Rust");
    try map.put(3, "C");

    // Fetch value by key
    if (map.get(1)) |val| {
        std.debug.print("Key 1: {s}\n", .{val});
        try std.testing.expectEqualStrings("Zig", val);
    }

    // Check key existence
    try std.testing.expect(map.contains(2));
    try std.testing.expect(!map.contains(99));

    // getOrPut: retrieve existing entry or insert a new one if not present
    const gop = try map.getOrPut(4);
    if (!gop.found_existing) {
        gop.value_ptr.* = "Go";
    }
    try std.testing.expectEqualStrings("Go", map.get(4).?);

    // Iterate over key-value pairs
    var it = map.iterator();
    var count: usize = 0;
    while (it.next()) |entry| {
        std.debug.print("  {d} => {s}\n", .{ entry.key_ptr.*, entry.value_ptr.* });
        count += 1;
    }
    try std.testing.expectEqual(4, count);

    // Remove entry by key
    const removed = map.remove(3);
    try std.testing.expect(removed);
    try std.testing.expect(!map.contains(3));

    std.debug.print("Final map count: {d}\n", .{map.count()});
}