shithub: mc

Download patch

ref: 8a662861ad3ded8e965ab43e5bc254dd7ee85815
parent: 29b2ae95484477418cd73bf221d35468fbbbb9e9
author: Lucas Gabriel Vuotto <[email protected]>
date: Wed Nov 1 12:53:25 EDT 2017

Add comparable and hashable traits

Signed-off-by: Lucas Gabriel Vuotto <[email protected]>

--- a/lib/std/bld.sub
+++ b/lib/std/bld.sub
@@ -75,6 +75,7 @@
 	striter.myr
 	swap.myr
 	threadhooks.myr
+	traits.myr
 	try.myr
 	types.myr
 	units.myr
--- a/lib/std/hashfuncs.myr
+++ b/lib/std/hashfuncs.myr
@@ -4,6 +4,7 @@
 use "getint"
 use "sleq"
 use "slpush"
+use "traits"
 use "types"
 use "utf"
 
@@ -24,6 +25,42 @@
 	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]
--- /dev/null
+++ b/lib/std/traits.myr
@@ -1,0 +1,9 @@
+pkg std =
+	trait comparable @a =
+		cmp	: (a : @a, b : @a -> bool)
+	;;
+
+	trait hashable @a =
+		hash	: (a : @a -> uint64)
+	;;
+;;