Claw 1.7.3
lzw_encoder.tpp
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 */
14template<typename InputBuffer, typename OutputBuffer>
15void 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()