-
-
Notifications
You must be signed in to change notification settings - Fork 373
/
Set.pm
41 lines (34 loc) · 1.2 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
my class Set does Setty {
has Int $!total;
has $!WHICH;
method ISINSET(\key) { True }
method total (--> Int) { $!total //= %!elems.elems }
multi method WHICH (Set:D:) {
$!WHICH := self.^name ~ '|' ~ %!elems.keys.sort if !$!WHICH.defined;
$!WHICH
}
method grab ($count?) {
X::Immutable.new( method => 'grab', typename => self.^name ).throw;
}
method grabpairs ($count?) {
X::Immutable.new( method => 'grabpairs', typename => self.^name ).throw;
}
multi method pairs(Set:D:) { # must copy else we can change the Set
%!elems.values.map: { Pair.new($_,True) };
}
multi method antipairs(Set:D:) { # must copy else we can change the Set
%!elems.values.map: { Pair.new(True,nqp::decont($_)) };
}
method Set { self }
method SetHash { SetHash.new(self.keys) }
multi method AT-KEY(Set:D: \k --> Bool) {
%!elems.EXISTS-KEY(k.WHICH);
}
multi method ASSIGN-KEY(Set:D: \k,\v) {
X::Assignment::RO.new(typename => self.^name).throw;
}
multi method DELETE-KEY(Set:D: \k) {
X::Immutable.new(method => 'DELETE-KEY', typename => self.^name).throw;
}
}
# vim: ft=perl6 expandtab sw=4