Class HuffmanCodec
- All Implemented Interfaces:
Codec,PrefixCodec,Serializable
public class HuffmanCodec extends Object implements PrefixCodec, Serializable
A Huffman coder is built starting from an array of frequencies corresponding to each symbol. Frequency 0 symbols are allowed, but they will degrade the resulting code.
Instances of this class compute a canonical Huffman code (Eugene S. Schwartz and Bruce Kallick, “Generating a Canonical Prefix Encoding”, Commun. ACM 7(3), pages 166−169, 1964), which can by quickly decoded using table lookups. The construction uses the most efficient one-pass in-place codelength computation procedure described by Alistair Moffat and Jyrki Katajainen in “In-Place Calculation of Minimum-Redundancy Codes”, Algorithms and Data Structures, 4th International Workshop, number 955 in Lecture Notes in Computer Science, pages 393−402, Springer-Verlag, 1995.
We note by passing that this coded uses a CanonicalFast64CodeWordDecoder, which does not support codelengths above 64.
However, since the worst case for codelengths is given by Fibonacci numbers, and frequencies are to be provided as integers,
no codeword longer than the base-[(51/2 + 1)/2] logarithm of 51/2 · 231 (less than 47) will ever be generated.
- See Also:
- Serialized Form
-
Field Summary
Fields Modifier and Type Field Description intsizeThe number of symbols of this coder. -
Constructor Summary
Constructors Constructor Description HuffmanCodec(int[] frequency)Creates a new Huffman codec using the given vector of frequencies.HuffmanCodec(long[] frequency)Creates a new Huffman codec using the given vector of frequencies. -
Method Summary
Modifier and Type Method Description CodeWordCodercoder()Returns a coder for the compression technique represented by this coded.BitVector[]codeWords()Returns the vector of prefix-free codewords used by this prefix coder.Decoderdecoder()Returns a decoder for the compression technique represented by this coded.intsize()Returns the number of symbols handled by this codec.
-
Field Details
-
size
public final int sizeThe number of symbols of this coder.
-
-
Constructor Details
-
HuffmanCodec
public HuffmanCodec(int[] frequency)Creates a new Huffman codec using the given vector of frequencies.- Parameters:
frequency- a vector of nonnnegative frequencies.
-
HuffmanCodec
public HuffmanCodec(long[] frequency)Creates a new Huffman codec using the given vector of frequencies.- Parameters:
frequency- a vector of nonnnegative frequencies.
-
-
Method Details
-
coder
Description copied from interface:CodecReturns a coder for the compression technique represented by this coded.- Specified by:
coderin interfaceCodec- Specified by:
coderin interfacePrefixCodec- Returns:
- a coder for the compression technique represented by this codec.
-
decoder
Description copied from interface:CodecReturns a decoder for the compression technique represented by this coded. -
size
public int size()Description copied from interface:CodecReturns the number of symbols handled by this codec. -
codeWords
Description copied from interface:PrefixCodecReturns the vector of prefix-free codewords used by this prefix coder.- Specified by:
codeWordsin interfacePrefixCodec- Returns:
- the vector of prefix-free codewords used by this prefix coder.
-