/
MixHash.pm6
216 lines (205 loc) Β· 7.05 KB
/
MixHash.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
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
my class MixHash does Mixy {
#--- interface methods
method total() { Rakudo::QuantHash.MIX-TOTAL($!elems) }
method !total-positive() { Rakudo::QuantHash.MIX-TOTAL-POSITIVE($!elems) }
method STORE(*@pairs --> MixHash:D) {
nqp::if(
(my $iterator := @pairs.iterator).is-lazy,
Failure.new(X::Cannot::Lazy.new(:action<initialize>,:what(self.^name))),
self.SET-SELF(
Rakudo::QuantHash.ADD-PAIRS-TO-MIX(
nqp::create(Rakudo::Internals::IterationSet), $iterator
)
)
)
}
multi method AT-KEY(MixHash:D: \k) is raw {
Proxy.new(
FETCH => {
nqp::if(
$!elems && nqp::existskey($!elems,(my $which := k.WHICH)),
nqp::getattr(nqp::atkey($!elems,$which),Pair,'$!value'),
0
)
},
STORE => -> $, Real() $value {
nqp::if(
nqp::istype($value,Failure), # RT 128927
$value.throw,
nqp::if(
$!elems,
nqp::if( # allocated hash
nqp::existskey($!elems,(my $which := k.WHICH)),
nqp::if( # existing element
$value == 0,
nqp::stmts(
nqp::deletekey($!elems,$which),
0
),
nqp::bindattr(
nqp::atkey($!elems,$which),
Pair,
'$!value',
nqp::decont($value)
),
),
nqp::unless(
$value == 0,
nqp::bindkey($!elems,$which,Pair.new(k,nqp::decont($value)))
)
),
nqp::unless( # no hash allocated yet
$value == 0,
nqp::bindkey(
nqp::bindattr(self,::?CLASS,'$!elems',
nqp::create(Rakudo::Internals::IterationSet)),
k.WHICH,
Pair.new(k,nqp::decont($value))
)
)
)
)
}
)
}
#--- object creation methods
multi method new(MixHash:_:) { nqp::create(self) }
#--- coercion methods
multi method Mix(MixHash:D: :$view) {
nqp::if(
$!elems && nqp::elems($!elems),
nqp::p6bindattrinvres(
nqp::create(Mix),Mix,'$!elems',
nqp::if($view,$!elems,$!elems.clone)
),
mix()
)
}
multi method MixHash(MixHash:D:) { self }
method clone() {
nqp::if(
$!elems && nqp::elems($!elems),
nqp::create(MixHash).SET-SELF(Rakudo::QuantHash.BAGGY-CLONE($!elems)),
nqp::create(MixHash)
)
}
#--- 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(
(my $which := nqp::iterkey_s(iter)),
# save for possible object recreation
(my $object := nqp::getattr(nqp::iterval(iter),Pair,'$!key')),
Proxy.new(
FETCH => {
nqp::if(
nqp::existskey(storage,$which),
nqp::getattr(nqp::atkey(storage,$which),Pair,'$!value'),
0
)
},
STORE => -> $, Real() $value {
nqp::if(
nqp::istype($value,Failure), # RT 128927
$value.throw,
nqp::if(
nqp::existskey(storage,$which),
nqp::if( # existing element
$value == 0,
nqp::stmts( # goodbye!
nqp::deletekey(storage,$which),
0
),
nqp::bindattr( # value ok
nqp::atkey(storage,$which),
Pair,
'$!value',
nqp::decont($value)
)
),
nqp::unless( # where did it go?
$value == 0,
nqp::bindkey(
storage,
$which,
Pair.new($object,nqp::decont($value))
)
)
)
)
}
)
)
}
multi method iterator(MixHash:D:) { # also .pairs
class :: does Rakudo::Iterator::Mappy {
method pull-one() is raw {
nqp::if(
$!iter,
nqp::p6bindattrinvres(
nqp::clone(nqp::iterval(nqp::shift($!iter))),
Pair,
'$!value',
proxy($!iter,$!hash)
),
IterationEnd
)
}
method push-all($target --> IterationEnd) {
nqp::while( # doesn't sink
$!iter,
$target.push(nqp::iterval(nqp::shift($!iter)))
)
}
}.new($!elems)
}
multi method values(MixHash:D:) {
Seq.new(class :: does Rakudo::Iterator::Mappy {
method pull-one() is raw {
nqp::if(
$!iter,
proxy(nqp::shift($!iter),$!hash),
IterationEnd
)
}
# same as Baggy.values
method push-all($target --> IterationEnd) {
nqp::while( # doesn't sink
$!iter,
$target.push(nqp::getattr(
nqp::iterval(nqp::shift($!iter)),Pair,'$!value'))
)
}
}.new($!elems))
}
multi method kv(MixHash: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,$!hash)
),
nqp::if(
$!iter,
nqp::stmts(
($!on = 1),
nqp::getattr(
nqp::iterval(nqp::shift($!iter)),Pair,'$!key')
),
IterationEnd
)
)
}
}.new($!elems))
}
}
# vim: ft=perl6 expandtab sw=4