tudocomp
– The TU Dortmund Compression Framework
CompactDec.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 #include <tudocomp/def.hpp>
6 #include <tudocomp/Algorithm.hpp>
7 
8 namespace tdc {
9 namespace lcpcomp {
10 
18 class CompactDec : public Algorithm {
19 public:
20  inline static Meta meta() {
21  Meta m("lcpcomp_dec", "compact");
22  return m;
23 
24  }
25  inline void decode_lazy() const {
26  }
27  inline void decode_eagerly() const {
28  }
29 
30 private:
31  len_compact_t** m_fwd;
32 
33  len_t m_cursor;
34  IF_STATS(len_t m_longest_chain);
35  IF_STATS(len_t m_current_chain);
36 
37  IntVector<uliteral_t> m_buffer;
38 
39  inline void decode_literal_at(len_t pos, uliteral_t c) {
40  IF_STATS(++m_current_chain);
41  IF_STATS(m_longest_chain = std::max(m_longest_chain, m_current_chain));
42 
43  m_buffer[pos] = c;
44  DCHECK(c != 0 || pos == m_buffer.size()-1); // we assume that the text to restore does not contain a NULL-byte but at its very end
45 
46  if(m_fwd[pos] != nullptr) {
47  const len_compact_t*const& bucket = m_fwd[pos];
48  for(size_t i = 1; i < bucket[0]; ++i) {
49  decode_literal_at(bucket[i], c); // recursion
50  }
51  free(m_fwd[pos]);
52  m_fwd[pos] = nullptr;
53  }
54 
55  IF_STATS(--m_current_chain);
56  }
57 
58 public:
60  Algorithm(std::move(*this)),
61  m_fwd(std::move(other.m_fwd)),
62  m_cursor(std::move(other.m_cursor)),
63  m_buffer(std::move(other.m_buffer))
64  {
65  IF_STATS(m_longest_chain = std::move(other.m_longest_chain));
66  IF_STATS(m_current_chain = std::move(other.m_current_chain));
67 
68  other.m_fwd = nullptr;
69  }
70 
72  if(m_fwd != nullptr) {
73  for(size_t i = 0; i < m_buffer.size(); ++i) {
74  if(m_fwd[i] == nullptr) continue;
75  free(m_fwd[i]);
76  }
77  delete [] m_fwd;
78  }
79  }
80  inline CompactDec(Env&& env, len_t size)
81  : Algorithm(std::move(env)), m_cursor(0), m_buffer(size,0) {
82 
83  IF_STATS(m_longest_chain = 0);
84  IF_STATS(m_current_chain = 0);
85 
86  m_fwd = new len_compact_t*[size];
87  std::fill(m_fwd,m_fwd+size,nullptr);
88  }
89 
90  inline void decode_literal(uliteral_t c) {
91  decode_literal_at(m_cursor++, c);
92  }
93 
94  inline void decode_factor(len_t pos, len_t num) {
95  for(len_t i = 0; i < num; i++) {
96  len_t src = pos+i;
97  if(m_buffer[src]) {
98  decode_literal_at(m_cursor, m_buffer[src]);
99  } else {
100  len_compact_t*& bucket = m_fwd[src];
101  if(bucket == nullptr) {
102  bucket = (len_compact_t*) malloc(sizeof(len_compact_t) * 2);
103  DCHECK(m_fwd[src] == bucket);
104  bucket[0] = 2;
105  bucket[1] = m_cursor;
106  }
107  else
108  { // this block implements the call of m_fwd[src]->push_back(m_cursor);
109  ++bucket[0]; // increase the size of a bucket only by one!
110  bucket = (len_compact_t*) realloc(bucket, sizeof(len_compact_t)*bucket[0]);
111  bucket[bucket[0]-1] = m_cursor;
112  }
113  }
114 
115  ++m_cursor;
116  }
117  }
118 
119  IF_STATS(
120  inline len_t longest_chain() const {
121  return m_longest_chain;
122  })
123 
124  inline void write_to(std::ostream& out) const {
125  for(auto c : m_buffer) out << c;
126  }
127 };
128 
129 }} //ns
130 
void decode_factor(len_t pos, len_t num)
Definition: CompactDec.hpp:94
Contains the text compression and encoding framework.
Definition: namespaces.hpp:11
Provides meta information about an Algorithm.
Definition: Meta.hpp:34
A vector over arbitrary unsigned integer types.
Definition: IntVector.hpp:175
uint8_t uliteral_t
Type to represent signed single literals.
Definition: def.hpp:131
Env & env()
Provides access to the environment that the algorithm works in.
Definition: Algorithm.hpp:51
Decodes lcpcomp compressed data as described in the paper.
Definition: CompactDec.hpp:18
CompactDec(CompactDec &&other)
Definition: CompactDec.hpp:59
len_compact_t src
Definition: LZSSFactors.hpp:38
uint32_t len_compact_t
Type to represent an bit-compact length value.
Definition: def.hpp:103
fast_t< len_compact_t > len_t
Type to represent an length value.
Definition: def.hpp:114
len_compact_t pos
Definition: LZSSFactors.hpp:38
void decode_literal(uliteral_t c)
Definition: CompactDec.hpp:90
void decode_eagerly() const
Definition: CompactDec.hpp:27
void decode_lazy() const
Definition: CompactDec.hpp:25
CompactDec(Env &&env, len_t size)
Definition: CompactDec.hpp:80
Local environment for a compression/encoding/decompression call.
size_type size() const
Definition: IntVector.hpp:284
Interface for algorithms.
Definition: Algorithm.hpp:15