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
143 ## FIXME: Parrot currently requires us to write our own "clone" method.
144 .sub 'clone' :vtable :method
145 $P0 = new 'CardinalHash'
147 iter = new 'Iterator', self
161 unless_null $P0, index_return
162 $P0 = getattribute self, 'default'
165 $I0 = iseq type, 'Closure'
166 unless $I0 goto index_return
188 =head1 TODO: Functions
198 our List multi method Hash::delete ( *@keys )
199 our Scalar multi method Hash::delete ( $key ) is default
201 Deletes the elements specified by C<$key> or C<$keys> from the invocant.
202 returns the value(s) that were associated to those keys.
206 our Bool multi method Hash::exists ( $key )
208 True if invocant has an element whose key matches C<$key>, false
217 .sub kv :multi('Hash')
228 multi Int|List Hash::keys ( %hash : MatchTest *@keytests )
229 multi Int|List Hash::kv ( %hash : MatchTest *@keytests )
230 multi Int|(List of Pair) Hash::pairs (%hash : MatchTest *@keytests )
231 multi Int|List Hash::values ( %hash : MatchTest *@keytests )
233 Iterates the elements of C<%hash> in no apparent order, but the order
234 will be the same between successive calls to these functions, as long as
235 C<%hash> doesn't change.
237 If C<@keytests> are provided, only elements whose keys evaluate
238 C<$key ~~ any(@keytests)> as true are iterated.
240 What is returned at each element of the iteration varies with function.
241 C<keys> only returns the key; C<values> the value; C<kv> returns both as
242 a 2 element list in (key, value) order, C<pairs> a C<Pair(key, value)>.
244 Note that C<kv %hash> returns the same as C<zip(keys %hash; values %hash)>
246 In Scalar context, they all return the count of elements that would have
249 The lvalue form of C<keys> is not longer supported. Use the C<.buckets>
261 $P1 = new 'CardinalArray'
268 .param pmc pairs :slurpy
270 ahash = new 'CardinalHash'
273 unless pairs goto pairs_loop_end
285 .sub 'new' :method :multi(_)
286 $P0 = new 'CardinalHash'
290 .sub 'new' :method :multi(_,_)
292 $P0 = new 'CardinalHash'
293 setattribute $P0, 'default', a
301 # vim: expandtab shiftwidth=4 ft=pir: