Created
April 23, 2015 16:10
-
-
Save exelotl/6281d40ae8599722202f to your computer and use it in GitHub Desktop.
Defining a custom hash function for structures
This file contains 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
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