| 1 | package Encode::Wechsler; | ||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 4 | my $i = 0; | |||||||||||||||||||||||||||
| 7 | # {0, 1, 2, ..., 8, 9, a, b, ..., v} correspond to the bitstrings {'00000', '00001', '00010', ..., '11111'}. | ||||||||||||||||||||||||||||
| 8 | 4 | our %bits = map { $_ => sprintf("%05d", unpack( 'B32', pack( 'N', $i++ ) ) ) } 0 .. 9, 'a' .. 'v'; | |||||||||||||||||||||||||||
| 9 | 4 | our %stib = reverse %bits; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 4 | $i = 4; | |||||||||||||||||||||||||||
| 12 | # We use the characters 'w' and 'x' to abbreviate '00' and '000' | ||||||||||||||||||||||||||||
| 13 | # the symbols {'y0', 'y1', y2', ..., 'yx', 'yy', 'yz'} correspond to runs of between 4 and 39 consecutive '0's. | ||||||||||||||||||||||||||||
| 14 | 4 | our %zero = map { 'y' . $_ => 0 x $i++ } 0 .. 9, 'a' .. 'z'; | |||||||||||||||||||||||||||
| 15 | 4 | $zero{w} = '00'; | |||||||||||||||||||||||||||
| 16 | 4 | $zero{x} = '000'; | |||||||||||||||||||||||||||
| 17 | #our %orez = reverse %zero; | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | sub new { | ||||||||||||||||||||||||||||
| 20 | 7 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | 7 | return bless {@_}, $self; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub encode { | ||||||||||||||||||||||||||||
| 26 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
encode: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 2 | my %args = @_; | |||||||||||||||||||||||||||
| 28 | 2 | my $thingy = $args{board}; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # multi-line string | ||||||||||||||||||||||||||||
| 31 | 2 | ▶ | $thingy = [ split "\n", $thingy ] unless ref( $thingy ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # array of strings | ||||||||||||||||||||||||||||
| 34 | 2 | ▶ | $thingy = [ map [ split //, $_ ], @$thingy ] unless ref( $thingy->[0] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # all become 2D array of true or false values | ||||||||||||||||||||||||||||
| 37 | 2 | ▶ | $thingy = [ map [ map { $_ eq '.' ? 0 : 1 } @$_ ], @$thingy ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =for later | ||||||||||||||||||||||||||||
| 40 | my @chunks; | ||||||||||||||||||||||||||||
| 41 | my $start_pruning; | ||||||||||||||||||||||||||||
| 42 | for (my $i = 0; $i < @$thingy; $i += 5) { | ||||||||||||||||||||||||||||
| 43 | # all zeros are ok until we found our first non all zeros | ||||||||||||||||||||||||||||
| 44 | # then prune all zeros that are "trailing" | ||||||||||||||||||||||||||||
| 45 | my @chunk; | ||||||||||||||||||||||||||||
| 46 | for (@$thingy[ $i .. $i + 4 ]) { | ||||||||||||||||||||||||||||
| 47 | $start_pruning ||= int( join '', @$_ ); | ||||||||||||||||||||||||||||
| 48 | if ($start_pruning) { | ||||||||||||||||||||||||||||
| 49 | push @chunk, $_ if ref($_) && int( join '', @$_ ); | ||||||||||||||||||||||||||||
| 50 | } else { | ||||||||||||||||||||||||||||
| 51 | push @chunk, $_ if ref($_); | ||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | push @chunks, [@chunk] if @chunk; | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | =cut | ||||||||||||||||||||||||||||
| 57 | 2 | my @chunks; | |||||||||||||||||||||||||||
| 58 | 2 | ▶ | for (my $i = 0; $i < @$thingy; $i += 5) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | my @chunk = map { | ||||||||||||||||||||||||||||
| 60 | 3 | ▶ | ref( $_ ) && int( join '', @$_ ) ? $_ : () | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $_ and int join("", @$_)
MC/DC100%2/2
ref $_ and int join("", @$_)
ref $_
int join("", @$_)
Truth table100%3/3
A and B
A= ref $_B= int join("", @$_)
| |||||||||||||||||||||||||||||
| 61 | } @$thingy[ $i .. $i + 4 ]; | ||||||||||||||||||||||||||||
| 62 | 3 | push @chunks, [@chunk]; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 2 | @chunks = map _transpose( $_ ), @chunks; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | #TODO: detect consecutive zero runs | ||||||||||||||||||||||||||||
| 68 | 2 | my @bits; | |||||||||||||||||||||||||||
| 69 | 2 | for my $chunk (@chunks) { | |||||||||||||||||||||||||||
| 70 | push @bits, join '', | ||||||||||||||||||||||||||||
| 71 | 25 | map { $stib{$_} } | |||||||||||||||||||||||||||
| 72 | 3 | map { sprintf '%05d', join '', reverse @$_ } @$chunk | |||||||||||||||||||||||||||
| 73 | ; | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # this could be removed by detecting consecutive zero runs | ||||||||||||||||||||||||||||
| 77 | 2 | my $str = join 'z', @bits; | |||||||||||||||||||||||||||
| 78 | 2 | $str =~ s/0*$//; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 2 | return $str; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | sub decode { | ||||||||||||||||||||||||||||
| 84 | 9 | ▶ | my ($self,$code) = @_; | ||||||||||||||||||||||||||
|
Subroutine
decode: called CC=15 SCAR=27.1
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 9 | $code =~ s/^\s+//; | |||||||||||||||||||||||||||
| 87 | 9 | $code =~ s/\s+$//; | |||||||||||||||||||||||||||
| 88 | 9 | ▶ | die "invalid format: $code\n" unless $code =~ /x[spq][0-9]+_[0-9a-z]+/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 9 | my ($prefix,$format) = split '_', $code, 2; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 9 | $self->{max} = 0; | |||||||||||||||||||||||||||
| 93 | $format = join 'z', map { | ||||||||||||||||||||||||||||
| 94 | 9 | s/(w|x|y.)/$zero{$1}/g; | |||||||||||||||||||||||||||
| 95 | 23 | ▶ | $self->{max} = length($_) if length($_) > $self->{max}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 23 | $_; | |||||||||||||||||||||||||||
| 97 | } split 'z', $format; | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 9 | my @grid; | |||||||||||||||||||||||||||
| 100 | 9 | for my $part (split 'z', $format ) { | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 23 | ▶ | if (length($part) < $self->{max}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 14 | $part .= '0' x ( $self->{max} - length($part) ); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # pad left and right | ||||||||||||||||||||||||||||
| 107 | 23 | ▶ | $part = ('0' x $self->{pad}) . $part . ('0' x $self->{pad}) if $self->{pad}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 23 | my $i = 0; | |||||||||||||||||||||||||||
| 110 | 23 | for (split '', $part) { | |||||||||||||||||||||||||||
| 111 | 342 | push @{ $grid[$i] }, map int $_, reverse split //, $bits{$_}; | |||||||||||||||||||||||||||
| 112 | 342 | $i++; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 9 | my @trans; | |||||||||||||||||||||||||||
| 117 | 9 | for my $i (reverse 0 .. $#{ $grid[0] }) { | |||||||||||||||||||||||||||
| 118 | 115 | ▶ | push @trans, [ map $_->[$i] || 0, @grid ]; | ||||||||||||||||||||||||||
|
Condition100%2/2
$_->[$i] || 0
MC/DC100%1/1
$_->[$i] || 0
$_->[$i]
Truth table100%2/2
A
A= $_->[$i]
| |||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | #remove leading blank rows | ||||||||||||||||||||||||||||
| 122 | 9 | for (@trans) { | |||||||||||||||||||||||||||
| 123 | 19 | ▶ | if (_sum( @$_ )) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 9 | last; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | 10 | shift @trans; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 9 | @grid = reverse @trans; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 9 | ▶ | if ($self->{pad}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 5 | ▶ | unshift @grid, ([(0) x ($self->{max} + (2 * $self->{pad}))]) x $self->{pad} if _sum( $grid[ 0] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 5 | ▶ | push @grid, ([(0) x ($self->{max} + (2 * $self->{pad}))]) x $self->{pad} if _sum( $grid[-1] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 9 | ▶ | return wantarray ? @grid : $self->_to_string( @grid ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | sub _to_string { | ||||||||||||||||||||||||||||
| 141 | 9 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_to_string: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 142 | 9 | my $str = ''; | |||||||||||||||||||||||||||
| 143 | 9 | for (@_) { | |||||||||||||||||||||||||||
| 144 | 133 | ▶ | $str .= $_ ? '*' : '.' for @$_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 133 | $str .= "\n"; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | 9 | return $str; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | sub _sum { | ||||||||||||||||||||||||||||
| 151 | 29 | ▶ | my $s = 0; | ||||||||||||||||||||||||||
|
Subroutine
_sum: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 152 | 29 | $s += $_ for @_; | |||||||||||||||||||||||||||
| 153 | 29 | return $s; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | # credit: Math::Matrix | ||||||||||||||||||||||||||||
| 157 | sub _transpose { | ||||||||||||||||||||||||||||
| 158 | 3 | ▶ | my $data = shift; | ||||||||||||||||||||||||||
|
Subroutine
_transpose: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 159 | 3 | my @trans; | |||||||||||||||||||||||||||
| 160 | 3 | for my $i (0 .. $#{ $data->[0] }) { | |||||||||||||||||||||||||||
| 161 | 25 | push @trans, [ map $_->[$i], @$data ] | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | 3 | return \@trans; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 4 | 1; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | __END__ | ||||||||||||||||||||||||||||