/
MixHash.pm
45 lines (42 loc) · 1.41 KB
/
MixHash.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
my class MixHash does Mixy {
#--- interface methods
multi method AT-KEY(MixHash:D: \k) is raw {
Proxy.new(
FETCH => {
my $hash := nqp::getattr(%!elems,Map,'$!storage');
my str $which = nqp::unbox_s(k.WHICH);
nqp::existskey($hash,$which)
?? nqp::getattr(nqp::decont(nqp::atkey($hash,$which)),Pair,'$!value')
!! 0
},
STORE => -> $, $value is copy {
my $hash := nqp::getattr(%!elems,Map,'$!storage');
my str $which = nqp::unbox_s(k.WHICH);
if nqp::existskey($hash,$which) {
$value == 0
?? nqp::deletekey($hash,$which)
!! (nqp::getattr(nqp::decont(nqp::atkey($hash,$which)),Pair,'$!value') = $value);
}
elsif $value {
nqp::bindkey($hash,$which,self!PAIR(k,$value));
}
$value;
}
);
}
#--- coercion methods
method Mix(:$view) {
if $view {
my \mix = nqp::create(Mix);
nqp::bindattr(mix,Mix,'%!elems',%!elems);
mix
}
else {
Mix.new-from-pairs(%!elems.values)
}
}
method MixHash { self }
method Bag { Bag.new-from-pairs(%!elems.values) }
method BagHash { BagHash.new-from-pairs(%!elems.values) }
}
# vim: ft=perl6 expandtab sw=4