Last active
February 13, 2023 17:56
-
-
Save apmckinlay/1b590eecc8e1843846706fd3181d0210 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Hmap[K any, V any, H helper[K]] struct { | |
help H | |
// ... | |
} | |
type helper[K any] interface { | |
Hash(k K) uint64 | |
Equal(x, y K) bool | |
} | |
// Meth is a helper that calls Hash and Equal methods on the key | |
type Meth[K Key] struct{} // zero size | |
type Key interface { | |
Hash() uint64 | |
Equal(any) bool | |
} | |
func (m Meth[K]) Hash(k K) uint64 { | |
return k.Hash() | |
} | |
func (m Meth[K]) Equal(x, y K) bool { | |
return x.Equal(y) | |
} | |
type HmapMeth[K Key, V any] Hmap[K, V, Meth[K]] | |
// Funcs is a helper that is configured with hash and equals functions. | |
// This allows using closures which allows context. | |
type Funcs[K any] struct { | |
hfn func(k K) uint64 | |
eqfn func(x, y K) bool | |
} | |
func (m Funcs[K]) Hash(k K) uint64 { | |
return m.hfn(k) | |
} | |
func (m Funcs[K]) Equal(x, y K) bool { | |
return m.eqfn(x, y) | |
} | |
type HmapFuncs[K any, V any] Hmap[K, V, Funcs[K]] | |
func NewHmapFuncs[K any, V any](hfn func(k K) uint64, | |
eqfn func(x, y K) bool) *HmapFuncs[K, V] { | |
return &HmapFuncs[K, V]{help: Funcs[K]{hfn: hfn, eqfn: eqfn}} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment