tudocomp
– The TU Dortmund Compression Framework
DebugContext.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 
6 
7 namespace tdc {namespace esp {
8  template<typename T>
10  struct Data {
11  std::ostream* m_out;
12  bool print_early = true;
13  std::vector<std::function<void(std::ostream&)>> m_print_instructions;
14  T m_child_data;
15  };
16  std::shared_ptr<Data> m_data;
17 
18  template<typename U>
19  friend class DebugContextBase;
20  protected:
21  template<typename F>
22  void with_child(F f) {
23  if (m_data) {
24  f(m_data->m_child_data);
25  }
26  }
27 
28  template<typename U>
29  static std::vector<size_t> cast_vec(const U& v) {
30  std::vector<size_t> r;
31  r.reserve(v.size());
32  for (auto e : v) {
33  r.push_back(e);
34  }
35  return r;
36  }
37 
38  template<typename F>
39  void print(F f) {
40  if (m_data) {
41  m_data->m_print_instructions.push_back(f); // TODO: Could add wrappers here
42  if (m_data->print_early) {
43  m_data->m_print_instructions.back()(*(m_data->m_out));
44  }
45  }
46  }
47  DebugContextBase(std::ostream& o, bool p_en, bool p_ea) {
48  if (p_en) {
49  m_data = std::make_shared<Data>(Data { &o, p_ea });
50  }
51  }
52  template<typename U>
54  if (parent.m_data) {
55  m_data = std::make_shared<Data>(Data {
56  parent.m_data->m_out,
57  parent.m_data->print_early
58  });
59  }
60  }
61  public:
63  m_data(other.m_data) {}
64 
65  void print_all() const {
66  if (m_data) {
67  if (!m_data->print_early) {
68  for (auto& f : m_data->m_print_instructions) {
69  f(*(m_data->m_out));
70  }
71  }
72  }
73  }
74  };
75 
77 
79  size_t alphabet_size;
80  std::vector<size_t> metablock;
81  size_t type;
82  size_t offset;
83  std::vector<TypedBlock> unadjusted_blocks;
84 
85  std::vector<size_t> mb2_initial;
86  std::vector<std::shared_ptr<std::vector<size_t>>> mb2_reduce_to_6_steps;
87  std::vector<std::shared_ptr<std::vector<size_t>>> mb2_reduce_to_3_steps;
88 
89  std::vector<size_t> mb2_high_landmarks;
90  std::vector<size_t> mb2_high_and_low_landmarks;
91  };
92  class DebugMetablockContext: public DebugContextBase<DebugMetablockContextData> {
93  public:
94  DebugMetablockContext(std::ostream& o, bool p_en, bool p_ea, size_t alphabet_size):
95  DebugContextBase(o, p_en, p_ea)
96  {
97  with_child([&] (auto& m_data) {
98  m_data.alphabet_size = alphabet_size;
99  });
100  }
101  template<typename U>
102  DebugMetablockContext(DebugContextBase<U>& parent, size_t alphabet_size):
103  DebugContextBase(parent)
104  {
105  with_child([&] (auto& m_data) {
106  m_data.alphabet_size = alphabet_size;
107  });
108  }
109 
110  template<typename T>
111  void init(size_t type, const T& string, size_t offset) {
112  with_child([&] (auto& m_data) {
113  m_data.type = type;
114  m_data.offset = offset;
115  m_data.metablock = cast_vec(string);
116 
117  this->print([m_data = &m_data](std::ostream& o) {
118  o << " type: " << m_data->type
119  << ", offset: " << m_data->offset
120  << ", metablock: " << vec_to_debug_string(m_data->metablock)
121  << "\n";
122  });
123  });
124  }
125 
126  void block(size_t width, size_t type) {
127  with_child([&] (auto& m_data) {
128  auto b = TypedBlock { uint8_t(width), uint8_t(type) };
129  m_data.unadjusted_blocks.push_back(b);
130 
131  this->print([m_data = &m_data, b](std::ostream& o) {
132  o << " block: " << b << "\n";
133  });
134  });
135  }
136 
137  template<typename T>
138  void mb2_initial(const T& buf) {
139  with_child([&] (auto& m_data) {
140  m_data.mb2_initial = cast_vec(buf);
141 
142  this->print([m_data = &m_data](std::ostream& o) {
143  o << " Alphabet reduction initial:\n"
144  << " " << vec_to_debug_string(m_data->mb2_initial) << "\n";
145  });
146  });
147  }
148 
150  with_child([&] (auto& m_data) {
151  this->print([m_data = &m_data](std::ostream& o) {
152  o << " Reduce to 6:\n";
153  });
154  });
155  }
156 
157  template<typename T>
158  void mb2_reduce_to_6_step(const T& buf) {
159  with_child([&] (auto& m_data) {
160  auto p = std::make_shared<std::vector<size_t>>(cast_vec(buf));
161  m_data.mb2_reduce_to_6_steps.push_back(p);
162 
163  this->print([m_data = &m_data, p](std::ostream& o) {
164  o << " " << vec_to_debug_string(*p) << "\n";
165  });
166  });
167  }
168 
170  with_child([&] (auto& m_data) {
171  this->print([m_data = &m_data](std::ostream& o) {
172  o << " Reduce to 3:\n";
173  });
174  });
175  }
176 
177  template<typename T>
178  void mb2_reduce_to_3_step(const T& buf) {
179  with_child([&] (auto& m_data) {
180  auto p = std::make_shared<std::vector<size_t>>(cast_vec(buf));
181  m_data.mb2_reduce_to_3_steps.push_back(p);
182 
183  this->print([m_data = &m_data, p](std::ostream& o) {
184  o << " " << vec_to_debug_string(*p) << "\n";
185  });
186  });
187  }
188 
189  template<typename T>
190  void mb2_high_landmarks(const T& buf) {
191  with_child([&] (auto& m_data) {
192  m_data.mb2_high_landmarks = cast_vec(buf);
193 
194  this->print([m_data = &m_data](std::ostream& o) {
195  o << " High landmarks:\n"
196  << " " << vec_to_debug_string(m_data->mb2_high_landmarks) << "\n";
197  });
198  });
199  }
200 
201  template<typename T>
202  void mb2_high_and_low_landmarks(const T& buf) {
203  with_child([&] (auto& m_data) {
204  m_data.mb2_high_and_low_landmarks = cast_vec(buf);
205 
206  this->print([m_data = &m_data](std::ostream& o) {
207  o << " High and low landmarks:\n"
208  << " " << vec_to_debug_string(m_data->mb2_high_and_low_landmarks) << "\n";
209  });
210  });
211  }
212  };
213 
215  size_t number;
216  std::vector<size_t> string;
217  size_t root_node = 0;
218  bool empty = false;
219  std::vector<DebugMetablockContext> metablocks;
221  std::vector<TypedBlock> adjusted_blocks;
222  std::vector<std::shared_ptr<std::pair<std::vector<size_t>, size_t>>> slice_symbol_map;
223  };
224  class DebugRoundContext: public DebugContextBase<DebugRoundContextData> {
225  public:
226  DebugRoundContext(std::ostream& o, bool p_en, bool p_ea):
227  DebugContextBase(o, p_en, p_ea)
228  {
229  with_child([&] (auto& m_data) {
230  });
231  }
232  template<typename U>
234  DebugContextBase(parent)
235  {
236  with_child([&] (auto& m_data) {
237  });
238  }
239 
240  template<typename X>
241  void init(size_t number,
242  const X& string,
243  size_t alphabet_size) {
244  with_child([&] (auto& m_data) {
245  m_data.number = number;
246  m_data.string = cast_vec(string);
247  m_data.alphabet_size = alphabet_size;
248 
249  this->print([m_data = &m_data](std::ostream& o) {
250  o << "\n[Round #" << m_data->number << "]:\n"
251  << " " << vec_to_debug_string(m_data->string) << "\n";
252  o << " Alphabet size: " << m_data->alphabet_size << "\n";
253  });
254  });
255  }
256 
257  void last_round(size_t rn, bool empty) {
258  with_child([&] (auto& m_data) {
259  m_data.root_node = rn;
260  m_data.empty = empty;
261  this->print([m_data = &m_data](std::ostream& o) {
262  if (m_data->empty) {
263  o << " DONE, empty input\n";
264  } else {
265  o << " DONE, root node: " << m_data->root_node << "\n";
266  }
267  });
268  });
269  }
270 
272  size_t as = 0;
273  with_child([&] (auto& m_data) {
274  as = m_data.alphabet_size;
275  });
276  DebugMetablockContext m_data_child(*this, as);
277  with_child([&] (auto& m_data) {
278  m_data.metablocks.push_back(m_data_child);
279 
280  this->print([m_data_child](std::ostream& o) {
281  m_data_child.print_all();
282  });
283 
284  });
285  return m_data_child;
286  }
287 
289  with_child([&] (auto& m_data) {
290  m_data.adjusted_blocks = buf;
291 
292  this->print([m_data = &m_data](std::ostream& o) {
293  o << " Adjusted blocks:\n";
294  for (auto b : m_data->adjusted_blocks) {
295  o << " block: " << b << "\n";
296  }
297  });
298  });
299  }
300 
302  with_child([&] (auto& m_data) {
303  this->print([m_data = &m_data](std::ostream& o) {
304  o << " Slice-Symbol map:\n";
305  });
306  });
307  }
308 
309  template<typename T>
310  void slice_symbol_map(const T& slice, size_t symbol) {
311  with_child([&] (auto& m_data) {
312  auto p = std::make_shared<std::pair<std::vector<size_t>, size_t>>(
313  std::pair<std::vector<size_t>, size_t> {
314  cast_vec(slice),
315  symbol,
316  }
317  );
318  m_data.slice_symbol_map.push_back(p);
319 
320  this->print([m_data = &m_data, p](std::ostream& o) {
321  o << " "
322  << vec_to_debug_string(p->first) << " -> "
323  << p->second << "\n";
324  });
325  });
326  }
327  };
328 
330  std::string input;
331  std::vector<DebugRoundContext> rounds;
332  bool empty;
333  size_t root_node;
337  std::vector<std::shared_ptr<std::vector<size_t>>> encode_slp;
338  };
339  class DebugContext: public DebugContextBase<DebugContextData> {
340  public:
341  DebugContext(std::ostream& o, bool p_en, bool p_ea):
342  DebugContextBase(o, p_en, p_ea)
343  {
344  with_child([&] (auto& m_data) {
345  });
346  }
347 
349  with_child([&] (auto& m_data) {
350  m_data.input = s;
351 
352  this->print([m_data = &m_data](std::ostream& o) {
353  o << "\n[Input]:\n\"" << m_data->input << "\"\n";
354  });
355  });
356  }
357 
358  void generate_grammar(bool empty, size_t root_node) {
359  with_child([&] (auto& m_data) {
360  m_data.empty = empty;
361  m_data.root_node = root_node;
362 
363  this->print([m_data = &m_data](std::ostream& o) {
364  o << "\n[Grammar]:\n"
365  << " Is empty: " << (m_data->empty? "yes" : "no") << "\n"
366  << " Root node: " << m_data->root_node << "\n";
367  });
368  });
369  }
370 
371  void encode_start() {
372  with_child([&] (auto& m_data) {
373  this->print([m_data = &m_data](std::ostream& o) {
374  o << "\n[Encode]:\n";
375  });
376  });
377  }
378 
379  void encode_max_value(size_t value, size_t bits) {
380  with_child([&] (auto& m_data) {
381  m_data.encode_max_value = value;
382  m_data.encode_max_value_bits = bits;
383 
384  this->print([m_data = &m_data](std::ostream& o) {
385  o << " Max value: " << m_data->encode_max_value << "\n";
386  o << " Bits: " << m_data->encode_max_value_bits << "\n";
387  });
388  });
389  }
390 
391  void encode_root_node(size_t node) {
392  with_child([&] (auto& m_data) {
393  m_data.encode_root_node = node;
394 
395  this->print([m_data = &m_data](std::ostream& o) {
396  o << " Root node: " << m_data->encode_root_node << "\n";
397  });
398  });
399  }
400 
402  with_child([&] (auto& m_data) {
403  this->print([m_data = &m_data](std::ostream& o) {
404  o << "\n [SLP]:\n";
405  });
406  });
407  }
408 
409  template<typename T>
410  void encode_rule(const T& rule) {
411  with_child([&] (auto& m_data) {
412  auto p = std::make_shared<std::vector<size_t>>(cast_vec(rule));
413  m_data.encode_slp.push_back(p);
414 
415  this->print([m_data = &m_data, p](std::ostream& o) {
416  o << " " << vec_to_debug_string(*p) << "\n";
417  });
418  });
419  }
420 
422  DebugRoundContext m_data_child(*this);
423  with_child([&] (auto& m_data) {
424  m_data.rounds.push_back(m_data_child);
425 
426  this->print([m_data_child](std::ostream& o) {
427  m_data_child.print_all();
428  });
429 
430  });
431  return m_data_child;
432  }
433  };
434 }}
Contains the text compression and encoding framework.
Definition: namespaces.hpp:11
std::vector< size_t > mb2_high_and_low_landmarks
void slice_symbol_map(const T &slice, size_t symbol)
void mb2_reduce_to_3_step(const T &buf)
DebugRoundContext(std::ostream &o, bool p_en, bool p_ea)
std::string vec_to_debug_string(const T &s, size_t indent=0)
Builds the string representation of a vector of byte values, sorrounded by square brackets ([ and ])...
DebugContextBase(std::ostream &o, bool p_en, bool p_ea)
A const view into a slice of memory.
DebugMetablockContext(DebugContextBase< U > &parent, size_t alphabet_size)
void adjusted_blocks(const ConstGenericView< TypedBlock > &buf)
std::vector< std::shared_ptr< std::pair< std::vector< size_t >, size_t > > > slice_symbol_map
void encode_root_node(size_t node)
std::vector< std::shared_ptr< std::vector< size_t > > > mb2_reduce_to_6_steps
DebugContext(std::ostream &o, bool p_en, bool p_ea)
std::vector< std::shared_ptr< std::vector< size_t > > > mb2_reduce_to_3_steps
void mb2_high_and_low_landmarks(const T &buf)
void input_string(string_ref s)
void block(size_t width, size_t type)
void init(size_t number, const X &string, size_t alphabet_size)
void mb2_high_landmarks(const T &buf)
void last_round(size_t rn, bool empty)
std::vector< DebugRoundContext > rounds
std::vector< std::shared_ptr< std::vector< size_t > > > encode_slp
void encode_max_value(size_t value, size_t bits)
static std::vector< size_t > cast_vec(const U &v)
DebugMetablockContext metablock()
std::vector< size_t > string
DebugMetablockContext(std::ostream &o, bool p_en, bool p_ea, size_t alphabet_size)
DebugRoundContext round()
void mb2_reduce_to_6_step(const T &buf)
uint64_t m_data
Definition: uint_t.hpp:30
DebugContextBase(const DebugContextBase &other)
void generate_grammar(bool empty, size_t root_node)
std::vector< size_t > mb2_high_landmarks
DebugContextBase(DebugContextBase< U > &parent)
void init(size_t type, const T &string, size_t offset)
std::vector< size_t > mb2_initial
std::vector< DebugMetablockContext > metablocks
DebugRoundContext(DebugContextBase< U > &parent)
std::vector< TypedBlock > adjusted_blocks
std::vector< TypedBlock > unadjusted_blocks
void encode_rule(const T &rule)