| 1 | package Iterator::Flex::Stack; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: An iterator which concatenates a set 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 ); | ||||||||||||||||||||||||||
|
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 List::Util 'all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 2 | ▶ | 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 | ▶ | sub new ( $class, @args ) { | |||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | 3 | ▶ | my $pars = Ref::Util::is_hashref( $args[-1] ) ? pop @args : {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 3 | $class->SUPER::new( { | |||||||||||||||||||||||||||
| 71 | depends => \@args, | ||||||||||||||||||||||||||||
| 72 | current_iterator_index => undef, | ||||||||||||||||||||||||||||
| 73 | }, | ||||||||||||||||||||||||||||
| 74 | $pars, | ||||||||||||||||||||||||||||
| 75 | ); | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | ▶ | sub construct ( $class, $state ) { | |||||||||||||||||||||||||||
|
Subroutine
construct: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 79 | 3 | ▶ | throw_failure( parameter => q{state must be a HASH reference} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | unless Ref::Util::is_hashref( $state ); | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 3 | ▶ | $state->{value} //= []; | ||||||||||||||||||||||||||
|
Condition50%1/2
$state->{"value"} //= []
MC/DC0%0/1
$state->{"value"} //= []
$state->{"value"}
Truth table50%1/2
A
A= $state->{"value"}
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | my ( \@depends, $prev, $current, undef, undef ) | ||||||||||||||||||||||||||||
| 85 | 3 | = @{$state}{ 'depends', 'prev', 'current', 'next', 'thaw' }; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # transform into iterators if required. | ||||||||||||||||||||||||||||
| 88 | my @stack | ||||||||||||||||||||||||||||
| 89 | 3 | = map { to_iterator( $_, { ( +EXHAUSTION ) => RETURN } ) } @depends; | |||||||||||||||||||||||||||
| 90 | 3 | my @snapshot = @stack; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # --- cargo cult?? | ||||||||||||||||||||||||||||
| 93 | # my $value; | ||||||||||||||||||||||||||||
| 94 | # $value = $current | ||||||||||||||||||||||||||||
| 95 | # if $thaw; | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 3 | my $self; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | my $iter; | ||||||||||||||||||||||||||||
| 100 | 3 | my $is_exhausted; | |||||||||||||||||||||||||||
| 101 | 3 | my $iterator_state; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | my sub init { | ||||||||||||||||||||||||||||
| 104 | 15 | ▶ | $iterator_state = IterState_EXHAUSTED; | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 105 | 15 | ▶ | if ( @stack ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 14 | $iter = $stack[0]; | |||||||||||||||||||||||||||
| 107 | 14 | $is_exhausted = $iter->can( 'is_exhausted' ); | |||||||||||||||||||||||||||
| 108 | 14 | $iterator_state = IterState_CLEAR; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 3 | init(); | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | my %params = ( | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | ( +_NAME ) => 'istack', | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | ( +_SELF ) => \$self, | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | ( +RESET ) => sub { | ||||||||||||||||||||||||||||
| 120 | 3 | ▶ | $prev = $current = undef; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 121 | 3 | @stack = @snapshot; | |||||||||||||||||||||||||||
| 122 | 3 | init(); | |||||||||||||||||||||||||||
| 123 | }, | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | ( +REWIND ) => sub { | ||||||||||||||||||||||||||||
| 126 | 3 | ▶ | @stack = @snapshot; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 127 | 3 | init(); | |||||||||||||||||||||||||||
| 128 | }, | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | ( +STATE ) => \$iterator_state, | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | ( +NEXT ) => sub { | ||||||||||||||||||||||||||||
| 133 | 70 | ▶ | return $self->signal_exhaustion if $iterator_state == IterState_EXHAUSTED; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 70 | while ( 1 ) { | |||||||||||||||||||||||||||
| 136 | 92 | my $value = $iter->(); | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 92 | ▶ | 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
| |||||||||||||||||||||||||||||
| 139 | 59 | $prev = $current; | |||||||||||||||||||||||||||
| 140 | 59 | return $current = $value; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 33 | ▶ | last unless defined( $iter = shift @stack ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 22 | $is_exhausted = $iter->can( 'is_exhausted' ); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 11 | $prev = $current; | |||||||||||||||||||||||||||
| 148 | 11 | return $current = $self->signal_exhaustion; | |||||||||||||||||||||||||||
| 149 | }, | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | ( +PREV ) => sub { | ||||||||||||||||||||||||||||
| 152 | 7 | ▶ | return $prev; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 153 | }, | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | ( +CURRENT ) => sub { | ||||||||||||||||||||||||||||
| 156 | 3 | ▶ | return $self->signal_exhaustion if !@stack; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 3 | return $current; | |||||||||||||||||||||||||||
| 158 | }, | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | ▶ | ( +_ROLES ) => [], | |||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | ( +_DEPENDS ) => \@snapshot, | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | ( +METHODS ) => { | ||||||||||||||||||||||||||||
| 165 | push => sub ( $, @iters ) { | ||||||||||||||||||||||||||||
| 166 | 6 | push @stack, map { to_iterator( $_, { ( +EXHAUSTION ) => RETURN } ) } @iters; | |||||||||||||||||||||||||||
| 167 | 6 | $self->_clear_state; | |||||||||||||||||||||||||||
| 168 | 6 | ▶ | init() if @stack == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | }, | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | ▶ | pop => sub ($) { | |||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=9.2
| |||||||||||||||||||||||||||||
| 172 | 1 | ▶ | if ( @stack == 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 0 | $iterator_state = IterState_EXHAUSTED; | |||||||||||||||||||||||||||
| 174 | 0 | $iter = $is_exhausted = undef; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | 1 | return CORE::pop( @stack ); | |||||||||||||||||||||||||||
| 177 | }, | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | ▶ | unshift => sub ( $, @iters ) { | |||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 180 | 2 | unshift @stack, map { to_iterator( $_, { ( +EXHAUSTION ) => RETURN } ) } @iters; | |||||||||||||||||||||||||||
| 181 | 2 | init(); | |||||||||||||||||||||||||||
| 182 | }, | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | ▶ | shift => sub ($) { | |||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.9
| |||||||||||||||||||||||||||||
| 185 | 1 | ▶ | if ( @stack == 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | 0 | $iterator_state = IterState_EXHAUSTED; | |||||||||||||||||||||||||||
| 187 | 0 | $iter = $is_exhausted = undef; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | 1 | my $ret = CORE::shift( @stack ); | |||||||||||||||||||||||||||
| 190 | 1 | init(); | |||||||||||||||||||||||||||
| 191 | 1 | return $ret; | |||||||||||||||||||||||||||
| 192 | }, | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | ▶ | snapshot => sub ($) { | |||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 195 | 2 | @snapshot = @stack; | |||||||||||||||||||||||||||
| 196 | }, | ||||||||||||||||||||||||||||
| 197 | }, | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 3 | ); | |||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 3 | return \%params; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 2 | __PACKAGE__->_add_roles( qw[ | |||||||||||||||||||||||||||
| 206 | State::Closure | ||||||||||||||||||||||||||||
| 207 | Next::ClosedSelf | ||||||||||||||||||||||||||||
| 208 | Rewind::Closure | ||||||||||||||||||||||||||||
| 209 | Reset::Closure | ||||||||||||||||||||||||||||
| 210 | Current::Closure | ||||||||||||||||||||||||||||
| 211 | Prev::Closure | ||||||||||||||||||||||||||||
| 212 | ] ); | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 2 | 1; | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | # | ||||||||||||||||||||||||||||
| 217 | # This file is part of Iterator-Flex | ||||||||||||||||||||||||||||
| 218 | # | ||||||||||||||||||||||||||||
| 219 | # This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory. | ||||||||||||||||||||||||||||
| 220 | # | ||||||||||||||||||||||||||||
| 221 | # This is free software, licensed under: | ||||||||||||||||||||||||||||
| 222 | # | ||||||||||||||||||||||||||||
| 223 | # The GNU General Public License, Version 3, June 2007 | ||||||||||||||||||||||||||||
| 224 | # | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | __END__ | ||||||||||||||||||||||||||||