From 71a56f94c86e86670543c86c0cdfdc5f2cc69283 Mon Sep 17 00:00:00 2001 From: sjinzh <99076655+sjinzh@users.noreply.github.com> Date: Fri, 13 Jan 2023 20:17:44 +0800 Subject: [PATCH] add zig codes for Section 'Hash Map' --- codes/zig/build.zig | 14 +++++++ codes/zig/chapter_hashing/hash_map.zig | 55 ++++++++++++++++++++++++++ codes/zig/include/PrintUtil.zig | 10 +++++ 3 files changed, 79 insertions(+) create mode 100644 codes/zig/chapter_hashing/hash_map.zig diff --git a/codes/zig/build.zig b/codes/zig/build.zig index a8738fae..a922ae86 100644 --- a/codes/zig/build.zig +++ b/codes/zig/build.zig @@ -160,6 +160,20 @@ pub fn build(b: *std.build.Builder) void { const run_step_array_stack = b.step("run_array_stack", "Run array_stack"); run_step_array_stack.dependOn(&run_cmd_array_stack.step); + // Section: "Hash Map" + // Source File: "chapter_hashing/hash_map.zig" + // Run Command: zig build run_hash_map + const exe_hash_map = b.addExecutable("hash_map", "chapter_hashing/hash_map.zig"); + exe_hash_map.addPackagePath("include", "include/include.zig"); + exe_hash_map.setTarget(target); + exe_hash_map.setBuildMode(mode); + exe_hash_map.install(); + const run_cmd_hash_map = exe_hash_map.run(); + run_cmd_hash_map.step.dependOn(b.getInstallStep()); + if (b.args) |args| run_cmd_hash_map.addArgs(args); + const run_step_hash_map= b.step("run_hash_map", "Run hash_map"); + run_step_hash_map.dependOn(&run_cmd_hash_map.step); + // Section: "Bubble Sort" // Source File: "chapter_sorting/bubble_sort.zig" // Run Command: zig build run_bubble_sort diff --git a/codes/zig/chapter_hashing/hash_map.zig b/codes/zig/chapter_hashing/hash_map.zig new file mode 100644 index 00000000..1e217c43 --- /dev/null +++ b/codes/zig/chapter_hashing/hash_map.zig @@ -0,0 +1,55 @@ +// File: hash_map.zig +// Created Time: 2023-01-13 +// Author: sjinzh (sjinzh@gmail.com) + +const std = @import("std"); +const inc = @import("include"); + +// Driver Code +pub fn main() !void { + // 初始化哈希表 + var map = std.AutoHashMap(i32, []const u8).init(std.heap.page_allocator); + // 延迟释放内存 + defer map.deinit(); + + // 添加操作 + // 在哈希表中添加键值对 (key, value) + try map.put(12836, "小哈"); + try map.put(15937, "小啰"); + try map.put(16750, "小算"); + try map.put(13276, "小法"); + try map.put(10583, "小鸭"); + std.debug.print("\n添加完成后,哈希表为\nKey -> Value\n", .{}); + inc.PrintUtil.printHashMap(i32, []const u8, map); + + // 查询操作 + // 向哈希表输入键 key ,得到值 value + var name = map.get(15937).?; + std.debug.print("\n输入学号 15937 ,查询到姓名 {s}\n", .{name}); + + // 删除操作 + // 在哈希表中删除键值对 (key, value) + _ = map.remove(10583); + std.debug.print("\n删除 10583 后,哈希表为\nKey -> Value\n", .{}); + inc.PrintUtil.printHashMap(i32, []const u8, map); + + // 遍历哈希表 + std.debug.print("\n遍历键值对 Key->Value\n", .{}); + inc.PrintUtil.printHashMap(i32, []const u8, map); + + std.debug.print("\n单独遍历键 Key\n", .{}); + var it = map.iterator(); + while (it.next()) |kv| { + std.debug.print("{}\n", .{kv.key_ptr.*}); + } + + std.debug.print("\n单独遍历值 value\n", .{}); + it = map.iterator(); + while (it.next()) |kv| { + std.debug.print("{s}\n", .{kv.value_ptr.*}); + } + + const getchar = try std.io.getStdIn().reader().readByte(); + _ = getchar; +} + diff --git a/codes/zig/include/PrintUtil.zig b/codes/zig/include/PrintUtil.zig index 4d2d2089..2ef9047c 100644 --- a/codes/zig/include/PrintUtil.zig +++ b/codes/zig/include/PrintUtil.zig @@ -46,6 +46,16 @@ pub fn printLinkedList(comptime T: type, node: ?*ListNode(T)) !void { } } +// Print a HashMap +pub fn printHashMap(comptime TKey: type, comptime TValue: type, map: std.AutoHashMap(TKey, TValue)) void { + var it = map.iterator(); + while (it.next()) |kv| { + var key = kv.key_ptr.*; + var value = kv.value_ptr.*; + std.debug.print("{} -> {s}\n", .{key, value}); + } +} + // This tree printer is borrowed from TECHIE DELIGHT // https://www.techiedelight.com/c-program-print-binary-tree/ const Trunk = struct {