| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# module to build |
| 4 |
require './recurse2txt'; |
| 5 |
require './lib/Math/Base/Convert/Bases.pm'; # pointer to all standard bases |
| 6 |
|
| 7 |
# will update the Bitmaps module version below |
| 8 |
# |
| 9 |
my $VERSION = do { my @r = (q$Revision: 0.02 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; |
| 10 |
|
| 11 |
my $bases = Math::Base::Convert->_bases; |
| 12 |
|
| 13 |
my %base2; |
| 14 |
my %dmaps; |
| 15 |
my @xmaps = (\%dmaps); |
| 16 |
|
| 17 |
# make lookup bit maps - there are two kinds |
| 18 |
# |
| 19 |
# 1) for generic in 'xmaps' of the form "bit pattern" => "decimal value" |
| 20 |
# |
| 21 |
# 2) for specific in 'dmaps' of the form "bit pattern" => "base character" |
| 22 |
# |
| 23 |
# i.e. |
| 24 |
# |
| 25 |
# case 1) |
| 26 |
# %bmap4 = ( # base 16 translation table |
| 27 |
# '0000' => 0, |
| 28 |
# '0001' => 1, |
| 29 |
# '0010' => 2, |
| 30 |
# '0011' => 3, |
| 31 |
# '0100' => 4, |
| 32 |
# '0101' => 5, |
| 33 |
# '0110' => 6, |
| 34 |
# '0111' => 7, |
| 35 |
# '1000' => 8, |
| 36 |
# '1001' => 9, |
| 37 |
# '1010' => 10, |
| 38 |
# '1011' => 11, |
| 39 |
# '1100' => 12, |
| 40 |
# '1101' => 13, |
| 41 |
# '1110' => 14, |
| 42 |
# '1111' => 15, |
| 43 |
#); |
| 44 |
# |
| 45 |
# case 2) |
| 46 |
#%dmap4 = ( # heX - base 16 translation table |
| 47 |
# '0000' => 0, |
| 48 |
# '0001' => 1, |
| 49 |
# '0010' => 2, |
| 50 |
# '0011' => 3, |
| 51 |
# '0100' => 4, |
| 52 |
# '0101' => 5, |
| 53 |
# '0110' => 6, |
| 54 |
# '0111' => 7, |
| 55 |
# '1000' => 8, |
| 56 |
# '1001' => 9, |
| 57 |
# '1010' => 'a', |
| 58 |
# '1011' => 'b', |
| 59 |
# '1100' => 'c', |
| 60 |
# '1101' => 'd', |
| 61 |
# '1110' => 'e', |
| 62 |
# '1111' => 'f', |
| 63 |
#); |
| 64 |
# |
| 65 |
my $mapbits = sub { |
| 66 |
my($bits,$val) = @_; |
| 67 |
my $bp = length($bits); |
| 68 |
|
| 69 |
# 0, binary, hex not mapped |
| 70 |
return if $bp == 4 || # hex is direct |
| 71 |
$bp == 1; # binary maps directly from value |
| 72 |
$xmaps[$bp]->{$bits} = $val; # build generic translation maps |
| 73 |
return if $bp == 3; # octal maps from standard lookup, numeric |
| 74 |
if (exists $base2{$bp}) { # standard base present with this base power? |
| 75 |
foreach(0..$#{$base2{$bp}}) { |
| 76 |
my $bnam = $base2{$bp}->[$_]; |
| 77 |
$dmaps{$bnam}->{$bits} = $bases->{$bnam}->[$val]; |
| 78 |
} |
| 79 |
} |
| 80 |
}; |
| 81 |
|
| 82 |
# create a hash of all standard power 2 bases where the key |
| 83 |
# points to a list of bases associated with each power of 2 |
| 84 |
# |
| 85 |
foreach (sort keys %$bases) { |
| 86 |
my $len = @{$bases->{$_}}; |
| 87 |
unless ($len & $len -1) { # isnotp2 |
| 88 |
my $bp = int(log($len)/log(2) +0.5); |
| 89 |
if (exists $base2{$bp}) { |
| 90 |
push @{$base2{$bp}}, $_; |
| 91 |
} else { |
| 92 |
$base2{$bp} = [$_]; |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
# create an 8 bit wide binary map of the range 0..255 |
| 98 |
# |
| 99 |
foreach (0..255) { |
| 100 |
my $p = pack('c',$_); |
| 101 |
my $bits = unpack('B*',$p); |
| 102 |
my @bits = $bits; |
| 103 |
while ($bits =~ s/^0// && length($bits)) { |
| 104 |
push @bits, $bits; |
| 105 |
} |
| 106 |
foreach $bits (@bits) { |
| 107 |
$mapbits->($bits,$_); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
my $moduletxt = q|#!/usr/bin/perl -w |
| 112 |
|
| 113 |
package Math::Base::Convert::Bitmaps; |
| 114 |
|
| 115 |
use vars qw($VERSION); |
| 116 |
|
| 117 |
$VERSION = '|. $VERSION .q|'; |
| 118 |
|
| •119 |
# created by Makefile.PL |. scalar gmtime($ENV{SOURCE_DATE_EPOCH}). q| |
| 120 |
# |
| 121 |
# Do not edit this package, |
| 122 |
# |
| 123 |
# edit the 'bitmaps' file in the source directory instead |
| 124 |
# |
| 125 |
# Why is this module here? The tables in this module |
| 126 |
# load from the disk and order of magnitude faster than |
| 127 |
# they can be created by perl at run time. |
| 128 |
# |
| 129 |
|
| 130 |
$VAR = |. (Dumper(\@xmaps))[0] .q| |
| 131 |
__END__ |
| 132 |
|
| 133 |
=head1 NAME |
| 134 |
|
| 135 |
Math::Base::Convert::Bitmaps - pregenerated bit pattern to base power of 2 translation tables |
| 136 |
|
| 137 |
This package contains no documentation |
| 138 |
|
| 139 |
=head1 AUTHOR |
| 140 |
|
| 141 |
Michael Robinton, [email protected] |
| 142 |
|
| 143 |
=head1 COPYRIGHT |
| 144 |
|
| 145 |
Copyright 2012-2015, Michael Robinton |
| 146 |
|
| 147 |
This program is free software; you may redistribute it and/or modify it |
| 148 |
under the same terms as Perl itself. |
| 149 |
|
| 150 |
This program is distributed in the hope that it will be useful, |
| 151 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 152 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 153 |
|
| 154 |
=cut |
| 155 |
|
| 156 |
1; |
| 157 |
|; |
| 158 |
|
| 159 |
open (F,'>lib/Math/Base/Convert/Bitmaps.pm') or return 0; |
| 160 |
select F; |
| 161 |
$| = 1; |
| 162 |
print F $moduletxt; |
| 163 |
close F; |
| 164 |
1; |