libUTL++
BWTencoder.h
1 #pragma once
2 
4 
5 #include <libutl/ArithmeticEncoder.h>
6 
8 
9 UTL_NS_BEGIN;
10 
177 
179 class BWTencoder : public Encoder
180 {
182 
183 public:
191  BWTencoder(uint_t mode, Stream* stream, bool owner = true, uint_t blockSize = KB(256))
192  {
193  init();
194  start(mode, stream, owner, blockSize);
195  }
196 
197  virtual size_t decode(byte_t* block, size_t num);
198 
199  virtual size_t encode(const byte_t* block, size_t num);
200 
208  void start(uint_t mode, Stream* stream, bool owner = true, uint_t blockSize = KB(256));
209 
210 protected:
211  virtual void clear();
212  virtual void finishEncoding();
213 
214 private:
215  void init();
216  void
217  deInit()
218  {
219  close();
220  }
221  void clearSelf();
222  // transform
223  void doTransform();
224  void undoTransform(byte_t* block);
225  // sort
226  inline int compare(uint_t i1, uint_t i2, uint_t depth);
227  void insertionSort(uint_t begin, uint_t end);
228  uint_t medianOfThree(uint_t l, uint_t m, uint_t h, uint_t depth);
229  void multiKeyInsertionSort(uint_t begin, uint_t end, uint_t depth);
230  void multiKeyQuickSort(uint_t begin, uint_t end, uint_t depth);
231  void quickSort(uint_t begin, uint_t end, uint_t depth);
232  void stripe();
233  inline void swap(uint_t i, uint_t j);
234  inline void swap(uint_t i, uint_t j, uint_t n);
235  // mtf encoder
236  void mtfEncodeBlock();
237  void mtfDecodeBlock();
238  void mtfEncodeSymbol(uint_t symbol);
239  uint_t mtfDecodeSymbol();
240  void mtfEncodeRandomWord(uint_t w);
241  uint_t mtfDecodeRandomWord();
242  void mtfEncodeRandom(uint_t symbol);
243  uint_t mtfDecodeRandom();
244  void mtfEncodeZeroes();
245 // debug
246 #ifdef DEBUG
247  void printBlock(uint_t begin = 0, uint_t end = uint_t_max, uint_t numBytes = 32);
248 #endif
249  // sort
250  uint_t* _ptr;
251  uint_t* _freq;
252  uint32_t* _stripe;
253  // mtf
255  ArithContext* _H[9];
256  uint_t _numZeroes;
257  uint_t _pos[256];
258  // general
259  byte_t* _block;
260  uint_t _blockSize;
261  uint_t _origin;
262 };
263 
265 
266 void
268 {
269  uint_t tmp = _ptr[i];
270  _ptr[i] = _ptr[j];
271  _ptr[j] = tmp;
272 }
273 
275 
276 void
278 {
279  uint_t* pi = _ptr + i;
280  uint_t* pj = _ptr + j;
281  uint_t* piLim = pi + n;
282  uint_t tmp;
283  while (pi < piLim)
284  {
285  tmp = *pi;
286  *pi = *pj;
287  *pj = tmp;
288  pi++;
289  pj++;
290  }
291 }
292 
294 
295 UTL_NS_END;
void deInit()
De-initialize UTL++.
Encoder/decoder abstraction.
Definition: Encoder.h:39
BWTencoder(uint_t mode, Stream *stream, bool owner=true, uint_t blockSize=KB(256))
Constructor.
Definition: BWTencoder.h:191
#define UTL_CLASS_DECL(DC, BC)
Declaration of standard UTL++ functionality for a non-template class.
Definition: macros.h:688
void multiKeyQuickSort(const FwdIt &begin, const FwdIt &end, bool key=true, bool reverse=false, size_t size=size_t_max)
Multi-key quick-sort a sequence.
unsigned char byte_t
Unsigned character.
Definition: types.h:31
void swap(T *array, size_t lhsIdx, size_t rhsIdx)
Swap two elements of the given array.
Definition: util_inl.h:930
Statistical context for ArithmeticEncoder.
unsigned int uint32_t
Unsigned 32-bit integer.
Definition: types.h:115
DCC95 arithmetic coder.
void quickSort(const FwdIt &begin, const FwdIt &end, const Ordering *ordering=nullptr, size_t size=size_t_max)
Quick-sort a sequence.
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
const uint_t uint_t_max
Maximum uint_t value.
Stream I/O abstraction.
Definition: Stream.h:68
void insertionSort(const FwdIt &begin, const FwdIt &end, const Ordering *ordering=nullptr, size_t size=size_t_max)
Insertion-sort a sequence.
constexpr size_t KB(size_t n)
Convert size in kilobytes to size in bytes.
Definition: util_inl.h:1008
void multiKeyInsertionSort(P *array, C cmp, size_t begin, size_t end, size_t depth=0)
Multi-key insertion-sort (part of) the given sequence.
BWT-based coder.
Definition: BWTencoder.h:179
int compare(bool lhs, bool rhs)
Compare two boolean values.
void init()
Initialize UTL++.