QuineMcCluskey/Util.pm

filter: Statementstmt 100% 127 / 127 Branchbran 96.1% 25 / 26 Conditioncond 88.2% 15 / 17 MC/DCmcdc 80.0% 8 / 10 Subroutinesub 100% 18 / 18 Podpod 100% 11 / 11 totaltotal 97.6% 204 / 209 CC 35 SCAR 15.0
CC
35
Cov
98%
row_dominance
8.0
find_essentials
6.0
covered_least
6.0
CRAP 4.5
« QuineMcCluskey.pm ↑ Summary QuineMcCluskey/Format.pm »
1 =head1 NAME
2  
3 Algorithm::QuineMcCluskey::Util - provide utility functions to
4 Algorithm::QuineMcCluskey
5  
6 =cut
7  
8 package Algorithm::QuineMcCluskey::Util;
9  
10 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 17 ▶use 5.016001;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 17 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 17 ▶use List::Compare::Functional qw(is_LequivalentR is_LsubsetR);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 17 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 17 our @ISA = qw(Exporter);
19  
20 17 our %EXPORT_TAGS = (
21 all => [ qw(
22 covered_least
23 find_essentials
24 hammingd1pos
25 maskedmatch
26 matchcount
27 purge_elements
28 remels
29 row_dominance
30 transpose
31 uniqels
32 ) ],
33 );
34  
35 our @EXPORT_OK = (
36 17 @{ $EXPORT_TAGS{all} }
37 );
38  
39 17 our $VERSION = 1.01;
40  
41 =head1 DESCRIPTION
42  
43 This module provides various utilities designed for (but not limited to) use in
44 Algorithm::QuineMcCluskey.
45  
46 The prime implicant and essentials "tables" are in the form of a hash of
47 array refs, and are manipulated with the functions find_essentials(),
48 least_covered(), purge_elements(), remels(), row_dominance(), transpose(),
49 and uniqels().
50  
51 =cut
52  
53 =head2 FUNCTIONS
54  
55 =head3 matchcount()
56  
57 Returns the count of a search string Y found in the source string X.
58  
59 E.g.:
60  
61 my $str = "d10d11d1d";
62 matchcount($str, "d"); # returns 4
63 matchcount($str, "d1"); # returns 3
64  
65 To search for only the string without a regular expression accidentally
66 interfering, enclose the search string between '\Q' and '\E'. E.g.:
67  
68 #
69 # We don't know what's in $looking, so de-magic it.
70 #
71 matchcount($str, '\E' . $looking . '\Q]);
72  
73 =cut
74  
75 sub matchcount
76 124 ▶{
Subroutine matchcount: called CC=1 SCAR=0.0
Pod documented
77 124 my($x, $y) = @_;
78  
79 124 return scalar(() = $x=~ m/$y/g);
80 }
81  
82 =head3 maskedmatch()
83  
84 Returns the terms that match a mask made up of zeros, ones, and don't-care
85 characters.
86  
87 my @rterms = maskedmatch("010-0", @terms);
88  
89 =cut
90  
91 sub maskedmatch
92 231 ▶{
Subroutine maskedmatch: called CC=4 SCAR=13.9
Pod documented
93 231 my($mask, @terms) = @_;
94 231 my @t;
95  
96 #
97 # Make two patterns based on the don't-care characters
98 # in the mask (assumed to be the character that's not
99 # a zero or a one, an assumption enforced in BUILD.)
100 #
101 231 (my $m0 = $mask) =~ s/[^01]/0/g;
102 231 (my $m1 = $mask) =~ s/[^01]/1/g;
103 231 $m0 = oct "0b" . $m0;
104 231 $m1 = oct "0b" . $m1;
105  
106 231 for my $x (@terms)
107 {
108 3368 my $b = oct "0b" . $x;
109 3368 ▶ push @t, $x if ((($m0 & $b) == $m0) && (($m1 & $b) == $b));
Branch: 1 branch
BranchTrueFalseTotal
if ($m0 & $b) == $m0 and ($m1 & $b) == $b 563 2805 3368
Condition100%3/3
($m0 & $b) == $m0 and ($m1 & $b) == $b
!ll&&!rl&&r
17631042563
MC/DC100%2/2
($m0 & $b) == $m0 and ($m1 & $b) == $b
($m0 & $b) == $m0 ($m1 & $b) == $b
Truth table100%3/3
A and B
A= ($m0 & $b) == $m0B= ($m1 & $b) == $b
ABresult
0X0
100
111
110 }
111  
112 231 return @t;
113 }
114  
115 =head3 maskedmatchindexes()
116  
117 Returns the indexes of the terms that match a mask made up of zeros,
118 ones, and don't-care characters.
119  
120 my @pos = maskedmatchindexes("010-0", @terms);
121  
122 =cut
123  
124 sub maskedmatchindexes
125 1048 ▶{
Subroutine maskedmatchindexes: called CC=4 SCAR=13.9
Pod documented
126 1048 my($mask, @terms) = @_;
127 1048 my @p;
128  
129 #
130 # Make two patterns based on the don't-care characters
131 # in the mask (assumed to be the character that's not
132 # a zero or a one, an assumption enforced in BUILD.)
133 #
134 1048 (my $m0 = $mask) =~ s/[^01]/0/g;
135 1048 (my $m1 = $mask) =~ s/[^01]/1/g;
136 1048 $m0 = oct "0b" . $m0;
137 1048 $m1 = oct "0b" . $m1;
138  
139 1048 for my $j (0 .. $#terms)
140 {
141 2214 my $b = oct "0b" . $terms[$j];
142 2214 ▶ push @p, $j if ((($m0 & $b) == $m0) && (($m1 & $b) == $b));
Branch: 1 branch
BranchTrueFalseTotal
if ($m0 & $b) == $m0 and ($m1 & $b) == $b 197 2017 2214
Condition100%3/3
($m0 & $b) == $m0 and ($m1 & $b) == $b
!ll&&!rl&&r
1668349197
MC/DC100%2/2
($m0 & $b) == $m0 and ($m1 & $b) == $b
($m0 & $b) == $m0 ($m1 & $b) == $b
Truth table100%3/3
A and B
A= ($m0 & $b) == $m0B= ($m1 & $b) == $b
ABresult
0X0
100
111
143 }
144  
145 1048 return @p;
146 }
147  
148 =head3 find_essentials()
149  
150 Find the essential prime implicants in a primes table.
151  
152 my @essentials = find_essentials(\%primes);
153  
154 =cut
155  
156 sub find_essentials
157 153 ▶{
Subroutine find_essentials: called CC=6 SCAR=17.9
Pod documented
158 153 my($primes) = @_;
159  
160 153 my %essentials;
161 my %bterms;
162  
163 #
164 # Invert the hash-of-arrays
165 #
166 153 ▶ while (my($k, $v) = each %{$primes})
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$primes;}) 540 153 693
167 {
168 540 for my $term (@{ $v })
169 {
170 995 ▶ $bterms{$term} = [] unless (exists $bterms{$term});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $bterms{$term} 503 492 995
171 995 push @{$bterms{$term}}, $k;
172 }
173 }
174  
175 #
176 # Find the term that can be covered by only one bit term. Those
177 # terms are essentials.
178 #
179 153 for my $k (keys %bterms)
180 {
181 503 ▶ if (scalar @{ $bterms{$k}} == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$bterms{$k};} == 1) 171 332 503
182 {
183 171 my @bt = @{ $bterms{$k}};
184 171 $essentials{ ${ $bterms{$k}}[0]} = 1;
185 }
186 }
187  
188 153 return keys %essentials;
189 }
190  
191 =head3 row_dominance()
192  
193 Row dominance checking.
194  
195 @dominated_rows = row_dominance(\%primes, 0);
196  
197 @dominant_rows = row_dominance(\%primes, 1);
198  
199 A row I<i> of a PI chart dominates row I<j> if row I<i> contains an x in each
200 column dominated by it.
201  
202 A column I<p> of a PI chart dominates column I<q> if column I<p> contains an x
203 in each row dominated by it.
204  
205 Return those rows (columns are handled by rotating the primes hash before
206 calling this function).
207  
208 =cut
209  
210 sub row_dominance
211 203 ▶{
Subroutine row_dominance: called CC=8 SCAR=20.8
Pod documented
212 203 my($primes, $dominant_rows) = @_;
213 203 my @kp = keys %$primes;
214 203 my %unique_rows;
215  
216 203 ▶ $dominant_rows //= 0;
Condition50%1/2
$dominant_rows //= 0
l!l
2030
MC/DC0%0/1
$dominant_rows //= 0
$dominant_rows
Truth table50%1/2
A
A= $dominant_rows
Aresult
11
00
217  
218 203 for my $row1 (@kp)
219 {
220 551 for my $row2 (@kp)
221 {
222 #
223 # Skip if
224 # 1) the same row, or
225 # 2) the the rows have identical content, or
226 # 3) row1's list isn't a subset of row2 (which means
227 # it isn't dominated by row2).
228 #
229 next if ($row1 eq $row2 or
230 is_LequivalentR([ $primes->{$row1}, $primes->{$row2} ]) or
231 4445 ▶ !is_LsubsetR([ $primes->{$row1}, $primes->{$row2} ]));
Branch: 1 branch
BranchTrueFalseTotal
if $row1 eq $row2 or is_LequivalentR([$primes->{$row1}, $primes->{$row2}]) or not is_LsubsetR([$primes->{$row1}, $primes->{$row2}]) 4292 153 4445
Condition100%6/6
$row1 eq $row2 or is_LequivalentR([$primes->{$row1}, $primes->{$row2}]) or not is_LsubsetR([$primes->{$row1}, $primes->{$row2}])
l!l&&r!l&&!r
7173575153
$row1 eq $row2 or is_LequivalentR([$primes->{$row1}, $primes->{$row2}])
l!l&&r!l&&!r
5511663728
MC/DC100%3/3
$row1 eq $row2 or is_LequivalentR([$primes->{$row1}, $primes->{$row2}]) or not is_LsubsetR([$primes->{$row1}, $primes->{$row2}])
$row1 eq $row2 is_LequivalentR([$primes->{$row1}, $primes->{$row2}]) not is_LsubsetR([$primes->{$row1}, $primes->{$row2}])
Truth table100%4/4
A or B or not(C)
A= $row1 eq $row2B= is_LequivalentR([$primes->{$row1}, $primes->{$row2}])C= not is_LsubsetR([$primes->{$row1}, $primes->{$row2}])
ABCresult
1XX1
01X1
0011
0000
232  
233 153 ▶ $unique_rows{(($dominant_rows)? $row1: $row2)} = 1;
Branch: 1 branch
BranchTrueFalseTotal
$dominant_rows ? : 83 70 153
234 }
235 }
236  
237 203 return keys %unique_rows;
238 }
239  
240 =head3 covered_least()
241  
242 Find the term with the fewest implicant covers, along with a list of
243 those covers.
244  
245 my($term, @covers) = covered_least(\%primes);
246  
247 =cut
248  
249 sub covered_least
250 36 ▶{
Subroutine covered_least: called CC=6 SCAR=17.9
Pod documented
251 36 my($primes) = @_;
252 36 my(@covers);
253  
254 #
255 # Collect the bit terms that are within the hash's arrays.
256 #
257 my %bterms;
258 36 $bterms{$_} += 1 for (map {@$_} values %{$primes});
259  
260 #
261 # Find out which keys in the primes hash
262 # cover each term (that is, have the term
263 # in each primes' arrays).
264 #
265 36 my @t = keys %bterms;
266 36 my @pkeys = keys %$primes;
267 36 my $count = 1 + scalar @pkeys;
268 36 my $term = "";
269  
270 #print STDERR "bit terms hash:\n";
271 #for my $j (@t)
272 #{
273 # print STDERR "\t$j => " . $bterms{$j} . "\n";
274 #}
275 #print STDERR "\n";
276  
277 #
278 # Now find a term with the lowest number of covers.
279 #
280 36 for my $o (@t)
281 {
282 66 my $c = $bterms{$o};
283 66 ▶ if ($c < $count)
Branch: 1 branch
BranchTrueFalseTotal
if ($c < $count) 37 29 66
284 {
285 37 $term = $o;
286 37 $count = $c;
287 }
288 }
289  
290 36 for my $p (@pkeys)
291 {
292 117 ▶ push @covers, $p if any { $_ eq $term } @{ $primes->{$p} };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $_ eq $term; } , @{$$primes{$p};}) 74 43 117
293 }
294  
295 #print STDERR "covered_least() returns term ($term) and covers (" . join(", ", @covers) . ")\n";
296  
297 36 return ($term, @covers);
298 }
299  
300 =head3 purge_elements()
301  
302 purge_elements(\%prime_implicants, @essentials);
303  
304 Given a table of prime implicants, delete the list of elements (usually
305 the essential prime implicants) from the table, both row-wise and column-wise.
306  
307 =cut
308  
309 sub purge_elements
310 173 ▶{
Subroutine purge_elements: called CC=4 SCAR=13.9
Pod documented
311 173 my($primes, @ess) = @_;
312  
313 173 ▶ return 0 if (scalar @ess == 0 or scalar keys %$primes == 0);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @ess == 0 or scalar keys %$primes == 0 56 117 173
Condition66%2/3
scalar @ess == 0 or scalar keys %$primes == 0
l!l&&r!l&&!r
560117
MC/DC50%1/2
scalar @ess == 0 or scalar keys %$primes == 0
scalar @ess == 0 scalar keys %$primes == 0
Truth table66%2/3
A or B
A= scalar @ess == 0B= scalar keys %$primes == 0
ABresult
1X1
011
000
314  
315 #
316 # Delete the rows of each element,
317 # then delete the columns associated with each element.
318 #
319 117 delete ${$primes}{$_} for @ess;
320  
321 117 return remels($primes, @ess);
322 }
323  
324 =head3 remels()
325  
326 Given a reference to a hash of arrayrefs and a reference to an array of
327 values, remove the values from the individual arrayrefs if the values
328 matches their masks.
329  
330 Deletes the entire arrayref from the hash if the last element of the
331 array is removed.
332  
333 remels(\%primes, @elements);
334  
335 Returns the number of removals made.
336  
337 =cut
338  
339 sub remels
340 216 ▶{
Subroutine remels: called CC=5 SCAR=16.1
Pod documented
341 216 my ($href, @els) = @_;
342 216 my $rems = 0;
343 216 my @kp = keys %$href;
344  
345 216 for my $el (@els)
346 {
347 227 for my $k (@kp)
348 {
349 1048 my @pos = maskedmatchindexes($el, @{$href->{$k}});
350 1048 $rems += scalar @pos;
351  
352 #
353 # If it turns out that all the elements in the array
354 # are to be removed, then just delete the entire
355 # array reference.
356 #
357 1048 ▶ if (scalar @pos == scalar @{$href->{$k}})
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @pos == scalar @{$href->{$k};}) { } 107 941 1048
358 {
359 107 delete $href->{$k};
360 }
361 else
362 {
363 941 splice(@{$href->{$k}}, $_, 1) for (reverse sort @pos);
364 }
365 }
366 }
367  
368 216 return $rems;
369 }
370  
371 =head3 uniqels()
372  
373 Returns the unique arrays from an array of arrays (i.e., we're
374 ensuring non-duplicate answers).
375  
376 my @uels = uniqels(@els);
377  
378 =cut
379  
380 sub uniqels
381 36 ▶{
Subroutine uniqels: called CC=2 SCAR=7.0
Pod documented
382 36 my %h;
383 36 ▶ return map { $h{ join(",", @{$_}) }++ == 0 ? $_ : () } @_;
Branch: 1 branch
BranchTrueFalseTotal
$h{join ',', @{$_;}}++ == 0 ? : 124 0 124
384 }
385  
386 =head3 transpose()
387  
388 Transposes a hash-of-arrays structure of the type used for %primes.
389  
390 my %table90 = transpose(\%table)
391  
392 =cut
393  
394 sub transpose
395 99 ▶{
Subroutine transpose: called CC=3 SCAR=11.0
Pod documented
396 99 my($table) = @_;
397 99 my(%r90, %hoh);
398  
399 #
400 # Set up a hash-of-hashes, inverting the
401 # key to array-of-values relationship.
402 #
403 99 for my $r (keys %{$table})
404 {
405 251 $hoh{$_}{$r} = 1 for (@{$table->{$r}});
406 }
407  
408 #
409 # For each key collect those sub-hash keys into arrays.
410 #
411 99 %r90 = map{ ($_ , [ keys %{$hoh{$_}} ]) } keys %hoh;
412  
413 99 return %r90;
414 }
415  
416 =head3 hammingd1pos()
417  
418 Very specialized Hamming distance and position function.
419  
420 Our calling code is only interested in Hamming distances of 1.
421 In those cases return the string position where the two values differ.
422 In all the other cases where the distance isn't one, return a -1.
423  
424 $idx = hammingd1pos($val1, $val2);
425  
426 =cut
427  
428 sub hammingd1pos
429 9080 ▶{
Subroutine hammingd1pos: called CC=2 SCAR=6.9
Pod documented
430 #
431 # Xor the strings. The result will be a string in the
432 # non-printing range (in fact equal characters will result
433 # in a null character), so to each character Or a '0'.
434 #
435 9080 my $v = ($_[0] ^ $_[1]) | (qq(\x30) x length $_[0]);
436  
437 #
438 # Strings that don't have a Hamming distance of one are of no
439 # interest. Otherwise, return that character position.
440 #
441 9080 ▶ return -1 unless(scalar(() = $v=~ m/[^0]/g) == 1);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar(() = $v =~ /[^0]/gu) == 1 7608 1472 9080
442  
443 1472 $v =~ m/[^0]/g;
444 1472 return pos($v) - 1;
445 }
446  
447 =head1 SEE ALSO
448  
449 L<Algorithm::QuineMcCluskey>
450  
451 =head1 AUTHOR
452  
453 Darren M. Kulp C<< <darren@kulp.ch> >>
454  
455 John M. Gamble B<jgamble@cpan.org> (current maintainer)
456  
457 =head1 LICENSE AND COPYRIGHT
458  
459 Copyright (c) 2006 Darren Kulp. All rights reserved. This program is
460 free software; you can redistribute it and/or modify it under the same
461 terms as Perl itself.
462  
463 See L<http://dev.perl.org/licenses/> for more information.
464  
465 =cut
466  
467 17 1;
468  
469 __END__
« QuineMcCluskey.pm ↑ Summary QuineMcCluskey/Format.pm »