NAME

MCE::Shared::Sequence - Sequence helper class

VERSION

This document describes MCE::Shared::Sequence version 1.699_011

SYNOPSIS

# non-shared
use MCE::Shared::Sequence;

my $seq_a = MCE::Shared::Sequence->new( $begin, $end, $step, $fmt );

my $seq_b = MCE::Shared::Sequence->new(
   { chunk_size => 10, bounds_only => 1 },
   $begin, $end, $step, $fmt
);

# shared
use MCE::Hobo;
use MCE::Shared;

my $seq_a = MCE::Shared->sequence( 1, 100 );

my $seq_b = MCE::Shared->sequence(
   { chunk_size => 10, bounds_only => 1 },
   1, 100
);

sub parallel_a {
   my ( $id ) = @_;
   while ( my $num = $seq_a->next ) {
      print "$id: $num\n";
   }
}

sub parallel_b {
   my ( $id ) = @_;
   while ( my ( $beg, $end ) = $seq_b->next ) {
      for my $num ( $beg .. $end ) {
         print "$id: $num\n";
      }
   }
}

MCE::Hobo->new( \&parallel_a, $_ ) for 1 .. 2;
MCE::Hobo->new( \&parallel_b, $_ ) for 3 .. 4;

$_->join for MCE::Hobo->list();

DESCRIPTION

A number sequence class for MCE::Shared.

API DOCUMENTATION

new ( { options }, begin, end [, step, format ] )
new ( begin, end [, step, format ] )

Constructs a new object. step, if omitted, defaults to 1 if begin is smaller than end or -1 if begin is greater than end. The format string is passed to sprintf behind the scene (% may be omitted).

$seq_n_formatted = sprintf( "%4.1f", $seq_n );

Two options chunk_size and bounds_only are supported, which default to 1 and 0 respectively. Chunking reduces the number of IPC calls to and from the shared-manager process for large sequences.

If bounds_only = 1> is specified, the next method computes the begin and end values only for the chunk and not the numbers in between (hence boundaries only).

$seq1 = MCE::Shared->sequence(
   { chunk_size => 10, bounds_only => 0 },
   1, 20
);

@chunk1 = $seq1->next;  # ( qw/  1  2  3  4  5  6  7  8  9 10 / )
@chunk2 = $seq1->next;  # ( qw/ 11 12 13 14 15 16 17 18 19 20 / )

$seq2 = MCE::Shared->sequence(
   { chunk_size => 10, bounds_only => 1 },
   1, 100
);

( $beg, $end ) = $seq2->next;  # (  1,  10 )
( $beg, $end ) = $seq2->next;  # ( 11,  20 )
( $beg, $end ) = $seq2->next;  # ( 21,  30 )
   ...
( $beg, $end ) = $seq2->next;  # ( 81,  90 )
( $beg, $end ) = $seq2->next;  # ( 91, 100 )

New values and options may be specified later with the rewind method before calling next.

# non-shared
use MCE::Shared::Sequence;

$seq = MCE::Shared::Sequence->new( -1, 1, 0.1, "%4.1f" );
$seq = MCE::Shared::Sequence->new( );
$seq->rewind( -1, 1, 0.1, "%4.1f" );

$seq = MCE::Shared::Sequence->new(
   { chunk_size => 10, bounds_only => 1 },
   1, 100
);

# shared
use MCE::Shared;

$seq = MCE::Shared->sequence( 1, 100 );
$seq = MCE::Shared->sequence( );
$seq->rewind( 1, 100 );

$seq = MCE::Shared->sequence(
   { chunk_size => 10, bounds_only => 1 },
   1, 100
);
next

Returns the next computed sequence(s). An undefined value is returned when the computed begin value exceeds the value held by end.

# { chunk_size =>  1, bounds_only => 0 }  default
  $num = $seq->next;

# { chunk_size => 10, bounds_only => 0 }
  @chunk = $seq->next;

# { chunk_size => 10, bounds_only => 1 }
  ( $beg, $end ) = $seq->next;
rewind ( { options }, begin, end [, step, format ] )
rewind ( begin, end [, step, format ] )

When arguments are specified, resets parameters internally. Otherwise, sets the initial value back to the value held by begin.

$seq->rewind( 10, 1, -1 );
$seq->next; # repeatedly

$seq->rewind;
$seq->next; # repeatedly

INDEX

MCE, MCE::Core, MCE::Shared

AUTHOR

Mario E. Roy, <marioeroy AT gmail DOT com>