Skip to content

Commit 314db01

Browse files
committed
add hasp map
1 parent beea809 commit 314db01

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

hash_map/basic.zig

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const std = @import("std");
2+
const print = std.debug.print;
3+
4+
pub fn main() !void {
5+
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
6+
defer _ = gpa.deinit();
7+
8+
const allocator = gpa.allocator();
9+
var map = std.AutoHashMap(u8, []const u8).init(allocator);
10+
defer map.deinit();
11+
12+
// Insert
13+
try map.put(0, "Zero");
14+
try map.put(1, "One");
15+
try map.put(2, "Two");
16+
17+
const value = map.get(1);
18+
if (value) |v| {
19+
print("{s}", .{v});
20+
}
21+
}

hash_map/default.zig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const std = @import("std");
2+
const print = std.debug.print;
3+
4+
pub fn main() !void {
5+
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
6+
defer _ = gpa.deinit();
7+
8+
const allocator = gpa.allocator();
9+
var map = std.AutoHashMap(u8, []const u8).init(allocator);
10+
defer map.deinit();
11+
12+
// Insert
13+
try map.put(0, "Zero");
14+
try map.put(1, "One");
15+
try map.put(2, "Two");
16+
17+
const value = map.get(99) orelse "Default";
18+
print("{s}", .{value});
19+
}

hash_map/readme.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Hash Maps
2+
3+
key-value pair
4+
5+
```bash
6+
$ zig run basic.zig
7+
One
8+
```
9+
10+
```bash
11+
$ zig run default.zig
12+
Default
13+
```
14+
15+
```bash
16+
$ zig run string.zig
17+
1
18+
```

hash_map/string.zig

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const std = @import("std");
2+
const print = std.debug.print;
3+
4+
pub fn main() !void {
5+
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
6+
defer _ = gpa.deinit();
7+
8+
const allocator = gpa.allocator();
9+
var map = std.StringHashMap(u8).init(allocator);
10+
defer map.deinit();
11+
12+
// Insert
13+
try map.put("Zero", 0);
14+
try map.put("One", 1);
15+
try map.put("Two", 2);
16+
17+
const value = map.get("One");
18+
if (value) |v| {
19+
print("{}", .{v});
20+
}
21+
}

0 commit comments

Comments
 (0)