/
Set.pm
128 lines (120 loc) · 3.49 KB
/
Set.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
my class Set does Setty {
has $!WHICH;
method SET-SELF(\elems) {
nqp::if(
nqp::elems(elems),
nqp::stmts(
nqp::bindattr(self,::?CLASS,'$!elems',elems),
self
),
set()
)
}
multi method new(Set:_:) {
nqp::if(
nqp::eqaddr(self.WHAT,Set),
set(),
nqp::create(self)
)
}
multi method WHICH (Set:D:) {
nqp::if(
nqp::attrinited(self,Set,'$!WHICH'),
$!WHICH,
$!WHICH := nqp::if(
nqp::istype(self.WHAT,Set),
'Set|',
nqp::concat(self.^name,'|')
) ~ nqp::sha1(
nqp::join('\0',Rakudo::Sorting.MERGESORT-str(self.raw_keys))
)
)
}
method iterator(Set:D:) {
class :: does Rakudo::Iterator::Mappy {
method pull-one() {
nqp::if(
$!iter,
Pair.new(nqp::iterval(nqp::shift($!iter)),True),
IterationEnd
)
}
}.new(self.hll_hash)
}
multi method kv(Set:D:) {
Seq.new(class :: does Rakudo::Iterator::Mappy {
has int $!on-value;
method pull-one() is raw {
nqp::if(
$!on-value,
nqp::stmts(
($!on-value = 0),
True,
),
nqp::if(
$!iter,
nqp::stmts(
($!on-value = 1),
nqp::iterval(nqp::shift($!iter))
),
IterationEnd
)
)
}
method skip-one() {
nqp::if(
$!on-value,
nqp::not_i($!on-value = 0), # skipped a value
nqp::if(
$!iter, # if false, we didn't skip
nqp::stmts( # skipped a key
nqp::shift($!iter),
($!on-value = 1)
)
)
)
}
method count-only() {
nqp::p6box_i(
nqp::add_i(nqp::elems($!storage),nqp::elems($!storage))
)
}
}.new(self.hll_hash))
}
multi method values(Set:D:) { True xx self.total }
multi method grab(Set:D: $count?) {
X::Immutable.new( method => 'grab', typename => self.^name ).throw;
}
multi method grabpairs(Set:D $count?) {
X::Immutable.new( method => 'grabpairs', typename => self.^name ).throw;
}
#--- coercion methods
multi method Set(Set:D:) { self }
multi method SetHash(Set:D:) {
nqp::if(
$!elems && nqp::elems($!elems),
nqp::p6bindattrinvres(
nqp::create(SetHash),SetHash,'$!elems',nqp::clone($!elems)
),
nqp::create(SetHash)
)
}
method clone() {
nqp::if(
$!elems && nqp::elems($!elems),
nqp::clone(self),
set()
)
}
#--- interface methods
multi method AT-KEY(Set:D: \k --> Bool:D) {
nqp::p6bool($!elems && nqp::existskey($!elems,k.WHICH))
}
multi method ASSIGN-KEY(Set:D: \k,\v) {
X::Assignment::RO.new(value => self).throw;
}
multi method DELETE-KEY(Set:D: \k) {
X::Immutable.new(method => 'DELETE-KEY', typename => self.^name).throw;
}
}
# vim: ft=perl6 expandtab sw=4