6 namespace tdc {
namespace esp {
10 template<
typename View>
13 sorted_indices.reserve(input.
size());
14 for(
size_t i = 0; i < input.
size(); i++) {
15 sorted_indices.push_back(i);
17 DCHECK_EQ(sorted_indices.capacity(), input.
size());
20 std::stable_sort(sorted_indices.begin(), sorted_indices.end(), [&](
const size_t& a,
const size_t& b) {
21 return input[a] < input[b];
33 return a.
x == b.
x && a.
y == b.
y;
37 return o <<
"(" << a.
x <<
", " << a.
y <<
")";
61 inline bool has_next();
62 inline Link advance();
67 size_t end = searchA.
size();
70 size_t mid = (end - start) / 2 + start;
72 if (searchA[mid] > piy) {
77 }
while ((end - start) > 1);
87 inverted_link = sindices.
size() - link - 1;
95 std::vector<Link> m_linked_list;
96 std::vector<Link> m_layers;
97 bool m_reverse =
false;
98 std::vector<size_t> m_rebuild_A;
99 size_t m_removed_counter = 0;
100 size_t m_remaining_size = 0;
101 std::vector<Link> m_free_list;
102 size_t m_free_start = 0;
103 size_t m_free_end = 0;
108 m_linked_list.reserve(sindices.
size());
109 m_linked_list.resize(sindices.
size());
110 m_sindices = sindices;
111 m_remaining_size = sindices.
size();
112 m_free_list.reserve(sindices.
size());
113 m_free_list.resize(sindices.
size());
115 DCHECK_GT(sindices.
size(), 0);
117 for(
size_t i = 1; i < m_linked_list.size(); i++) {
118 m_linked_list[i] = i - 1;
120 m_linked_list[0] = 0;
122 for(
size_t i = 0; i < m_free_list.size() - 1; i++) {
123 m_free_list[i] = i + 1;
125 m_free_list[m_free_list.size() - 1] = m_free_list.size() - 1;
128 m_free_end = m_linked_list.size() - 1;
134 auto& A = m_rebuild_A;
136 A.push_back(
size_t(-1));
141 while (layers_iter.has_next()) {
142 auto link = layers_iter.advance();
145 auto is_new_layer = (j == l);
147 m_layers.push_back(
Link());
151 auto& Lj_plus = m_layers[j];
154 m_linked_list[link] = link;
160 auto old_head = Lj_plus;
161 m_linked_list[link] = old_head;
166 if (A.size() == (j + 1)) {
178 return (lhsp.x > rhsp.x) && (lhsp.y > rhsp.y);
183 inline bool lis(
size_t k, std::vector<size_t>& q) {
184 if (k > m_layers.size()) {
return false; }
188 if (k == 0) {
return true; }
201 Link search_link = m_layers[l];
204 if (dominates(search_link, q[l + 1])) {
208 DCHECK_NE(m_linked_list[search_link], search_link)
209 <<
"should always break out before this happens";
210 search_link = m_linked_list[search_link];
216 std::reverse(q.begin(), q.end());
224 for(
auto link : links) {
225 m_linked_list[link] = m_removed_counter;
230 Link free_link = m_free_start;
231 Link prev_free_link = free_link;
232 Link next_free_link = m_free_list[free_link];
234 auto has_prev = [&](){
return prev_free_link != free_link; };
235 auto has_next = [&](){
return free_link != next_free_link; };
237 auto update_free_list = [&](
Link remove_link) {
238 while (free_link != remove_link) {
240 prev_free_link = free_link;
241 free_link = next_free_link;
242 next_free_link = m_free_list[free_link];
245 DCHECK_EQ(free_link, remove_link);
247 if (has_prev() && has_next()) {
248 m_free_list[prev_free_link] = next_free_link;
249 free_link = next_free_link;
250 next_free_link = m_free_list[free_link];
251 }
else if (has_prev() && !has_next()) {
252 DCHECK_EQ(m_free_end, free_link);
254 m_free_list[prev_free_link] = prev_free_link;
255 m_free_end = prev_free_link;
256 free_link = next_free_link;
257 next_free_link = m_free_list[free_link];
258 }
else if (!has_prev() && has_next()) {
259 DCHECK_EQ(m_free_start, free_link);
261 m_free_start = next_free_link;
262 prev_free_link = next_free_link;
263 free_link = next_free_link;
264 next_free_link = m_free_list[free_link];
266 DCHECK_EQ(m_free_start, prev_free_link);
267 DCHECK_EQ(prev_free_link, free_link);
268 DCHECK_EQ(free_link, next_free_link);
269 DCHECK_EQ(next_free_link, m_free_end);
275 if (links.size() < 2 || links[0] < links[1]) {
277 for (
size_t i = 0; i < links.size(); i++) {
278 update_free_list(links[i]);
282 for (
size_t i = 0; i < links.size(); i++) {
283 update_free_list(links[links.size() - i - 1]);
291 m_remaining_size -= links.size();
294 if (m_remaining_size > 0) {
295 Link link = m_free_start;
296 Link prev_link = link;
298 m_linked_list[link] = prev_link;
299 Link next = m_free_list[link];
363 std::vector<std::vector<Point>> r;
364 for (
auto link : m_layers) {
365 std::vector<Point> list;
369 if (m_linked_list[link] == link) {
372 link = m_linked_list[link];
376 r.push_back(std::move(list));
382 return m_layers.size();
386 return m_sindices[link];
390 return m_remaining_size;
395 return std::move(m_linked_list);
401 m_front(l.m_free_start),
402 m_back(l.m_free_end),
404 m_has_next(l.m_remaining_size > 0)
406 inline void LayersIterator::debug() {
407 std::cout <<
"front: " << m_front <<
", back: " << m_back <<
", bits: ";
413 if (m_front == m_back) {
421 m_back = m_l->m_linked_list[m_back];
424 m_front = m_l->m_free_list[m_front];
436 template<
typename SortedIndices,
size_t tie_to_decreasing = false>
438 std::vector<size_t> Dpi;
441 auto l =
esp::L(sorted_indices);
442 std::vector<esp::Link> links;
443 while (l.sindices_size() > 0) {
446 l.lis(l.layers_size(), links);
449 if (!tie_to_decreasing && (links.size() == l.layers_size())) {
451 }
else if (tie_to_decreasing && (links.size() == l.layers_size())) {
452 l.lis(l.layers_size(), links);
454 }
else if (links.size() > l.layers_size()) {
457 l.lis(l.layers_size(), links);
462 l.remove_all_and_slice(links);
464 Dpi = std::move(l).extract();
467 return Dpi_and_b { std::move(Dpi), std::move(b) };
470 template<
typename SortedIndices,
typename Dpi_t>
475 auto Dsi = std::vector<size_t> {};
476 Dsi.reserve(sorted_indices.size());
477 Dsi.resize(sorted_indices.size());
478 for (
size_t i = 0; i < sorted_indices.size(); i++) {
479 Dsi[sorted_indices[i]] = Dpi[i];
484 template<
typename Dxx_t>
485 auto make_wt(
const Dxx_t& v,
size_t max_char) -> std::vector<IntVector<uint_t<1>>> {
486 auto rev = [](uint64_t x) -> uint64_t {
487 x = ((x & 0x5555555555555555ULL) << 1) | ((x & 0xAAAAAAAAAAAAAAAAULL) >> 1);
488 x = ((x & 0x3333333333333333ULL) << 2) | ((x & 0xCCCCCCCCCCCCCCCCULL) >> 2);
489 x = ((x & 0x0F0F0F0F0F0F0F0FULL) << 4) | ((x & 0xF0F0F0F0F0F0F0F0ULL) >> 4);
490 x = ((x & 0x00FF00FF00FF00FFULL) << 8) | ((x & 0xFF00FF00FF00FF00ULL) >> 8);
491 x = ((x & 0x0000FFFF0000FFFFULL) <<16) | ((x & 0xFFFF0000FFFF0000ULL) >>16);
492 x = ((x & 0x00000000FFFFFFFFULL) <<32) | ((x & 0xFFFFFFFF00000000ULL) >>32);
495 auto wt_bvs = std::vector<IntVector<uint_t<1>>>();
505 size_t alloc_size = (v.size() + 63ULL) >> 6;
514 auto wt = wt_pc<size_t, size_t>(v, v.size(), wt_depth).get_bv();
516 for (
size_t i = 0; i < wt.size(); i++) {
520 auto start = tmp.
data();
521 auto end = start + alloc_size;
523 while (start != end) {
524 *start = rev(*start2);
530 wt_bvs.push_back(std::move(tmp));
549 return m_start != m_end;
557 size_t bit = (*m_bv)[m_start++];
558 if (m_next0 !=
nullptr) {
561 return m_next0->
next();
563 return m_next1->
next();
566 DCHECK_EQ(m_max_value - m_min_value, 2)
567 <<
"\nmin: " << m_min_value <<
", " 568 <<
"\nmax: " << m_max_value <<
", " 569 <<
"\nstart: " << m_start <<
", " 570 <<
"\nend: " << m_end <<
", " 575 return m_max_value - 1;
583 size_t size) -> std::vector<size_t>
586 for(
size_t depth = 0; depth < bvs.size(); depth++) {
587 count = count * 2 + 1;
590 std::vector<size_t> ret;
594 size_t max_value = node_sizes.back().size() * 2;
597 auto iters = std::vector<WTIter>();
598 iters.reserve(count);
601 iters[0].m_min_value = 0;
602 iters[0].m_max_value = max_value;
605 for(
size_t depth = 0; depth < bvs.size(); depth++) {
606 auto& layer = node_sizes.at(depth);
608 size_t layer_bv_offset = 0;
609 for(
size_t node_i = 0; node_i < layer.size(); node_i++) {
610 size_t node_size = layer.at(node_i);
611 auto& iter = iters.at(iters_i);
613 iter.m_start = layer_bv_offset;
614 iter.m_end = iter.m_start + node_size;
615 iter.m_bv = &bvs.at(depth);
616 iter.m_depth = depth;
617 iter.m_next0 =
nullptr;
618 iter.m_next1 =
nullptr;
620 if (depth < (bvs.size() - 1)) {
621 auto& child0 = iters[iters_i * 2 + 1];
622 auto& child1 = iters[iters_i * 2 + 2];
624 iter.m_next0 = &child0;
625 iter.m_next1 = &child1;
627 size_t min = iter.m_min_value;
628 size_t max = iter.m_max_value;
630 size_t mid = (max - min) / 2 + min;
632 child0.m_min_value = min;
633 child0.m_max_value = mid;
634 child1.m_min_value = mid;
635 child1.m_max_value = max;
639 layer_bv_offset += node_size;
645 ret.push_back(iters[0].next());
658 size_t size) -> std::vector<size_t>
660 auto wt_sizes = std::vector<std::vector<size_t>> { { size } };
661 size_t wt_sizes_i = 0;
662 size_t sizes_sizes = 1;
664 for (
size_t bvs_i = 0; (bvs_i + 1) < bvs.size(); bvs_i++) {
665 auto& layer = bvs[bvs_i];
667 auto wt_sizes_next = std::vector<size_t> {};
668 wt_sizes_next.reserve(sizes_sizes);
669 wt_sizes_next.resize(sizes_sizes);
670 size_t wt_sizes_next_i = 0;
674 for(
size_t w : wt_sizes[wt_sizes_i]) {
675 for(
size_t i = start; i < (start + w); i++) {
676 size_t bit = layer[i];
678 wt_sizes_next[wt_sizes_next_i * 2 + bit] += 1;
684 wt_sizes.push_back(std::move(wt_sizes_next));
697 template<
typename Dxx_t,
typename b_t,
typename Bde_t,
typename D_t>
704 std::vector<size_t> ss_ll;
705 ss_ll.reserve(Dpi.size());
706 ss_ll.resize(Dpi.size());
708 std::vector<size_t> ss_ll_front;
709 ss_ll_front.reserve(b.size());
710 ss_ll_front.resize(b.size(), size_t(-1));
715 for (
size_t i = 0; i < Dpi.size(); i++) {
716 size_t list_i = Dpi[i];
717 if (ss_ll_front[list_i] ==
size_t(-1)) {
718 ss_ll_front[list_i] = i;
721 size_t root_node = ss_ll_front[list_i];
722 size_t root_node_next = ss_ll[root_node];
724 ss_ll[root_node] = i;
725 ss_ll[i] = root_node_next;
728 ss_ll_front[list_i] = i;
733 for (
size_t& link : ss_ll_front) {
738 DCHECK_EQ(D.size(), Dpi.size());
739 for (
size_t i = 0; i < Dsi.size(); i++) {
740 size_t j = Dsi.size() - i - 1;
742 size_t list_i = Dsi[j];
744 auto front = ss_ll_front[list_i];
745 ss_ll_front[list_i] = ss_ll[front];
Contains the text compression and encoding framework.
std::vector< size_t > extract() &&
std::vector< Sindex > sorted_indices(const View &input)
A vector over arbitrary unsigned integer types.
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 ])...
A const view into a slice of memory.
auto make_wt(const Dxx_t &v, size_t max_char) -> std::vector< IntVector< uint_t< 1 >>>
std::vector< size_t > Dpi
auto recover_D_from_encoding(const Dxx_t &Dpi, const Dxx_t &Dsi, const b_t &b, const Bde_t &Bde, D_t *out)
size_type size() const
Returns size of the View.
Point point_coord_for_link(ConstGenericView< Sindex > sindices, Link link, bool reverse)
void rebuild(bool reverse)
void remove_all_and_slice(ConstGenericView< size_t > links)
auto recover_Dxx(const std::vector< IntVector< uint_t< 1 >>> &bvs, size_t size) -> std::vector< size_t >
bool operator==(const Array< N, T > &lhs, const Array< N, T > &rhs)
L(ConstGenericView< Sindex > sindices)
typename uint_dispatch_t< N >::type uint_t
std::ostream & operator<<(std::ostream &o, const Point &a)
LayersIterator(L &l, bool reverse)
std::vector< std::vector< Point > > to_debug_layer_points()
internal_data_type * data() noexcept
std::vector< size_t > create_dsigma_from_dpi_and_sorted_indices(const SortedIndices &sorted_indices, const Dpi_t &Dpi)
IntVector< uint_t< 1 > > b
bool lis(size_t k, std::vector< size_t > &q)
Sindex sindex_for_link(Link link)
Point point_coord_if_at_index(Sindex self, size_t index)
const IntVector< uint_t< 1 > > * m_bv
bool dominates(Link lhs, Link rhs)
void reserve(size_type n)
size_t layers_A_search(ConstGenericView< size_t > searchA, size_t piy)
auto extract_from_wt(const std::vector< std::vector< size_t >> &node_sizes, const std::vector< IntVector< uint_t< 1 >>> &bvs, size_t size) -> std::vector< size_t >
Dpi_and_b create_dpi_and_b_from_sorted_indices(const SortedIndices &sorted_indices)