2 CLAW - a C++ Library Absolutely Wonderful
4 CLAW is a free library without any particular aim but being useful to
7 Copyright (C) 2005-2011 Julien Jorge
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 contact: julien.jorge@gamned.org
26 * \file rle_encoder.tpp
27 * \brief Implementation of the rle_encoder class.
28 * \author Julien Jorge
32/*----------------------------------------------------------------------------*/
34 * \brief Encode a range of datas.
35 * \param first Iterator on the first data.
36 * \param last Iterator past the last data.
37 * \param output The buffer on which we write the compressed data.
39 * \pre Iterator::value_type must be castable to pattern_type.
41template<typename OutputBuffer >
42template<typename Iterator>
43void claw::rle_encoder<OutputBuffer>::encode( Iterator first, Iterator last,
44 output_buffer_type& output ) const
46 const unsigned int max_encodable = output.max_encodable();
47 const unsigned int min_interesting = output.min_interesting();
48 raw_buffer_type raw_buffer;
50 assert( max_encodable > 0 );
54 unsigned int count = 1;
55 pattern_type pattern = *first;
56 Iterator saved_it = first;
60 // try to find enough similar data
61 while ( ok && (first != last) && (count < max_encodable) )
62 if (*first == pattern)
70 // if we have enough data
71 if ( count >= min_interesting )
73 if ( !raw_buffer.empty() )
75 output.raw( raw_buffer.begin(), raw_buffer.end() );
79 output.encode( count, pattern );
82 raw_buffer.insert( raw_buffer.end(), saved_it, first );
86 if ( !raw_buffer.empty() )
87 output.raw( raw_buffer.begin(), raw_buffer.end() );
88} // rle_encoder::encode()