| 1 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | Logic::TruthTable::Convert81 - provide Base81 encoding to Logic::TruthTable | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | package Logic::TruthTable::Convert81; | ||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 14 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 14 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 14 | ▶ | use 5.016001; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 14 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 14 | ▶ | use Exporter; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 14 | ▶ | use Convert::Base81 qw(b3_pack81 b3_unpack81 base81_check); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 14 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 14 | our %EXPORT_TAGS = ( | |||||||||||||||||||||||||||||||||||||||||
| 20 | all => [ qw( | ||||||||||||||||||||||||||||||||||||||||||
| 21 | terms_to_base81 | ||||||||||||||||||||||||||||||||||||||||||
| 22 | terms_from_base81 | ||||||||||||||||||||||||||||||||||||||||||
| 23 | ) ], | ||||||||||||||||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | our @EXPORT_OK = ( | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 14 | @{$EXPORT_TAGS{all}}, | |||||||||||||||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 14 | our $VERSION = 1.02; | |||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | This module provides Base81 encoding of the truth table's columns | ||||||||||||||||||||||||||||||||||||||||||
| 35 | when saving the table in JSON format. | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | =head2 FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | =head3 terms_to_base81 | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | Take the terms of a truth table's column and pack it into a Base81 string. | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | $col = $tt->fncolumn("f1"); | ||||||||||||||||||||||||||||||||||||||||||
| 44 | $b81str = terms_to_base81($tt->width, $col->has_minterms, | ||||||||||||||||||||||||||||||||||||||||||
| 45 | $col->has_minterms? $col->minterms: $col->maxterms, | ||||||||||||||||||||||||||||||||||||||||||
| 46 | $col->dontcares); | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | sub terms_to_base81 | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 6 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
terms_to_base81: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 6 | my($width, $isminterms, $termref, $dontcaresref)= @_; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 6 | ▶ | my ($dfltbit, $setbit, $dcbit) = ($isminterms)? qw(0 1 -): qw(1 0 -); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | # | ||||||||||||||||||||||||||||||||||||||||||
| 56 | # Set up the list of trits to be packed into Base81 code. | ||||||||||||||||||||||||||||||||||||||||||
| 57 | # | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 6 | my @blist = ($dfltbit) x (1 << $width); | |||||||||||||||||||||||||||||||||||||||||
| 59 | 6 | map {$blist[$_] = $setbit} @{$termref}; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 6 | map {$blist[$_] = $dcbit} (@{$dontcaresref}); | |||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 6 | return b3_pack81("01-", \@blist); | |||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | =head3 terms_from_base81 | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | Retrieve arrayrefs of the minterms, maxterms, and don't-cares of a | ||||||||||||||||||||||||||||||||||||||||||
| 68 | truth table's column from a Base81 string. | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | (@min_max_dc) = terms_from_base81($width, $b81str); | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | sub terms_from_base81 | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 4 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
terms_from_base81: called CC=8 SCAR=23.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 4 | my($width, $base81str) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | # | ||||||||||||||||||||||||||||||||||||||||||
| 79 | # Does the string we read in create a column of the correct length? | ||||||||||||||||||||||||||||||||||||||||||
| 80 | # (With the edge case exception width == 1, of course.) | ||||||||||||||||||||||||||||||||||||||||||
| 81 | # | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 4 | ▶ | unless (length($base81str) == (1 << ($width - 2)) or | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition16%1/6
length $base81str == 1 << $width - 2 or length $base81str == 1 and $width == 1
length $base81str == 1 and $width == 1
MC/DC0%0/3
length $base81str == 1 << $width - 2 or length $base81str == 1 and $width == 1
length $base81str == 1 << $width - 2
length $base81str == 1
$width == 1
Truth table25%1/4
A or B and C
A= length $base81str == 1 << $width - 2B= length $base81str == 1C= $width == 1
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | (length($base81str) == 1 and $width == 1)) | ||||||||||||||||||||||||||||||||||||||||||
| 84 | { | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | return (undef, undef, undef); | |||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 4 | my @char81 = split(//, $base81str); | |||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 4 | ▶ | if (my $c_idx = base81_check($base81str) >= 0) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | { | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | carp "Incorrect character '" . $char81[$c_idx] . | |||||||||||||||||||||||||||||||||||||||||
| 93 | "' at position " . $c_idx . | ||||||||||||||||||||||||||||||||||||||||||
| 94 | "; cannot create columnlist"; | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | return (undef, undef, undef); | |||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 4 | my(@maxterms, @minterms, @dontcares); | |||||||||||||||||||||||||||||||||||||||||
| 99 | 4 | my @clist = b3_unpack81("01-", $base81str); | |||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 4 | for my $t (0 .. $#clist) | |||||||||||||||||||||||||||||||||||||||||
| 102 | { | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 128 | my $x = $clist[$t]; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 128 | ▶ | if ($x eq '1') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | { | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 36 | push @minterms, $t; | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | elsif ($x eq '0') | ||||||||||||||||||||||||||||||||||||||||||
| 109 | { | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 44 | push @maxterms, $t; | |||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | else | ||||||||||||||||||||||||||||||||||||||||||
| 113 | { | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 48 | push @dontcares, $t; | |||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 4 | return (\@minterms, \@maxterms, \@dontcares); | |||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | L<Convert::Base81> | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | L<Logic::TruthTable::Util> | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | John M. Gamble C<< <jgamble@cpan.org> >> | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | 14 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | __END__ | ||||||||||||||||||||||||||||||||||||||||||