Data-BitStream
view release on metacpan or search on metacpan
examples/compression-code.pl view on Meta::CPAN
my @list;
my $listn;
{
my $s1 = [gettimeofday];
my $stream = Data::BitStream->new( file => $file, mode => 'ro' );
my $e1 = int(tv_interval($s1)*1_000_000);
my $s2 = [gettimeofday];
@list = $stream->get_unary(-1);
my $e2 = int(tv_interval($s2)*1_000_000);
$listn = scalar @list;
printf "Slurped $listn numbers in %.1f ms, decoded in %.1f ms\n", $e1/1000,$e2/1000;
}
# average value
my $avg = int( ((sum @list) / $listn) + 0.5);
# bytes required in fixed size (FOR encoding)
my $bytes = int(ceillog2(max @list) * $listn / 8);
print "List (avg $avg, max ", max(@list), ", $bytes binary):\n";
time_list($_, @list) for (@encodings);
examples/integercoding.pl view on Meta::CPAN
string of 0 and 1 characters representing the bit encoding of the integer
using that code.
$str = encode_unary(8); # die unless $str eq '000000001';
$str = encode_gamma(8); # die unless $str eq '0001000';
$str = encode_delta(8); # die unless $str eq '00100000';
$str = encode_omega(8); # die unless $str eq '1110000';
$str = encode_fib(8); # die unless $str eq '000011';
The C<decode_> methods take a single binary string as input and produce an
unsigned integer output decoded from the bit encoding.
$n = decode_unary('000000000000001'); # die unless $n == 14;
$n = decode_gamma('0001110'); # die unless $n == 14;
$n = decode_delta('00100110'); # die unless $n == 14;
$n = decode_omega('1111100'); # die unless $n == 14;
$n = decode_fib( '1000011'); # die unless $n == 14;
=head1 SEE ALSO
The CPAN module L<Data::BitStream> includes these codes and more.
t/72-rand-bad-gets.t view on Meta::CPAN
$s->write_close;
# Pick a random position to start
my $pos = int(rand(64));
foreach my $code (@encodings) {
# Set position to a little way in
$s->rewind; $s->skip($pos); die "Position error" unless $s->pos == $pos;
my $v;
eval { $v = $s->code_get($code); };
if ($@ eq '') {
# The random data is decoded as a value. Good for us.
isnt($v, undef, "Read a value with $code");
cmp_ok($s->pos, '>', $pos, "Good $code read at least one bit");
} else {
# The only error we should see is a code error, and we expect the
# stream position to remain unchanged after the trapped read.
like($@, qr/code error/i, "$code trapped bad read");
is($s->pos, $pos, "Bad $code read left position unchanged");
}
}
}
( run in 0.334 second using v1.01-cache-2.11-cpan-a9ef4e587e4 )