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   209416 use v5.28;
  15         49  
6 15     15   63 use strict;
  15         23  
  15         328  
7 15     15   58 use warnings;
  15         22  
  15         955  
8              
9             our $VERSION = '0.34';
10              
11 15     15   509 use Iterator::Flex::Utils ':IterAttrs', ':IterStates', 'throw_failure';
  15         25  
  15         2651  
12 15     15   75 use Ref::Util;
  15         20  
  15         443  
13 15     15   529 use namespace::clean;
  15         13479  
  15         102  
14 15     15   4767 use experimental 'signatures';
  15         37  
  15         84  
15              
16 15     15   2311 use parent 'Iterator::Flex::Base';
  15         24  
  15         104  
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 102     102 1 123 sub new ( $class, $array, $pars = {} ) {
  102         129  
  102         106  
  102         129  
  102         111  
48 102 50       204 throw_failure( parameter => 'argument must be an ARRAY reference' )
49             unless Ref::Util::is_arrayref( $array );
50              
51 102         348 $class->SUPER::new( { array => $array }, $pars );
52             }
53              
54              
55 110     110 0 132 sub construct ( $class, $state ) {
  110         122  
  110         126  
  110         118  
56              
57 110 50       173 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 110         134 = @{$state}{qw[ array prev current next ]};
  110         258  
62              
63 110 50       187 throw_failure( parameter => q{state 'array' parameter must be a HASH reference} )
64             unless Ref::Util::is_arrayref( $arr );
65              
66 110         128 my $len = @$arr;
67              
68 110 100       190 $next = 0 unless defined $next;
69              
70 110 50 33     202 throw_failure( parameter => q{illegal value for state 'prev' argument} )
      66        
71             if defined $prev && ( $prev < 0 || $prev >= $len );
72              
73 110 50 33     192 throw_failure( parameter => q{illegal value for state 'current' argument} )
      66        
74             if defined $current && ( $current < 0 || $current >= $len );
75              
76 110 50 33     342 throw_failure( parameter => q{illegal value for state 'next' argument} )
77             if $next < 0 || $next > $len;
78              
79 110         134 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         65 $next = 0;
93             },
94              
95             ( +REWIND ) => sub {
96 28     28   48 $next = 0;
97             },
98              
99             ( +PREV ) => sub {
100 42 100   42   114 return defined $prev ? $arr->[$prev] : undef;
101             },
102              
103             ( +CURRENT ) => sub {
104 44 100   44   115 return defined $current ? $arr->[$current] : undef;
105             },
106              
107             ( +NEXT ) => sub {
108 572 100   572   942 if ( $next == $len ) {
109             # if first time through, set prev
110 144 100       267 $prev = $current
111             if $iterator_state != IterState_EXHAUSTED;
112 144         374 return $current = $self->signal_exhaustion;
113             }
114 428         482 $prev = $current;
115 428         481 $current = $next++;
116              
117 428         700 return $arr->[$current];
118             },
119              
120             ( +FREEZE ) => sub {
121             return [
122 8     8   39 $class,
123             {
124             array => $arr,
125             prev => $prev,
126             current => $current,
127             next => $next,
128             },
129             ];
130             },
131 110         1524 };
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__