| 1 | package Iterator::Flex::Product; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: An iterator which produces a Cartesian product of iterators | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use v5.28; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use experimental qw( signatures declared_refs refaliasing ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | our $VERSION = '0.33'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Iterator::Flex::Utils qw( RETURN STATE EXHAUSTION :IterAttrs :IterStates can_meth ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Iterator::Flex::Factory 'to_iterator'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use parent 'Iterator::Flex::Base'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use Ref::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use List::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 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 | ▶ | sub new ( $class, @args ) { | |||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 6 | ▶ | my $pars = Ref::Util::is_hashref( $args[-1] ) ? pop @args : {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 6 | ▶ | throw_failure( parameter => 'not enough parameters' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | unless @args; | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 6 | my @iterators; | |||||||||||||||||||||||||||
| 69 | my @keys; | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # distinguish between ( key => iterator, key =>iterator ) and ( iterator, iterator ); | ||||||||||||||||||||||||||||
| 72 | 6 | ▶ | if ( Ref::Util::is_ref( $args[0] ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 4 | @iterators = @args; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | else { | ||||||||||||||||||||||||||||
| 76 | 2 | ▶ | throw_failure( parameter => 'expected an even number of arguments' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | if @args % 2; | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 2 | ▶ | while ( @args ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 4 | push @keys, shift @args; | |||||||||||||||||||||||||||
| 81 | 4 | push @iterators, shift @args; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 6 | $class->SUPER::new( { keys => \@keys, depends => \@iterators, value => [] }, $pars ); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | ▶ | sub construct ( $class, $state ) { ## no critic (ExcessComplexity) | |||||||||||||||||||||||||||
|
Subroutine
construct: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 89 | 8 | ▶ | throw_failure( parameter => q{state must be a HASH reference} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | unless Ref::Util::is_hashref( $state ); | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 8 | ▶ | $state->{value} //= []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$state->{"value"} //= []
MC/DC100%1/1
$state->{"value"} //= []
$state->{"value"}
Truth table100%2/2
A
A= $state->{"value"}
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | my ( \@depends, \@keys, \@value, $thaw ) | ||||||||||||||||||||||||||||
| 95 | 8 | = @{$state}{qw[ depends keys value thaw ]}; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # transform into iterators if required. | ||||||||||||||||||||||||||||
| 98 | my @iterators | ||||||||||||||||||||||||||||
| 99 | 8 | = map { to_iterator( $_, { ( +EXHAUSTION ) => RETURN } ) } @depends; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # can only work if the iterators support a rewind method | ||||||||||||||||||||||||||||
| 102 | throw_failure( parameter => q{all iterables must provide a rewind method} ) | ||||||||||||||||||||||||||||
| 103 | 8 | ▶ | unless List::Util::all { defined can_meth( $_, 'rewind' ) } @iterators; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 8 | ▶ | throw_failure( parameter => q{number of keys not equal to number of iterators} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@keys and @keys != @iterators
MC/DC0%0/2
@keys and @keys != @iterators
@keys
@keys != @iterators
Truth table66%2/3
A and B
A= @keysB= @keys != @iterators
| |||||||||||||||||||||||||||||
| 106 | if @keys && @keys != @iterators; | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 8 | ▶ | @value = map { $_->current } @iterators | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | if $thaw; | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | ## no critic ( AmbiguousNames ) | ||||||||||||||||||||||||||||
| 112 | 8 | my @set = ( 1 ) x @value; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 8 | my @src = map { [ $_, $_->can( 'is_exhausted' ) ] } @iterators; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 8 | my $self; | |||||||||||||||||||||||||||
| 117 | my $iterator_state; | ||||||||||||||||||||||||||||
| 118 | my %params = ( | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | ( +_SELF ) => \$self, | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | ( +STATE ) => \$iterator_state, | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | ( +NEXT ) => sub { | ||||||||||||||||||||||||||||
| 125 | 44 | ▶ | return $self->signal_exhaustion if $iterator_state == IterState_EXHAUSTED; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=11 SCAR=24.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # first time through | ||||||||||||||||||||||||||||
| 128 | 33 | ▶ | if ( !@value ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 7 | for my $src ( @src ) { | |||||||||||||||||||||||||||
| 131 | 14 | my ( $iter, $is_exhausted ) = $src->@*; | |||||||||||||||||||||||||||
| 132 | 14 | push @value, $iter->(); | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 14 | ▶ | return $self->signal_exhaustion | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | if $iter->$is_exhausted; | ||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 7 | @set = ( 1 ) x @value; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | else { | ||||||||||||||||||||||||||||
| 142 | 26 | my ( $iter, $is_exhausted ) = $src[-1]->@*; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 26 | $value[-1] = $iter->(); | |||||||||||||||||||||||||||
| 145 | 26 | ▶ | if ( $iter->$is_exhausted ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 13 | $set[-1] = 0; | |||||||||||||||||||||||||||
| 147 | 13 | my $idx = @src - 1; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 13 | ▶ | while ( --$idx >= 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 13 | ( $iter, $is_exhausted ) = $src[$idx]->@*; | |||||||||||||||||||||||||||
| 151 | 13 | $value[$idx] = $iter->(); | |||||||||||||||||||||||||||
| 152 | 13 | ▶ | last unless $iter->$is_exhausted; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 7 | $set[$idx] = 0; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 13 | ▶ | return $self->signal_exhaustion | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | if !$set[0]; | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 6 | ▶ | while ( ++$idx < @src ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 6 | $src[$idx][0]->rewind; | |||||||||||||||||||||||||||
| 161 | 6 | $value[$idx] = $src[$idx][0]->(); | |||||||||||||||||||||||||||
| 162 | 6 | $set[$idx] = 1; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | 26 | ▶ | if ( @keys ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 8 | my %value; | |||||||||||||||||||||||||||
| 169 | 8 | @value{@keys} = @value; | |||||||||||||||||||||||||||
| 170 | 8 | return \%value; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | else { | ||||||||||||||||||||||||||||
| 173 | 18 | return [@value]; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | }, | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | ( +CURRENT ) => sub { | ||||||||||||||||||||||||||||
| 178 | 33 | ▶ | return undef if !@value; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 27 | ▶ | return $self->signal_exhaustion if $iterator_state eq IterState_EXHAUSTED; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 22 | ▶ | if ( @keys ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 8 | my %value; | |||||||||||||||||||||||||||
| 182 | 8 | @value{@keys} = @value; | |||||||||||||||||||||||||||
| 183 | 8 | return \%value; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | else { | ||||||||||||||||||||||||||||
| 186 | 14 | return [@value]; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | }, | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 0 | ▶ | ( +RESET ) => sub { @value = () }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 191 | 1 | ▶ | ( +REWIND ) => sub { @value = () }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 192 | 8 | ( +_DEPENDS ) => \@iterators, | |||||||||||||||||||||||||||
| 193 | ); | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | # can only freeze if the iterators support a current method | ||||||||||||||||||||||||||||
| 196 | 8 | ▶ | if ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | 16 | ▶ | List::Util::all { defined can_meth( $_, 'current' ) } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 198 | @iterators | ||||||||||||||||||||||||||||
| 199 | ) | ||||||||||||||||||||||||||||
| 200 | { | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | $params{ +FREEZE } = sub { | ||||||||||||||||||||||||||||
| 203 | 2 | ▶ | return [ $class, { keys => \@keys } ]; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 204 | 8 | }; | |||||||||||||||||||||||||||
| 205 | 8 | $params{ +_ROLES } = ['Freeze']; | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 8 | $params{ +_NAME } = 'iproduct'; | |||||||||||||||||||||||||||
| 209 | 8 | return \%params; | |||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 2 | __PACKAGE__->_add_roles( qw[ | |||||||||||||||||||||||||||
| 214 | State::Closure | ||||||||||||||||||||||||||||
| 215 | Next::ClosedSelf | ||||||||||||||||||||||||||||
| 216 | Current::Closure | ||||||||||||||||||||||||||||
| 217 | Reset::Closure | ||||||||||||||||||||||||||||
| 218 | Rewind::Closure | ||||||||||||||||||||||||||||
| 219 | ] ); | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 2 | 1; | |||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | # | ||||||||||||||||||||||||||||
| 224 | # This file is part of Iterator-Flex | ||||||||||||||||||||||||||||
| 225 | # | ||||||||||||||||||||||||||||
| 226 | # This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory. | ||||||||||||||||||||||||||||
| 227 | # | ||||||||||||||||||||||||||||
| 228 | # This is free software, licensed under: | ||||||||||||||||||||||||||||
| 229 | # | ||||||||||||||||||||||||||||
| 230 | # The GNU General Public License, Version 3, June 2007 | ||||||||||||||||||||||||||||
| 231 | # | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | __END__ | ||||||||||||||||||||||||||||