Algorithm-Huffman
view release on metacpan or search on metacpan
}
}
defined $decode
or die "Unknown bit sequence starting at index $index in the bitstring";
}
return $string;
}
sub decode {
my ($self, $bitvector) = @_;
my $max_length_decoding_key = $self->{max_length_decoding_key};
my $min_length_decoding_key = $self->{min_length_decoding_key};
my %decode_hash = %{$self->decode_hash};
my $string = "";
my ($offset, $max_offset) = (0, 8 * (length($bitvector)-1));
while ($offset < $max_offset) {
my $decode = undef;
my $bitpattern = "";
my $last_offset_ok = $offset;
foreach my $l (1 .. $max_length_decoding_key) {
$bitpattern .= vec($bitvector,$offset++,1);
if ($decode = $decode_hash{$bitpattern}) {
$string .= $decode;
last;
}
}
defined $decode
or die "Unknown bit sequence starting at offset $last_offset_ok in the bitstring";
}
return $string;
}
sub __validate_counting_hash {
my $c = shift;
my $error_msg = undef;
defined $c
or croak "Undefined counting hash";
ref($c) eq 'HASH'
or croak "The argument for the counting hash is not a hash reference, as expected";
scalar(keys %$c) >= 2
or croak "The counting hash must have at least 2 keys";
}
1;
package KeyValuePair;
use Heap::Elem;
require Exporter;
our @ISA = qw/Exporter Heap::Elem/;
sub new {
my ($proto, $key, $value) = @_;
my $class = ref($proto) || $proto;
my $self = $class->SUPER::new;
$self->{"KeyValuePair::key"} = $key;
$self->{"KeyValuePair::value"} = $value;
return $self;
}
sub cmp {
my ($self, $other) = @_;
$self->{"KeyValuePair::value"} <=> $other->{"KeyValuePair::value"};
}
sub key {
my $self = shift;
return $self->{"KeyValuePair::key"};
}
sub value {
my $self = shift;
return $self->{"KeyValuePair::value"};
}
1;
__END__
=head1 NAME
Algorithm::Huffman - Perl extension that implements the Huffman algorithm
=head1 SYNOPSIS
use Algorithm::Huffman;
my %char_counting = map {$_ => int rand(100)} ('a' .. 'z', 'A' .. 'Z');
# or better the real counting for your characters
# as the huffman algorithm doesn't work good with random data :-))
my $huff = Algorithm::Huffman->new(\%char_counting);
my $encode_hash = $huff->encode_hash;
my $decode_hash = $huff->decode_hash;
my $encode_of_hello = $huff->encode_bitstring("Hello");
print "Look at the encoding bitstring of 'Hello': $encode_of_hello\n";
print "The decoding of $encode_of_hello is '", $huff->decode_bitstring($encode_of_hello), "'";
=head1 DESCRIPTION
This modules implements the huffman algorithm.
The aim is to create a good coding scheme for a given list
of different characters (or even strings) and their occurence numbers.
=head2 ALGORITHM
Please have a look to a good data compression book for a detailed view.
However, the algorithm is like every good algorithm very easy.
Assume we have a heap (keys are the characters/strings;
( run in 1.095 second using v1.01-cache-2.11-cpan-39bf76dae61 )