summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLucas Gabriel Vuotto <lvuotto92@gmail.com>2017-11-01 16:53:25 -0300
committerOri Bernstein <ori@markovcorp.com>2017-11-14 13:49:56 -0800
commit8a662861ad3ded8e965ab43e5bc254dd7ee85815 (patch)
tree9236f76b8fb6f19c404e8aff9c5795176413d0d4
parent29b2ae95484477418cd73bf221d35468fbbbb9e9 (diff)
downloadmc-8a662861ad3ded8e965ab43e5bc254dd7ee85815.tar.gz
Add comparable and hashable traits
Signed-off-by: Lucas Gabriel Vuotto <lvuotto92@gmail.com>
-rw-r--r--lib/std/bld.sub1
-rw-r--r--lib/std/hashfuncs.myr37
-rw-r--r--lib/std/traits.myr9
3 files changed, 47 insertions, 0 deletions
diff --git a/lib/std/bld.sub b/lib/std/bld.sub
index a834dbb..d79e512 100644
--- a/lib/std/bld.sub
+++ b/lib/std/bld.sub
@@ -75,6 +75,7 @@ lib std {inc=.} =
striter.myr
swap.myr
threadhooks.myr
+ traits.myr
try.myr
types.myr
units.myr
diff --git a/lib/std/hashfuncs.myr b/lib/std/hashfuncs.myr
index 08a83aa..070a7d2 100644
--- a/lib/std/hashfuncs.myr
+++ b/lib/std/hashfuncs.myr
@@ -4,6 +4,7 @@ use "die"
use "getint"
use "sleq"
use "slpush"
+use "traits"
use "types"
use "utf"
@@ -24,6 +25,42 @@ pkg std =
const siphash24 : (data : byte[:], seed : byte[16] -> uint64)
generic slhash : (sl : @a[:] -> uint64)
+
+ impl comparable @a[:] =
+ cmp = {a, b
+ -> sleq(a, b)
+ }
+ ;;
+
+ impl hashable @a[:] =
+ hash = {a
+ -> siphash24((a : byte#)[:a.len * sizeof(@a)], Seed)
+ }
+ ;;
+
+ impl comparable @a::(integral,numeric) =
+ cmp = {a, b
+ -> a == b
+ }
+ ;;
+
+ impl hashable @a::(integral,numeric) =
+ hash = {a
+ -> siphash24((&a : byte#)[:sizeof(@a)], Seed)
+ }
+ ;;
+
+ impl comparable @a# =
+ cmp = {a, b
+ -> a == b
+ }
+ ;;
+
+ impl hashable @a# =
+ hash = {a
+ -> hash((a : intptr))
+ }
+ ;;
;;
const Seed : byte[16] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
diff --git a/lib/std/traits.myr b/lib/std/traits.myr
new file mode 100644
index 0000000..1c5678b
--- /dev/null
+++ b/lib/std/traits.myr
@@ -0,0 +1,9 @@
+pkg std =
+ trait comparable @a =
+ cmp : (a : @a, b : @a -> bool)
+ ;;
+
+ trait hashable @a =
+ hash : (a : @a -> uint64)
+ ;;
+;;