tudocomp
– The TU Dortmund Compression Framework
SLP.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <array>
5 
6 namespace tdc {namespace esp {
7  static_assert(sizeof(std::array<size_t, 2>) == sizeof(size_t) * 2, "Something is not right");
8 
9  // TODO: Fix to work with = 0
11 
12  struct SLP {
13  std::vector<std::array<size_t, 2>> rules;
14  size_t root_rule = 0;
15  bool empty = true;
16 
17  inline SLP() {}
18  inline SLP(std::vector<std::array<size_t, 2>>&& r,
19  size_t root,
20  bool e):
21  rules(std::move(r)),
22  root_rule(root),
23  empty(e) {}
24 
25  inline void derive_text_rec(std::ostream& o, size_t rule) const {
26  if (rule < 256) {
27  o << char(rule);
28  } else for (auto r : rules[rule - GRAMMAR_PD_ELLIDED_PREFIX]) {
29  derive_text_rec(o, r);
30  }
31  }
32 
33  inline std::ostream& derive_text(std::ostream& o) const {
34  if (!empty) {
35  derive_text_rec(o, root_rule);
36  }
37  return o;
38  }
39 
40  inline std::string derive_text_s() const {
41  std::stringstream ss;
42  derive_text(ss);
43  return ss.str();
44  }
45 
46  // Returns idx of node n
47  inline size_t node_idx(size_t n) const {
48  DCHECK_GE(n, GRAMMAR_PD_ELLIDED_PREFIX);
49  return n - GRAMMAR_PD_ELLIDED_PREFIX;
50  }
51 
52  inline const std::array<size_t, 2>& node(size_t n) const {
53  return rules.at(node_idx(n));
54  }
55  };
56 
57  struct SLPRhsAdapter {
58  using value_type = size_t;
59 
60  SLP* slp;
61  inline const size_t& operator[](size_t i) const {
62  return slp->rules[i][1];
63  }
64  inline size_t& operator[](size_t i) {
65  return slp->rules[i][1];
66  }
67  inline size_t size() const {
68  return slp->rules.size();
69  }
70  };
71 }}
Contains the text compression and encoding framework.
Definition: namespaces.hpp:11
std::vector< std::array< size_t, 2 > > rules
Definition: SLP.hpp:13
size_t GRAMMAR_PD_ELLIDED_PREFIX
Definition: SLP.hpp:10
size_t root_rule
Definition: SLP.hpp:14
std::ostream & derive_text(std::ostream &o) const
Definition: SLP.hpp:33
const std::array< size_t, 2 > & node(size_t n) const
Definition: SLP.hpp:52
SLP(std::vector< std::array< size_t, 2 >> &&r, size_t root, bool e)
Definition: SLP.hpp:18
std::string derive_text_s() const
Definition: SLP.hpp:40
size_t & operator[](size_t i)
Definition: SLP.hpp:64
bool empty
Definition: SLP.hpp:15
size_t size() const
Definition: SLP.hpp:67
size_t node_idx(size_t n) const
Definition: SLP.hpp:47
void derive_text_rec(std::ostream &o, size_t rule) const
Definition: SLP.hpp:25
const size_t & operator[](size_t i) const
Definition: SLP.hpp:61