I am trying to write a hashset data structure and want to allow the user to give their own comparison function (eql) along with a hasher.
I want Context to be more flexible but I have no good idea how to do this.
Preferably I want the initializing to look the same i.e. not take additional types and use obscure methods.
If anyone would like to take a look at the code I have shared a clean example at this link:
https://zigbin.io/3cd96a
#Good way to do context
1 messages · Page 1 of 1 (latest)
when you say "more flexible", do you mean that you want to be able to substitute the hasher and eql function at runtime?
No just in the setup (typing during comptime).
I want the freedom of using slightly different methods, for example ahasher that would take different data or output a bigger digest.
Since my types in Context are fixed, I am limited.
well you don't need to accept a struct, you can accept a type
like
pub const DefaultContext = struct {
pub fn eql(comptime T: type, a: T, b: T) bool {
return std.mem.eql(T, a, b);
}
pub fn hash(data: u32) u32 {
return std.hash.Murmur3_32.hashUint32(data);
}
};
var set = StaticSet(u8, DefaultContext).init(size);
it depends if you need to store data in the context too, or if you just need functions
what I'm trying to get at is like