-
-
Notifications
You must be signed in to change notification settings - Fork 372
/
Iterable.pm6
170 lines (158 loc) Β· 6.16 KB
/
Iterable.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
# Iterable is done by anything that we should be able to get an iterator
# from. Things that are Iterable will flatten in flattening contexts, so a
# default implementation of .flat is provided by this role. As itemization is
# what defeats flattening, this role also provides a default .item method.
# Additionally, as .lazy and .eager are about iterator behavior, they are
# provided by this role. Overriding those is not likely to be needed, and
# discouraged to maintain predictable semantics. Finally, both .hyper() and
# .race() are methods to enter the hyper and race paradigm and implemented
# here, so they can use any Iterable as a source.
my class HyperSeq { ... }
my class RaceSeq { ... }
my class Rakudo::Internals::HyperIteratorBatcher { ... }
my role Iterable {
method iterator() { ... }
method item() {
nqp::p6bindattrinvres(nqp::create(Scalar), Scalar, '$!value', self)
}
method flat(Iterable:D:) { Seq.new(Rakudo::Iterator.Flat(self.iterator)) }
method lazy-if($flag) { $flag ?? self.lazy !! self }
method lazy() {
# Return a Seq with an iterator wrapping this Iterable, claiming to
# be lazy, and implicitly preventing working ahead (by hiding any
# push-at-least-n of the source iterator).
Seq.new(Rakudo::Iterator.Lazy(self))
}
method hyper(
Int(Cool) :$batch = 64,
Int(Cool) :$degree = max(nqp::cpucores() - 1,1)
) {
#?if !js
HyperSeq.new:
configuration =>
HyperConfiguration.new(:$degree, :$batch, :method<hyper>),
work-stage-head =>
Rakudo::Internals::HyperIteratorBatcher.new(:$.iterator)
#?endif
#?if js
HyperSeq.new($.iterator)
#?endif
}
method race(
Int(Cool) :$batch = 64,
Int(Cool) :$degree = max(nqp::cpucores() - 1,1)
) {
#?if !js
RaceSeq.new:
configuration =>
HyperConfiguration.new(:$degree, :$batch, :method<race>),
work-stage-head =>
Rakudo::Internals::HyperIteratorBatcher.new(:$.iterator)
#?endif
#?if js
RaceSeq.new($.iterator)
#?endif
}
sub MIXIFY(\iterable, \type) {
(my \iterator := iterable.flat.iterator).is-lazy
?? Failure.new(X::Cannot::Lazy.new(:action<coerce>,:what(type.^name)))
!! nqp::elems(my \elems := Rakudo::QuantHash.ADD-PAIRS-TO-MIX(
nqp::create(Rakudo::Internals::IterationSet),iterator,Mu
))
?? nqp::create(type).SET-SELF(elems)
!! nqp::eqaddr(type,Mix)
?? mix()
!! nqp::create(type)
}
multi method Mix(Iterable:D:) { MIXIFY(self, Mix) }
multi method MixHash(Iterable:D:) { MIXIFY(self, MixHash) }
sub BAGGIFY(\iterable, \type) {
(my \iterator := iterable.flat.iterator).is-lazy
?? Failure.new(X::Cannot::Lazy.new(:action<coerce>,:what(type.^name)))
!! nqp::elems(my \elems := Rakudo::QuantHash.ADD-PAIRS-TO-BAG(
nqp::create(Rakudo::Internals::IterationSet),iterator,Mu
))
?? nqp::create(type).SET-SELF(elems)
!! nqp::eqaddr(type,Bag)
?? bag()
!! nqp::create(type)
}
multi method Bag(Iterable:D:) { BAGGIFY(self, Bag) }
multi method BagHash(Iterable:D:) { BAGGIFY(self, BagHash) }
sub SETIFY(\iterable, \type) {
(my \iterator := iterable.flat.iterator).is-lazy
?? Failure.new(X::Cannot::Lazy.new(:action<coerce>,:what(type.^name)))
!! nqp::elems(my $elems := Rakudo::QuantHash.ADD-PAIRS-TO-SET(
nqp::create(Rakudo::Internals::IterationSet),iterator,Mu
))
?? nqp::create(type).SET-SELF($elems)
!! nqp::eqaddr(type,Set)
?? set()
!! nqp::create(type)
}
multi method Set(Iterable:D:) { SETIFY(self,Set) }
multi method SetHash(Iterable:D:) { SETIFY(self,SetHash) }
}
multi sub infix:<eqv>(Iterable:D \a, Iterable:D \b) {
nqp::hllbool(
nqp::unless(
nqp::eqaddr(nqp::decont(a),nqp::decont(b)),
nqp::if( # not same object
nqp::eqaddr(a.WHAT,b.WHAT),
nqp::if( # same type
nqp::istype(a, Positional),
nqp::if( # Positional
a.is-lazy,
nqp::if( # a lazy
b.is-lazy,
die(X::Cannot::Lazy.new: :action<eqv>) # a && b lazy
),
nqp::if( # a NOT lazy
b.is-lazy,
0, # b lazy
nqp::if( # a && b NOT lazy
nqp::iseq_i((my int $elems = a.elems),b.elems),
nqp::stmts( # same # elems
(my int $i = -1),
nqp::while(
nqp::islt_i(($i = nqp::add_i($i,1)),$elems) # not exhausted
&& a.AT-POS($i) eqv b.AT-POS($i), # still same
nqp::null
),
nqp::iseq_i($i,$elems) # exhausted = success!
)
)
)
),
nqp::if( # NOT Positional
nqp::iseq_i(
(my \ia := a.iterator).is-lazy,
(my \ib := b.iterator).is-lazy
),
nqp::if(
ia.is-lazy,
die(X::Cannot::Lazy.new: :action<eqv>),
nqp::stmts(
nqp::until(
nqp::stmts(
(my \pa := ia.pull-one),
(my \pb := ib.pull-one),
nqp::eqaddr(pa,IterationEnd)
|| nqp::eqaddr(pb,IterationEnd)
|| nqp::not_i(pa eqv pb)
),
nqp::null
),
nqp::eqaddr(pa,pb) # both IterationEnd = success!
)
)
)
)
)
)
)
}
#?if jvm
nqp::p6setitertype(Iterable);
#?endif
# vim: expandtab shiftwidth=4