~ubuntu-branches/ubuntu/oneiric/libclaw/oneiric

« back to all changes in this revision

Viewing changes to claw/impl/lzw_encoder.tpp

  • Committer: Bazaar Package Importer
  • Author(s): Julien Jorge
  • Date: 2008-05-17 15:36:57 UTC
  • Revision ID: james.westby@ubuntu.com-20080517153657-0b1204j754ykoz48
Tags: upstream-1.5.2b
ImportĀ upstreamĀ versionĀ 1.5.2b

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * \file lzw_encoder.tpp
 
3
 * \brief Implementation of the claw::lzw_encoder class.
 
4
 * \author Julien Jorge
 
5
 */
 
6
#include <map>
 
7
 
 
8
/*----------------------------------------------------------------------------*/
 
9
/**
 
10
 * \brief Encode a sequence of datas.
 
11
 * \param input Where we read the uncompressed data.
 
12
 * \param output Where we write compressed data.
 
13
 */
 
14
template<typename InputBuffer, typename OutputBuffer>
 
15
void claw::lzw_encoder<InputBuffer, OutputBuffer>::encode
 
16
( input_buffer_type& input, output_buffer_type& output ) const
 
17
{
 
18
  typedef std::pair<unsigned int, unsigned int> word;
 
19
 
 
20
  if ( !input.end_of_data() )
 
21
    {
 
22
      std::map<word, unsigned int> table;
 
23
      
 
24
      unsigned int symbol = input.get_next();
 
25
      unsigned int prefix_code = symbol;
 
26
      unsigned int next_code = input.symbols_count();
 
27
 
 
28
      while ( !input.end_of_data() && (next_code != output.max_code()) )
 
29
        {
 
30
          symbol = input.get_next();
 
31
 
 
32
          word new_word(prefix_code, symbol);
 
33
 
 
34
          if ( table.find(new_word) != table.end() )
 
35
            prefix_code = table[new_word];
 
36
          else
 
37
            {
 
38
              output.write(prefix_code);
 
39
              output.new_code(next_code);
 
40
              table[new_word] = next_code;
 
41
              prefix_code = symbol;
 
42
 
 
43
              ++next_code;
 
44
            }
 
45
        }
 
46
 
 
47
      output.write(prefix_code);
 
48
    }
 
49
} // lzw_encoder::encode()