/
Order.pm6
170 lines (155 loc) · 4.79 KB
/
Order.pm6
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
## Order enumeration, for cmp and <=>
my enum Order (:Less(-1), :Same(0), :More(1));
role Rational { ... }
sub ORDER(int $i --> Order) is implementation-detail {
$i
?? nqp::islt_i($i,0)
?? Less
!! More
!! Same
}
proto sub infix:<cmp>($, $, *% --> Order:D) is pure {*}
multi sub infix:<cmp>(\a, \b) {
nqp::eqaddr(nqp::decont(a), nqp::decont(b))
?? Same
!! a.Stringy cmp b.Stringy
}
multi sub infix:<cmp>(Real:D $a, \b) {
$a === -Inf
?? Less
!! $a === Inf
?? More
!! $a.Stringy cmp b.Stringy
}
multi sub infix:<cmp>(\a, Real:D $b) {
$b === Inf
?? Less
!! $b === -Inf
?? More
!! a.Stringy cmp $b.Stringy
}
multi sub infix:<cmp>(Real:D $a, Real:D $b) {
nqp::istype($a,Rational) && nqp::istype($b,Rational)
?? $a.isNaN || $b.isNaN
?? $a.Num cmp $b.Num
!! $a <=> $b
!! (nqp::istype($a, Rational) && nqp::isfalse($a.denominator))
|| (nqp::istype($b, Rational) && nqp::isfalse($b.denominator))
?? $a.Bridge cmp $b.Bridge
!! $a === -Inf || $b === Inf
?? Less
!! $a === Inf || $b === -Inf
?? More
!! $a.Bridge cmp $b.Bridge
}
multi sub infix:<cmp>(Int:D $a, Rational:D $b) {
$a.isNaN || $b.isNaN ?? $a.Num cmp $b.Num !! $a <=> $b
}
multi sub infix:<cmp>(Rational:D $a, Int:D $b) {
$a.isNaN || $b.isNaN ?? $a.Num cmp $b.Num !! $a <=> $b
}
multi sub infix:<cmp>(Int:D $a, Int:D $b) {
ORDER(nqp::cmp_I($a,$b))
}
multi sub infix:<cmp>(int $a, int $b) {
ORDER(nqp::cmp_i($a, $b))
}
multi sub infix:<cmp>(Code:D $a, Code:D $b) {
$a.name cmp $b.name
}
multi sub infix:<cmp>(Code:D $a, \b) {
$a.name cmp b.Stringy
}
multi sub infix:<cmp>(\a, Code:D $b) {
a.Stringy cmp $b.name
}
multi sub infix:<cmp>(List:D \a, List:D \b) {
nqp::if(
a.is-lazy || b.is-lazy,
infix:<cmp>(a.iterator, b.iterator),
nqp::stmts(
(my int $elems-a = a.elems), # reifies
(my int $elems-b = b.elems), # reifies
(my $list-a := nqp::getattr(nqp::decont(a),List,'$!reified')),
(my $list-b := nqp::getattr(nqp::decont(b),List,'$!reified')),
nqp::if(
(my int $elems = nqp::if(
nqp::islt_i($elems-a,$elems-b),
$elems-a,
$elems-b
)),
nqp::stmts( # elements to compare
(my $i = -1),
nqp::while(
nqp::islt_i(++$i,$elems)
&& nqp::eqaddr(
(my $order := infix:<cmp>(
nqp::atpos($list-a,$i),
nqp::atpos($list-b,$i)
)),
Same
),
nqp::null
),
nqp::if(
nqp::eqaddr($order,Same),
ORDER(nqp::cmp_i($elems-a,$elems-b)), # same, length significant
$order # element different
)
),
ORDER(nqp::cmp_i($elems-a,$elems-b)), # only length significant
)
)
)
}
multi sub infix:<cmp>(
Iterator:D \iter-a, Iterator:D \iter-b
) is implementation-detail {
nqp::until(
nqp::eqaddr((my $a := iter-a.pull-one),IterationEnd)
|| nqp::eqaddr((my $b := iter-b.pull-one),IterationEnd)
|| nqp::not_i(nqp::eqaddr(
(my $order := infix:<cmp>($a,$b)),
Same
)),
nqp::null
);
nqp::if(
nqp::eqaddr($order,Same), # ended because different?
nqp::if(
nqp::eqaddr($a,IterationEnd), # left exhausted?
nqp::if(
nqp::eqaddr(iter-b.pull-one,IterationEnd), # right exhausted?
Same,
Less
),
More
),
$order
)
}
proto sub infix:«<=>»($, $, *% --> Order:D) is pure {*}
multi sub infix:«<=>»(\a, \b) { a.Real <=> b.Real }
multi sub infix:«<=>»(Real $a, Real $b) { $a.Bridge <=> $b.Bridge }
multi sub infix:«<=>»(Int:D $a, Int:D $b) {
ORDER(nqp::cmp_I($a,$b))
}
multi sub infix:«<=>»(int $a, int $b) {
ORDER(nqp::cmp_i($a, $b))
}
proto sub infix:<before>($?, $?, *% --> Bool:D) is pure {*}
multi sub infix:<before>($? --> True) { }
multi sub infix:<before>($a, $b) {
nqp::hllbool(nqp::eqaddr(($a cmp $b),Order::Less))
}
proto sub infix:<after>($?, $?, *% --> Bool:D) is pure {*}
multi sub infix:<after>($x? --> True) { }
multi sub infix:<after>($a, $b) {
nqp::hllbool(nqp::eqaddr(($a cmp $b),Order::More))
}
proto sub infix:<leg>($, $, *% --> Order:D) is pure {*}
multi sub infix:<leg>(\a, \b) { a.Stringy cmp b.Stringy }
proto sub infix:<unicmp>($, $, *% --> Order:D) is pure {*}
# NOT is pure because of $*COLLATION
proto sub infix:<coll>( $, $, *% --> Order:D) {*}
# vim: expandtab shiftwidth=4