summaryrefslogtreecommitdiff
path: root/lib/std/sleq.myr
blob: 30f49d515eedc7b48166d356893d06a537beca42 (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use "types"
use "traits"
use "memops"

pkg std =
	impl equatable @a[:]
	impl equatable bool[:]
	impl equatable byte[:]
	impl equatable char[:]
	impl equatable int8[:]
	impl equatable int16[:]
	impl equatable int[:]
	impl equatable int32[:]
	impl equatable int64[:]
	impl equatable uint8[:]
	impl equatable uint16[:]
	impl equatable uint[:]
	impl equatable uint32[:]
	impl equatable uint64[:]

	generic sleq    : (a : @a[:], b : @a[:] -> bool)
;;

/* for compatibility */
generic sleq = {a, b
	-> eq(a, b)
}

impl equatable @a[:] =
	eq = {a, b
		if a.len != b.len
			-> false
		;;
		/* can't use memeq because of padding */
		for var i = 0; i < a.len; i++
			if !eq(a[i], b[i])
				-> false
			;;
		;;
		-> true
	}
;;

impl equatable bool[:] = 
	eq = {a, b; -> primsleq((a : byte#), (b : byte#), a.len, b.len, sizeof(bool))}
;;
impl equatable byte[:] = 
	eq = {a, b; -> primsleq((a : byte#), (b : byte#), a.len, b.len, sizeof(byte))}
;;
impl equatable char[:] = 
	eq = {a, b; -> primsleq((a : byte#), (b : byte#), a.len, b.len, sizeof(char))}
;;
impl equatable int8[:] = 
	eq = {a, b; -> primsleq((a : byte#), (b : byte#), a.len, b.len, sizeof(int8))}
;;
impl equatable int16[:] = 
	eq = {a, b; -> primsleq((a : byte#), (b : byte#), a.len, b.len, sizeof(int16))}
;;
impl equatable int[:] = 
	eq = {a, b; -> primsleq((a : byte#), (b : byte#), a.len, b.len, sizeof(int))}
;;
impl equatable int32[:] = 
	eq = {a, b; -> primsleq((a : byte#), (b : byte#), a.len, b.len, sizeof(int32))}
;;
impl equatable int64[:] = 
	eq = {a, b; -> primsleq((a : byte#), (b : byte#), a.len, b.len, sizeof(int64))}
;;
impl equatable uint8[:] = 
	eq = {a, b; -> primsleq((a : byte#), (b : byte#), a.len, b.len, sizeof(uint8))}
;;
impl equatable uint16[:] = 
	eq = {a, b; -> primsleq((a : byte#), (b : byte#), a.len, b.len, sizeof(uint16))}
;;
impl equatable uint[:] = 
	eq = {a, b; -> primsleq((a : byte#), (b : byte#), a.len, b.len, sizeof(uint))}
;;
impl equatable uint32[:] = 
	eq = {a, b; -> primsleq((a : byte#), (b : byte#), a.len, b.len, sizeof(uint32))}
;;

impl equatable uint64[:] = 
	eq = {a, b; -> primsleq((a : byte#), (b : byte#), a.len, b.len, sizeof(uint64))}
;;

const primsleq = {a, b, na, nb, sz
	if na == nb
		-> memeq((a : byte#), (b : byte#), na * sz)
	else
		-> false
	;;
}