Claw  1.7.3
lzw_encoder.tpp
Go to the documentation of this file.
1 
6 #include <map>
7 
8 /*----------------------------------------------------------------------------*/
14 template<typename InputBuffer, typename OutputBuffer>
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()