TruthTable/Util.pm

filter: Statementstmt 98.5% 68 / 69 Branchbran 78.5% 11 / 14 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 11 / 11 Podpod 100% 6 / 6 totaltotal 92.3% 97 / 105 CC 12 SCAR 9.8
CC
12
Cov
93%
shift_terms
4.1
var_column
3.0
rotate_terms
3.0
CRAP 2.7
« TruthTable/Convert81.pm ↑ Summary
1 =head1 NAME
2  
3 Logic::TruthTable::Util - provide utility functions to Logic::TruthTable
4  
5 =cut
6  
7 package Logic::TruthTable::Util;
8  
9 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use 5.016001;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 our @ISA = qw(Exporter);
16  
17 4 our %EXPORT_TAGS = (
18 all => [ qw(
19 push_minterm_columns
20 push_maxterm_columns
21 var_column
22 shift_terms
23 rotate_terms
24 reverse_terms
25 ) ],
26 );
27  
28 our @EXPORT_OK = (
29 4 @{$EXPORT_TAGS{all}},
30 );
31  
32 4 our $VERSION = 1.02;
33  
34 =head1 DESCRIPTION
35  
36 This module provides various utilities designed for (but not limited to)
37 creating or manipulating term lists for Logic::TruthTable.
38  
39 =cut
40  
41 =head2 FUNCTIONS
42  
43 =head3 push_minterm_columns()
44  
45 =head3 push_maxterm_columns()
46  
47 push_minterm_columns($idx, $dir, \@colx, \@coly, \@colz);
48  
49 or
50  
51 push_maxterm_columns($idx, $dir, \@colx, \@coly, \@colz);
52  
53 Often the outputs to be simulated by boolean expressions are values that
54 are split across more than one column. For example, say that you want
55 to model a function to direct a pointer that uses the eight
56 L<cardinal and ordinal |https://en.wikipedia.org/wiki/Points_of_the_compass#Compass_point_names>
57 compass directions, from North (value 0) to NorthWest (value 7).
58  
59 Numbering these directions takes three bits, which means you'd need three
60 columns to represent them.
61  
62 To make it easier to create these columns, C<push_minterm_columns()>
63 (or, if you prefer, C<push_maxterm_columns()>) will take a value
64 from your function and, for each set bit (or if using maxterms,
65 unset bit), will push the minterm (or maxterm) onto each array
66 corresponding to its column.
67  
68 For example, if the value of row 20 is 5 (in binary C<0b101>),
69 then a call to C<push_minterm_columns(20, 5, \@x, \@y, \@z);>
70 will push 20 onto array variables C<@x>, and C<@z>, while a call to
71 C<push_maxterm_columns(20, 5, \@x, \@y, \@z);> will push a 20 onto
72 array variable C<@y> only.
73  
74 Bit values past the available columns will simply be dropped, while
75 excess columns will either never have terms pushed on them
76 (C<push_minterm_columns()>) or always have terms pushed on them
77 (C<push_maxterm_columns()>).
78  
79 For example:
80  
81 #
82 # Each column gets its own term list.
83 # The don't-care terms will be common across
84 # all columns.
85 #
86 my(@col2, @col1, @col0, @dontcares);
87  
88 #
89 # For each cell, return a direction.
90 #
91 for my $idx (0..63)
92 {
93 my $dir = sp_moveto($idx);
94  
95 #
96 # In this example, a cell that cannot be exited cannot
97 # be entered either, so mark it as a don't-care.
98 #
99 if ($dir < 0 or $dir > 7)
100 {
101 push @dontcares, $idx;
102 }
103 else
104 {
105 #
106 # For any set bit in $dir, push $idx onto the corresponding
107 # column list.
108 #
109 push_minterm_columns($idx, $dir, \@col2, \@col1, \@col0);
110 }
111 }
112  
113 You will then have the minterms available for each column of your
114 truth table.
115  
116 my $dir_table = Logic::TruthTable->new(
117 title => "Sandusky Path",
118 width => 6,
119 vars => [qw(a b c d e f)],
120 functions => [qw(m2 m1 m0)],
121 columns => [
122 {
123 minterms => \@col2,
124 dontcares => \@dontcares,
125 },
126 {
127 minterms => \@col1,
128 dontcares => \@dontcares,
129 },
130 {
131 minterms => \@col0,
132 dontcares => \@dontcares,
133 } ],
134 );
135  
136 =cut
137  
138 sub push_minterm_columns
139 121 ▶{
Subroutine push_minterm_columns: called CC=3 SCAR=11.0
Pod documented
140 121 my($idx, $val, @colrefs) = @_;
141  
142 121 my $ncols = $#colrefs;
143 121 my $bit = 1 << $ncols;
144  
145 #
146 # Slice the bits across the columns.
147 #
148 121 for my $j (0..$ncols)
149 {
150 547 ▶ push @{ $colrefs[$j] }, $idx if ($val & $bit);
Branch: 1 branch
BranchTrueFalseTotal
if $val & $bit 191 356 547
151 547 $bit >>= 1;
152 }
153 }
154  
155 sub push_maxterm_columns
156 8 ▶{
Subroutine push_maxterm_columns: called CC=3 SCAR=11.0
Pod documented
157 8 my($idx, $val, @colrefs) = @_;
158  
159 8 my $ncols = $#colrefs;
160 8 my $bit = 1 << $ncols;
161  
162 #
163 # Slice the bits across the columns.
164 #
165 8 for my $j (0..$ncols)
166 {
167 16 ▶ push @{ $colrefs[$j] }, $idx unless ($val & $bit);
Branch: 1 branch
BranchTrueFalseTotal
unless $val & $bit 8 8 16
168 16 $bit >>= 1;
169 }
170 }
171  
172 =head3 var_column()
173  
174 Return the list of terms that correspond to the set bits of a
175 variable's column.
176  
177 my @terms = var_column($width, $col);
178  
179 For example, in a three-variable table
180  
181 x y z | f
182 -----------|--
183 0 | 0 0 0 |
184 1 | 0 0 1 |
185 2 | 0 1 0 |
186 3 | 0 1 1 |
187 4 | 1 0 0 |
188 5 | 1 0 1 |
189 6 | 1 1 0 |
190 7 | 1 1 1 |
191  
192 column 2 (the x column) has terms (4, 5, 6, 7) set, while column 0
193 (the z column) has terms (1, 3, 5, 7) set.
194  
195 =cut
196  
197 sub var_column
198 1 ▶{
Subroutine var_column: called CC=3 SCAR=11.0
Pod documented
199 1 my($width, $col) = @_;
200  
201 1 ▶ croak "Column $col doesn't exist in a $width-column table." if ($col >= $width);
Branch: 1 branch
BranchTrueFalseTotal
if $col >= $width 0 1 1
202  
203 #
204 # A 'block' is a sequence of ones in the column.
205 # 'blocklen' is the number of ones, blocks is the number of
206 # those sequences.
207 #
208 # So in a set of four variable columns, column one's ones come
209 # in eight sets of two ones.
210 #
211 1 my $blocklen = 1 << $col;
212 1 my $blocks = 1 << ($width - $col - 1);
213 1 my @terms;
214  
215 1 for my $n (0 .. $blocks - 1)
216 {
217 2 push @terms, map { (2 * $n + 1) * $blocklen + $_} (0 .. $blocklen - 1);
218 }
219  
220 1 return @terms;
221 }
222  
223 =head3 reverse_terms()
224  
225 Reverses the list of terms by index. For example, within a four-bit range:
226  
227 $width = 4; # values range 0 .. 15.
228 @terms = (1, 3, 6, 8, 13, 14);
229 @terms = reverse_terms($width, \@terms);
230  
231 The values in C<@terms> will become (14, 12, 9, 7, 2, 1).
232  
233 =cut
234  
235 sub reverse_terms
236 1 ▶{
Subroutine reverse_terms: called CC=1 SCAR=0.0
Pod documented
237 1 my($width, $tref) = @_;
238 1 my $last = (1 << $width) - 1;
239  
240 1 return map {$last - $_} @{ $tref };
241 }
242  
243 =head3 rotate_terms()
244  
245 Rotates the list of terms by index. For example, within a four-bit range:
246  
247 $width = 4; # values range 0 .. 15.
248 $shift = 5; # term 0 becomes term 5, term 1 becomes term 6
249 @terms = (1, 3, 7, 9, 13, 15);
250 @terms = rotate_terms($width, \@terms, $shift);
251  
252 The values in C<@terms> will become will be (6, 8, 12, 14, 2, 4),
253 with the last two list items rotated around to the beginning, having
254 been rotated past what C<$width> allows. A negative-valued shift rotates
255 the terms backward.
256  
257 =cut
258  
259 sub rotate_terms
260 3 ▶{
Subroutine rotate_terms: called CC=3 SCAR=11.0
Pod documented
261 3 my($width, $tref, $shift) = @_;
262 3 my $length = 1 << $width;
263  
264 3 $shift %= $length;
265  
266 3 ▶ $shift += $length while ($shift < 0);
Branch: 1 branch
BranchTrueFalseTotal
$shift < 0 and do { $shift += $length } 0 3 3
267  
268 3 ▶ return @{ $tref } if ($shift == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $shift == 0 1 2 3
269  
270 2 return map {($shift + $_) % $length} @{ $tref };
271 }
272  
273 =head3 shift_terms()
274  
275 Shifts the list of terms by index. For example, within a four-bit range:
276  
277 $width = 4; # values range 0 .. 15.
278 $shift = 5; # term 0 becomes term 5, term 1 becomes term 6
279 @terms = (1, 3, 7, 9, 13, 15);
280 @terms = shift_terms($width, \@terms, $shift);
281  
282 The values in C<@terms> will become (6, 8, 12, 14), with the last
283 two list items dropped, having been shifted past what C<$width> allows.
284 A negative-valued shift shifts the terms downward.
285  
286 =cut
287  
288 sub shift_terms
289 3 ▶{
Subroutine shift_terms: called CC=4 SCAR=14.2
Pod documented
290 3 my($width, $tref, $shift) = @_;
291 3 my $length = 1 << $width;
292  
293 3 ▶ if ($shift >= $length or $shift <= -$length)
Branch: 2 branches
BranchTrueFalseTotal
if ($shift >= $length or $shift <= -$length) { } 0 3 3
elsif ($shift > 0) { } 1 2 3
Condition33%1/3
$shift >= $length or $shift <= -$length
l!l&&r!l&&!r
003
MC/DC0%0/2
$shift >= $length or $shift <= -$length
$shift >= $length $shift <= -$length
Truth table33%1/3
A or B
A= $shift >= $lengthB= $shift <= -$length
ABresult
1X1
011
000
294 {
295 0 return ();
296 }
297 elsif ($shift > 0)
298 {
299 1 return map {$shift + $_} grep {$_ < ($length - $shift)} @{ $tref };
300 }
301 else
302 {
303 2 return map {$shift + $_} grep {$_ >= -$shift} @{ $tref };
304 }
305 }
306  
307 =head1 SEE ALSO
308  
309 L<Logic::TruthTable::Convert81>
310  
311 =head1 AUTHOR
312  
313 John M. Gamble C<< <jgamble@cpan.org> >>
314  
315 =cut
316  
317 4 1;
318  
319 __END__
« TruthTable/Convert81.pm ↑ Summary