/
Hash.pm
452 lines (425 loc) Β· 16.1 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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
my class Hash { # declared in BOOTSTRAP
# my class Hash is EnumMap {
# has Mu $!descriptor;
multi method at_key(Hash:D: \key) is rw {
my Mu $storage := nqp::getattr(self, EnumMap, '$!storage');
$storage := nqp::bindattr(self, EnumMap, '$!storage', nqp::hash())
unless nqp::defined($storage);
my str $skey = nqp::istype(key, Str) ?? key !! key.Str;
if nqp::existskey($storage, $skey) {
nqp::atkey($storage, $skey);
}
else {
nqp::p6bindattrinvres(
(my \v := nqp::p6scalarfromdesc($!descriptor)),
Scalar,
'$!whence',
-> { nqp::bindkey($storage, $skey, v) }
);
}
}
multi method assign_key(Hash:D: \key, Mu \assignval) {
my Mu $storage := nqp::getattr(self, EnumMap, '$!storage');
$storage := nqp::bindattr(self, EnumMap, '$!storage', nqp::hash())
unless nqp::defined($storage);
my str $key = nqp::istype(key, Str) ?? key !! key.Str;
nqp::existskey($storage, $key)
?? (nqp::atkey($storage, $key) = assignval)
!! nqp::bindkey($storage, $key,
nqp::p6scalarfromdesc($!descriptor) = assignval)
}
method bind_key(Hash:D: \key, Mu \bindval) is rw {
my Mu $storage := nqp::getattr(self, EnumMap, '$!storage');
$storage := nqp::bindattr(self, EnumMap, '$!storage', nqp::hash())
unless nqp::defined($storage);
my str $key = nqp::istype(key, Str) ?? key !! key.Str;
nqp::bindkey($storage, $key, bindval)
}
multi method perl(Hash:D \SELF:) {
nqp::iscont(SELF)
?? '{' ~ SELF.pairs.pick(*).map({.perl}).join(', ') ~ '}'
!! '(' ~ SELF.pairs.pick(*).map({.perl}).join(', ') ~ ').hash'
}
multi method gist(Hash:D \SELF:) {
SELF.pairs.sort.map( -> $elem {
given ++$ {
when 101 { '...' }
when 102 { last }
default { $elem.gist }
}
} ).join: ', ';
}
multi method DUMP(Hash:D: :$indent-step = 4, :%ctx?) {
return DUMP(self, :$indent-step) unless %ctx;
my Mu $attrs := nqp::list();
nqp::push($attrs, '$!descriptor');
nqp::push($attrs, $!descriptor );
nqp::push($attrs, '$!storage' );
nqp::push($attrs, nqp::getattr(nqp::decont(self), EnumMap, '$!storage'));
self.DUMP-OBJECT-ATTRS($attrs, :$indent-step, :%ctx);
}
method STORE_AT_KEY(\key, Mu $x) is rw {
my $v := nqp::p6scalarfromdesc($!descriptor);
nqp::findmethod(EnumMap, 'STORE_AT_KEY')(self, key, $v = $x);
}
# introspection
method name() {
my $d := $!descriptor;
nqp::isnull($d) ?? Str !! $d.name()
}
method keyof () { Any }
method of() {
my $d := $!descriptor;
nqp::isnull($d) ?? Mu !! $d.of;
}
method default() {
my $d := $!descriptor;
nqp::isnull($d) ?? Mu !! $d.default;
}
method dynamic() {
my $d := $!descriptor;
nqp::isnull($d) ?? Mu !! so $d.dynamic;
}
proto method delete_key(|) { * }
multi method delete_key(Hash:U:) { Nil }
multi method delete_key(Str:D \key) {
my Mu $val = self.at_key(key);
nqp::deletekey(
nqp::getattr(self, EnumMap, '$!storage'),
nqp::unbox_s(key)
);
$val;
}
multi method delete_key(Str:D \key, :$SINK!) {
nqp::deletekey(
nqp::getattr(self, EnumMap, '$!storage'),
nqp::unbox_s(key)
);
Nil;
}
multi method delete_key(\key as Str) {
my Mu $val = self.at_key(key);
nqp::deletekey(
nqp::getattr(self, EnumMap, '$!storage'),
nqp::unbox_s(key)
);
$val;
}
multi method delete_key(\key as Str, :$SINK!) {
nqp::deletekey(
nqp::getattr(self, EnumMap, '$!storage'),
nqp::unbox_s(key)
);
Nil;
}
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
}
proto method classify-list(|) { * }
multi method classify-list( &test, *@list ) {
fail 'Cannot .classify an infinite list' if @list.infinite;
if @list {
# multi-level classify
if nqp::istype(test(@list[0]),List) {
for @list -> $l {
my @keys = test($l);
my $last := @keys.pop;
my $hash = self;
$hash = $hash{$_} //= self.new for @keys;
nqp::push(
nqp::p6listitems(nqp::decont($hash{$last} //= [])), $l );
}
}
# just a simple classify
else {
nqp::push(
nqp::p6listitems(nqp::decont(self{test $_} //= [])), $_ )
for @list;
}
}
self;
}
multi method classify-list( %test, *@list ) {
samewith( { %test{$^a} }, @list );
}
multi method classify-list( @test, *@list ) {
samewith( { @test[$^a] }, @list );
}
proto method categorize-list(|) { * }
multi method categorize-list( &test, *@list ) {
fail 'Cannot .categorize an infinite list' if @list.infinite;
if @list {
# multi-level categorize
if nqp::istype(test(@list[0])[0],List) {
for @list -> $l {
for test($l) -> $k {
my @keys = @($k);
my $last := @keys.pop;
my $hash = self;
$hash = $hash{$_} //= self.new for @keys;
nqp::push(
nqp::p6listitems(
nqp::decont($hash{$last} //= [])), $l );
}
}
}
# just a simple categorize
else {
for @list -> $l {
nqp::push(
nqp::p6listitems(nqp::decont(self{$_} //= [])), $l )
for test($l);
}
}
}
self;
}
multi method categorize-list( %test, *@list ) {
samewith( { %test{$^a} }, @list );
}
multi method categorize-list( @test, *@list ) {
samewith( { @test[$^a] }, @list );
}
# push a value onto a hash slot, constructing an array if necessary
method !_push_construct(Mu $key, Mu $value) {
if self.exists_key($key) {
if self.{$key}.^isa(Array) {
self.{$key}.push($value);
} else {
self.{$key} = [ self.{$key}, $value ];
}
} else {
self.{$key} = $value;
}
}
my role TypedHash[::TValue] does Associative[TValue] {
method at_key(::?CLASS:D: $key is copy) is rw {
$key = $key.Str;
if self.exists_key($key) {
nqp::findmethod(EnumMap, 'at_key')(self, $key);
}
else {
nqp::p6bindattrinvres(
(my \v := nqp::p6scalarfromdesc(nqp::getattr(self, Hash, '$!descriptor'))),
Scalar,
'$!whence',
-> { nqp::findmethod(EnumMap, 'STORE_AT_KEY')(self, $key, v) }
);
}
}
method STORE_AT_KEY(Str \key, TValue $x) is rw {
my $v :=
nqp::p6scalarfromdesc(nqp::getattr(self, Hash, '$!descriptor'));
nqp::findmethod(EnumMap, 'STORE_AT_KEY')(self, key, $v = $x);
}
multi method assign_key(::?CLASS:D: \key, TValue \assignval) {
my Mu $storage := nqp::getattr(self, EnumMap, '$!storage');
$storage := nqp::bindattr(self, EnumMap, '$!storage', nqp::hash())
unless nqp::defined($storage);
my str $key = nqp::istype(key, Str) ?? key !! key.Str;
if nqp::existskey($storage, $key) {
nqp::atkey($storage, $key) = assignval;
}
else {
nqp::bindkey($storage, $key,
nqp::p6scalarfromdesc(nqp::getattr(self, Hash, '$!descriptor')) = assignval)
}
}
method bind_key($key, TValue \bindval) is rw {
nqp::defined(nqp::getattr(self, EnumMap, '$!storage')) ||
nqp::bindattr(self, EnumMap, '$!storage', nqp::hash());
nqp::bindkey(
nqp::getattr(self, EnumMap, '$!storage'),
nqp::unbox_s($key.Str),
bindval)
}
multi method perl(::?CLASS:D \SELF:) {
'Hash['
~ TValue.perl
~ '].new('
~ self.pairs.pick(*).map({.perl}).join(', ')
~ ')';
}
}
my role TypedHash[::TValue, ::TKey] does Associative[TValue] {
has $!keys;
method keyof () { TKey }
method at_key(::?CLASS:D: TKey \key) is rw {
my $key_which = key.WHICH;
if self.exists_key(key) {
nqp::findmethod(EnumMap, 'at_key')(self, $key_which);
}
else {
nqp::p6bindattrinvres(
(my \v := nqp::p6scalarfromdesc(nqp::getattr(self, Hash, '$!descriptor'))),
Scalar,
'$!whence',
-> {
nqp::defined(nqp::getattr(self, $?CLASS, '$!keys')) ||
nqp::bindattr(self, $?CLASS, '$!keys', nqp::hash());
nqp::defined(nqp::getattr(self, EnumMap, '$!storage')) ||
nqp::bindattr(self, EnumMap, '$!storage', nqp::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 rw {
my $key_which = key.WHICH;
nqp::defined(nqp::getattr(self, $?CLASS, '$!keys')) ||
nqp::bindattr(self, $?CLASS, '$!keys', nqp::hash());
nqp::defined(nqp::getattr(self, EnumMap, '$!storage')) ||
nqp::bindattr(self, EnumMap, '$!storage', nqp::hash());
nqp::bindkey(
nqp::getattr(self, $?CLASS, '$!keys'),
nqp::unbox_s($key_which),
key);
my $v :=
nqp::p6scalarfromdesc(nqp::getattr(self, Hash, '$!descriptor'));
nqp::bindkey(
nqp::getattr(self, EnumMap, '$!storage'),
nqp::unbox_s($key_which),
$v = $x);
}
method assign_key(::?CLASS:D: TKey \key, TValue \assignval) {
my Mu $storage := nqp::getattr(self, EnumMap, '$!storage');
$storage := nqp::bindattr(self, EnumMap, '$!storage', nqp::hash())
unless nqp::defined($storage);
my str $key_which = nqp::unbox_s(key.WHICH);
if nqp::existskey($storage, $key_which) {
nqp::atkey($storage, $key_which) = assignval;
}
else {
nqp::defined(nqp::getattr(self, $?CLASS, '$!keys')) ||
nqp::bindattr(self, $?CLASS, '$!keys', nqp::hash());
nqp::bindkey(nqp::getattr(self, $?CLASS, '$!keys'), $key_which, key);
nqp::bindkey($storage, $key_which,
nqp::p6scalarfromdesc(nqp::getattr(self, Hash, '$!descriptor')) = assignval)
}
}
method bind_key(TKey \key, TValue \bindval) is rw {
my $key_which = key.WHICH;
nqp::defined(nqp::getattr(self, $?CLASS, '$!keys')) ||
nqp::bindattr(self, $?CLASS, '$!keys', nqp::hash());
nqp::defined(nqp::getattr(self, EnumMap, '$!storage')) ||
nqp::bindattr(self, EnumMap, '$!storage', nqp::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 exists_key(TKey \key) {
nqp::defined($!keys)
?? nqp::p6bool(nqp::existskey($!keys, nqp::unbox_s(key.WHICH)))
!! False
}
method keys(EnumMap:) {
return unless self.DEFINITE && nqp::defined($!keys);
HashIter.keys(self,$!keys).list
}
method kv(EnumMap:) {
return unless self.DEFINITE && nqp::defined($!keys);
HashIter.kv(self,$!keys).list
}
method values(EnumMap:) {
return unless self.DEFINITE && nqp::defined($!keys);
HashIter.values(self,$!keys).list
}
method pairs(EnumMap:) {
return unless self.DEFINITE && nqp::defined($!keys);
HashIter.pairs(self,$!keys).list
}
method invert(EnumMap:) {
return unless self.DEFINITE && nqp::defined($!keys);
HashIter.invert(self,$!keys).list
}
multi method perl(::?CLASS:D \SELF:) {
if TKey === Any and TValue === Any and nqp::iscont(SELF) {
':{' ~ SELF.pairs.pick(*).map({.perl}).join(', ') ~ '}'
}
else {
'Hash['
~ TValue.perl
~ ','
~ TKey.perl
~ '].new('
~ self.pairs.pick(*).map({.perl}).join(', ')
~ ')';
}
}
multi method delete_key($key) {
my Mu $val = self.at_key($key);
my $key-which = $key.WHICH;
nqp::deletekey(
nqp::getattr(self, $?CLASS, '$!keys'),
nqp::unbox_s($key-which)
);
nqp::deletekey(
nqp::getattr(self, EnumMap, '$!storage'),
nqp::unbox_s($key-which)
);
$val;
}
# gotta force capture keys to strings or binder fails
method Capture(EnumMap:D:) {
my $cap := nqp::create(Capture);
my $h := nqp::hash();
for self.kv -> \k, \v {
my str $skey = nqp::istype(k, Str) ?? k !! k.Str;
nqp::bindkey($h, $skey, v);
}
nqp::bindattr($cap, Capture, '$!hash', $h);
$cap
}
}
method PARAMETERIZE_TYPE(Mu $t, |c) {
if c.elems == 0 {
# my $what := self but TypedHash[$t.WHAT]; # too early in bootstrap
my $what := self.HOW.mixin(self.WHAT, TypedHash[$t.WHAT]);
# needs to be done in COMPOSE phaser when that works
$what.HOW.set_name(self,"{self.HOW.name(self)}[{$t.HOW.name($t)}]");
$what;
}
elsif c.elems == 1 {
my $what := self.HOW.mixin(self.WHAT, TypedHash[$t.WHAT,c[0]]);
# my $what := self but TypedHash[$t.WHAT, c[0]]; # too early in bootstrap
# needs to be done in COMPOSE phaser when that works
$what.HOW.set_name(self,"{self.HOW.name(self)}[{$t.HOW.name($t)},{c[0].HOW.name(c[0])}]");
$what;
}
else {
die "Can only type-constraint Hash with [ValueType] or [ValueType,KeyType]";
}
}
}
sub circumfix:<{ }>(*@elems) { my $ = Hash.new(@elems) }
sub hash(*@a, *%h) { my % = @a, %h }
# XXX parse hangs with ordinary sub declaration
BEGIN my &circumfix:<:{ }> = sub (*@elems) { my $ = Hash.PARAMETERIZE_TYPE(Any,Any).new(@elems) }
# vim: ft=perl6 expandtab sw=4