Octane v1.01.20 - The Open Compression Toolkit for C++ | http://octane.sourceforge.net/ |
#include <huffmancoder.hpp>
Inheritance diagram for HuffmanCoder:
During decompression, the coder is responsible for reading bitcodes from the input and providing a stream of symbol numbers to the compressor.
The Huffman Coding algorithm was invented by David A. Huffman, http://www.wikipedia.org/wiki/Huffman_coding
The idea of using an STL priority queue to build the Huffman tree is from Mark Nelson, http://dogma.net/markn/articles/pq_stl/priority.htm.
Definition at line 49 of file huffmancoder.hpp.
Public Member Functions | |
HuffmanCoder () | |
constructor | |
virtual | ~HuffmanCoder () |
destructor | |
virtual std::string | GetName () |
provide a unique name for the coder, used in some cases to automatically register the object with a manager | |
virtual std::string | GetDescription () |
optionally provide a longer (maybe 20-60 characters) description | |
virtual std::string | GetHelpInformation () |
optionally provide more information about the object on request for help | |
virtual void | ShowDebuggingInfo () |
show any debugging info on request (used by various derived classes) [optional] | |
virtual unsigned int | GetMemoryUsed () |
Report actual current memory usage (in bytes). | |
virtual bool | WriteSymbolBits (int symbolnum, bitwriter &bw) |
write bits for symbol number | |
virtual bool | DecodeSymbolFromInput (int &symbolnum, bitreader &br) |
Decode the next symbol from the input. | |
virtual void | ResetState () |
virtual void | SynchronizeStateForNewStream () |
Synchronize state for a new stream - this *MUST* be called before beginning a new parsing stream. | |
virtual bool | IsReadyToCode () |
Are we ready to actually code and decode? | |
virtual void | ReceiveNotification_ModelChange_AllSymbolWeights (OctaneModeler *modelerp) |
Notify coder that all probabilities are being updated. | |
virtual void | ReceiveNotification_ModelChange_SingleSymbolWeight (OctaneModeler *modelerp, int symbolnum) |
notify that a single symbol probability has changed (by default just calls AllSymbolWeights change above) | |
Protected Member Functions | |
bool | BuildUpdateHuffmanTreeAndBitcodes (OctaneModeler *modelerp) |
Using the current probability vector from the modeler, build a huffman tree and compute bitcodes for each symbol. | |
void | TraverseHuffmanTreeBuildBitcodes () |
Traverse built tree and assign bitcodes. | |
void | FreeSymbols () |
Free any current tree and symbols in preparation for rebuilding. | |
void | ShowBitStrings () |
Show bitcodes for each symbol - useful for debugging. | |
Protected Attributes | |
THuffmanNodePriorityQueue | symbolsetpq |
Stl priority queue for Huffman tree, built from "huffman nodes". | |
vector< HuffNode_Leaf * > | symbolvector |
vector of pointers to symbols, for fast lookup when it comes time to writing symbols by symbol# | |
vector< HuffNode_Leaf * >::iterator | symbolvector_pos |
vector iterator |
|
provide a unique name for the coder, used in some cases to automatically register the object with a manager
Reimplemented from OctaneCoder. Definition at line 69 of file huffmancoder.hpp.
00069 {return "HuffmanCoder";} |
|
optionally provide a longer (maybe 20-60 characters) description
Reimplemented from OctaneCoder. Definition at line 70 of file huffmancoder.hpp.
00070 {return "Huffman Coder";} |
|
optionally provide more information about the object on request for help
Reimplemented from OctaneCoder. Definition at line 71 of file huffmancoder.hpp.
00071 { return ""; } |
|
write bits for symbol number
Implements OctaneCoder. Definition at line 54 of file huffmancoder.cpp. References symbolvector.
00055 { 00056 // write bits for symbol number - this is easy because we've precomputed the bitvectors for every symbol 00057 assert(symbolnum<(int)(symbolvector.size())); 00058 return symbolvector[symbolnum]->AddBitsToBitWriter(bw); 00059 } |
|
Decode the next symbol from the input.
Implements OctaneCoder. Definition at line 61 of file huffmancoder.cpp. References bitreader::empty(), bitreader::get_bit(), HuffNode::get_child0(), HuffNode::get_child1(), HuffNode::isleaf(), and symbolsetpq.
00062 { 00063 // decode a symbol from the input, and set symbolnum to symbol number 00064 // return false on no symbols left in stream 00065 HuffNode* nodep; 00066 bool bitvalue; 00067 00068 // now walk through the input bits 00069 nodep = symbolsetpq.top(); 00070 while (!br.empty()) 00071 { 00072 // grab a bit 00073 bitvalue = br.get_bit(); 00074 00075 // traverse down tree 00076 if (bitvalue==false) 00077 nodep=nodep->get_child0(); 00078 else 00079 nodep=nodep->get_child1(); 00080 if (nodep==NULL) 00081 { 00082 cerr << "ERROR: Internal error traversing huffman tree in huffmancoder.cpp, in HuffmanCoder::DecodeSymbolFromInput()." << endl; 00083 break; 00084 } 00085 00086 // if its leaf we got our symbol 00087 if (nodep->isleaf()) 00088 { 00089 // we hit a leaf, so this is our decoded symbol - return its number 00090 symbolnum=((HuffNode_Leaf*)nodep)->get_symbolid(); 00091 return true; 00092 } 00093 else 00094 { 00095 // just a middle node, so keep traversing 00096 } 00097 } 00098 00099 // no symbols to read 00100 return false; 00101 } |
|
Are we ready to actually code and decode?
Reimplemented from OctaneCoder. Definition at line 90 of file huffmancoder.hpp. References symbolvector.
00090 {if (symbolvector.size()>0) return true; else return false;}; |
|
Notify coder that all probabilities are being updated. We can be informed by a model when the underlying probabilities are changing this part of the API exists so that we can flexibly and efficiently handle different kinds of situations where the coder needs to be synchronized with the model probabilities. in some cases, a coder may ignore these messages and simply rebuild its internal datastructure on each coding event in other cases, it will want to incrementally update as model changes. Two kinds of notifications are supported, depending on how the model updates itself during processing, whether it updates one symbol per iteration, or modifies all symbols at once. Reimplemented from OctaneCoder. Definition at line 95 of file huffmancoder.hpp. References BuildUpdateHuffmanTreeAndBitcodes().
00095 {BuildUpdateHuffmanTreeAndBitcodes(modelerp);}; |
|
notify that a single symbol probability has changed (by default just calls AllSymbolWeights change above)
Reimplemented from OctaneCoder. Definition at line 96 of file huffmancoder.hpp. References BuildUpdateHuffmanTreeAndBitcodes().
00096 {BuildUpdateHuffmanTreeAndBitcodes(modelerp);}; |
|
Stl priority queue for Huffman tree, built from "huffman nodes".
Definition at line 54 of file huffmancoder.hpp. Referenced by BuildUpdateHuffmanTreeAndBitcodes(), DecodeSymbolFromInput(), FreeSymbols(), GetMemoryUsed(), ShowBitStrings(), and TraverseHuffmanTreeBuildBitcodes(). |