1MCE::Shared::Ordhash(3)User Contributed Perl DocumentatioMnCE::Shared::Ordhash(3)
2
3
4
6 MCE::Shared::Ordhash - An ordered hash class featuring tombstone
7 deletion
8
10 This document describes MCE::Shared::Ordhash version 1.840
11
13 An ordered-hash helper class for use as a standalone or managed by
14 MCE::Shared.
15
16 This module implements an ordered hash featuring tombstone deletion,
17 inspired by Hash::Ordered. An ordered hash is very much like a normal
18 hash but with key insertion order preserved.
19
20 It provides "splice", sorting, plus extra capabilities for use with
21 MCE::Shared::Minidb. Tombstone deletion is further optimized to not
22 impact "store", "push", "unshift", and "merge". Tombstones are purged
23 in-place for lesser memory consumption. In addition, "pop" and "shift"
24 run optimally when an index is present. The optimization also applies
25 to forward and reverse deletes. The end result is achieving a new level
26 of performance, for a pure-Perl ordered hash implementation.
27
29 # non-shared or local construction for use by a single process
30
31 use MCE::Shared::Ordhash;
32
33 my $oh = MCE::Shared::Ordhash->new( @pairs );
34
35 # construction for sharing with other threads and processes
36
37 use MCE::Shared;
38
39 my $oh = MCE::Shared->ordhash( @pairs );
40
41 # hash-like dereferencing
42
43 my $val = $oh->{$key};
44 $oh->{$key} = $val;
45
46 %{$oh} = ();
47
48 # OO interface
49
50 if ( !defined ( $val = $oh->get("some_key") ) ) {
51 $val = $oh->set( some_key => "some_value" );
52 }
53
54 $val = $oh->set( $key, $val );
55 $val = $oh->get( $key );
56 $val = $oh->delete( $key ); # del is an alias for delete
57 $bool = $oh->exists( $key );
58 void = $oh->clear();
59 $len = $oh->len(); # scalar keys %{ $oh }
60 $len = $oh->len( $key ); # length $oh->{ $key }
61 @pair = $oh->pop();
62 $len = $oh->push( @pairs );
63 @pair = $oh->shift();
64 $len = $oh->unshift( @pairs );
65 %pairs = $oh->splice( $offset, $length, @pairs );
66
67 $oh2 = $oh->clone( @keys ); # @keys is optional
68 $oh3 = $oh->flush( @keys );
69 $iter = $oh->iterator( @keys ); # ($key, $val) = $iter->()
70 @keys = $oh->keys( @keys );
71 %pairs = $oh->pairs( @keys );
72 @vals = $oh->values( @keys ); # vals is an alias for values
73
74 $len = $oh->assign( $key/$val pairs ); # equivalent to ->clear, ->mset
75 $cnt = $oh->mdel( @keys );
76 @vals = $oh->mget( @keys );
77 $bool = $oh->mexists( @keys ); # true if all keys exists
78 $len = $oh->mset( $key/$val pairs ); # merge is an alias for mset
79
80 @vals = $oh->sort(); # by val $a <=> $b default
81 @vals = $oh->sort( "desc" ); # by val $b <=> $a
82 @vals = $oh->sort( "alpha" ); # by val $a cmp $b
83 @vals = $oh->sort( "alpha desc" ); # by val $b cmp $a
84
85 @vals = $oh->sort( "key" ); # by key $a <=> $b
86 @vals = $oh->sort( "key desc" ); # by key $b <=> $a
87 @vals = $oh->sort( "key alpha" ); # by key $a cmp $b
88 @vals = $oh->sort( "key alpha desc" ); # by key $b cmp $a
89
90 # included, sugar methods without having to call set/get explicitly
91
92 $len = $oh->append( $key, $string ); # $val .= $string
93 $val = $oh->decr( $key ); # --$val
94 $val = $oh->decrby( $key, $number ); # $val -= $number
95 $val = $oh->getdecr( $key ); # $val--
96 $val = $oh->getincr( $key ); # $val++
97 $val = $oh->incr( $key ); # ++$val
98 $val = $oh->incrby( $key, $number ); # $val += $number
99 $old = $oh->getset( $key, $new ); # $o = $v, $v = $n, $o
100
101 # pipeline, provides atomicity for shared objects, MCE::Shared v1.09+
102
103 @vals = $oh->pipeline( # ( "a_a", "b_b", "c_c" )
104 [ "set", foo => "a_a" ],
105 [ "set", bar => "b_b" ],
106 [ "set", baz => "c_c" ],
107 [ "mget", qw/ foo bar baz / ]
108 );
109
110 For normal hash behavior, the TIE interface is supported.
111
112 # non-shared or local construction for use by a single process
113
114 use MCE::Shared::Ordhash;
115
116 tie my %oh, "MCE::Shared::Ordhash", @pairs;
117 tie my %oh, "MCE::Shared::Ordhash";
118
119 # construction for sharing with other threads and processes
120 # the ordered option is needed to know to use MCE::Shared::Ordhash
121
122 use MCE::Shared;
123
124 tie my %oh, "MCE::Shared", { ordered => 1 }, @pairs;
125 tie my %oh, "MCE::Shared", ordered => 1;
126
127 # usage
128
129 my $val;
130
131 if ( !defined ( $val = $oh{some_key} ) ) {
132 $val = $oh{some_key} = "some_value";
133 }
134
135 $oh{some_key} = 0;
136
137 tied(%oh)->incrby("some_key", 20);
138 tied(%oh)->incrby(some_key => 20);
139
141 Several methods take a query string for an argument. The format of the
142 string is described below. In the context of sharing, the query
143 mechanism is beneficial for the shared-manager process. It is able to
144 perform the query where the data resides versus the client-process grep
145 locally involving lots of IPC.
146
147 o Basic demonstration
148
149 @keys = $oh->keys( "query string given here" );
150 @keys = $oh->keys( "val =~ /pattern/" );
151
152 o Supported operators: =~ !~ eq ne lt le gt ge == != < <= > >=
153 o Multiple expressions delimited by :AND or :OR, mixed case allowed
154
155 "key eq 'some key' :or (val > 5 :and val < 9)"
156 "key eq some key :or (val > 5 :and val < 9)"
157 "key =~ /pattern/i :And val =~ /pattern/i"
158 "val eq foo baz :OR key !~ /pattern/i"
159
160 * key matches on keys in the hash
161 * likewise, val matches on values
162
163 o Quoting is optional inside the string
164
165 "key =~ /pattern/i :AND val eq 'foo bar'" # val eq "foo bar"
166 "key =~ /pattern/i :AND val eq foo bar" # val eq "foo bar"
167
168 Examples.
169
170 # search capability key/val: =~ !~ eq ne lt le gt ge == != < <= > >=
171 # key/val means to match against actual key/val respectively
172
173 @keys = $oh->keys( "key eq 'some key' :or (val > 5 :and val < 9)" );
174 @keys = $oh->keys( "key eq some key :or (val > 5 :and val < 9)" );
175
176 @keys = $oh->keys( "key =~ /$pattern/i" );
177 @keys = $oh->keys( "key !~ /$pattern/i" );
178 @keys = $oh->keys( "val =~ /$pattern/i" );
179 @keys = $oh->keys( "val !~ /$pattern/i" );
180
181 %pairs = $oh->pairs( "key == $number" );
182 %pairs = $oh->pairs( "key != $number :and val > 100" );
183 %pairs = $oh->pairs( "key < $number :or key > $number" );
184 %pairs = $oh->pairs( "val <= $number" );
185 %pairs = $oh->pairs( "val > $number" );
186 %pairs = $oh->pairs( "val >= $number" );
187
188 @vals = $oh->vals( "key eq $string" );
189 @vals = $oh->vals( "key ne $string with space" );
190 @vals = $oh->vals( "key lt $string :or val =~ /$pat1|$pat2/" );
191 @vals = $oh->vals( "val le $string :and val eq 'foo bar'" );
192 @vals = $oh->vals( "val le $string :and val eq foo bar" );
193 @vals = $oh->vals( "val gt $string" );
194 @vals = $oh->vals( "val ge $string" );
195
197 This module involves TIE when accessing the object via hash-like
198 behavior. Both non-shared and shared instances are impacted if doing
199 so. Although likely fast enough for many use cases, the OO interface is
200 recommended for best performance.
201
202 new ( key, value [, key, value, ... ] )
203 Constructs a new object, with an optional list of key-value pairs.
204
205 # non-shared or local construction for use by a single process
206
207 use MCE::Shared::Ordhash;
208
209 $oh = MCE::Shared::Ordhash->new( @pairs );
210 $oh = MCE::Shared::Ordhash->new( );
211
212 # construction for sharing with other threads and processes
213
214 use MCE::Shared;
215
216 $oh = MCE::Shared->ordhash( @pairs );
217 $oh = MCE::Shared->ordhash( );
218
219 assign ( key, value [, key, value, ... ] )
220 Clears the hash, then sets multiple key-value pairs and returns the
221 number of keys stored in the hash. This is equivalent to "clear",
222 "mset".
223
224 $len = $oh->assign( "key1" => "val1", "key2" => "val2" ); # 2
225 $len = %{$oh} = ( "key1" => "val1", "key2" => "val2" ); # 4
226
227 API available since 1.007.
228
229 clear
230 Removes all key-value pairs from the hash.
231
232 $oh->clear;
233 %{$oh} = ();
234
235 clone ( key [, key, ... ] )
236 Creates a shallow copy, a "MCE::Shared::Ordhash" object. It returns
237 an exact copy if no arguments are given. Otherwise, the object
238 includes only the given keys in the same order. Keys that do not
239 exist in the hash will have the "undef" value.
240
241 $oh2 = $oh->clone( "key1", "key2" );
242 $oh2 = $oh->clone;
243
244 delete ( key )
245 Deletes and returns the value by given key or "undef" if the key
246 does not exists in the hash.
247
248 $val = $oh->delete( "some_key" );
249 $val = delete $oh->{ "some_key" };
250
251 del
252 "del" is an alias for "delete".
253
254 exists ( key )
255 Determines if a key exists in the hash.
256
257 if ( $oh->exists( "some_key" ) ) { ... }
258 if ( exists $oh->{ "some_key" } ) { ... }
259
260 flush ( key [, key, ... ] )
261 Same as "clone". Though, clears all existing items before returning.
262
263 get ( key )
264 Gets the value of a hash key or "undef" if the key does not exists.
265
266 $val = $oh->get( "some_key" );
267 $val = $oh->{ "some_key" };
268
269 iterator ( key [, key, ... ] )
270 Returns a code reference for iterating a list of key-value pairs
271 stored in the hash when no arguments are given. Otherwise, returns a
272 code reference for iterating the given keys in the same order. Keys
273 that do not exist will have the "undef" value.
274
275 The list of keys to return is set when the closure is constructed.
276 Later keys added to the hash are not included. Subsequently, the
277 "undef" value is returned for deleted keys.
278
279 $iter = $oh->iterator;
280 $iter = $oh->iterator( "key1", "key2" );
281
282 while ( my ( $key, $val ) = $iter->() ) {
283 ...
284 }
285
286 iterator ( "query string" )
287 Returns a code reference for iterating a list of key-value pairs
288 that match the given criteria. It returns an empty list if the
289 search found nothing. The syntax for the "query string" is
290 described above.
291
292 $iter = $oh->iterator( "val eq some_value" );
293 $iter = $oh->iterator( "key eq some_key :AND val =~ /sun|moon|air|wind/" );
294 $iter = $oh->iterator( "val eq sun :OR val eq moon :OR val eq foo" );
295 $iter = $oh->iterator( "key =~ /$pattern/" );
296
297 while ( my ( $key, $val ) = $iter->() ) {
298 ...
299 }
300
301 keys ( key [, key, ...] )
302 Returns hash keys in the same insertion order when no arguments are
303 given. Otherwise, returns the given keys in the same order. Keys
304 that do not exist will have the "undef" value. In scalar context,
305 returns the size of the hash.
306
307 @keys = $oh->keys( "key1", "key2" );
308
309 @keys = $oh->keys; # faster
310 @keys = keys %{$oh}; # involves TIE overhead
311
312 $len = $oh->keys; # ditto
313 $len = keys %{$oh};
314
315 keys ( "query string" )
316 Returns only keys that match the given criteria. It returns an empty
317 list if the search found nothing. The syntax for the "query string"
318 is described above. In scalar context, returns the size of the
319 resulting list.
320
321 @keys = $oh->keys( "val eq some_value" );
322 @keys = $oh->keys( "key eq some_key :AND val =~ /sun|moon|air|wind/" );
323 @keys = $oh->keys( "val eq sun :OR val eq moon :OR val eq foo" );
324 $len = $oh->keys( "key =~ /$pattern/" );
325
326 len ( key )
327 Returns the size of the hash when no arguments are given. For the
328 given key, returns the length of the value stored at key or the
329 "undef" value if the key does not exists.
330
331 $size = $oh->len;
332 $len = $oh->len( "key1" );
333 $len = length $oh->{ "key1" };
334
335 mdel ( key [, key, ... ] )
336 Deletes one or more keys in the hash and returns the number of keys
337 deleted. A given key which does not exist in the hash is not
338 counted.
339
340 $cnt = $oh->mdel( "key1", "key2" );
341
342 mexists ( key [, key, ... ] )
343 Returns a true value if all given keys exists in the hash. A false
344 value is returned otherwise.
345
346 if ( $oh->mexists( "key1", "key2" ) ) { ... }
347
348 mget ( key [, key, ... ] )
349 Gets the values of all given keys. It returns "undef" for keys which
350 do not exists in the hash.
351
352 ( $val1, $val2 ) = $oh->mget( "key1", "key2" );
353
354 mset ( key, value [, key, value, ... ] )
355 Sets multiple key-value pairs in a hash and returns the number of
356 keys stored in the hash.
357
358 $len = $oh->mset( "key1" => "val1", "key2" => "val2" );
359
360 merge
361 "merge" is an alias for "mset".
362
363 pairs ( key [, key, ... ] )
364 Returns key-value pairs in the same insertion order when no
365 arguments are given. Otherwise, returns key-value pairs for the
366 given keys in the same order. Keys that do not exist will have the
367 "undef" value. In scalar context, returns the size of the hash.
368
369 @pairs = $oh->pairs( "key1", "key2" );
370
371 @pairs = $oh->pairs;
372 $len = $oh->pairs;
373
374 pairs ( "query string" )
375 Returns only key-value pairs that match the given criteria. It
376 returns an empty list if the search found nothing. The syntax for
377 the "query string" is described above. In scalar context, returns
378 the size of the resulting list.
379
380 @pairs = $oh->pairs( "val eq some_value" );
381 @pairs = $oh->pairs( "key eq some_key :AND val =~ /sun|moon|air|wind/" );
382 @pairs = $oh->pairs( "val eq sun :OR val eq moon :OR val eq foo" );
383 $len = $oh->pairs( "key =~ /$pattern/" );
384
385 pipeline ( [ func1, @args ], [ func2, @args ], ... )
386 Combines multiple commands for the object to be processed serially.
387 For shared objects, the call is made atomically due to single IPC to
388 the shared-manager process. The "pipeline" method is fully
389 "wantarray"-aware and receives a list of commands and their
390 arguments. In scalar or list context, it returns data from the last
391 command in the pipeline.
392
393 @vals = $oh->pipeline( # ( "a_a", "b_b", "c_c" )
394 [ "set", foo => "a_a" ],
395 [ "set", bar => "b_b" ],
396 [ "set", baz => "c_c" ],
397 [ "mget", qw/ foo bar baz / ]
398 );
399
400 $len = $oh->pipeline( # 3, same as $oh->len
401 [ "set", foo => "i_i" ],
402 [ "set", bar => "j_j" ],
403 [ "set", baz => "k_k" ],
404 [ "len" ]
405 );
406
407 $oh->pipeline(
408 [ "set", foo => "m_m" ],
409 [ "set", bar => "n_n" ],
410 [ "set", baz => "o_o" ]
411 );
412
413 Current API available since 1.809.
414
415 pipeline_ex ( [ func1, @args ], [ func2, @args ], ... )
416 Same as "pipeline", but returns data for every command in the
417 pipeline.
418
419 @vals = $oh->pipeline_ex( # ( "a_a", "b_b", "c_c" )
420 [ "set", foo => "a_a" ],
421 [ "set", bar => "b_b" ],
422 [ "set", baz => "c_c" ]
423 );
424
425 Current API available since 1.809.
426
427 pop
428 Removes and returns the last key-value pair or value in scalar
429 context of the ordered hash. If there are no keys in the hash,
430 returns the undefined value.
431
432 ( $key, $val ) = $oh->pop;
433
434 $val = $oh->pop;
435
436 purge
437 A utility method for purging any *tombstones* in the keys array. It
438 also resets a couple counters internally. Call this method before
439 serializing to a file, which is the case in "MCE::Shared::Minidb".
440
441 $oh->purge;
442
443 push ( key, value [, key, value, ... ] )
444 Appends one or multiple key-value pairs to the tail of the ordered
445 hash and returns the new length. Any keys already existing in the
446 hash are re-inserted with the new values.
447
448 $len = $oh->push( "key1", "val1", "key2", "val2" );
449
450 set ( key, value )
451 Sets the value of the given hash key and returns its new value.
452
453 $val = $oh->set( "key", "value" );
454 $val = $oh->{ "key" } = "value";
455
456 shift
457 Removes and returns the first key-value pair or value in scalar
458 context of the ordered hash. If there are no keys in the hash,
459 returns the undefined value.
460
461 ( $key, $val ) = $oh->shift;
462
463 $val = $oh->shift;
464
465 sort ( "BY key [ ASC | DESC ] [ ALPHA ]" )
466 sort ( "BY val [ ASC | DESC ] [ ALPHA ]" )
467 Returns sorted keys in list context, leaving the elements intact. In
468 void context, sorts the hash in-place. By default, sorting is
469 numeric and applied to values when no arguments are given.
470
471 @keys = $oh->sort( "BY val" );
472
473 $oh->sort();
474
475 If the keys or values contain string values and you want to sort
476 them lexicographically, specify the "ALPHA" modifier.
477
478 @keys = $oh->sort( "BY key ALPHA" );
479
480 $oh->sort( "BY val ALPHA" );
481
482 The default is "ASC" for sorting the hash from small to large. In
483 order to sort the hash from large to small, specify the "DESC"
484 modifier.
485
486 @keys = $oh->sort( "BY val DESC ALPHA" );
487
488 $oh->sort( "BY key DESC ALPHA" );
489
490 splice ( offset [, length [, key, value, ... ] ] )
491 Removes the key-value pairs designated by "offset" and "length" from
492 the ordered hash, and replaces them with "key-value pairs", if any.
493 The behavior is similar to the Perl "splice" function.
494
495 @pairs = $oh->splice( 20, 2, @pairs );
496 @pairs = $oh->splice( 20, 2 );
497 @pairs = $oh->splice( 20 );
498
499 unshift ( key, value [, key, value, ... ] )
500 Prepends one or multiple key-value pairs to the head of the ordered
501 hash and returns the new length. Any keys already existing in the
502 hash are re-inserted with the new values.
503
504 $len = $oh->unshift( "key1", "val1", "key2", "val2" );
505
506 values ( key [, key, ... ] )
507 Returns hash values in the same insertion order when no arguments
508 are given. Otherwise, returns values for the given keys in the same
509 order. Keys that do not exist will have the "undef" value. In scalar
510 context, returns the size of the hash.
511
512 @vals = $oh->values( "key1", "key2" );
513
514 @vals = $oh->values; # faster
515 @vals = values %{$oh}; # involves TIE overhead
516
517 $len = $oh->values; # ditto
518 $len = values %{$oh};
519
520 values ( "query string" )
521 Returns only values that match the given criteria. It returns an
522 empty list if the search found nothing. The syntax for the "query
523 string" is described above. In scalar context, returns the size of
524 the resulting list.
525
526 @vals = $oh->values( "val eq some_value" );
527 @vals = $oh->values( "key eq some_key :AND val =~ /sun|moon|air|wind/" );
528 @vals = $oh->values( "val eq sun :OR val eq moon :OR val eq foo" );
529 $len = $oh->values( "key =~ /$pattern/" );
530
531 vals
532 "vals" is an alias for "values".
533
535 This module is equipped with sugar methods to not have to call "set"
536 and "get" explicitly. In shared context, the benefit is atomicity and
537 reduction in inter-process communication.
538
539 The API resembles a subset of the Redis primitives
540 <http://redis.io/commands#strings> with key representing the hash key.
541
542 append ( key, string )
543 Appends a value to a key and returns its new length.
544
545 $len = $oh->append( $key, "foo" );
546
547 decr ( key )
548 Decrements the value of a key by one and returns its new value.
549
550 $num = $oh->decr( $key );
551
552 decrby ( key, number )
553 Decrements the value of a key by the given number and returns its
554 new value.
555
556 $num = $oh->decrby( $key, 2 );
557
558 getdecr ( key )
559 Decrements the value of a key by one and returns its old value.
560
561 $old = $oh->getdecr( $key );
562
563 getincr ( key )
564 Increments the value of a key by one and returns its old value.
565
566 $old = $oh->getincr( $key );
567
568 getset ( key, value )
569 Sets the value of a key and returns its old value.
570
571 $old = $oh->getset( $key, "baz" );
572
573 incr ( key )
574 Increments the value of a key by one and returns its new value.
575
576 $num = $oh->incr( $key );
577
578 incrby ( key, number )
579 Increments the value of a key by the given number and returns its
580 new value.
581
582 $num = $oh->incrby( $key, 2 );
583
585 Many thanks to David Golden for Hash::Ordered. This implementation is
586 inspired by Hash::Ordered v0.009.
587
589 I wanted an ordered hash implementation for use with MCE::Shared
590 without any side effects. For example, linear scans, slow deletes, or
591 excessive memory consumption. The closest module on CPAN to pass in
592 this regard is Hash::Ordered by David Golden.
593
594 MCE::Shared has only one shared-manager process which is by design.
595 Therefore, re-factored tombstone deletion with extras for lesser impact
596 to the rest of the library. This module differs in personality from
597 Hash::Ordered mainly for compatibility with other classes included with
598 MCE::Shared.
599
600 The following simulates a usage pattern inside MCE::Hobo involving
601 random key deletion. For example, an application joining a list of
602 Hobos provided by "MCE::Hobo-"list_joinable>.
603
604 use MCE::Shared::Ordhash;
605 use List::Util 'shuffle';
606 use Time::HiRes 'time';
607
608 srand 0;
609
610 my $oh = MCE::Shared::Ordhash->new();
611 my $num_keys = 200000;
612 my $start = time();
613
614 $oh->set($_,$_) for 1 .. $num_keys;
615
616 for ( shuffle $oh->keys ) {
617 $oh->delete($_);
618 }
619
620 printf "duration: %7.03f secs\n", time() - $start;
621
622 Both the runtime and memory consumption are captured for the
623 demonstration. Results are included for MCE::Shared::Hash (unordered
624 hash) for comparison.
625
626 for ( shuffle $oh->keys ) { $oh->delete($_) }
627
628 0.378 secs. 35 MB MCE::Shared::Hash (unordered)
629 0.437 secs. 49 MB Tie::Hash::Indexed (XS)
630 0.743 secs. 54 MB MCE::Shared::Ordhash
631 1.028 secs. 60 MB Hash::Ordered
632 1.752 secs. 112 MB Tie::LLHash
633 > 42 mins. 66 MB Tie::IxHash
634
635 Using the same demonstration above, another usage pattern inside
636 MCE::Hobo involves orderly hash-key deletion. For example, waiting for
637 and joining all Hobos provided by "MCE::Hobo-"list>.
638
639 for ( $oh->keys ) { $oh->delete($_) }
640
641 0.353 secs. 35 MB MCE::Shared::Hash (unordered)
642 0.349 secs. 49 MB Tie::Hash::Indexed (XS)
643 0.452 secs. 41 MB MCE::Shared::Ordhash
644 0.735 secs. 54 MB Hash::Ordered
645 1.338 secs. 112 MB Tie::LLHash
646 > 42 mins. 66 MB Tie::IxHash
647
648 No matter if orderly or randomly, even backwards, hash-key deletion in
649 "MCE::Shared::Ordhash" performs reasonably well. The following provides
650 the construction used for the modules mentioned.
651
652 my $oh = Hash::Ordered->new();
653 $oh->set($_,$_); $oh->keys; $oh->delete($_);
654
655 my $oh = Tie::Hash::Indexed->new();
656 $oh->set($_,$_); $oh->keys; $oh->delete($_);
657
658 my $oh = Tie::IxHash->new();
659 $oh->STORE($_,$_); $oh->Keys; $oh->DELETE($_);
660
661 my $oh = tie my %hash, 'Tie::LLHash';
662 $oh->last($_,$_); keys %hash; $oh->DELETE($_);
663
664 Hash::Ordered is supported for use with MCE::Shared. This includes on-
665 demand hash-like dereferencing, similarly to "hash" and "ordhash".
666
667 use feature 'say';
668
669 use MCE::Hobo;
670 use MCE::Shared;
671 use Hash::Ordered; # 0.010 or later
672
673 my $ha = MCE::Shared->hash(); # shared MCE::Shared::Hash
674 my $oh = MCE::Shared->ordhash(); # shared MCE::Shared::Ordhash
675
676 my $ho = MCE::Shared->share( Hash::Ordered->new() );
677
678 sub parallel_task {
679 my ($id) = @_;
680
681 # OO interface
682 if ($id == 1) {
683 $ha->set("$id", "foo");
684 $oh->set("$id", "foo");
685 $ho->set("$id", "foo");
686 }
687 # hash-like dereferencing
688 elsif ($id == 2) {
689 $ha->{"$id"} = "baz";
690 $oh->{"$id"} = "baz";
691 $ho->{"$id"} = "baz";
692 }
693
694 return;
695 }
696
697 MCE::Hobo->create("parallel_task", $_) for 1..2;
698 MCE::Hobo->waitall;
699
700 say $ha->{"1"}; # foo
701 say $oh->{"1"};
702 say $ho->{"1"};
703
704 say $ha->get("2"); # baz
705 say $oh->get("2");
706 say $ho->get("2");
707
709 · Hash::Ordered
710
711 · Tie::Hash::Indexed
712
713 · Tie::IxHash
714
715 · Tie::LLHash
716
718 MCE, MCE::Hobo, MCE::Shared
719
721 Mario E. Roy, <marioeroy AT gmail DOT com>
722
723
724
725perl v5.28.1 2019-01-04 MCE::Shared::Ordhash(3)