QuineMcCluskey.pm

filter: Statementstmt 92.4% 184 / 199 Branchbran 79.6% 43 / 54 Conditioncond 77.7% 7 / 9 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 90.4% 19 / 21 Podpod 58.3% 7 / 12 totaltotal 87.6% 263 / 300 CC 41 SCAR 20.3
CC
41
Cov
89%
generate_primes
14.0
recurse_solve
12.1
BUILD
8.0
CRAP 7.6
↑ Summary QuineMcCluskey/Util.pm »
1 package Algorithm::QuineMcCluskey;
2  
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use 5.016001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 15 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 15 ▶use Algorithm::QuineMcCluskey::Util qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use List::Util qw(uniqnum);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 15 ▶use List::Compare::Functional qw(get_complement is_LequivalentR);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 15 extends 'Logic::Minimizer';
17  
18 #
19 # Vaguely consistent Smart-Comment rules:
20 # 3 pound signs for the code in BUILD() and generate_*() functions.
21 #
22 # 4 pound signs for code that manipulates prime/essentials/covers hashes:
23 # row_dominance().
24 #
25 # 5 pound signs for the solve() and recurse_solve() code, and the remels()
26 # calls.
27 #
28 # The ::Format package is only needed for Smart Comments -- comment or uncomment
29 # in concert with Smart::Comments as needed.
30 #
31 #use Algorithm::QuineMcCluskey::Format qw(arrayarray hasharray chart);
32 #use Smart::Comments ('###', '#####');
33  
34 #
35 # Attributes inherited from Logic::Minimizer are width, minterms, maxterms,
36 # dontcares, columnstring, title, dc, vars, primes, essentials, covers,
37 # group_symbols, order_by, and minonly.
38 #
39  
40 #
41 # The '_bits' fields are the terms' bitstring fields, and are
42 # internal attributes. No setting them at object creation.
43 #
44 15 has 'dc_bits' => (
45 isa => 'ArrayRef[Str]', is => 'rw', required => 0,
46 init_arg => undef,
47 predicate => 'has_dc_bits'
48 );
49 15 has 'min_bits' => (
50 isa => 'ArrayRef[Str]', is => 'rw', required => 0,
51 init_arg => undef,
52 predicate => 'has_min_bits'
53 );
54 15 has 'max_bits' => (
55 isa => 'ArrayRef[Str]', is => 'rw', required => 0,
56 init_arg => undef,
57 predicate => 'has_max_bits'
58 );
59  
60 15 our $VERSION = 1.01;
61  
62 sub BUILD
63 36 ▶{
Subroutine BUILD: called CC=8 SCAR=20.8
Pod undocumented
64 36 my $self = shift;
65 36 my $w = $self->width;
66 36 my $last_idx = (1 << $w) - 1;
67 36 my @terms;
68  
69 #
70 # Catch errors involving minterms, maxterms, and don't-cares.
71 #
72 36 $self->catch_errors();
73  
74 #
75 # We've gotten past the error-checking. Create the object.
76 #
77 36 ▶ if ($self->has_columnstring)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_columnstring) 1 35 36
78 {
79 1 my($min_ref, $max_ref, $dc_ref) =
80 $self->list_to_terms(split(//, $self->columnstring));
81  
82 ### min_ref: $min_ref
83 ### max_ref: $max_ref
84 ### don't cares: $dc_ref
85  
86 1 ▶ $self->minterms($min_ref) if (scalar @{$min_ref} );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$min_ref;} 1 0 1
87 1 ▶ $self->dontcares($dc_ref) if (scalar @{$dc_ref} );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$dc_ref;} 0 1 1
88 }
89  
90 36 ▶ if ($self->has_minterms)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_minterms) 31 5 36
91 {
92 31 @terms = sort(uniqnum(@{$self->minterms}));
93  
94 my @bitstrings = map {
95 31 substr(unpack("B32", pack("N", $_)), -$w)
96 } @terms;
97  
98 31 $self->min_bits(\@bitstrings);
99 ### Min terms binary: $self->min_bits()
100 }
101 36 ▶ if ($self->has_maxterms)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_maxterms) 5 31 36
102 {
103 5 @terms = sort(uniqnum(@{$self->maxterms}));
104  
105 my @bitstrings = map {
106 5 substr(unpack("B32", pack("N", $_)), -$w)
107 } @terms;
108  
109 5 $self->max_bits(\@bitstrings);
110 ### Max terms binary: $self->max_bits()
111 }
112  
113 36 ▶ if ($self->has_dontcares)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_dontcares) 19 17 36
114 {
115 19 my @dontcares = sort(uniqnum(@{$self->dontcares}));
116  
117 my @bitstrings = map {
118 19 substr(unpack("B32", pack("N", $_)), -$w)
119 } @dontcares;
120  
121 19 $self->dc_bits(\@bitstrings);
122 ### Don't-cares binary: $self->dc_bits()
123 }
124  
125 36 ▶ $self->title("$w-variable truth table") unless ($self->has_title);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_title 0 36 36
126  
127 36 return $self;
128 }
129  
130 sub complement_terms
131 2 ▶{
Subroutine complement_terms: called CC=3 SCAR=12.3
Pod undocumented
132 2 my $self = shift;
133 2 my @bitlist = (0 .. (1 << $self->width) - 1);
134 0 ▶ my @termlist = @{$self->dontcares} if ($self->has_dontcares);
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_dontcares 0 2 2
135  
136 2 ▶ if ($self->has_minterms)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_minterms) { } 2 0 2
137 {
138 2 push @termlist, @{ $self->minterms };
139 }
140 else
141 {
142 0 push @termlist, @{ $self->maxterms };
143 }
144  
145 2 return get_complement([\@termlist, \@bitlist]);
146 }
147  
148 #
149 # Build another Quine-McCluskey object that's the complement
150 # of the existing object.
151 #
152 sub complement
153 1 ▶{
Subroutine complement: called CC=3 SCAR=11.7
Pod documented
154 1 my $self = shift;
155 1 my %term;
156  
157 0 ▶ $term{dontcares} = [@{$self->dontcares}] if ($self->has_dontcares);
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_dontcares 0 1 1
158  
159 1 ▶ if ($self->has_minterms)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_minterms) { } 1 0 1
160 {
161 1 $term{minterms} = [$self->complement_terms()];
162 }
163 else
164 {
165 0 $term{maxterms} = [$self->complement_terms()];
166 }
167  
168 1 my $title = "Complement of '" . $self->title() . "'";
169  
170 return Algorithm::QuineMcCluskey->new(
171 title => $title,
172 width => $self->width,
173 dc => $self->dc,
174 1 vars => [ @{$self->vars} ],
175 %term
176 );
177 }
178  
179 #
180 # Build another Quine-McCluskey object that's the dual
181 # of the existing object.
182 #
183 sub dual
184 1 ▶{
Subroutine dual: called CC=3 SCAR=11.4
Pod documented
185 1 my $self = shift;
186 1 my $last = (1 << $self->width) - 1;
187 1 my %term;
188  
189 0 ▶ $term{dontcares} = [@{$self->dontcares}] if ($self->has_dontcares);
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_dontcares 0 1 1
190  
191 1 my @dualterms = sort map {$last - $_} $self->complement_terms();
192  
193 1 ▶ if ($self->has_minterms)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_minterms) { } 1 0 1
194 {
195 1 $term{minterms} = [@dualterms];
196 }
197 else
198 {
199 0 $term{maxterms} = [@dualterms];
200 }
201  
202 1 my $title = "Dual of '" . $self->title() . "'";
203  
204 return Algorithm::QuineMcCluskey->new(
205 title => $title,
206 width => $self->width,
207 dc => $self->dc,
208 1 vars => [ @{$self->vars} ],
209 %term
210 );
211 }
212  
213 sub all_bit_terms
214 30 ▶{
Subroutine all_bit_terms: called CC=3 SCAR=11.0
Pod undocumented
215 30 my $self = shift;
216 30 my @terms;
217  
218 30 ▶ push @terms, ($self->has_min_bits)? @{$self->min_bits}: @{$self->max_bits};
Branch: 1 branch
BranchTrueFalseTotal
$self->has_min_bits ? : 26 4 30
219 30 ▶ push @terms, @{ $self->dc_bits } if ($self->has_dc_bits);
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_dc_bits 19 11 30
220 30 return sort @terms;
221 }
222  
223 sub minmax_bit_terms
224 30 ▶{
Subroutine minmax_bit_terms: called CC=2 SCAR=6.9
Pod undocumented
225 30 my $self = shift;
226  
227 30 ▶ return ($self->has_min_bits)? @{$self->min_bits}: @{$self->max_bits};
Branch: 1 branch
BranchTrueFalseTotal
$self->has_min_bits ? : 26 4 30
228 }
229  
230 sub generate_primes
231 30 ▶{
Subroutine generate_primes: called CC=14 SCAR=26.4
Pod documented
232 30 my $self = shift;
233 30 my @bits;
234 my %implicant;
235  
236 #
237 # Separate into bins based on number of 1's (the weight).
238 #
239 ### generate_primes() group the bit terms
240 #
241 30 for ($self->all_bit_terms())
242 {
243 429 my $setcount = scalar(() = $_ =~ m/1/g);
244 429 push @{$bits[0][ $setcount ]}, $_;
245 }
246  
247 #
248 # Now for each level, we look for terms that can be absorbed into
249 # simpler product terms (for example, _ab_c + ab_c can be simplified
250 # to b_c).
251 #
252 # Level 0 consists of the fundemental
253 # product terms; level 1 consists of pairs of fundemental terms
254 # that have a variable in common; level 2 consists of pairs of pairs
255 # that have a variable in common; and so on until we're out of
256 # levels (number of variables) or cannot find any more products
257 # with terms in common.
258 #
259 30 for my $level (0 .. $self->width)
260 {
261 #
262 # Skip if we haven't generated data for this level.
263 #
264 125 ▶ last unless ref $bits[$level];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $bits[$level] 26 99 125
265  
266 #
267 ### Level: $level
268 ### grouped by bit count: $bits[$level]
269 #
270 # Find pairs with Hamming distance of 1 (i.e., a weight
271 # difference of 1).
272 #
273 99 for my $low (0 .. $#{ $bits[$level] })
274 {
275 485 my %nextlvlimp;
276  
277 #
278 # These nested for-loops get all permutations
279 # of adjacent sets.
280 #
281 485 for my $lv (@{ $bits[$level][$low] })
282 {
283 #
284 # Initialize the implicant as unused.
285 #
286 1471 ▶ $implicant{$lv} //= 0;
Condition100%2/2
$implicant{$lv} //= 0
l!l
1154317
MC/DC100%1/1
$implicant{$lv} //= 0
$implicant{$lv}
Truth table100%2/2
A
A= $implicant{$lv}
Aresult
11
00
287  
288 #
289 # Skip ahead if there are no terms at
290 # this level.
291 #
292 1471 ▶ next unless ref $bits[$level][$low + 1];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $bits[$level][$low + 1] 340 1131 1471
293  
294 1131 for my $hv (@{ $bits[$level][$low + 1] })
295 {
296 #
297 # Initialize the implicant.
298 #
299 9080 ▶ $implicant{$hv} //= 0;
Condition100%2/2
$implicant{$hv} //= 0
l!l
79261154
MC/DC100%1/1
$implicant{$hv} //= 0
$implicant{$hv}
Truth table100%2/2
A
A= $implicant{$hv}
Aresult
11
00
300  
301 #
302 # If there are matching terms, save
303 # the new implicant at the next 'level',
304 # creating the level if it doesn't exist.
305 #
306 9080 my $hd1pos = hammingd1pos($lv, $hv);
307 9080 ▶ if ($hd1pos != -1)
Branch: 1 branch
BranchTrueFalseTotal
if ($hd1pos != -1) 1472 7608 9080
308 {
309 1472 my $new = $lv; # or $hv
310 1472 substr($new, $hd1pos, 1) = $self->dc;
311  
312 #
313 ### Compared: $lv
314 ### to : $hv
315 ### saving : $new
316 #
317 # Save the new implicant to the
318 # next level, then mark the two
319 # values as used.
320 #
321 1472 $nextlvlimp{$new} = 1;
322 1472 $implicant{$lv} = 1;
323 1472 $implicant{$hv} = 1;
324 }
325 }
326 }
327  
328 #
329 # Push those next-level implicants to the next level,
330 # assuming we found any.
331 #
332 485 ▶ push @{ $bits[$level + 1][$low + 1] }, keys %nextlvlimp if (%nextlvlimp);
Branch: 1 branch
BranchTrueFalseTotal
if %nextlvlimp 177 308 485
333 }
334 }
335  
336 #
337 ### generate_primes() implicant hash (we use the unmarked entries
338 ### [i.e., prime => 0] ) : %implicant
339 #
340  
341 #
342 # For each unmarked (value == 0) implicant, match it against the
343 # minterms (or maxterms). The resulting hash of arrays is our
344 # set of prime implicants.
345 #
346 30 my %p;
347 30 my @bit_terms = $self->minmax_bit_terms();
348  
349 30 for my $unmarked (grep { !$implicant{$_} } keys %implicant)
350 {
351 227 my @matched = maskedmatch($unmarked, @bit_terms);
352 227 ▶ $p{$unmarked} = [@matched] if (@matched);
Branch: 1 branch
BranchTrueFalseTotal
if @matched 212 15 227
353 }
354  
355 #
356 ### generate_primes() -- prime implicants: hasharray(\%p)
357 #
358 30 return \%p;
359 }
360  
361 sub generate_covers
362 28 ▶{
Subroutine generate_covers: called CC=1 SCAR=0.0
Pod documented
363 28 my $self = shift;
364  
365 28 return [ $self->recurse_solve($self->get_primes, 0) ];
366 }
367  
368 sub generate_essentials
369 0 ▶{
Subroutine generate_essentials: not called CC=1 SCAR=6.9
Pod documented
370 0 my $self = shift;
371  
372 0 return [sort find_essentials($self->get_primes) ];
373 }
374  
375 sub solve
376 28 ▶{
Subroutine solve: called CC=1 SCAR=0.0
Pod documented
377 28 my $self = shift;
378 28 my $c = $self->get_covers();
379  
380 ### solve -- get_covers() returned: arrayarray($c)
381  
382 28 return $self->to_boolean($c->[0]);
383 }
384  
385 sub all_solutions
386 0 ▶{
Subroutine all_solutions: not called CC=1 SCAR=6.9
Pod documented
387 0 my $self = shift;
388 0 my $c = $self->get_covers();
389  
390 ### all_solutions -- get_covers() returned: arrayarray($c)
391  
392 0 return map {$self->to_boolean($_)} @$c;
393 }
394  
395 #
396 # recurse_solve
397 #
398 # Recursive divide-and-conquer solver
399 #
400 # "To reduce the complexity of the prime implicant chart:
401 #
402 # 1. Select all the essential prime impliciants. If these PIs cover all
403 # minterms, stop; otherwise go the second step.
404 #
405 # 2. Apply Rules 1 and 2 to eliminate redundant rows and columns from
406 # the PI chart of non-essential PIs. When the chart is thus reduced,
407 # some PIs will become essential (i.e., some columns will have a single
408 # 'x'. Go back to step 1."
409 #
410 # Introduction To Logic Design, by Sajjan G. Shiva, page 129.
411 #
412 sub recurse_solve
413 54 ▶{
Subroutine recurse_solve: called CC=12 SCAR=25.0
Pod undocumented
414 54 my $self = shift;
415 54 my %primes = %{ $_[0] };
416 54 my $level = $_[1];
417 54 my @prefix;
418 my @covers;
419 54 my @essentials;
420  
421 #
422 ##### recurse_solve() level: $level
423 ##### recurse_solve() called with
424 ##### primes: "\n" . chart(\%primes, $self->width)
425 #
426 54 my @essentials_next = find_essentials(\%primes);
427  
428 #
429 ##### Begin prefix/essentials loop.
430 #
431 do
432 54 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
is_LequivalentR([[@essentials], [@essentials_next]]) or do { do { @essentials = @essentials_next; purge_elements(\%primes, @essentials); push @prefix, @essentials; my(@rows) = row_dominance(\%primes, 1); delete $primes{$_} foreach (@rows); my(%cols) = transpose(\%primes); my(@cols) = row_dominance(\%cols, 0); remels(\%primes, @cols); @essentials_next = find_essentials(\%primes) } } 54 45 99
433 #
434 ##### recurse_solve() do loop, essentials: @essentials
435 #
436 # Remove the essential prime implicants from
437 # the prime implicants table.
438 #
439 99 @essentials = @essentials_next;
440  
441 #
442 ##### Purging prime hash of: "[" . join(", ", sort @essentials) . "]"
443 #
444 99 purge_elements(\%primes, @essentials);
445 99 push @prefix, @essentials;
446  
447 ##### recurse_solve() @prefix now: "[" . join(", ", sort @prefix) . "]"
448  
449 #
450 # Now eliminate dominated rows and columns.
451 #
452 # Rule 1: A row dominated by another row can be eliminated.
453 # Rule 2: A column that dominated another column can be eliminated.
454 #
455 #### Looking for rows dominated by other rows
456 #### primes table: "\n" . chart(\%primes, $self->width)
457 99 my @rows = row_dominance(\%primes, 1);
458 99 delete $primes{$_} for (@rows);
459 #### row_dominance returns rows for removal: "[" . join(", ", @rows) . "]"
460 #### primes now: "\n" . chart(\%primes, $self->width)
461  
462 99 my %cols = transpose(\%primes);
463 99 my @cols = row_dominance(\%cols, 0);
464 99 remels(\%primes, @cols);
465 #### row_dominance returns cols for removal: "[" . join(", ", @cols) . "]"
466 #### primes now: "\n" . chart(\%primes, $self->width)
467  
468 99 @essentials_next = find_essentials(\%primes);
469  
470 ##### recurse_solve() essentials after purge/dom: @essentials
471  
472 } until (is_LequivalentR([
473 [ @essentials] => [ @essentials_next ]
474 ]));
475  
476 54 ▶ return [ reverse sort @prefix ] unless (keys %primes);
Branch: 1 branch
BranchTrueFalseTotal
unless keys %primes 18 36 54
477  
478 #
479 # Find a term (there may be more than one) that has the least
480 # number of prime implicants covering it, and a list of those
481 # prime implicants. Use that list to figure out the best set
482 # to cover the rest of the terms.
483 #
484 ##### recurse_solve() Primes after loop
485 ##### primes: "\n" . chart(\%primes, $self->width)
486 #
487 36 my($term, @ta) = covered_least(\%primes);
488  
489 #
490 ##### Least Covered term: $term
491 ##### Covered by: @ta
492 #
493 # Make a copy of the section of the prime implicants
494 # table that don't cover that term.
495 #
496 my %r = map {
497 36 $_ => [ grep { $_ ne $term } @{ $primes{$_} } ]
498 } keys %primes;
499  
500 #
501 # For each such cover, recursively solve the table with that column
502 # removed and add the result(s) to the covers table after adding
503 # back the removed term.
504 #
505 36 for my $ta (@ta)
506 {
507 74 my (@c, @results);
508 74 my %reduced = %r;
509  
510 #
511 # Use this prime implicant -- delete its row and columns
512 #
513 ##### Purging reduced hash of: $ta
514 #
515 74 purge_elements(\%reduced, $ta);
516  
517 74 ▶ if (keys %reduced and scalar(@c = $self->recurse_solve(\%reduced, $level + 1)))
Branch: 1 branch
BranchTrueFalseTotal
if (keys %reduced and scalar(@c = $self->recurse_solve(\%reduced, $level + 1))) { } 26 48 74
Condition100%2/2
keys %reduced and scalar(@c = $self->recurse_solve(\%reduced, $level + 1))
!ll
4826
MC/DC100%1/1
keys %reduced and scalar(@c = $self->recurse_solve(\%reduced, $level + 1))
keys %reduced
Truth table100%2/2
A
A= keys %reduced
Aresult
00
11
518 {
519 #
520 ##### recurse_solve() at level: $level
521 ##### returned (in loop): arrayarray(\@c)
522 #
523 26 @results = map { [ reverse sort (@prefix, $ta, @$_) ] } @c;
524 }
525 else
526 {
527 48 @results = [ reverse sort (@prefix, $ta) ]
528 }
529  
530 74 push @covers, @results;
531  
532 #
533 ##### Covers now at: arrayarray(\@covers)
534 #
535 }
536  
537 #
538 ##### Weed out the duplicated and expensive solutions.
539 #
540 36 @covers = uniqels @covers;
541  
542 36 ▶ if ($self->minonly and scalar @covers > 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->minonly and scalar @covers > 1) 36 0 36
Condition33%1/3
$self->minonly and scalar @covers > 1
!ll&&!rl&&r
0036
MC/DC0%0/2
$self->minonly and scalar @covers > 1
$self->minonly scalar @covers > 1
Truth table33%1/3
A and B
A= $self->minonlyB= scalar @covers > 1
ABresult
0X0
100
111
543 {
544 36 my @weededcovers = shift @covers;
545 36 my $mincost = matchcount(join('', @{$weededcovers[0]}), "[01]");
546  
547 36 for my $c (@covers)
548 {
549 88 my $cost = matchcount(join('', @$c), "[01]");
550  
551 #
552 ##### Cover: join(',', @$c)
553 ##### Cost: $cost
554 #
555 88 ▶ next if ($cost > $mincost);
Branch: 1 branch
BranchTrueFalseTotal
if $cost > $mincost 8 80 88
556  
557 80 ▶ if ($cost < $mincost)
Branch: 1 branch
BranchTrueFalseTotal
if ($cost < $mincost) 0 80 80
558 {
559 0 $mincost = $cost;
560 0 @weededcovers = ();
561 }
562 80 push @weededcovers, $c;
563 }
564 36 @covers = @weededcovers;
565 }
566  
567 #
568 ##### Covers is: arrayarray(\@covers)
569 ##### after the weeding out.
570 #
571 # Return our covers table to be treated similarly one level up.
572 #
573 36 return @covers;
574 }
575  
576 15 1;
577 __END__
↑ Summary QuineMcCluskey/Util.pm »