/
Bag.pm6
87 lines (79 loc) · 2.43 KB
/
Bag.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
my class Bag does Baggy {
has Int $!total;
has $!WHICH;
#--- introspection methods
multi method WHICH(Bag:D:) {
nqp::if(
nqp::attrinited(self,Bag,'$!WHICH'),
$!WHICH,
$!WHICH := ValueObjAt.new('Bag!' ~ nqp::sha1(
nqp::join('\0',Rakudo::Sorting.MERGESORT-str(
Rakudo::QuantHash.BAGGY-RAW-KEY-VALUES(self)
))
))
)
}
method total(Bag:D: --> Int:D) {
nqp::if(
nqp::attrinited(self,Bag,'$!total'),
$!total,
$!total := Rakudo::QuantHash.BAG-TOTAL($!elems)
)
}
#--- interface methods
method STORE(*@pairs, :$initialize --> Bag:D) {
nqp::if(
(my $iterator := @pairs.iterator).is-lazy,
Failure.new(X::Cannot::Lazy.new(:action<initialize>,:what(self.^name))),
nqp::if(
$initialize,
self.SET-SELF(
Rakudo::QuantHash.ADD-PAIRS-TO-BAG(
nqp::create(Rakudo::Internals::IterationSet), $iterator
)
),
X::Assignment::RO.new(value => self).throw
)
)
}
multi method DELETE-KEY(Bag:D: \k) {
X::Immutable.new(method => 'DELETE-KEY', typename => self.^name).throw;
}
method Setty(Bag:D:) { self.Set }
method Mixy(Bag:D:) { self.Mix }
#--- selection methods
multi method grabpairs(Bag:D: $count?) {
X::Immutable.new( method => 'grabpairs', typename => self.^name ).throw;
}
#--- coercion methods
multi method Bag(Bag:D:) { self }
multi method BagHash(Bag:D) {
nqp::if(
$!elems && nqp::elems($!elems),
nqp::create(BagHash).SET-SELF(Rakudo::QuantHash.BAGGY-CLONE($!elems)),
nqp::create(BagHash)
)
}
multi method Mix(Bag:D:) {
nqp::if(
$!elems && nqp::elems($!elems),
nqp::create(Mix).SET-SELF($!elems),
mix()
)
}
multi method MixHash(Bag:D) {
nqp::if(
$!elems && nqp::elems($!elems),
nqp::create(MixHash).SET-SELF(Rakudo::QuantHash.BAGGY-CLONE($!elems)),
nqp::create(MixHash)
)
}
#--- illegal methods
proto method classify-list(|) {
X::Immutable.new(:method<classify-list>, :typename(self.^name)).throw;
}
proto method categorize-list(|) {
X::Immutable.new(:method<categorize-list>, :typename(self.^name)).throw;
}
}
# vim: ft=perl6 expandtab sw=4