#ifndef _231095_153248_hufftree_h #define _231095_153248_hufftree_h #include #include #include #include #include #include "bitstream.h" #include "huffnode.h" // If you use the STL priority_queue class, this object can be used as the // comparator object for its construction. // Provides a strict ordering based on counts for the hufftree creation process. // When counts are equal we rely on the lexiographic ordering of the characters. // If you don't use priority_queue, just leave it here or erase it. struct lesshuffnodeptr : public binary_function { bool operator()(const huffnode* dis, const huffnode* dat) const { return dis->count() > dat->count() || ((dis->count() == dat->count()) && dis->character() > dat->character()); } }; // A class encapsulating the functionality of a Huffman tree // Below is only a frame of class hufftree. You can modify, add or delete any data and functions // when you feel necessary // class hufftree { protected: int _chars; // The number of leaves, i.e. number of distinct characters. int _length; // The total number of characters in the input. // Here we assume the size of the inputfile is less than 65532 huffnode* _root; // The root of the Huffman tree. typedef map > charmap_type; charmap_type _charmap; // STL container "map" provides for fast retrieval of values of // type T based on a unique key // TODO: // Some container for mapping characters to leaves, // e.g. an STL map. public: // Constructor and destructors // hufftree() : _root(NULL), _chars(0), _length(0) { // TODO: initialize the mapping container if needed. }; ~hufftree() { if( _root ) delete _root; }; // For encode // Create huffman leaves for all distinct characters in the original input // You can change the name of this function if you like. // bool scan_from_source( istream& ins ) { _length = 0; // TODO: // Write code which inserts the character c into the map leaves if it // isn't already there. If it is already there, it should increment the // count of number of occurences for it. }; // For decode // Create huffman leaves from the encoded file // You can change the name of this function if you like. bool scan_from_table( istream& ins ) { ins.read( (char*) &_chars, sizeof(int) ); // TODO: // Write code which will read Character Table and create all the leaves }; // For both encode an decode // Build the actual Huffman tree // You can change the name of this function if you like. void build() { charmap_type::iterator i; // TODO: // Copy the huffman leaves and their counts into a vector // and build priority queue on top of the vector. // // Then combine the forest of huffman leaves into a single huffman tree }; // For encode // Output the mapping in a table. // You can change the name of this function if you like. // bool write_table( ostream& out ) const { // TODO // Write the CharCount and Character Table to the file. }; //Above is only a frame of class hufftree. You can modify, add or delete any data and functions //when you feel necessary //For example, you can declare a function that provides an interface for output. // // bool output( obitstream& outbits, const byte c ) // { // charmap_type::iterator character = _charmap.find(c); // if( character == _charmap.end() ) // return false; // const vector& bits = (*character).second->bits(); // for(vector::const_iterator i = bits.begin(); // i != bits.end(); ++i) // outbits.put( *i ); // return outbits.good(); // }; }; #ifdef DEBUG #endif #endif /* _231095_153248_hufftree_h */