5 src/classes/CardinalHash.pir - Cardinal hash class and related functions
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'
25 .sub 'get_string' :vtable :method
28 iter = new 'Iterator', self
37 $S0 = concat $S0, '=>'
47 Returns a string of keys and values appended together.
54 iter = new 'Iterator', self
55 rv = new 'CardinalString'
71 Returns elements of hash as array of C<Pair(key, value)>
78 iter = new 'Iterator', self
96 iter = new 'Iterator', self
108 .sub 'values' :method
111 iter = new 'Iterator', self
125 Run C<block> once for each item in C<self>, with the key and value passed as args.
132 iter = new 'Iterator', self
134 unless iter goto each_loop_end
146 newlist = new 'CardinalArray'
147 iter = new 'Iterator', self
149 unless iter goto each_loop_end
152 item = new 'CardinalArray'
162 ## FIXME: Parrot currently requires us to write our own "clone" method.
163 .sub 'clone' :vtable :method
164 $P0 = new 'CardinalHash'
166 iter = new 'Iterator', self
180 unless_null $P0, index_return
181 $P0 = getattribute self, 'default'
184 $I0 = iseq type, 'Closure'
185 unless $I0 goto index_return
207 =head1 TODO: Functions
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.
225 our Bool multi method Hash::exists ( $key )
227 True if invocant has an element whose key matches C<$key>, false
236 .sub kv :multi('Hash')
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
268 The lvalue form of C<keys> is not longer supported. Use the C<.buckets>
280 $P1 = new 'CardinalArray'
287 .param pmc pairs :slurpy
289 ahash = new 'CardinalHash'
292 unless pairs goto pairs_loop_end
304 .sub 'new' :method :multi(_)
305 $P0 = new 'CardinalHash'
309 .sub 'new' :method :multi(_,_)
311 $P0 = new 'CardinalHash'
312 setattribute $P0, 'default', a
320 # vim: expandtab shiftwidth=4 ft=pir: