tagged release 0.7.1
[parrot.git] / languages / perl6 / src / classes / Mapping.pir
blobabca0b1c9a790945c3ba6bcb6731861c049df91e
1 ## $Id$
3 =head1 NAME
5 src/classes/Mapping.pir - Perl 6 hash class and related functions
7 =head1 Methods
9 =over 4
11 =cut
13 .namespace ['Mapping']
15 .sub 'onload' :anon :load :init
16     .local pmc p6meta, mappingproto
17     p6meta = get_hll_global ['Perl6Object'], '$!P6META'
18     mappingproto = p6meta.'new_class'('Mapping', 'parent'=>'Hash Any')
19     p6meta.'register'('Hash', 'parent'=>mappingproto, 'protoobject'=>mappingproto)
20 .end
23 .sub 'get_string' :method :vtable
24     $S0 = ''
25     .local pmc iter
26     iter = new 'Iterator', self
27   loop:
28     unless iter goto end
29     $S1 = shift iter
30     $S2 = iter[$S1]
31     $S0 = concat $S0, $S1
32     concat $S0, "\t"
33     concat $S0, $S2
34     concat $S0, "\n"
35     goto loop
36   end:
37     .return ($S0)
38 .end
41 =item hash()
43 Return invocant as a Hash
45 =cut
47 .sub 'hash' :method
48     .local pmc result, iter
49     result = new 'Perl6Hash'
50     iter = new 'Iterator', self
51   iter_loop:
52     unless iter goto iter_end
53     $S0 = shift iter
54     $P0 = self[$S0]
55     result[$S0] = $P0
56     goto iter_loop
57   iter_end:
58     .return (result)
59 .end
62 =item perl()
64 Return perl representation of the invocant.
66 =cut
68 .sub 'perl' :method
69     .local string result
70     .local pmc keys
71     result = '{'
72     keys = self.'keys'()
73     unless keys goto iter_end
74   iter_loop:
75     .local pmc key, value
76     key = shift keys
77     value = self[key]
78     $S0 = key.'perl'()
79     result .= $S0
80     result .= ' => '
81     $S0 = value.'perl'()
82     result .= $S0
83     unless keys goto iter_end
84     result .= ', '
85     goto iter_loop
86   iter_end:
87     result .= '}'
88     .return (result)
89 .end
92 =item kv (method)
94 Returns elements of hash as array of C<Pair(key, value)>
96 =cut
98 .sub 'kv' :method
99     .local pmc iter
100     .local pmc rv
101     iter = new 'Iterator', self
102     rv   = new 'List'
103   loop:
104     unless iter goto end
105     $S1 = shift iter
106     push rv, $S1
107     $P1 = iter[$S1]
108     push rv, $P1
109     goto loop
110   end:
111     .return (rv)
112 .end
116 .sub 'keys' :method
117     .local pmc iter
118     .local pmc rv
119     iter = new 'Iterator', self
120     rv   = new 'List'
121   loop:
122     unless iter goto end
123     $S1 = shift iter
124     push rv, $S1
125     goto loop
126   end:
127     .return (rv)
128 .end
131 .sub 'values' :method
132     .local pmc iter
133     .local pmc rv
134     iter = new 'Iterator', self
135     rv   = new 'List'
136   loop:
137     unless iter goto end
138     $S1 = shift iter
139     $P1 = iter[$S1]
140     push rv, $P1
141     goto loop
142   end:
143     .return (rv)
144 .end
147 =back
149 =head1 Functions
151 =over 4
153 =back
155 =head1 TODO: Functions
157 =over 4
159 =cut
161 .namespace []
163 =item delete
165  our List  multi method Hash::delete ( *@keys )
166  our Scalar multi method Hash::delete ( $key ) is default
168 Deletes the elements specified by C<$key> or C<$keys> from the invocant.
169 returns the value(s) that were associated to those keys.
171 =item exists
173  our Bool multi method Hash::exists ( $key )
175 True if invocant has an element whose key matches C<$key>, false
176 otherwise.
178 =item keys
180 =item kv
182 =cut
184 .sub kv :multi(Mapping)
185     .param pmc hash
187     .return hash.'kv'()
188 .end
191 =item pairs
193 =item values
195  multi Int|List Hash::keys ( %hash : MatchTest *@keytests )
196  multi Int|List Hash::kv ( %hash : MatchTest *@keytests )
197  multi Int|(List of Pair) Hash::pairs  (%hash : MatchTest *@keytests )
198  multi Int|List Hash::values ( %hash : MatchTest *@keytests )
200 Iterates the elements of C<%hash> in no apparent order, but the order
201 will be the same between successive calls to these functions, as long as
202 C<%hash> doesn't change.
204 If C<@keytests> are provided, only elements whose keys evaluate
205 C<$key ~~ any(@keytests)> as true are iterated.
207 What is returned at each element of the iteration varies with function.
208 C<keys> only returns the key; C<values> the value; C<kv> returns both as
209 a 2 element list in (key, value) order, C<pairs> a C<Pair(key, value)>.
211 Note that C<kv %hash> returns the same as C<zip(keys %hash; values %hash)>
213 In Scalar context, they all return the count of elements that would have
214 been iterated.
216 The lvalue form of C<keys> is not longer supported. Use the C<.buckets>
217 property instead.
219 =back
221 =cut
223 # Local Variables:
224 #   mode: pir
225 #   fill-column: 100
226 # End:
227 # vim: expandtab shiftwidth=4 ft=pir: