File: bitmaps

package info (click to toggle)
libmath-base-convert-perl 0.11-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 308 kB
  • sloc: perl: 870; makefile: 2
file content (164 lines) | stat: -rw-r--r-- 3,894 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/perl

# module to build
require './recurse2txt';
require './lib/Math/Base/Convert/Bases.pm';	# pointer to all standard bases

# will update the Bitmaps module version below
#
my $VERSION = do { my @r = (q$Revision: 0.02 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };

my $bases = Math::Base::Convert->_bases;

my %base2;
my %dmaps;
my @xmaps = (\%dmaps);

# make lookup bit maps - there are two kinds
#
# 1) for generic in 'xmaps' of the form "bit pattern" => "decimal value"
#
# 2) for specific in 'dmaps' of the form "bit pattern" => "base character"
#
# i.e.
#
# case 1)
# %bmap4 = ( # base 16 translation table
#        '0000'  =>  0,  
#        '0001'  =>  1,  
#        '0010'  =>  2,  
#        '0011'  =>  3,  
#        '0100'  =>  4,  
#        '0101'  =>  5,  
#        '0110'  =>  6,  
#        '0111'  =>  7,  
#        '1000'  =>  8,  
#        '1001'  =>  9,  
#        '1010'  =>  10, 
#        '1011'  =>  11, 
#        '1100'  =>  12,
#        '1101'  =>  13,
#        '1110'  =>  14, 
#        '1111'  =>  15, 
#);
#
# case 2)
#%dmap4 = ( # heX - base 16 translation table
#        '0000'  =>  0,  
#        '0001'  =>  1,  
#        '0010'  =>  2,  
#        '0011'  =>  3,  
#        '0100'  =>  4,  
#        '0101'  =>  5,  
#        '0110'  =>  6,  
#        '0111'  =>  7,  
#        '1000'  =>  8,  
#        '1001'  =>  9,  
#        '1010'  =>  'a', 
#        '1011'  =>  'b', 
#        '1100'  =>  'c',
#        '1101'  =>  'd',
#        '1110'  =>  'e', 
#        '1111'  =>  'f', 
#);
#
my $mapbits = sub {
  my($bits,$val) = @_;
  my $bp = length($bits);

# 0, binary, hex not mapped
  return if $bp == 4 ||			# hex is direct
	    $bp == 1;			# binary maps directly from value
  $xmaps[$bp]->{$bits} = $val;		# build generic translation maps
  return if $bp == 3;			# octal maps from standard lookup, numeric
  if (exists $base2{$bp}) {		# standard base present with this base power?
    foreach(0..$#{$base2{$bp}}) {
      my $bnam = $base2{$bp}->[$_];
      $dmaps{$bnam}->{$bits} = $bases->{$bnam}->[$val];
    }
  }
};

# create a hash of all standard power 2 bases where the key
# points to a list of bases associated with each power of 2
#
foreach (sort keys %$bases) {
  my $len = @{$bases->{$_}};
  unless ($len & $len -1) {	# isnotp2
    my $bp = int(log($len)/log(2) +0.5);
    if (exists $base2{$bp}) {
      push @{$base2{$bp}}, $_;
    } else {
      $base2{$bp} = [$_];
    }
  }
}

# create an 8 bit wide binary map of the range 0..255
#
foreach (0..255) {
  my $p = pack('c',$_);
  my $bits = unpack('B*',$p);
  my @bits = $bits;
  while ($bits =~ s/^0// && length($bits)) {
    push @bits, $bits;
  }
  foreach $bits (@bits) {
    $mapbits->($bits,$_);
  }
}  

my $moduletxt = q|#!/usr/bin/perl -w

package Math::Base::Convert::Bitmaps;

use vars qw($VERSION);

$VERSION = '|. $VERSION .q|';

# created by Makefile.PL |. scalar gmtime($ENV{SOURCE_DATE_EPOCH}). q|
#
# Do not edit this package, 
#
# edit the 'bitmaps' file in the source directory instead
#
# Why is this module here? The tables in this module
# load from the disk and order of magnitude faster than
# they can be created by perl at run time.
#

$VAR = |. (Dumper(\@xmaps))[0] .q|
__END__

=head1 NAME

Math::Base::Convert::Bitmaps - pregenerated bit pattern to base power of 2 translation tables

This package contains no documentation

=head1 AUTHOR

Michael Robinton, michael@bizsystems.com

=head1 COPYRIGHT

Copyright 2012-2015, Michael Robinton

This program is free software; you may redistribute it and/or modify it
under the same terms as Perl itself.   

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

=cut

1;
|;

open (F,'>lib/Math/Base/Convert/Bitmaps.pm') or return 0;
select F;
$| = 1;
print F $moduletxt;
close F;
1;