Thread: Huffman Template Library

  1. #1
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Huffman Template Library

    Well, the Huffman contest just sort of floundered, so I've decided to go ahead and release the implementation I've been working on.

    So basically, it's just templated version of the algorithm that works on iterators, the only real requirement being that the input data appears as a stream of bytes (the output is treated the same).

    I'd love to hear your opinions/criticisms.

    Test program:

    Code:
           
        
        
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <iterator>
    #include <cassert>
    #include <ctime>
    #include <iomanip>
    #include "huffman.hpp"
    
    using namespace 
        std;
    
    typedef xtd::compression::huffman< size_t >
        encoder_t;
    encoder_t
        encoder;
        
    enum encoding_mode_t
    {
        compress, 
        decompress
    };    
        
    void process( vector< char >& pre_processed, vector< char >& post_processed, encoding_mode_t mode )
    {    
        clock_t
            start, 
            stop;
        post_processed.clear( );
        start = clock( );
        if( mode == compress )
            encoder.compress( pre_processed.begin( ), pre_processed.end( ), back_inserter( post_processed ) );
        else
            encoder.decompress( pre_processed.begin( ), pre_processed.end( ), back_inserter( post_processed ) );
        stop = clock( );
        double
            result = double( stop - start ) / CLK_TCK;
        cout << pre_processed.size( ) << " bytes processed in " << result << " seconds" << endl;    
    } 
        
    int main( int argc, char** argv )
    {
        vector< char >
            uncompressed, 
            compressed, 
            decompressed;
        cout << "Huffman Test" << endl;
        if( argc == 1 )
        {
            cout << "Usage: " << *argv << " <FILES>" << endl;
            return 1;
        }
        while( *( ++argv ) )
        {
            char const*
                filename = *argv;
            ifstream
                in( filename, ios::binary );
            if( !in )
            {
                cerr << "Error: cannot process file '" << filename << "'" << endl;
                continue;
            }        
            cout << "File: '" << filename << "'" << endl;
            cout << "...reading file..." << endl;
            uncompressed.clear( );
            char
                ch;
            while( in.read( &ch, 1 ) )
                uncompressed.push_back( ch );
            if( in.bad( ) )
            {
                cerr << "Error: could not read file '" << filename << "'" << endl;
                continue;
            }
            cout << "Uncompressed size: " << uncompressed.size( ) << endl;
            cout << "...compressing..." << endl;
            process( uncompressed, compressed, compress );
            double
                ratio = ( double( compressed.size( ) ) / uncompressed.size( ) ) * 100;
            cout << "Compressed size: " << compressed.size( ) 
                << " (" << setprecision( 2 ) << ratio << "%)" << endl;
            cout << "...decompressing..." << endl;
            process( compressed, decompressed, decompress );
            cout << "...verifying..." << endl;
            bool
                passed = ( uncompressed == decompressed );
            cout << "Result: " << ( passed ? "pass" : "fail" ) << endl;
        }
        return 0;
    }

  2. #2
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by Sebastiani View Post
    Well, the Huffman contest just sort of floundered, so I've decided to go ahead and release the implementation I've been working on.
    Perhaps it was the overly strict rules, here try something like this (starting compression contest in contest forum)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with dll, template and library dir
    By Smjert in forum C++ Programming
    Replies: 5
    Last Post: 08-13-2009, 03:45 AM
  2. template overloading question
    By plutino in forum C++ Programming
    Replies: 14
    Last Post: 02-27-2009, 02:10 AM
  3. Property Set Library (PSL) - Announcement
    By vultur_gryphus in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 05-29-2008, 06:04 AM
  4. Future of the Standard Template Library
    By ed bitwise in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2006, 04:27 PM
  5. Template library
    By r_damodharan in forum C++ Programming
    Replies: 2
    Last Post: 09-27-2002, 09:39 AM