File Coverage

blib/lib/Quantum/Superpositions/Lazy/Util.pm
Criterion Covered Total %
statement 45 45 100.0
branch 6 6 100.0
condition 7 9 77.7
subroutine 11 11 100.0
pod 0 4 0.0
total 69 75 92.0


line stmt bran cond sub pod time code
1             package Quantum::Superpositions::Lazy::Util;
2             $Quantum::Superpositions::Lazy::Util::VERSION = '1.12';
3 15     15   124667 use v5.24;
  15         55  
4 15     15   68 use warnings;
  15         26  
  15         361  
5 15     15   66 use Exporter qw(import);
  15         24  
  15         411  
6 15     15   76 use Scalar::Util qw(blessed);
  15         23  
  15         730  
7 15     15   100 use List::Util qw(max);
  15         30  
  15         1628  
8 15     15   6368 use Random::Any qw(rand);
  15         4193  
  15         79  
9              
10             our @EXPORT_OK = qw(
11             is_collapsible
12             is_state
13             get_rand
14             get_iterator
15             );
16              
17             sub is_collapsible
18             {
19 16838     16838 0 28455 my ($item) = @_;
20              
21 16838   66     220291 return blessed $item && $item->DOES("Quantum::Superpositions::Lazy::Role::Collapsible");
22             }
23              
24             sub is_state
25             {
26 245819     245819 0 335921 my ($item) = @_;
27              
28 245819   66     737227 return blessed $item && $item->isa("Quantum::Superpositions::Lazy::State");
29             }
30              
31             # MUST return a value in range of [0, 1)
32 35     35 0 136 sub get_rand { rand() }
33              
34             sub get_iterator
35             {
36 146     146 0 308 my (@states) = @_;
37              
38 146         279 my @indexes = map { 0 } @states;
  299         485  
39 146         242 my @max_indexes = map { $#$_ } @states;
  299         523  
40              
41             # we can't iterate if one of the elements do not exist
42 146         234 my $finished = scalar grep { $_ < 0 } @max_indexes;
  299         582  
43             return sub {
44 122958     122958   192784 my ($with_indexes) = @_;
45 122958 100       186909 return if $finished;
46              
47 122876         151616 my $i = 0;
48             my @ret =
49 245819 100       356368 map { is_state($_) ? $_->value : $_ }
50 122876         174326 map { $states[$i++][$_] }
  245819         395189  
51             @indexes;
52              
53 122876 100       204862 if ($with_indexes) {
54 39529         101172 @ret = map { $indexes[$_], $ret[$_] } 0 .. max($#indexes, $#ret);
  79058         161973  
55             }
56              
57 122876         151579 $i = 0;
58 122876   100     348996 while ($i < @indexes && ++$indexes[$i] > $max_indexes[$i]) {
59 16097         20470 $indexes[$i] = 0;
60 16097         39104 $i += 1;
61             }
62              
63 122876         177741 $finished = $i == @indexes;
64 122876         327468 return @ret;
65 146         922 };
66             }
67              
68             1;
69