Skip to content

Instantly share code, notes, and snippets.

@exelotl
Created April 23, 2015 16:10
Show Gist options
  • Save exelotl/6281d40ae8599722202f to your computer and use it in GitHub Desktop.
Save exelotl/6281d40ae8599722202f to your computer and use it in GitHub Desktop.
Defining a custom hash function for structures
import structs/HashMap
Vec2: cover {
x, y:Int
}
vec2equals: func <T> (_a, _b:T) -> Bool {
a := _a as Vec2
b := _b as Vec2
a x == b x && a y == b y
}
vec2hash: func <T> (_a:T) -> SizeT {
a := _a as Vec2
a x * 31 + a y
}
main: func {
map := HashMap<Vec2, String> new()
map hashKey = vec2hash
map keyEquals = vec2equals
a := (10, 20) as Vec2
b := (10, 20) as Vec2
map put(a, "foo")
map get(b) println()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment