8000 GitHub - galactixx/comphash: comphash is a very lightweight Zig package offering a zero-cost compile-time hash map for immutable, O(1) string-keyed lookups without any runtime allocations.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

comphash is a very lightweight Zig package offering a zero-cost compile-time hash map for immutable, O(1) string-keyed lookups without any runtime allocations.

License

Notifications You must be signed in to change notification settings

galactixx/comphash

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

comptime-hashmap logo

comphash is a very lightweight Zig package offering a zero-cost compile-time hash map for immutable, O(1) string-keyed lookups without any runtime allocations.

Perfect for command‑line flag tables, protocol constant look‑ups, compile‑time DSLs, and any situation where the set of keys is known ahead of time.


Features

Category Details
Lookup O(1) expected access.
Hash Algorithms Use any fn([]const u8) u64 — the default is xxHash64 from zighash.
Probing Prober enum: Linear, DoubleHash, Bidirectional, Triangular.
Iterators Type‑driven keys(), values(), items()—all lazy and allocation‑free.
No Deps Pure Zig, no libc, no external libs.
Allocation No runtime allocations or pointer chasing.
Comprehensive Tests Built‑in std.testing ensures correctness across probing strategies.

🚀 Getting Started

Fetch via zig fetch

zig fetch --save git+https://github.com/galactixx/comphash#v0.1.0

This adds a comphash entry under .dependencies in your build.zig.zon.

Then in your build.zig:

const comphash_mod = b.dependency("comphash", .{
    .target = target,
    .optimize = optimize,
}).module("comphash");

// add to library
lib_mod.addImport("comphash", comphash_mod);

// add to executable
exe.root_module.addImport("comphash", comphash_mod);

This lets you const ch = @import("comphash"); in your Zig code.

📚 Usage

const std = @import("std");
const ch = @import("comphash");

const FruitMap = ch.ComptimeHashMap(u32, null, null, null);

pub fn main() !void {
    const kv = &.{ .{ "apple", 10 }, .{ "banana", 20 }, .{ "cherry", 30 } };
    const map = FruitMap.init(kv);

    std.debug.print("apple → {}\n", .{map.get("apple") orelse 0});
    std.debug.print("Length: {}\n",  .{map.length()});
}

🔍 API

ComptimeHashMap

pub fn ComptimeHashMap(
    comptime V:      type,                          // value type
    comptime hasher: ?fn([]const u8) u64,           // optional hash func
    comptime prober: ?Prober,                       // optional probe strategy
    comptime eql:    ?fn([]const u8,[]const u8)bool // optional equality
) type

Calling this generic returns a new struct type with the following notable members:

Method Description
init(kvs) Build the table at comptime. Accepts []const struct { []const u8, V }.
get(key) Returns ?V.
contains(key) Boolean containment check.
length() / isEmpty() Item count helpers.
keys() / values() / items() Lazy iterators.
capacity() Underlying table size (power‑of‑two).

Prober

Enum controlling collision resolution:

  • Linearbase + i
  • DoubleHashbase + i * (h(key) | 1)
  • Bidirectional – Alternates ±offsets
  • Triangularbase + i * (i + 1) / 2

Select with the third parameter to ComptimeHashMap.


🤝 License

This project is licensed under the MIT License. See the LICENSE file for details.


📞 Contact & Contributing

Feel free to open an issue or a pull request. Discussion and feedback are welcome!

About

comphash is a very lightweight Zig package offering a zero-cost compile-time hash map for immutable, O(1) string-keyed lookups without any runtime allocations.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

0