Skip to content

Commit 8711582

Browse files
committed
chore: add example and improve readme
1 parent f5269e2 commit 8711582

File tree

3 files changed

+78
-6
lines changed

3 files changed

+78
-6
lines changed

README.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,40 @@
11
# ChibiHash64-Zig
22

3-
A Zig port of [ChibiHash64](https://github.com/N-R-K/ChibiHash) - a small, fast 64-bit hash function.
3+
A Zig port of [ChibiHash64](https://github.com/N-R-K/ChibiHash) - a small, fast 64-bit hash function. See the article [ChibiHash: A small, fast 64-bit hash function](https://nrk.neocities.org/articles/chibihash) for more information.
4+
5+
All credit for the algorithm goes to [N-R-K](https://github.com/N-R-K).
46

57
## Features
8+
69
- Simple 64-bit hash function
10+
- Supports both v1 and v2 of the hash function
711
- HashMap implementation
812
- Thoroughly tested with known test vectors
913

1014
## Usage
1115

1216
```
1317
const std = @import("std");
14-
const ChibiHash64 = @import("chibihash64.zig");
18+
const ChibiHash64v1 = @import("chibihash64_v1.zig");
19+
const ChibiHash64v2 = @import("chibihash64_v2.zig");
20+
21+
// Basic hashing v1
22+
const hash = ChibiHash64v1.hash("Hello, world!", 0);
1523
16-
// Basic hashing
17-
const hash = ChibiHash64.hash("Hello, world!", 0);
24+
// Using HashMap v1
25+
var map = ChibiHash64v1.HashMap([]const u8, i32).init(allocator);
26+
defer map.deinit();
27+
28+
// Basic hashing v2
29+
const hash = ChibiHash64v2.hash("Hello, world!", 0);
1830
19-
// Using HashMap
20-
var map = ChibiHash64.HashMap([]const u8, i32).init(allocator);
31+
// Using HashMap v2
32+
var map = ChibiHash64v2.HashMap([]const u8, i32).init(allocator);
2133
defer map.deinit();
2234
```
2335

36+
See `example/example.zig` for a complete example. Run it with `zig build run-example`.
37+
2438
## License
2539

2640
MIT.

build.zig

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,32 @@ pub fn build(b: *std.Build) void {
6868
const docs_step = b.step("docs", "Generate library documentation");
6969
docs_step.dependOn(&lib_v1_docs.step);
7070
docs_step.dependOn(&lib_v2_docs.step);
71+
72+
// Create example executable
73+
const example = b.addExecutable(.{
74+
.name = "example",
75+
.root_source_file = .{ .cwd_relative = "example/example.zig" },
76+
.target = target,
77+
.optimize = optimize,
78+
});
79+
80+
// Create modules
81+
const chibihash64_v1 = b.addModule("chibihash64_v1", .{
82+
.root_source_file = .{ .cwd_relative = "src/chibihash64_v1.zig" },
83+
});
84+
const chibihash64_v2 = b.addModule("chibihash64_v2", .{
85+
.root_source_file = .{ .cwd_relative = "src/chibihash64_v2.zig" },
86+
});
87+
88+
// Add module dependencies to example
89+
example.root_module.addImport("chibihash64_v1", chibihash64_v1);
90+
example.root_module.addImport("chibihash64_v2", chibihash64_v2);
91+
92+
// Install the example
93+
b.installArtifact(example);
94+
95+
// Create a run step for the example
96+
const run_example = b.addRunArtifact(example);
97+
const run_step = b.step("run-example", "Run the example program");
98+
run_step.dependOn(&run_example.step);
7199
}

example/example.zig

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const std = @import("std");
2+
const ChibiHash64v1 = @import("chibihash64_v1");
3+
const ChibiHash64v2 = @import("chibihash64_v2");
4+
5+
pub fn main() !void {
6+
// Get an allocator
7+
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
8+
defer _ = gpa.deinit();
9+
const allocator = gpa.allocator();
10+
11+
// Basic hashing v1
12+
const hash_v1 = ChibiHash64v1.chibihash64("Hello, world!", 0);
13+
std.debug.print("V1 hash of 'Hello, world!': 0x{X}\n", .{hash_v1});
14+
15+
// Using HashMap v1
16+
var map_v1 = ChibiHash64v1.HashMap([]const u8, i32).init(allocator);
17+
defer map_v1.deinit();
18+
try map_v1.put("example", 42);
19+
std.debug.print("V1 HashMap get 'example': {?}\n", .{map_v1.get("example")});
20+
21+
// Basic hashing v2
22+
const hash_v2 = ChibiHash64v2.chibihash64("Hello, world!", 0);
23+
std.debug.print("V2 hash of 'Hello, world!': 0x{X}\n", .{hash_v2});
24+
25+
// Using HashMap v2
26+
var map_v2 = ChibiHash64v2.HashMap([]const u8, i32).init(allocator);
27+
defer map_v2.deinit();
28+
try map_v2.put("example", 42);
29+
std.debug.print("V2 HashMap get 'example': {?}\n", .{map_v2.get("example")});
30+
}

0 commit comments

Comments
 (0)