tagged release 0.7.1
[parrot.git] / languages / cardinal / src / classes / Hash.pir
bloba3740bd0eb04397b2bd73c4ab28f331f17863c8a
1 ## $Id$
3 =head1 NAME
5 src/classes/CardinalHash.pir - Cardinal hash class and related functions
7 =head1 Methods
9 =over 4
11 =cut
13 .namespace ['CardinalHash']
15 .sub 'onload' :anon :load :init
16     .local pmc cardinalmeta, mappingproto
17     cardinalmeta = get_hll_global ['CardinalObject'], '!CARDINALMETA'
18     mappingproto = cardinalmeta.'new_class'('CardinalHash', 'parent'=>'Hash CardinalObject')
19     cardinalmeta.'register'('Hash', 'parent'=>'CardinalObject', 'protoobject'=>mappingproto)
20     $P0 = get_class 'CardinalHash'
21     addattribute $P0, 'default'
22 .end
25 .sub 'get_string' :vtable :method
26     $S0 = '{'
27     .local pmc iter
28     iter = new 'Iterator', self
29     goto loop_start
30   loop:
31     unless iter goto end
32     $S0 = concat $S0, ','
33   loop_start:
34     $S1 = shift iter
35     $S2 = iter[$S1]
36     $S0 = concat $S0, $S1
37     $S0 = concat $S0, '=>'
38     concat $S0, $S2
39     goto loop
40   end:
41     concat $S0, '}'
42     .return ($S0)
43 .end
45 =item to_s (method)
47 Returns a string of keys and values appended together.
49 =cut
51 .sub 'to_s' :method
52     .local pmc iter
53     .local pmc rv
54     iter = new 'Iterator', self
55     rv   = new 'CardinalString'
56   loop:
57     unless iter goto end
58     $S1 = shift iter
59     concat rv, $S1
60     $S1 = iter[$S1]
61     concat rv, $S1
62     goto loop
63   end:
64     .return (rv)
65 .end
69 =item kv (method)
71 Returns elements of hash as array of C<Pair(key, value)>
73 =cut
75 .sub 'kv' :method
76     .local pmc iter
77     .local pmc rv
78     iter = new 'Iterator', self
79     rv   = new 'List'
80   loop:
81     unless iter goto end
82     $S1 = shift iter
83     push rv, $S1
84     $S1 = iter[$S1]
85     push rv, $S1
86     goto loop
87   end:
88     .return (rv)
89 .end
93 .sub 'keys' :method
94     .local pmc iter
95     .local pmc rv
96     iter = new 'Iterator', self
97     rv   = new 'List'
98   loop:
99     unless iter goto end
100     $S1 = shift iter
101     push rv, $S1
102     goto loop
103   end:
104     .return (rv)
105 .end
108 .sub 'values' :method
109     .local pmc iter
110     .local pmc rv
111     iter = new 'Iterator', self
112     rv   = new 'List'
113   loop:
114     unless iter goto end
115     $S1 = shift iter
116     $S1 = iter[$S1]
117     push rv, $S1
118     goto loop
119   end:
120     .return (rv)
121 .end
123 =item each(block)
125 Run C<block> once for each item in C<self>, with the key and value passed as args.
127 =cut
129 .sub 'each' :method
130     .param pmc block
131     .local pmc iter
132     iter = new 'Iterator', self
133   each_loop:
134     unless iter goto each_loop_end
135     $P1 = shift iter
136     $P2 = iter[$P1]
137     block($P1,$P2)
138     goto each_loop
139   each_loop_end:
140 .end
142 .sub 'to_a' :method
143     .local pmc newlist
144     .local pmc item
145     .local pmc iter
146     newlist = new 'CardinalArray'
147     iter = new 'Iterator', self
148   each_loop:
149     unless iter goto each_loop_end
150     $P1 = shift iter
151     $P2 = iter[$P1]
152     item = new 'CardinalArray'
153     push item, $P1
154     push item, $P2
155     push newlist, item
156     goto each_loop
157   each_loop_end:
158     .return (newlist)
159 .end
162 ## FIXME:  Parrot currently requires us to write our own "clone" method.
163 .sub 'clone' :vtable :method
164     $P0 = new 'CardinalHash'
165     .local pmc iter
166     iter = new 'Iterator', self
167   loop:
168     unless iter goto end
169     $P1 = shift iter
170     $P2 = iter[$P1]
171     $P0[$P1] = $P2
172     goto loop
173   end:
174     .return ($P0)
175 .end
177 .sub '[]' :method
178     .param pmc i
179     $P0 = self[i]
180   unless_null $P0, index_return
181     $P0 = getattribute self, 'default'
182     .local string type
183     type = typeof $P0
184     $I0 = iseq type, 'Closure'
185     unless $I0 goto index_return
186     $P1 = $P0(self,i)
187     $P0 = $P1
188   index_return:
189     .return($P0)
190 .end
192 .sub '[]=' :method
193     .param pmc k
194     .param pmc v
195     self[k] = v
196     .return(v)
197 .end
199 =back
201 =head1 Functions
203 =over 4
205 =back
207 =head1 TODO: Functions
209 =over 4
211 =cut
213 .namespace []
215 =item delete
217  our List  multi method Hash::delete ( *@keys )
218  our Scalar multi method Hash::delete ( $key ) is default
220 Deletes the elements specified by C<$key> or C<$keys> from the invocant.
221 returns the value(s) that were associated to those keys.
223 =item exists
225  our Bool multi method Hash::exists ( $key )
227 True if invocant has an element whose key matches C<$key>, false
228 otherwise.
230 =item keys
232 =item kv
234 =cut
236 .sub kv :multi('Hash')
237     .param pmc hash
239     .return hash.'kv'()
240 .end
243 =item pairs
245 =item values
247  multi Int|List Hash::keys ( %hash : MatchTest *@keytests )
248  multi Int|List Hash::kv ( %hash : MatchTest *@keytests )
249  multi Int|(List of Pair) Hash::pairs  (%hash : MatchTest *@keytests )
250  multi Int|List Hash::values ( %hash : MatchTest *@keytests )
252 Iterates the elements of C<%hash> in no apparent order, but the order
253 will be the same between successive calls to these functions, as long as
254 C<%hash> doesn't change.
256 If C<@keytests> are provided, only elements whose keys evaluate
257 C<$key ~~ any(@keytests)> as true are iterated.
259 What is returned at each element of the iteration varies with function.
260 C<keys> only returns the key; C<values> the value; C<kv> returns both as
261 a 2 element list in (key, value) order, C<pairs> a C<Pair(key, value)>.
263 Note that C<kv %hash> returns the same as C<zip(keys %hash; values %hash)>
265 In Scalar context, they all return the count of elements that would have
266 been iterated.
268 The lvalue form of C<keys> is not longer supported. Use the C<.buckets>
269 property instead.
271 =back
273 =cut
275 .namespace []
277 .sub 'infix:=>'
278     .param pmc key
279     .param pmc value
280     $P1 = new 'CardinalArray'
281     $P1.'push'(key)
282     $P1.'push'(value)
283     .return($P1)
284 .end
286 .sub 'hash'
287     .param pmc pairs        :slurpy
288     .local pmc ahash
289     ahash = new 'CardinalHash'
290     .local pmc item
291   pairs_loop:
292     unless pairs goto pairs_loop_end
293     item = shift pairs
294     $P0 = shift item
295     $P1 = shift item
296     ahash[$P0] = $P1
297     goto pairs_loop
298   pairs_loop_end:
299     .return(ahash)
300 .end
302 .namespace ['Hash']
304 .sub 'new' :method :multi(_)
305     $P0 = new 'CardinalHash'
306     .return($P0)
307 .end
309 .sub 'new' :method :multi(_,_)
310     .param pmc a
311     $P0 = new 'CardinalHash'
312     setattribute $P0, 'default', a
313     .return($P0)
314 .end
316 # Local Variables:
317 #   mode: pir
318 #   fill-column: 100
319 # End:
320 # vim: expandtab shiftwidth=4 ft=pir: