| 1 | package Iterator::Flex::Cat; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: An iterator which concatenates a set of iterators | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use v5.28; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use experimental qw( signatures declared_refs refaliasing ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 4 | our $VERSION = '0.33'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use Iterator::Flex::Utils qw( RETURN STATE EXHAUSTION :IterAttrs :IterStates can_meth ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use Iterator::Flex::Factory 'to_iterator'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use parent 'Iterator::Flex::Base'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use List::Util 'all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 4 | ▶ | 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 | ▶ | sub new ( $class, @args ) { | |||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.7
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | 3 | ▶ | my $pars = Ref::Util::is_hashref( $args[-1] ) ? pop @args : {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | @args | ||||||||||||||||||||||||||||
| 59 | 3 | ▶ | or throw_failure( parameter => 'not enough parameters' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 3 | $class->SUPER::new( { | |||||||||||||||||||||||||||
| 62 | depends => \@args, | ||||||||||||||||||||||||||||
| 63 | current_iterator_index => undef, | ||||||||||||||||||||||||||||
| 64 | }, | ||||||||||||||||||||||||||||
| 65 | $pars, | ||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | ▶ | sub construct ( $class, $state ) { | |||||||||||||||||||||||||||
|
Subroutine
construct: called CC=6 SCAR=18.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 70 | 3 | ▶ | throw_failure( parameter => q{state must be a HASH reference} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | unless Ref::Util::is_hashref( $state ); | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 3 | ▶ | $state->{value} //= []; | ||||||||||||||||||||||||||
|
Condition50%1/2
$state->{"value"} //= []
MC/DC0%0/1
$state->{"value"} //= []
$state->{"value"}
Truth table50%1/2
A
A= $state->{"value"}
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | my ( \@depends, $current_iterator, $prev, $current, $next, undef ) | ||||||||||||||||||||||||||||
| 76 | 3 | = @{$state}{ 'depends', 'current_iterator', 'prev', 'current', 'next', 'thaw' }; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # transform into iterators if required. | ||||||||||||||||||||||||||||
| 79 | my @iterators | ||||||||||||||||||||||||||||
| 80 | 3 | = map { to_iterator( $_, { ( +EXHAUSTION ) => RETURN } ) } @depends; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # -- not sure if this is just cargo-culted | ||||||||||||||||||||||||||||
| 83 | # my $value; | ||||||||||||||||||||||||||||
| 84 | # $value = $current | ||||||||||||||||||||||||||||
| 85 | # if $thaw; | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 3 | my $self; | |||||||||||||||||||||||||||
| 88 | my $iterator_state; | ||||||||||||||||||||||||||||
| 89 | 3 | my $iter; | |||||||||||||||||||||||||||
| 90 | 3 | my $is_exhausted; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | my sub init { | ||||||||||||||||||||||||||||
| 93 | 7 | ▶ | $current_iterator //= 0; | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=3 SCAR=12.0
Condition50%1/2
$current_iterator //= 0
MC/DC0%0/1
$current_iterator //= 0
$current_iterator
Truth table50%1/2
A
A= $current_iterator
| |||||||||||||||||||||||||||||
| 94 | 7 | ▶ | if ( $current_iterator >= @iterators ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | $iterator_state = IterState_EXHAUSTED; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | else { | ||||||||||||||||||||||||||||
| 98 | 7 | $iter = $iterators[$current_iterator]; | |||||||||||||||||||||||||||
| 99 | 7 | $is_exhausted = $iter->can( 'is_exhausted' ); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 3 | init(); | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | my %params = ( | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | ( +_SELF ) => \$self, | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | ( +STATE ) => \$iterator_state, | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | ( +NEXT ) => sub { | ||||||||||||||||||||||||||||
| 112 | 65 | ▶ | return $self->signal_exhaustion if $iterator_state == IterState_EXHAUSTED; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 65 | while ( 1 ) { | |||||||||||||||||||||||||||
| 115 | 73 | my $value = $iter->(); | |||||||||||||||||||||||||||
| 116 | 73 | ▶ | if ( defined $value || !$iter->$is_exhausted ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $value or not $iter->$is_exhausted
MC/DC50%1/2
defined $value or not $iter->$is_exhausted
defined $value
not $iter->$is_exhausted
Truth table66%2/3
A or not(B)
A= defined $valueB= not $iter->$is_exhausted
| |||||||||||||||||||||||||||||
| 117 | 58 | $prev = $current; | |||||||||||||||||||||||||||
| 118 | 58 | $current = $value; | |||||||||||||||||||||||||||
| 119 | 58 | return $current; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 15 | ▶ | last if ++$current_iterator >= @iterators; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 8 | $iter = $iterators[$current_iterator]; | |||||||||||||||||||||||||||
| 124 | 8 | $is_exhausted = $iter->can( 'is_exhausted' ); | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # if we haven't returned, we've exhausted things | ||||||||||||||||||||||||||||
| 128 | 7 | $current_iterator = undef; | |||||||||||||||||||||||||||
| 129 | 7 | $prev = $current; | |||||||||||||||||||||||||||
| 130 | 7 | return $current = $self->signal_exhaustion; | |||||||||||||||||||||||||||
| 131 | }, | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | ( +PREV ) => sub { | ||||||||||||||||||||||||||||
| 134 | 6 | ▶ | return $prev; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 135 | }, | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | ( +CURRENT ) => sub { | ||||||||||||||||||||||||||||
| 138 | 0 | ▶ | return $self->signal_exhaustion if $iterator_state eq IterState_EXHAUSTED; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 0 | return $current; | |||||||||||||||||||||||||||
| 140 | }, | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 3 | ( +_ROLES ) => [], | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | ( +_DEPENDS ) => \@iterators, | ||||||||||||||||||||||||||||
| 145 | ); | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | # can only freeze if the iterators support a current method | ||||||||||||||||||||||||||||
| 148 | 3 | ▶ | if ( all { defined can_meth( $_, 'current' ) } @iterators ) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | $params{ +FREEZE } = sub { | ||||||||||||||||||||||||||||
| 150 | return [ | ||||||||||||||||||||||||||||
| 151 | 0 | ▶ | $class, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 152 | { | ||||||||||||||||||||||||||||
| 153 | current_iterator => $current_iterator, | ||||||||||||||||||||||||||||
| 154 | prev => $prev, | ||||||||||||||||||||||||||||
| 155 | current => $current, | ||||||||||||||||||||||||||||
| 156 | next => $next, | ||||||||||||||||||||||||||||
| 157 | } ]; | ||||||||||||||||||||||||||||
| 158 | 3 | }; | |||||||||||||||||||||||||||
| 159 | 3 | push $params{ +_ROLES }->@*, 'Freeze'; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 3 | ▶ | if ( all { defined can_meth( $_, 'reset' ) } @iterators ) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | $params{ +RESET } = sub { | ||||||||||||||||||||||||||||
| 164 | 2 | ▶ | $prev = $current = $current_iterator = undef; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 165 | 2 | init(); | |||||||||||||||||||||||||||
| 166 | 3 | }; | |||||||||||||||||||||||||||
| 167 | 3 | push $params{ +_ROLES }->@*, 'Reset::Closure'; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 3 | ▶ | if ( all { defined can_meth( $_, 'rewind' ) } @iterators ) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | $params{ +REWIND } = sub { | ||||||||||||||||||||||||||||
| 172 | 2 | ▶ | $current_iterator = undef; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 173 | 2 | init(); | |||||||||||||||||||||||||||
| 174 | 3 | }; | |||||||||||||||||||||||||||
| 175 | 3 | push $params{ +_ROLES }->@*, 'Rewind::Closure'; | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 3 | $params{ +_NAME } = 'icat'; | |||||||||||||||||||||||||||
| 179 | 3 | return \%params; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 4 | __PACKAGE__->_add_roles( qw[ | |||||||||||||||||||||||||||
| 184 | State::Closure | ||||||||||||||||||||||||||||
| 185 | Next::ClosedSelf | ||||||||||||||||||||||||||||
| 186 | Current::Closure | ||||||||||||||||||||||||||||
| 187 | Prev::Closure | ||||||||||||||||||||||||||||
| 188 | ] ); | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 4 | 1; | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | # | ||||||||||||||||||||||||||||
| 193 | # This file is part of Iterator-Flex | ||||||||||||||||||||||||||||
| 194 | # | ||||||||||||||||||||||||||||
| 195 | # This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory. | ||||||||||||||||||||||||||||
| 196 | # | ||||||||||||||||||||||||||||
| 197 | # This is free software, licensed under: | ||||||||||||||||||||||||||||
| 198 | # | ||||||||||||||||||||||||||||
| 199 | # The GNU General Public License, Version 3, June 2007 | ||||||||||||||||||||||||||||
| 200 | # | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | __END__ | ||||||||||||||||||||||||||||