| 1 | package Iterator::Flex::Flatten; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Flatten Iterator Class | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use v5.28; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use experimental 'signatures'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 3 | our $VERSION = '0.33'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Iterator::Flex::Utils qw( STATE RETURN EXHAUSTION :IterAttrs :IterStates throw_failure); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Iterator::Flex::Factory 'construct_from_iterable', 'to_iterator'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use Ref::Util 'is_plain_arrayref', 'is_plain_hashref', 'is_blessed_ref'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use parent 'Iterator::Flex::Base'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 3 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 6 | ▶ | sub new ( $class, $iterable, $pars = {} ) { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | 6 | $class->SUPER::new( { src => $iterable }, $pars ); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | ## no critic( ExcessComplexity ) | ||||||||||||||||||||||||||||
| 72 | ▶ | sub construct ( $class, $state ) { | |||||||||||||||||||||||||||
|
Subroutine
construct: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 6 | ▶ | throw_failure( parameter => q{'state' parameter must be a HASH reference} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | unless Ref::Util::is_hashref( $state ); | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | my ( $prev, $current, $idx, $iter ) | ||||||||||||||||||||||||||||
| 78 | 6 | = @{$state}{qw[ prev current idx iter src ]}; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 6 | my $src = @{$state}{qw[ code src ]}; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 6 | $src = to_iterator( $src ); | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 6 | my $self; | |||||||||||||||||||||||||||
| 85 | my $iterator_state; | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 6 | my $is_exhausted = $src->can( 'is_exhausted' ); | |||||||||||||||||||||||||||
| 88 | 6 | ▶ | my $iter_is_exhausted = is_blessed_ref( $iter ) | ||||||||||||||||||||||||||
|
Condition33%1/3
Ref::Util::XS::is_blessed_ref($iter) && $iter->can('is_exhausted')
MC/DC0%0/2
Ref::Util::XS::is_blessed_ref($iter) && $iter->can('is_exhausted')
Ref::Util::XS::is_blessed_ref($iter)
$iter->can('is_exhausted')
Truth table33%1/3
A && B
A= Ref::Util::XS::is_blessed_ref($iter)B= $iter->can('is_exhausted')
| |||||||||||||||||||||||||||||
| 89 | && $iter->can( 'is_exhausted' ); | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 6 | my $rewind = !!0; | |||||||||||||||||||||||||||
| 92 | 6 | my $reset = !!0; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | return { | ||||||||||||||||||||||||||||
| 95 | ( +_NAME ) => 'iflatten', | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | ( +_SELF ) => \$self, | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | ( +STATE ) => \$iterator_state, | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | ( +PREV ) => sub { | ||||||||||||||||||||||||||||
| 102 | 11 | ▶ | return $prev; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 103 | }, | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | ( +CURRENT ) => sub { | ||||||||||||||||||||||||||||
| 106 | 13 | ▶ | return $current; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 107 | }, | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | ( +NEXT ) => sub { | ||||||||||||||||||||||||||||
| 110 | 74 | ▶ | return $self->signal_exhaustion if $iterator_state == IterState_EXHAUSTED; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=17 SCAR=28.4
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 74 | my $value; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | LOOP: | ||||||||||||||||||||||||||||
| 115 | 74 | while ( 1 ) { | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 91 | ▶ | if ( !defined $iter ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 32 | $value = $src->(); | |||||||||||||||||||||||||||
| 120 | 32 | ▶ | if ( !defined $value && $src->$is_exhausted ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $value and $src->$is_exhausted
MC/DC50%1/2
not defined $value and $src->$is_exhausted
not defined $value
$src->$is_exhausted
Truth table66%2/3
not(A) and B
A= not defined $valueB= $src->$is_exhausted
| |||||||||||||||||||||||||||||
| 121 | 4 | $prev = $current; | |||||||||||||||||||||||||||
| 122 | 4 | $current = undef; | |||||||||||||||||||||||||||
| 123 | 4 | return $self->signal_exhaustion; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # most likely | ||||||||||||||||||||||||||||
| 127 | last LOOP | ||||||||||||||||||||||||||||
| 128 | 28 | ▶ | if !ref $value || is_plain_hashref( $value ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not ref $value or Ref::Util::XS::is_plain_hashref($value)
MC/DC100%2/2
not ref $value or Ref::Util::XS::is_plain_hashref($value)
not ref $value
Ref::Util::XS::is_plain_hashref($value)
Truth table100%3/3
not(A) or B
A= not ref $valueB= Ref::Util::XS::is_plain_hashref($value)
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # handle arrays directly; could use an iterator, but this is | ||||||||||||||||||||||||||||
| 131 | # faster | ||||||||||||||||||||||||||||
| 132 | 19 | ▶ | if ( is_plain_arrayref( $value ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 10 | $iter = $value; | |||||||||||||||||||||||||||
| 134 | 10 | $idx = 0; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | else { | ||||||||||||||||||||||||||||
| 137 | 9 | $iter = construct_from_iterable( | |||||||||||||||||||||||||||
| 138 | $value, | ||||||||||||||||||||||||||||
| 139 | { | ||||||||||||||||||||||||||||
| 140 | +( EXHAUSTION ) => RETURN, | ||||||||||||||||||||||||||||
| 141 | action_on_failure => RETURN, | ||||||||||||||||||||||||||||
| 142 | } ); | ||||||||||||||||||||||||||||
| 143 | 9 | ▶ | last LOOP if !defined $iter; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 9 | $iter_is_exhausted = $iter->can( 'is_exhausted' ); | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 9 | ▶ | if ( $rewind ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 148 | 3 | ▶ | throw_failure( Unsupported => 'unable to rewind element' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | unless $iter->can( 'rewind' ); | ||||||||||||||||||||||||||||
| 150 | 2 | $iter->rewind; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | elsif ( $reset ) { | ||||||||||||||||||||||||||||
| 153 | 3 | ▶ | throw_failure( Unsupported => 'unable to reset element' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | unless $iter->can( 'reset' ); | ||||||||||||||||||||||||||||
| 155 | 2 | $iter->reset; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 76 | ▶ | if ( defined $idx ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 36 | ▶ | if ( $idx < $iter->@* ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 26 | $value = $iter->[ $idx++ ]; | |||||||||||||||||||||||||||
| 163 | 26 | last LOOP; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | 10 | $iter = $idx = undef; | |||||||||||||||||||||||||||
| 166 | 10 | next; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 40 | $value = $iter->(); | |||||||||||||||||||||||||||
| 170 | last LOOP | ||||||||||||||||||||||||||||
| 171 | 40 | ▶ | if defined $value || !$iter->$iter_is_exhausted; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $value or not $iter->$iter_is_exhausted
MC/DC50%1/2
defined $value or not $iter->$iter_is_exhausted
defined $value
not $iter->$iter_is_exhausted
Truth table66%2/3
A or not(B)
A= defined $valueB= not $iter->$iter_is_exhausted
| |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 7 | $iter = undef; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 68 | $prev = $current; | |||||||||||||||||||||||||||
| 177 | 68 | return $current = $value; | |||||||||||||||||||||||||||
| 178 | }, | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | ( +RESET ) => sub { | ||||||||||||||||||||||||||||
| 181 | 2 | ▶ | $iter = $idx = $prev = $current = undef; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 182 | 2 | $rewind = !!0; | |||||||||||||||||||||||||||
| 183 | 2 | $reset = !!1; | |||||||||||||||||||||||||||
| 184 | }, | ||||||||||||||||||||||||||||
| 185 | ( +REWIND ) => sub { | ||||||||||||||||||||||||||||
| 186 | 2 | ▶ | $iter = $idx = undef; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 187 | 2 | $reset = !!0; | |||||||||||||||||||||||||||
| 188 | 2 | $rewind = !!1; | |||||||||||||||||||||||||||
| 189 | }, | ||||||||||||||||||||||||||||
| 190 | 6 | ( +_DEPENDS ) => $src, | |||||||||||||||||||||||||||
| 191 | }; | ||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 3 | __PACKAGE__->_add_roles( qw[ | |||||||||||||||||||||||||||
| 196 | Current::Closure | ||||||||||||||||||||||||||||
| 197 | Next::ClosedSelf | ||||||||||||||||||||||||||||
| 198 | Prev::Closure | ||||||||||||||||||||||||||||
| 199 | Reset::Closure | ||||||||||||||||||||||||||||
| 200 | Rewind::Closure | ||||||||||||||||||||||||||||
| 201 | State::Closure | ||||||||||||||||||||||||||||
| 202 | ] ); | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 3 | 1; | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | # | ||||||||||||||||||||||||||||
| 207 | # This file is part of Iterator-Flex | ||||||||||||||||||||||||||||
| 208 | # | ||||||||||||||||||||||||||||
| 209 | # This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory. | ||||||||||||||||||||||||||||
| 210 | # | ||||||||||||||||||||||||||||
| 211 | # This is free software, licensed under: | ||||||||||||||||||||||||||||
| 212 | # | ||||||||||||||||||||||||||||
| 213 | # The GNU General Public License, Version 3, June 2007 | ||||||||||||||||||||||||||||
| 214 | # | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | __END__ | ||||||||||||||||||||||||||||