[PATCH 1/2] Add comparable and hashable traits
[Thread Prev] | [Thread Next]
- Subject: [PATCH 1/2] Add comparable and hashable traits
- From: Lucas Gabriel Vuotto <lvuotto92@xxxxxxxxx>
- Reply-to: myrddin-dev@xxxxxxxxxxxxxx
- Date: Wed, 1 Nov 2017 16:53:25 -0300
- To: myrddin-dev@xxxxxxxxxxxxxx
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
| RFC: comparable and hashable traits | Lucas Gabriel Vuotto <lvuotto92@xxxxxxxxx> |
- Prev by Date: RFC: comparable and hashable traits
- Next by Date: [PATCH 2/2] Use new traits to implement hash tables
- Previous by thread: RFC: comparable and hashable traits
- Next by thread: [PATCH 2/2] Use new traits to implement hash tables
- Index(es):