tudocomp
– The TU Dortmund Compression Framework
SubseqStrategy.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <tudocomp/Algorithm.hpp>
6 
7 namespace tdc {namespace esp {
8  class SubSeqOptimal: public Algorithm {
9  public:
10  inline static Meta meta() {
11  Meta m("subseq", "optimal");
12  return m;
13  };
14 
16 
17  template<typename SortedIndices>
18  inline Dpi_and_b create_dpi_and_b_from_sorted_indices(const SortedIndices& sorted_indices) const {
19  return esp::create_dpi_and_b_from_sorted_indices(sorted_indices);
20  }
21  };
22  class SubSeqGreedy: public Algorithm {
23  public:
24  inline static Meta meta() {
25  Meta m("subseq", "greedy");
26  return m;
27  };
28 
30 
31  template<typename SortedIndices>
32  inline Dpi_and_b create_dpi_and_b_from_sorted_indices(const SortedIndices& sis) const {
33  Dpi_and_b ret;
34  ret.Dpi.reserve(sis.size());
35  ret.Dpi.resize(sis.size(), 999);
36 
37  size_t Dpi_counter = 0;
38 
39  DCHECK_GE(sis.size(), 1);
40  std::vector<std::pair<size_t, size_t>> free_list;
41  free_list.reserve(sis.size() + 2);
42  free_list.resize(sis.size() + 2);
43  for (size_t i = 1; i < (free_list.size() - 3); i++) {
44  free_list[i].first = i - 1;
45  free_list[i].second = i + 1;
46  }
47  const size_t start_dummy_link = free_list.size() - 2;
48  const size_t end_dummy_link = free_list.size() - 1;
49 
50  if ((free_list.size() - 2) > 1) {
51  free_list[0].first = start_dummy_link;
52  free_list[0].second = 1;
53  free_list[free_list.size() - 3].first = free_list.size() - 4;
54  free_list[free_list.size() - 3].second = end_dummy_link;
55  } else {
56  free_list[0].first = start_dummy_link;
57  free_list[0].second = end_dummy_link;
58  }
59 
60  free_list[start_dummy_link].first = start_dummy_link;
61  free_list[end_dummy_link].second = end_dummy_link;
62 
63  free_list[start_dummy_link].second = 0;
64  free_list[end_dummy_link].first = free_list.size() - 3;
65 
66  std::vector<size_t> increasing;
67  std::vector<size_t> decreasing;
68  //std::cout << "sis:\n";
69  //std::cout << vec_to_debug_string(sis, 3) << "\n\n";
70 
71  auto si_of = [&](size_t link) {
72  DCHECK(link != start_dummy_link);
73  DCHECK(link != end_dummy_link);
74  return sis[link];
75  };
76 
77  auto remove = [&](size_t link) {
78  DCHECK(link != start_dummy_link);
79  DCHECK(link != end_dummy_link);
80  auto prev = free_list[link].first;
81  auto next = free_list[link].second;
82 
83  free_list[prev].second = next;
84  free_list[next].first = prev;
85 
86  free_list[link].first = link;
87  free_list[link].second = link;
88  };
89 
90  auto handle = [&](const std::vector<size_t>& seq) {
91  for (auto link : seq) {
92  DCHECK(link != start_dummy_link);
93  DCHECK(link != end_dummy_link);
94  ret.Dpi[link] = Dpi_counter;
95  }
96 
97  for (auto link : seq) {
98  remove(link);
99  }
100 
101  Dpi_counter++;
102  };
103 
104  while (free_list[start_dummy_link].second != end_dummy_link) {
105  {
106  size_t cstart = free_list[start_dummy_link].second;
107  increasing.clear();
108  increasing.push_back(cstart);
109  while(true) {
110 
111 
112  if (free_list[cstart].second == end_dummy_link) {
113  //std::cout << "inc done\n";
114  break;
115  } else {
116  //std::cout << cstart << "\n";
117  cstart = free_list[cstart].second;
118  //std::cout << cstart << "\n";
119  }
120 
121  DCHECK(si_of(cstart) != si_of(increasing.back()));
122  if (si_of(cstart) > si_of(increasing.back())) {
123  increasing.push_back(cstart);
124  }
125  }
126  }
127  {
128  size_t cend = free_list[end_dummy_link].first;
129  decreasing.clear();
130  decreasing.push_back(cend);
131  while(true) {
132 
133  if (free_list[cend].first == start_dummy_link) {
134  //std::cout << "dec done\n";
135  break;
136  } else {
137  cend = free_list[cend].first;
138  }
139 
140  DCHECK(si_of(cend) != si_of(decreasing.back()));
141  if (si_of(cend) > si_of(decreasing.back())) {
142  decreasing.push_back(cend);
143  }
144  }
145  // TODO: Just for debug
146  std::reverse(decreasing.begin(), decreasing.end());
147  }
148 
149  //debug(increasing);
150  //debug(decreasing);
151 
152  if (increasing.size() >= decreasing.size()) {
153  handle(increasing);
154  ret.b.push_back(uint_t<1>(0));
155  //std::cout << "-> "; debug(increasing);
156  } else {
157  handle(decreasing);
158  ret.b.push_back(uint_t<1>(1));
159  //std::cout << "-> "; debug(decreasing);
160  }
161  //std::cout << vec_to_debug_string(ret.Dpi, 3) << "\n";
162  //std::cout << vec_to_debug_string(ret.b) << "\n\n";
163  //std::cout << "\n";
164  }
165  //std::cout << "\n";
166 
167  return ret;
168  }
169  };
170 }}
Contains the text compression and encoding framework.
Definition: namespaces.hpp:11
Provides meta information about an Algorithm.
Definition: Meta.hpp:34
std::vector< Sindex > sorted_indices(const View &input)
Dpi_and_b create_dpi_and_b_from_sorted_indices(const SortedIndices &sorted_indices) const
void push_back(const value_type &val)
Definition: IntVector.hpp:426
std::vector< size_t > Dpi
Algorithm(Algorithm const &)=default
typename uint_dispatch_t< N >::type uint_t
Definition: uint_t.hpp:165
IntVector< uint_t< 1 > > b
Dpi_and_b create_dpi_and_b_from_sorted_indices(const SortedIndices &sis) const
Interface for algorithms.
Definition: Algorithm.hpp:15
Dpi_and_b create_dpi_and_b_from_sorted_indices(const SortedIndices &sorted_indices)