File Coverage

blib/lib/Iterator/Flex/Array.pm
Criterion Covered Total %
statement 57 57 100.0
branch 16 22 72.7
condition 7 15 46.6
subroutine 16 16 100.0
pod 1 2 50.0
total 97 112 86.6


line stmt bran cond sub pod time code
1             package Iterator::Flex::Array;
2              
3             # ABSTRACT: Array Iterator Class
4              
5 15     15   278297 use v5.28;
  15         58  
6 15     15   85 use strict;
  15         33  
  15         466  
7 15     15   78 use warnings;
  15         27  
  15         1288  
8              
9             our $VERSION = '0.33';
10              
11 15     15   686 use Iterator::Flex::Utils ':IterAttrs', ':IterStates', 'throw_failure';
  15         33  
  15         3886  
12 15     15   107 use Ref::Util;
  15         31  
  15         732  
13 15     15   846 use namespace::clean;
  15         23938  
  15         151  
14 15     15   7975 use experimental 'signatures';
  15         65  
  15         150  
15              
16 15     15   3054 use parent 'Iterator::Flex::Base';
  15         32  
  15         158  
17              
18              
19              
20              
21              
22              
23              
24              
25              
26              
27              
28              
29              
30              
31              
32              
33              
34              
35              
36              
37              
38              
39              
40              
41              
42              
43              
44              
45              
46              
47 95     95 1 150 sub new ( $class, $array, $pars = {} ) {
  95         158  
  95         143  
  95         149  
  95         125  
48 95 50       261 throw_failure( parameter => 'argument must be an ARRAY reference' )
49             unless Ref::Util::is_arrayref( $array );
50              
51 95         550 $class->SUPER::new( { array => $array }, $pars );
52             }
53              
54              
55 103     103 0 149 sub construct ( $class, $state ) {
  103         160  
  103         165  
  103         143  
56              
57 103 50       234 throw_failure( parameter => q{'state' parameter must be a HASH reference} )
58             unless Ref::Util::is_hashref( $state );
59              
60             my ( $arr, $prev, $current, $next )
61 103         165 = @{$state}{qw[ array prev current next ]};
  103         311  
62              
63 103 50       236 throw_failure( parameter => q{state 'array' parameter must be a HASH reference} )
64             unless Ref::Util::is_arrayref( $arr );
65              
66 103         170 my $len = @$arr;
67              
68 103 100       252 $next = 0 unless defined $next;
69              
70 103 50 33     264 throw_failure( parameter => q{illegal value for state 'prev' argument} )
      66        
71             if defined $prev && ( $prev < 0 || $prev >= $len );
72              
73 103 50 33     252 throw_failure( parameter => q{illegal value for state 'current' argument} )
      66        
74             if defined $current && ( $current < 0 || $current >= $len );
75              
76 103 50 33     460 throw_failure( parameter => q{illegal value for state 'next' argument} )
77             if $next < 0 || $next > $len;
78              
79 103         158 my $self;
80             my $iterator_state;
81              
82             return {
83              
84             ( +_NAME ) => 'iarray',
85              
86             ( +_SELF ) => \$self,
87              
88             ( +STATE ) => \$iterator_state,
89              
90             ( +RESET ) => sub {
91 28     28   55 $prev = $current = undef;
92 28         71 $next = 0;
93             },
94              
95             ( +REWIND ) => sub {
96 28     28   54 $next = 0;
97             },
98              
99             ( +PREV ) => sub {
100 42 100   42   188 return defined $prev ? $arr->[$prev] : undef;
101             },
102              
103             ( +CURRENT ) => sub {
104 44 100   44   189 return defined $current ? $arr->[$current] : undef;
105             },
106              
107             ( +NEXT ) => sub {
108 560 100   560   1142 if ( $next == $len ) {
109             # if first time through, set prev
110 141 100       381 $prev = $current
111             if $iterator_state != IterState_EXHAUSTED;
112 141         539 return $current = $self->signal_exhaustion;
113             }
114 419         550 $prev = $current;
115 419         530 $current = $next++;
116              
117 419         890 return $arr->[$current];
118             },
119              
120             ( +FREEZE ) => sub {
121             return [
122 8     8   60 $class,
123             {
124             array => $arr,
125             prev => $prev,
126             current => $current,
127             next => $next,
128             },
129             ];
130             },
131 103         1916 };
132             }
133              
134              
135             __PACKAGE__->_add_roles( qw[
136             State::Closure
137             Next::ClosedSelf
138             Rewind::Closure
139             Reset::Closure
140             Prev::Closure
141             Current::Closure
142             Freeze
143             ] );
144              
145             1;
146              
147             #
148             # This file is part of Iterator-Flex
149             #
150             # This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory.
151             #
152             # This is free software, licensed under:
153             #
154             # The GNU General Public License, Version 3, June 2007
155             #
156              
157             __END__