Eigenstate: myrddin-dev mailing list

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 1/2] Add comparable and hashable traits


Signed-off-by: Lucas Gabriel Vuotto <lvuotto92@xxxxxxxxx>
---
 lib/std/bld.sub       |  1 +
 lib/std/hashfuncs.myr | 37 +++++++++++++++++++++++++++++++++++++
 lib/std/traits.myr    |  9 +++++++++
 3 files changed, 47 insertions(+)
 create mode 100644 lib/std/traits.myr

diff --git a/lib/std/bld.sub b/lib/std/bld.sub
index a834dbb3..d79e512e 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 da47215f..41422a16 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 00000000..1c5678b1
--- /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)
+	;;
+;;
-- 
2.14.2


References:
RFC: comparable and hashable traitsLucas Gabriel Vuotto <lvuotto92@xxxxxxxxx>