Claw  1.7.3
rle_encoder.tpp
Go to the documentation of this file.
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
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.
13 
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.
18 
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
22 
23  contact: julien.jorge@gamned.org
24 */
30 #include <cassert>
31 
32 /*----------------------------------------------------------------------------*/
41 template<typename OutputBuffer >
42 template<typename Iterator>
43 void claw::rle_encoder<OutputBuffer>::encode( Iterator first, Iterator last,
44  output_buffer_type& output ) const
45 {
46  const unsigned int max_encodable = output.max_encodable();
47  const unsigned int min_interesting = output.min_interesting();
48  raw_buffer_type raw_buffer;
49 
50  assert( max_encodable > 0 );
51 
52  while (first != last)
53  {
54  unsigned int count = 1;
55  pattern_type pattern = *first;
56  Iterator saved_it = first;
57  ++first;
58  bool ok = true;
59 
60  // try to find enough similar data
61  while ( ok && (first != last) && (count < max_encodable) )
62  if (*first == pattern)
63  {
64  ++count;
65  ++first;
66  }
67  else
68  ok = false;
69 
70  // if we have enough data
71  if ( count >= min_interesting )
72  {
73  if ( !raw_buffer.empty() )
74  {
75  output.raw( raw_buffer.begin(), raw_buffer.end() );
76  raw_buffer.clear();
77  }
78 
79  output.encode( count, pattern );
80  }
81  else
82  raw_buffer.insert( raw_buffer.end(), saved_it, first );
83  }
84 
85 
86  if ( !raw_buffer.empty() )
87  output.raw( raw_buffer.begin(), raw_buffer.end() );
88 } // rle_encoder::encode()