/
Hash.pm
199 lines (187 loc) Β· 7.53 KB
/
Hash.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
my class Hash {
# Has attributes and parent EnumMap declared in BOOTSTRAP
method new(*@args) { @args.hash }
method at_key($key is copy) is rw {
my Mu $storage := pir::defined(nqp::getattr(self, EnumMap, '$!storage')) ??
nqp::getattr(self, EnumMap, '$!storage') !!
nqp::bindattr(self, EnumMap, '$!storage', pir::new__Ps('Hash'));
$key = $key.Str;
nqp::existskey($storage, nqp::unbox_s($key))
?? nqp::atkey($storage, nqp::unbox_s($key))
!! pir::setattribute__0PPsP(my $v, Scalar, '$!whence',
-> { nqp::bindkey($storage, nqp::unbox_s($key), $v) } )
}
method bind_key($key, \$bindval) is rw {
pir::defined(nqp::getattr(self, EnumMap, '$!storage')) ||
nqp::bindattr(self, EnumMap, '$!storage', pir::new__Ps('Hash'));
nqp::bindkey(
nqp::getattr(self, EnumMap, '$!storage'),
nqp::unbox_s($key.Str),
$bindval)
}
multi method perl(Hash:D \$self:) {
nqp::iscont($self)
?? '{' ~ self.pairs.map({.perl}).join(', ') ~ '}'
!! '(' ~ self.pairs.map({.perl}).join(', ') ~ ').hash'
}
method STORE_AT_KEY(Str \$key, Mu $x is copy) is rw {
pir::find_method__PPs(EnumMap, 'STORE_AT_KEY')(self, $key, $x);
}
method STORE(\$to_store) is hidden_from_backtrace {
my $items = ($to_store,).flat.eager;
nqp::bindattr(self, EnumMap, '$!storage', pir::new__Ps('Hash'));
while $items {
my Mu $x := $items.shift;
if Enum.ACCEPTS($x) { self.STORE_AT_KEY($x.key.Str, $x.value) }
elsif EnumMap.ACCEPTS($x) {
for $x.list { self.STORE_AT_KEY(.key.Str, .value) }
}
elsif $items { self.STORE_AT_KEY($x.Str, $items.shift) }
else {
die 'Odd number of elements found where hash expected'
}
}
self
}
proto method delete(|$) { * }
multi method delete($key as Str) {
my Mu $val = self.at_key($key);
pir::delete(
nqp::getattr(self, EnumMap, '$!storage'),
nqp::unbox_s($key)
);
$val;
}
multi method delete(@keys) {
@keys.map({ self.delete($^key) })
}
multi method delete(*@keys) {
@keys.map({ self.delete($^key) })
}
method push(*@values) {
my $previous;
my $has_previous;
for @values -> $e {
if $has_previous {
self!_push_construct($previous, $e);
$has_previous = 0;
} elsif $e.^isa(Enum) {
self!_push_construct($e.key, $e.value);
} else {
$previous = $e;
$has_previous = 1;
}
}
if $has_previous {
warn "Trailing item in Hash.push";
}
self
}
# push a value onto a hash slot, constructing an array if necessary
method !_push_construct(Mu $key, Mu $value) {
if self.exists($key) {
if self.{$key}.^isa(Array) {
self.{$key}.push($value);
} else {
my Mu $tmp = self.{$key};
self.{$key} = [ $tmp, $value];
}
} else {
self.{$key} = $value;
}
}
my role TypedHash[::TValue] does Associative[TValue] {
method at_key($key is copy, TValue $v? is copy) is rw {
$key = $key.Str;
self.exists($key)
?? pir::find_method__PPs(EnumMap, 'at_key')(self, $key)
!! pir::setattribute__0PPsP($v, Scalar, '$!whence',
-> { pir::find_method__PPs(EnumMap, 'STORE_AT_KEY')(self, $key, $v) } )
}
method STORE_AT_KEY(Str \$key, TValue $x is copy) is rw {
pir::find_method__PPs(EnumMap, 'STORE_AT_KEY')(self, $key, $x);
}
method bind_key($key, TValue \$bindval) is rw {
pir::defined(nqp::getattr(self, EnumMap, '$!storage')) ||
nqp::bindattr(self, EnumMap, '$!storage', pir::new__Ps('Hash'));
nqp::bindkey(
nqp::getattr(self, EnumMap, '$!storage'),
nqp::unbox_s($key.Str),
$bindval)
}
}
my role TypedHash[::TValue, ::TKey] does Associative[TValue] {
has $!keys;
method at_key(TKey \$key, TValue $v? is copy) is rw {
my $key_which = $key.WHICH;
self.exists($key_which)
?? pir::find_method__PPs(EnumMap, 'at_key')(self, $key_which)
!! pir::setattribute__0PPsP($v, Scalar, '$!whence',
-> {
pir::defined(nqp::getattr(self, $?CLASS, '$!keys')) ||
nqp::bindattr(self, $?CLASS, '$!keys', pir::new__Ps('Hash'));
pir::defined(nqp::getattr(self, EnumMap, '$!storage')) ||
nqp::bindattr(self, EnumMap, '$!storage', pir::new__Ps('Hash'));
nqp::bindkey(
nqp::getattr(self, $?CLASS, '$!keys'),
nqp::unbox_s($key_which),
$key);
nqp::bindkey(
nqp::getattr(self, EnumMap, '$!storage'),
nqp::unbox_s($key_which),
$v);
})
}
method STORE_AT_KEY(TKey \$key, TValue $x is copy) is rw {
my $key_which = $key.WHICH;
pir::defined(nqp::getattr(self, $?CLASS, '$!keys')) ||
nqp::bindattr(self, $?CLASS, '$!keys', pir::new__Ps('Hash'));
pir::defined(nqp::getattr(self, EnumMap, '$!storage')) ||
nqp::bindattr(self, EnumMap, '$!storage', pir::new__Ps('Hash'));
nqp::bindkey(
nqp::getattr(self, $?CLASS, '$!keys'),
nqp::unbox_s($key_which),
$key);
nqp::bindkey(
nqp::getattr(self, EnumMap, '$!storage'),
nqp::unbox_s($key_which),
$x);
}
method bind_key(TKey $key, TValue \$bindval) is rw {
my $key_which = $key.WHICH;
pir::defined(nqp::getattr(self, $?CLASS, '$!keys')) ||
nqp::bindattr(self, $?CLASS, '$!keys', pir::new__Ps('Hash'));
pir::defined(nqp::getattr(self, EnumMap, '$!storage')) ||
nqp::bindattr(self, EnumMap, '$!storage', pir::new__Ps('Hash'));
nqp::bindkey(
nqp::getattr(self, $?CLASS, '$!keys'),
nqp::unbox_s($key_which),
$key);
nqp::bindkey(
nqp::getattr(self, EnumMap, '$!storage'),
nqp::unbox_s($key_which),
$bindval)
}
method pairs() {
return unless pir::defined(nqp::getattr(self, EnumMap, '$!storage'));
gather {
my Mu $iter := nqp::iterator(nqp::getattr(self, EnumMap, '$!storage'));
my Mu $pair;
my Mu $key;
while $iter {
$pair := nqp::shift($iter);
$key := nqp::atkey(nqp::getattr(self, $?CLASS, '$!keys'), $pair.key);
take Pair.new(:key($key), :value($pair.value));
}
Nil
}
}
}
method PARAMETERIZE_TYPE(Mu $t, |$c) {
$c.elems ??
self but TypedHash[$t.WHAT, $c[0]] !!
self but TypedHash[$t.WHAT]
}
}
sub circumfix:<{ }>(*@elems) { my $x = Hash.new.STORE(@elems); }
sub hash(*@a, *%h) { my % = @a, %h }