/
EnumMap.pm
136 lines (117 loc) · 3.87 KB
/
EnumMap.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
my class EnumMap does Associative { # declared in BOOTSTRAP
# my class EnumMap is Iterable is Cool {
# has Mu $!storage;
multi method Bool(EnumMap:D:) {
nqp::p6bool(nqp::defined($!storage) ?? nqp::elems($!storage) !! 0)
}
method elems(EnumMap:) {
self.DEFINITE && nqp::defined($!storage)
?? nqp::p6box_i(nqp::elems($!storage))
!! 0
}
multi method ACCEPTS(EnumMap:D: Any $topic) {
so self.exists_key($topic.any);
}
multi method ACCEPTS(EnumMap:D: Cool:D $topic) {
so self.exists_key($topic);
}
multi method ACCEPTS(EnumMap:D: Positional $topic) {
so self.exists_key($topic.any);
}
multi method ACCEPTS(EnumMap:D: Regex $topic) {
so self.keys.any.match($topic);
}
proto method exists(|) {*}
multi method exists (EnumMap:U:) { # is DEPRECATED doesn't work in settings
DEPRECATED("the :exists adverb");
self.exists_key;
}
multi method exists (EnumMap:D: \key) { # is DEPRECATED doesn't work in settings
DEPRECATED("the :exists adverb");
self.exists_key(key);
}
proto method exists_key(|) {*}
multi method exists_key(EnumMap:U:) { False }
multi method exists_key(EnumMap:D: Str:D \key) {
nqp::p6bool(
nqp::defined($!storage)
&& nqp::existskey($!storage, nqp::unbox_s(key))
)
}
multi method exists_key(EnumMap:D: \key) {
nqp::p6bool(
nqp::defined($!storage)
&& nqp::existskey($!storage, nqp::unbox_s(key.Stringy))
)
}
multi method perl(EnumMap:D:) {
self.^name ~ '.new('
~ self.keys.map({ .perl ~ ', ' ~ self.at_key($_).perl ~ ', '}).join
~ ')';
}
method iterator(EnumMap:) { self.pairs.iterator }
method list(EnumMap:) { self.pairs }
method keys(EnumMap:) {
return unless self.DEFINITE && nqp::defined($!storage);
HashIter.new(self, :k).list
}
method kv(EnumMap:) {
return unless self.DEFINITE && nqp::defined($!storage);
HashIter.new(self, :kv).list
}
method values(EnumMap:) {
return unless self.DEFINITE && nqp::defined($!storage);
HashIter.new(self, :v).list
}
method pairs(EnumMap:) {
return unless self.DEFINITE && nqp::defined($!storage);
HashIter.new(self, :pairs).list
}
method invert(EnumMap:) {
return unless self.DEFINITE && nqp::defined($!storage);
HashIter.new(self, :invert).list
}
method at_key($key) is rw {
my str $skey = nqp::unbox_s($key.Str);
nqp::defined($!storage) && nqp::existskey($!storage, $skey)
?? nqp::atkey($!storage, $skey)
!! Any
}
method STORE_AT_KEY(\key, Mu \value) is rw {
nqp::defined($!storage) ||
nqp::bindattr(self, EnumMap, '$!storage', nqp::hash());
nqp::bindkey($!storage, nqp::unbox_s(key.Str), value)
}
method Capture(EnumMap:D:) {
my $cap := nqp::create(Capture);
nqp::bindattr($cap, Capture, '$!hash', $!storage);
$cap
}
method FLATTENABLE_LIST() { nqp::list() }
method FLATTENABLE_HASH() {
nqp::defined($!storage) ||
nqp::bindattr(self, EnumMap, '$!storage', nqp::hash());
$!storage
}
method fmt(EnumMap: Cool $format = "%s\t\%s", $sep = "\n") {
if nqp::p6box_i(nqp::sprintfdirectives( nqp::unbox_s($format.Stringy) )) == 1 {
self.keys.fmt($format, $sep);
}
else {
self.pairs.fmt($format, $sep);
}
}
method hash(\SELF:) is rw {
SELF
}
}
multi sub infix:<eqv>(EnumMap:D $a, EnumMap:D $b) {
if +$a != +$b { return Bool::False }
for $a.kv -> $k, $v {
unless $b.exists_key($k) && $b{$k} eqv $v {
return Bool::False;
}
}
Bool::True;
}
# vim: ft=perl6 expandtab sw=4