File Coverage

blib/lib/Iterator/RoundRobin.pm
Criterion Covered Total %
statement 57 58 98.2
branch 15 18 83.3
condition n/a
subroutine 7 7 100.0
pod 4 4 100.0
total 83 87 95.4


line stmt bran cond sub pod time code
1             package Iterator::RoundRobin;
2              
3 3     3   24197 use strict;
  3         6  
  3         111  
4 3     3   16 use warnings;
  3         5  
  3         247  
5              
6             our $VERSION = '0.2';
7              
8             use overload
9 3         34 'eq' => \&next,
10             'ne' => \&next,
11             "cmp" => \&next,
12             "<=>" => \&next,
13 3     3   1710 '""' => \&next;
  3         1205  
14              
15             sub new {
16 6     6 1 2707 my ($class, @arr) = @_;
17 6         40 my %args = (
18             'arrays' => \@arr,
19             'completed' => [],
20             'max' => 0,
21             'current' => 0,
22             'cols' => 0,
23             'track_completed' => 1,
24             );
25 6         40 my $self = bless { %args }, $class;
26 6         19 for my $arr (@arr) {
27 14         83 $self->{'cols'}++;
28 14         15 my $count = scalar @{$arr};
  14         24  
29 14 100       40 if ($count > $self->{'max'}) { $self->{'max'} = $count; }
  8         20  
30             }
31 6 50       19 if (! $self->{'col'}) { $self->{'col'} = 0; }
  6         10  
32 6         25 return $self;
33             }
34              
35             sub isempty {
36 36     36 1 47 my ($self) = @_;
37 36 100       38 if (scalar @{$self->{'arrays'}}) { return 0; }
  36         121  
  32         80  
38 4         16 return 1;
39             }
40              
41             sub next {
42 36     36 1 22785 my ($self) = @_;
43 36 100       75 if ($self->isempty()) { return undef; }
  4         31  
44 32 50       44 if (my $data = shift @{ $self->{'arrays'}->[$self->{'col'}] }) {
  32         109  
45 32         43 my $oldcol = $self->{'col'};
46 32         39 $self->{'col'}++;
47 32 100       32 unless ( scalar @{ $self->{'arrays'}->[ $oldcol ] }) { $self->rebuildarrays(); $self->{'col'} = $oldcol; }
  32         80  
  10         23  
  10         16  
48 32 50       69 if ($self->{'track_completed'}) {
49 32         35 push @{ $self->{'completed'} }, $data;
  32         73  
50             }
51 32 100       76 if ($self->{'col'} == $self->{'cols'}) {
52 14         21 $self->{'col'} = 0;
53             }
54 32         87 return $data;
55             }
56 0         0 return undef;
57             }
58              
59             sub rebuildarrays {
60 10     10 1 11 my ($self) = @_;
61 10         17 my @arrays = ();
62 10         16 $self->{'cols'} = 0;
63 10         11 my $col = 1;
64 10         60 for my $list (@{$self->{'arrays'}}) {
  10         21  
65 18         19 my $count = scalar @{$list};
  18         26  
66 18 100       42 if ($count) {
67 8         15 push @arrays, $list; $self->{'cols'}++;
  8         11  
68             }
69 18         33 $col++;
70             }
71 10         23 $self->{'arrays'} = \@arrays;
72 10         24 return 1;
73             }
74              
75             1;
76             __END__