/
SetHash.pm
229 lines (216 loc) Β· 6.67 KB
/
SetHash.pm
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
my class SetHash does Setty {
method SET-SELF(\elems) {
nqp::stmts(
nqp::if(
nqp::elems(elems),
nqp::bindattr(self,::?CLASS,'$!elems',elems)
),
self
)
}
#--- selector methods
multi method grab(SetHash:D:) {
nqp::if(
$!elems,
nqp::stmts(
(my $object := nqp::iterval(
my $iter := Rakudo::QuantHash.ROLL($!elems)
)),
nqp::deletekey($!elems,nqp::iterkey_s($iter)),
$object
),
Nil
)
}
multi method grab(SetHash:D: Callable:D $calculate) {
self.grab($calculate(self.elems))
}
multi method grab(SetHash:D: Whatever) {
self.grab(Inf)
}
multi method grab(SetHash:D: $count) {
Seq.new(class :: does Rakudo::QuantHash::Pairs {
method pull-one() is raw {
nqp::if(
nqp::elems($!picked),
nqp::stmts(
(my $object := nqp::atkey(
$!elems,
(my $key := nqp::pop_s($!picked))
)),
nqp::deletekey($!elems,$key),
$object
),
IterationEnd
)
}
}.new($!elems, $count))
}
multi method grabpairs(SetHash:D:) {
Pair.new(self.grab,True)
}
multi method grabpairs(SetHash:D: Callable:D $calculate) {
self.grabpairs($calculate(self.elems))
}
multi method grabpairs(SetHash:D: Whatever) {
self.grabpairs(Inf)
}
multi method grabpairs(SetHash:D: $count) {
Seq.new(class :: does Rakudo::QuantHash::Pairs {
method pull-one() is raw {
nqp::if(
nqp::elems($!picked),
nqp::stmts(
(my $object := nqp::atkey(
$!elems,
(my $key := nqp::pop_s($!picked))
)),
nqp::deletekey($!elems,$key),
Pair.new($object,True)
),
IterationEnd
)
}
}.new($!elems, $count))
}
#--- iterator methods
sub proxy(Mu \iter,Mu \storage) is raw {
# We are only sure that the key exists when the Proxy
# is made, but we cannot be sure of its existence when
# either the FETCH or STORE block is executed. So we
# still need to check for existence, and handle the case
# where we need to (re-create) the key and value. The
# logic is therefore basically the same as in AT-KEY,
# except for tests for allocated storage and .WHICH
# processing.
nqp::stmts(
# save object for potential recreation
(my $object := nqp::iterval(iter)),
Proxy.new(
FETCH => {
nqp::p6bool(nqp::existskey(storage,nqp::iterkey_s(iter)))
},
STORE => -> $, $value {
nqp::stmts(
nqp::if(
$value,
nqp::bindkey(storage,nqp::iterkey_s(iter),$object),
nqp::deletekey(storage,nqp::iterkey_s(iter))
),
$value.Bool
)
}
)
)
}
method iterator(SetHash:D:) {
class :: does Rakudo::Iterator::Mappy {
method pull-one() {
nqp::if(
$!iter,
Pair.new(
nqp::iterval(nqp::shift($!iter)),
proxy($!iter,$!storage)
),
IterationEnd
)
}
}.new(self.hll_hash)
}
multi method kv(SetHash:D:) {
Seq.new(class :: does Rakudo::Iterator::Mappy-kv-from-pairs {
method pull-one() is raw {
nqp::if(
$!on,
nqp::stmts(
($!on = 0),
proxy($!iter,$!storage)
),
nqp::if(
$!iter,
nqp::stmts(
($!on = 1),
nqp::iterval(nqp::shift($!iter))
),
IterationEnd
)
)
}
method push-all($target --> IterationEnd) {
nqp::while(
$!iter,
nqp::stmts( # doesn't sink
$target.push(nqp::iterval(nqp::shift($!iter))),
$target.push(True)
)
)
}
}.new(self.hll_hash))
}
multi method values(SetHash:D:) {
Seq.new(class :: does Rakudo::Iterator::Mappy {
method pull-one() {
nqp::if(
$!iter,
proxy(nqp::shift($!iter),$!storage),
IterationEnd
)
}
}.new(self.hll_hash))
}
#--- coercion methods
multi method Set(SetHash:D: :$view) {
nqp::if(
$!elems && nqp::elems($!elems),
nqp::create(Set).SET-SELF(nqp::if($view,$!elems,nqp::clone($!elems))),
nqp::create(Set)
)
}
multi method SetHash(SetHash:D:) { self }
method clone() {
nqp::if(
$!elems && nqp::elems($!elems),
nqp::create(self).SET-SELF(nqp::clone($!elems)),
nqp::create(self)
)
}
#--- interface methods
multi method AT-KEY(SetHash:D: \k --> Bool:D) is raw {
Proxy.new(
FETCH => {
nqp::p6bool($!elems && nqp::existskey($!elems,k.WHICH))
},
STORE => -> $, $value {
nqp::stmts(
nqp::if(
$value,
nqp::stmts(
nqp::unless(
$!elems,
# XXX for some reason, $!elems := nqp::create(...) doesn't work
# Type check failed in binding; expected NQPMu but got Rakudo::Internals::IterationSet
nqp::bindattr(self,::?CLASS,'$!elems',
nqp::create(Rakudo::Internals::IterationSet))
),
nqp::bindkey($!elems,k.WHICH,nqp::decont(k))
),
$!elems && nqp::deletekey($!elems,k.WHICH)
),
$value.Bool
)
}
)
}
multi method DELETE-KEY(SetHash:D: \k --> Bool:D) {
nqp::p6bool(
nqp::if(
$!elems && nqp::existskey($!elems,(my $which := k.WHICH)),
nqp::stmts(
nqp::deletekey($!elems,$which),
1
)
)
)
}
}
# vim: ft=perl6 expandtab sw=4