File Coverage

blib/lib/List/Rotation.pm
Criterion Covered Total %
statement 42 46 91.3
branch 2 4 50.0
condition n/a
subroutine 14 14 100.0
pod n/a
total 58 64 90.6


line stmt bran cond sub pod time code
1             package List::Rotation;
2 4     4   104698 use 5.006;
  4         14  
  4         233  
3             our $VERSION = '1.010';
4              
5             package List::Rotation::Cycle;
6 4     4   21 use strict;
  4         9  
  4         142  
7 4     4   22 use warnings;
  4         7  
  4         148  
8              
9 4     4   5194 use Memoize;
  4         10602  
  4         1577  
10             memoize('new');
11              
12             sub new {
13             my $class = shift;
14              
15             do {
16             require Carp;
17             Carp::croak ("Incorrect number of arguments; must be >= 1.");
18             } unless 1 <= @_;
19             my $r_values = [ @_ ];
20             my $position = undef;
21             my $length = @$r_values;
22              
23             my $method = {
24             _next => sub {
25             $position = defined $position ? ++$position : 0;
26             my $i = $position % $length;
27             return $r_values->[$i];
28             },
29             _prev => sub {
30             $position = defined $position ? --$position : -1;
31             my $i = $position % $length;
32             return $r_values->[$i];
33             },
34             _curr => sub {
35             return unless defined $position;
36             my $i = $position % $length;
37             return $r_values->[$i];
38             },
39             _reset => sub {
40             $position = undef;
41             },
42             };
43              
44             my $closure = sub {
45             my $call = shift;
46             &{ $method->{$call} };
47             };
48              
49             bless $closure, $class;
50             }
51              
52 48     48   2179 sub next { my $self = shift; &{ $self }( '_next' ); }
  48         60  
  48         109  
53 8     8   15 sub prev { my $self = shift; &{ $self }( '_prev' ); }
  8         11  
  8         19  
54 8     8   17 sub curr { my $self = shift; &{ $self }( '_curr' ); }
  8         602  
  8         19  
55 4     4   7 sub reset { my $self = shift; &{ $self }( '_reset' ); }
  4         7  
  4         29  
56              
57             #-------------------------------------------------------------------------------
58              
59             package List::Rotation::Alternate;
60              
61 4     4   29 use strict;
  4         8  
  4         959  
62              
63 4     4   27 use vars qw( @ISA );
  4         16  
  4         479  
64             @ISA = qw(List::Rotation::Cycle);
65              
66             sub new {
67 8     8   1096 my $class = shift;
68              
69 8 50       23 do {
70 0         0 require Carp;
71 0         0 Carp::croak ("Incorrect number of arguments; must be <2>.");
72             } unless 2 == @_;
73              
74 8         225 $class->SUPER::new(@_);
75             }
76              
77             #-------------------------------------------------------------------------------
78              
79             package List::Rotation::Toggle;
80              
81 4     4   20 use strict;
  4         17  
  4         121  
82              
83 4     4   27 use vars qw( @ISA );
  4         6  
  4         463  
84             @ISA = qw(List::Rotation::Alternate);
85              
86             sub new {
87 4     4   623 my $class = shift;
88              
89 4 50       12 do {
90 0         0 require Carp;
91 0         0 Carp::croak ("No arguments accepted.");
92             } unless 0 == @_;
93              
94 4         16 $class->SUPER::new( 1 == 1, 0 == 1 );
95             }
96              
97             #-------------------------------------------------------------------------------
98              
99             1;
100              
101              
102             __END__