/
hashfun.pony
53 lines (46 loc) · 1.29 KB
/
hashfun.pony
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
interface Hashable
"""
Anything with a hash method is hashable.
"""
fun hash(): U64
trait HashFunction[A] val
"""
A pluggable hash function.
"""
new val create()
"""
Data structures create instances internally. Use a primitive if possible.
"""
fun hash(x: box->A!): U64
"""
Calculate the hash of some type. This is an alias of the type parameter to
allow data structures to hash things without consuming them.
"""
fun eq(x: box->A!, y: box->A!): Bool
"""
Determine equality between two keys with the same hash. This is done with
viewpoint adapted aliases to allow data structures to determine equality
in a box fun without consuming keys.
"""
primitive HashEq[A: (Hashable box & Equatable[A] box)] is HashFunction[A]
fun hash(x: box->A): U64 =>
"""
Use the hash function from the type parameter.
"""
x.hash()
fun eq(x: box->A, y: box->A): Bool =>
"""
Use the structural equality function from the type parameter.
"""
x == y
primitive HashIs[A] is HashFunction[A]
fun hash(x: box->A!): U64 =>
"""
Hash the identity rather than the contents.
"""
(identityof x).hash()
fun eq(x: box->A!, y: box->A!): Bool =>
"""
Determine equality by identity rather than structurally.
"""
x is y