QuineMcCluskey/Format.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 96 CC 3 SCAR 12.0
CC
3
Cov
0%
arrayarray
6.0
chart
6.0
hasharray
2.0
CRAP 3.3
1 =head1 NAME
2  
3 Algorithm::QuineMcCluskey::Format - provide formatting functions to
4 Algorithm::QuineMcCluskey
5  
6 =cut
7  
8 package Algorithm::QuineMcCluskey::Format;
9  
10 use strict;
11 use warnings;
12 use 5.016001;
13  
14 use List::Util qw(any uniqstr);
15  
16 use Exporter;
17 our @ISA = qw(Exporter);
18  
19 our @EXPORT_OK = qw(arrayarray hasharray chart);
20  
21 our $VERSION = 1.01;
22  
23 =head1 DESCRIPTION
24  
25 This module provides formatting utilities designed for (but not limited
26 to) use in Algorithm::QuineMcCluskey.
27  
28 =cut
29  
30 =head1 FUNCTIONS
31  
32 =head3 arrayarray()
33  
34 Returns a more compact string form of the covers structure.
35  
36 =cut
37  
38 sub arrayarray
39 {
40 my ($ar) = @_;
41 my $fmt = "%" . length(scalar @{$ar}) . "d: [%s]";
42 my $idx = 0;
43 my @output;
44  
45 for my $ref (@{$ar})
46 {
47 push @output, sprintf($fmt, $idx, (defined $ref)? join(", ", @{ $ref }): " ");
48 $idx++;
49 }
50  
51 return "\n" . join("\n", @output);
52 }
53  
54 =head3 hasharray()
55  
56 Returns a more compact string form of primes structure.
57  
58 =cut
59  
60 sub hasharray
61 {
62 my ($hr) = @_;
63 my @output;
64  
65 for my $r (sort setbit_cmp keys %$hr)
66 {
67 push @output, "$r => [" . join(", ", @{ $hr->{$r} }) . "]";
68 }
69  
70 return "\n" . join("\n", @output);
71 }
72  
73 =head3 chart()
74  
75 $chart = chart(\%prime_implicants, $width);
76  
77 Return a string that interprets the primes' hash-of-array structure
78 into a column and row chart usable for visual searching of essential prime
79 implicants.
80  
81 =cut
82  
83 sub chart
84 {
85 my($hr, $width) = @_;
86  
87 my @rows = sort setbit_cmp keys %$hr;
88 my @columns = sort(uniqstr(map{ @{ $hr->{$_} } } @rows));
89 my $fmt = "%" . ($width+2) . "s";
90 my @output;
91  
92 push @output, join("", map{sprintf($fmt, $_)} ' ', @columns);
93  
94 #
95 # Having set up our list of row and column headers, check
96 # to see which column values are present in each row.
97 #
98 for my $r (@rows)
99 {
100 my @present = map {my $v = $_; (any{$v eq $_} @{ $hr->{$r} }) } @columns;
101  
102 my @marks = map{ sprintf($fmt, $_? 'x': '.') } @present;
103  
104 push @output, join("", sprintf($fmt, $r), @marks);
105 }
106  
107 return join("\n", @output);
108 }
109  
110 =head3 setbit_cmp()
111  
112 =head3 unsetbit_cmp()
113  
114 Comparison function for sort() that orders the rows in the chart()
115 and hasharray() functions.
116  
117 =cut
118  
119 sub setbit_cmp
120 {
121 my $a1 = scalar(() = $a=~ m/1/g);
122 my $b1 = scalar(() = $b=~ m/1/g);
123 return (($a1 <=> $b1) || ($a cmp $b));
124 }
125  
126 sub unsetbit_cmp
127 {
128 my $a0 = scalar(() = $a=~ m/0/g);
129 my $b0 = scalar(() = $b=~ m/0/g);
130 return (($a0 <=> $b0) || ($a cmp $b));
131 }
132  
133 =head1 SEE ALSO
134  
135 L<Algorithm::QuineMcCluskey>
136  
137 =head1 AUTHOR
138  
139 John M. Gamble B<jgamble@cpan.org>
140  
141 =cut
142  
143 1;
144  
145 __END__