/
set_union.pm6
173 lines (162 loc) Β· 6.16 KB
/
set_union.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
171
172
173
# This file implements the following set operators:
# (|) union (ASCII)
# βͺ union
proto sub infix:<(|)>(|) is pure {*}
multi sub infix:<(|)>() { set() }
multi sub infix:<(|)>(QuantHash:D $a) { $a } # Set/Bag/Mix
multi sub infix:<(|)>(SetHash:D $a) { $a.Set }
multi sub infix:<(|)>(BagHash:D $a) { $a.Bag }
multi sub infix:<(|)>(MixHash:D $a) { $a.Mix }
multi sub infix:<(|)>(Any $a) { $a.Set } # also for Iterable/Map
multi sub infix:<(|)>(Setty:D $a, Setty:D $b) {
nqp::if(
(my $araw := $a.RAW-HASH) && nqp::elems($araw),
nqp::if( # first has elems
(my $braw := $b.RAW-HASH) && nqp::elems($braw),
nqp::stmts( # second has elems
(my $elems := nqp::clone($araw)),
(my $iter := nqp::iterator($braw)),
nqp::while( # loop over keys of second
$iter,
nqp::bindkey( # bind into clone of first
$elems,
nqp::iterkey_s(nqp::shift($iter)),
nqp::iterval($iter)
)
),
nqp::create(Set).SET-SELF($elems) # make it a Set
),
$a.Set # no second, so first
),
nqp::if( # no first
($braw := $b.RAW-HASH) && nqp::elems($braw),
$b.Set, # but second
set() # both empty
)
)
}
multi sub infix:<(|)>(Setty:D $a, Mixy:D $b) { $a.Mix (|) $b }
multi sub infix:<(|)>(Setty:D $a, Baggy:D $b) { $a.Bag (|) $b }
multi sub infix:<(|)>(Mixy:D $a, Mixy:D $b) {
nqp::if(
(my $araw := $a.RAW-HASH) && nqp::elems($araw),
nqp::if( # first has elems
(my $braw := $b.RAW-HASH) && nqp::elems($braw),
nqp::stmts( # second has elems
(my $elems := nqp::clone($araw)),
(my $iter := nqp::iterator($braw)),
nqp::while( # loop over keys of second
$iter,
nqp::if(
nqp::existskey(
$araw,
(my $key := nqp::iterkey_s(nqp::shift($iter)))
),
nqp::if( # must use HLL < because values can be bignums
nqp::getattr(
nqp::decont(nqp::atkey($araw,$key)),Pair,'$!value')
< nqp::getattr(
nqp::decont(nqp::atkey($braw,$key)),Pair,'$!value'),
nqp::bindkey($elems,$key,nqp::atkey($braw,$key))
),
nqp::bindkey($elems,$key,nqp::atkey($braw,$key))
)
),
nqp::create(Mix).SET-SELF($elems) # make it a Mix
),
$a.Mix # no second, so first
),
nqp::if( # no first
($braw := $b.RAW-HASH) && nqp::elems($braw),
$b.Mix, # but second
mix() # both empty
)
)
}
multi sub infix:<(|)>(Mixy:D $a, Baggy:D $b) { $a (|) $b.Mix }
multi sub infix:<(|)>(Mixy:D $a, Setty:D $b) { $a (|) $b.Mix }
multi sub infix:<(|)>(Baggy:D $a, Mixy:D $b) { $a.Mix (|) $b }
multi sub infix:<(|)>(Baggy:D $a, Baggy:D $b) {
nqp::if(
(my $araw := $a.RAW-HASH) && nqp::elems($araw),
nqp::if( # first has elems
(my $braw := $b.RAW-HASH) && nqp::elems($braw),
nqp::stmts( # second has elems
(my $elems := nqp::clone($araw)),
(my $iter := nqp::iterator($braw)),
nqp::while( # loop over keys of second
$iter,
nqp::if(
nqp::existskey(
$araw,
(my $key := nqp::iterkey_s(nqp::shift($iter)))
),
nqp::if(
nqp::islt_i(
nqp::getattr(
nqp::decont(nqp::atkey($araw,$key)),Pair,'$!value'),
nqp::getattr(
nqp::decont(nqp::atkey($braw,$key)),Pair,'$!value')
),
nqp::bindkey($elems,$key,nqp::atkey($braw,$key))
),
nqp::bindkey($elems,$key,nqp::atkey($braw,$key))
)
),
nqp::create(Bag).SET-SELF($elems) # make it a Bag
),
$a.Bag # no second, so first
),
nqp::if( # no first
($braw := $b.RAW-HASH) && nqp::elems($braw),
$b.Bag, # but second
bag() # both empty
)
)
}
multi sub infix:<(|)>(Baggy:D $a, Setty:D $b) { $a (|) $b.Bag }
multi sub infix:<(|)>(Map:D $a, Map:D $b) {
nqp::create(Set).SET-SELF(
Rakudo::QuantHash.ADD-MAP-TO-SET(
Rakudo::QuantHash.COERCE-MAP-TO-SET($a),
$b
)
)
}
multi sub infix:<(|)>(Iterable:D $a, Iterable:D $b) {
nqp::if(
(my $aiterator := $a.flat.iterator).is-lazy
|| (my $biterator := $b.flat.iterator).is-lazy,
Failure.new(X::Cannot::Lazy.new(:action<union>,:what<set>)),
nqp::create(Set).SET-SELF(
Rakudo::QuantHash.ADD-PAIRS-TO-SET(
Rakudo::QuantHash.ADD-PAIRS-TO-SET(
nqp::create(Rakudo::Internals::IterationSet),
$aiterator
),
$biterator
)
)
)
}
multi sub infix:<(|)>(Failure:D $a, Any $b) { $a.throw }
multi sub infix:<(|)>(Any $a, Failure:D $b) { $b.throw }
multi sub infix:<(|)>(Any $a, Any $b) {
nqp::if(
nqp::istype($a,Mixy) || nqp::istype($b,Mixy),
infix:<(|)>($a.Mix, $b.Mix),
nqp::if(
nqp::istype($a,Baggy) || nqp::istype($b,Baggy),
infix:<(|)>($a.Bag, $b.Bag),
infix:<(|)>($a.Set, $b.Set)
)
)
}
multi sub infix:<(|)>(**@p) {
my $result = @p.shift;
$result = $result (|) @p.shift while @p;
$result
}
# U+222A UNION
my constant &infix:<βͺ> := &infix:<(|)>;
# vim: ft=perl6 expandtab sw=4