19 std::vector<BIT_SEQUENCE> bits_seq;
20 std::vector<PREFIX_SUM> length_seq;
26 using iterator_category = std::random_access_iterator_tag;
28 using difference_type = std::ptrdiff_t;
36 uint64_t operator*()
const noexcept {
return this->x_rank; }
44 this->x_rank = this->container->access_x_rank(this->y_rank);
48 this->x_rank = this->container->size();
63 this->x_rank = this->container->access_x_rank(this->y_rank);
67 this->x_rank = this->container->size();
84 this->x_rank = this->container->access_x_rank(this->y_rank);
88 this->x_rank = this->container->size();
97 this->x_rank = this->container->access_x_rank(this->y_rank);
101 this->x_rank = this->container->size();
122 using iterator_category = std::random_access_iterator_tag;
124 using difference_type = std::ptrdiff_t;
129 XRankIterator() : container(
nullptr), x_rank(0), y_rank(0) {}
132 uint64_t operator*()
const noexcept {
return this->y_rank; }
140 this->y_rank = this->container->access_y_rank(this->x_rank);
144 this->y_rank = this->container->size();
156 if (this->x_rank > 0)
159 this->y_rank = this->container->access_y_rank(this->x_rank);
163 this->y_rank = this->container->size();
180 this->y_rank = this->container->access_y_rank(this->x_rank);
184 this->y_rank = this->container->size();
193 this->y_rank = this->container->access_y_rank(this->x_rank);
197 this->y_rank = this->container->size();
224 this->bits_seq[i].clear();
225 this->length_seq[i].clear();
227 this->bits_seq.clear();
228 this->length_seq.clear();
233 uint64_t get_node_x_pos_in_bit_sequence(int64_t h, uint64_t h_node_id)
const{
238 return this->length_seq[h].psum(h_node_id-1);
244 uint64_t rank0_in_bit_sequence_of_node(uint64_t h, [[maybe_unused]] uint64_t h_node_id, uint64_t node_x_pos_in_bit_sequence, uint64_t i)
const {
245 assert(i <= this->length_seq[h].at(h_node_id));
246 assert(node_x_pos_in_bit_sequence == this->get_node_x_pos_in_bit_sequence(h, h_node_id));
247 return this->bits_seq[h].rank0(node_x_pos_in_bit_sequence + i + 1) - this->bits_seq[h].rank0(node_x_pos_in_bit_sequence);
253 uint64_t rank1_in_bit_sequence_of_node(uint64_t h, [[maybe_unused]] uint64_t h_node_id, uint64_t node_x_pos_in_bit_sequence, uint64_t i)
const {
254 assert(i <= this->length_seq[h].at(h_node_id));
255 assert(node_x_pos_in_bit_sequence == this->get_node_x_pos_in_bit_sequence(h, h_node_id));
256 return this->bits_seq[h].rank1(node_x_pos_in_bit_sequence + i + 1) - this->bits_seq[h].rank1(node_x_pos_in_bit_sequence);
261 void recursive_add(int64_t h, uint64_t h_node_id, uint64_t x_rank, uint64_t y_rank, std::vector<uint64_t> &output_path)
263 output_path[h] = h_node_id;
264 uint64_t node_size = this->length_seq[h].at(h_node_id);
265 uint64_t node_x_pos_in_bit_sequence = this->get_node_x_pos_in_bit_sequence(h, h_node_id);
267 if (h + 1 < this->height())
269 uint64_t left_node_id = 2 * h_node_id;
270 uint64_t right_node_id = 2 * h_node_id + 1;
271 uint64_t left_tree_size = this->length_seq[h + 1].at(left_node_id);
273 if (x_rank <= left_tree_size)
275 uint64_t new_y_rank = y_rank > 0 ? this->rank0_in_bit_sequence_of_node(h, h_node_id, node_x_pos_in_bit_sequence, y_rank-1) : 0;
276 this->recursive_add(h + 1, left_node_id, x_rank, new_y_rank, output_path);
277 this->bits_seq[h].insert(node_x_pos_in_bit_sequence + y_rank,
false);
278 this->length_seq[h].increment(h_node_id, 1);
282 uint64_t new_y_rank = y_rank > 0 ? this->rank1_in_bit_sequence_of_node(h, h_node_id, node_x_pos_in_bit_sequence, y_rank - 1) : 0;
283 uint64_t new_x_rank = x_rank - left_tree_size;
285 this->recursive_add(h + 1, right_node_id, new_x_rank, new_y_rank, output_path);
286 this->bits_seq[h].insert(node_x_pos_in_bit_sequence + y_rank,
true);
287 this->length_seq[h].increment(h_node_id, 1);
307 assert(this->get_bit_count_in_node(h, h_node_id) <= 1);
309 this->bits_seq[h].insert(node_x_pos_in_bit_sequence + y_rank,
false);
310 this->length_seq[h].increment(h_node_id, 1);
311 }
else if(node_size == 1){
314 this->bits_seq[h].set_bit(node_x_pos_in_bit_sequence,
true);
315 this->bits_seq[h].insert(node_x_pos_in_bit_sequence + y_rank,
false);
319 this->bits_seq[h].insert(node_x_pos_in_bit_sequence + y_rank,
true);
321 this->length_seq[h].increment(h_node_id, 1);
323 throw std::runtime_error(
"node_size > 1");
332 static uint64_t _get_upper_size_of_root(uint64_t H)
334 return _get_upper_size_of_internal_node(0, H);
338 static uint64_t _get_upper_size_of_internal_node(uint64_t h, uint64_t H)
342 for (uint64_t p = h + 1; p < H; p++)
354 uint64_t get_upper_size_of_internal_node(uint64_t h)
const
356 return _get_upper_size_of_internal_node(h, this->height());
358 uint64_t get_lower_size_of_internal_node(uint64_t h)
const
360 uint64_t fsize = _get_upper_size_of_internal_node(h, this->height());
365 void build_h_bit_sequence(uint64_t h,
const std::vector<uint64_t> &rank_elements, std::vector<uint64_t> &output_next_rank_elements, std::vector<uint64_t> &output_next_length_seq)
368 uint64_t h_node_count = 1ULL << h;
369 uint64_t counter = 0;
370 uint64_t node_x_pos = 0;
371 std::vector<bool> tmp_bit_sequence(rank_elements.size(),
false);
373 if((int64_t)(h + 1) < this->height()){
374 output_next_rank_elements.resize(rank_elements.size(), UINT64_MAX);
375 output_next_length_seq.resize(h_node_count * 2, UINT64_MAX);
380 for(uint64_t i = 0; i < h_node_count; i++){
381 uint64_t bit_size = this->get_bit_count_in_node(h, i);
382 uint64_t half_size = bit_size / 2;
386 if((int64_t)(h + 1) < this->height())
388 uint64_t left_counter = 0;
389 for (uint64_t j = 0; j < bit_size; j++)
391 if (rank_elements[node_x_pos + j] < half_size)
394 output_next_rank_elements[counter++] = rank_elements[node_x_pos + j];
398 output_next_length_seq[i * 2] = left_counter;
403 uint64_t right_counter = 0;
405 if((int64_t)(h + 1) < this->height()){
406 for (uint64_t j = 0; j < bit_size; j++)
408 if (rank_elements[node_x_pos + j] >= half_size)
411 tmp_bit_sequence[node_x_pos + j] =
true;
412 output_next_rank_elements[counter++] = rank_elements[node_x_pos + j] - half_size;
416 output_next_length_seq[(i * 2) + 1] = right_counter;
419 throw std::runtime_error(
"Error in build_h_bit_sequence, bit_size > 1");
425 node_x_pos += bit_size;
427 this->bits_seq[h].clear();
428 this->bits_seq[h].push_many(tmp_bit_sequence);
431 void rebuild_h_bit_sequence(uint64_t h, uint64_t first_node_id, uint64_t local_h_node_count,
const std::vector<uint64_t> &rank_elements, std::vector<uint64_t> &output_next_rank_elements, std::vector<uint64_t> &output_next_length_seq)
433 assert(first_node_id + local_h_node_count - 1 < this->length_seq[h].size());
436 uint64_t counter = 0;
437 uint64_t node_x_pos = this->get_node_x_pos_in_bit_sequence(h, first_node_id);
438 const uint64_t first_node_x_pos = node_x_pos;
439 std::vector<bool> tmp_bit_sequence(rank_elements.size(),
false);
441 if((int64_t)(h + 1) < this->height()){
442 output_next_rank_elements.resize(rank_elements.size(), UINT64_MAX);
443 output_next_length_seq.resize(local_h_node_count * 2, UINT64_MAX);
448 for(uint64_t node_id = first_node_id; node_id <= first_node_id + local_h_node_count - 1; node_id++){
449 uint64_t bit_size = this->get_bit_count_in_node(h, node_id);
450 uint64_t half_size = bit_size / 2;
454 if((int64_t)(h + 1) < this->height())
456 uint64_t left_counter = 0;
457 for (uint64_t j = 0; j < bit_size; j++)
459 if (rank_elements[(node_x_pos - first_node_x_pos) + j] < half_size)
461 output_next_rank_elements[counter++] = rank_elements[(node_x_pos - first_node_x_pos) + j];
465 output_next_length_seq[(node_id - first_node_id) * 2] = left_counter;
470 uint64_t right_counter = 0;
472 if((int64_t)(h + 1) < this->height()){
473 for (uint64_t j = 0; j < bit_size; j++)
475 if (rank_elements[(node_x_pos - first_node_x_pos) + j] >= half_size)
478 tmp_bit_sequence[(node_x_pos - first_node_x_pos) + j] =
true;
479 output_next_rank_elements[counter++] = rank_elements[(node_x_pos - first_node_x_pos) + j] - half_size;
483 output_next_length_seq[(node_id - first_node_id) * 2 + 1] = right_counter;
486 throw std::runtime_error(
"Error in rebuild_h_bit_sequence, bit_size > 1");
492 node_x_pos += bit_size;
494 this->bits_seq[h].set_bits(first_node_x_pos, tmp_bit_sequence);
498 void build(
const std::vector<uint64_t> &rank_elements,
int message_paragraph = stool::Message::NO_MESSAGE)
507 uint64_t fsize = _get_upper_size_of_root(height);
508 if (rank_elements.size() < fsize)
518 if(message_paragraph != stool::Message::NO_MESSAGE){
519 std::cout << stool::Message::get_paragraph_string(message_paragraph) <<
"Building wavelet tree for range search... " <<
"(input size = " << rank_elements.size() <<
", tree height = " << height <<
")" << std::endl;
521 std::chrono::system_clock::time_point st1, st2;
522 st1 = std::chrono::system_clock::now();
525 this->bits_seq.resize(height);
526 this->length_seq.resize(height);
527 for(uint64_t h = 0; h < height; h++){
528 this->bits_seq[h].clear();
529 this->length_seq[h].clear();
533 this->length_seq[0].push_back(rank_elements.size());
534 std::vector<uint64_t> tmp_rank_elements = rank_elements;
536 for(uint64_t h = 0; h < height; h++){
538 if(message_paragraph != stool::Message::NO_MESSAGE){
539 std::cout << stool::Message::get_paragraph_string(message_paragraph+1) <<
"Building the " << h <<
"-th bit sequence in the wavelet tree... " << std::endl;
541 std::vector<uint64_t> next_rank_elements;
542 std::vector<uint64_t> next_length_seq;
544 this->build_h_bit_sequence(h, tmp_rank_elements, next_rank_elements, next_length_seq);
547 tmp_rank_elements.swap(next_rank_elements);
549 this->length_seq[h+1].push_many(next_length_seq);
554 assert(this->verify());
556 st2 = std::chrono::system_clock::now();
557 uint64_t sec_time = std::chrono::duration_cast<std::chrono::seconds>(st2 - st1).count();
559 if(message_paragraph != stool::Message::NO_MESSAGE){
560 std::cout << stool::Message::get_paragraph_string(message_paragraph) <<
"[DONE] Elapsed Time: " << sec_time <<
" sec" << std::endl;
564 uint64_t get_bit_count_in_node(uint64_t h, uint64_t h_node_id)
const {
565 assert(h < this->length_seq.size());
566 assert(h_node_id < this->length_seq[h].size());
567 return this->length_seq[h].at(h_node_id);
571 bool is_unbalanced_node(uint8_t h, uint64_t h_node_id)
const
573 if (h + 1 < this->height())
575 uint64_t left_node_id = 2 * h_node_id;
576 uint64_t right_node_id = 2 * h_node_id + 1;
577 uint64_t left_tree_size = this->get_bit_count_in_node(h + 1, left_node_id);
578 uint64_t right_tree_size = this->get_bit_count_in_node(h + 1, right_node_id);
579 bool unbalance_flag_LR = left_tree_size > (right_tree_size * 2) || right_tree_size > (left_tree_size * 2);
580 uint64_t child_upper_size = this->get_upper_size_of_internal_node(h + 1);
581 bool full_flag_L = left_tree_size >= child_upper_size;
582 bool full_flag_R = right_tree_size >= child_upper_size;
583 return unbalance_flag_LR || full_flag_L || full_flag_R;
587 return this->length_seq[h].at(h_node_id) >= 2;
597 void swap(DynamicWaveletTreeOnGrid &item)
599 this->length_seq.swap(item.length_seq);
600 this->bits_seq.swap(item.bits_seq);
603 int64_t height()
const
605 return this->bits_seq.size();
607 uint64_t size()
const
609 if (this->height() > 0)
611 return this->bits_seq[0].size();
619 uint64_t access_x_rank(uint64_t y_rank)
const
621 assert(y_rank < this->size());
622 uint64_t x_rank = this->compute_local_x_rank(0, 0, y_rank);
625 uint64_t find_leaf_index(uint64_t x_rank)
const
628 if (x_rank >= this->size())
630 throw std::runtime_error(
"ERROR in find_leaf_index: x_rank is out of range");
633 uint64_t current_x_rank = x_rank;
634 uint64_t current_node_id = 0;
635 uint64_t height = this->height();
637 for (uint64_t h = 0; h + 1 < height; h++)
639 uint64_t left_tree_size = this->get_bit_count_in_node(h+1, 2 * current_node_id);
641 if (current_x_rank < left_tree_size)
643 current_node_id = 2 * current_node_id;
647 current_x_rank = current_x_rank - left_tree_size;
648 current_node_id = 2 * current_node_id + 1;
651 return current_node_id;
654 uint64_t access_y_rank(uint64_t x_rank)
const
656 uint64_t leaf_index = this->find_leaf_index(x_rank);
657 uint64_t current_y_rank = 0;
658 uint64_t prev_node_id = leaf_index;
659 int64_t height = this->height();
660 for (int64_t h = height - 2; h >= 0; h--)
662 uint64_t next_node_id = prev_node_id / 2;
663 uint64_t next_x_pos = this->get_node_x_pos_in_bit_sequence(h, next_node_id);
665 if (prev_node_id % 2 == 0)
667 uint64_t count_zero_offset = this->bits_seq[h].rank0(next_x_pos);
668 uint64_t next_y_rank = this->bits_seq[h].select0(current_y_rank + count_zero_offset) - next_x_pos;
669 current_y_rank = next_y_rank;
670 prev_node_id = next_node_id;
676 uint64_t count_one_offset = this->bits_seq[h].rank1(next_x_pos);
677 int64_t select_result = this->bits_seq[h].select1(current_y_rank + count_one_offset);
678 assert(select_result >= 0);
679 uint64_t next_y_rank = select_result - next_x_pos;
683 current_y_rank = next_y_rank;
684 prev_node_id = next_node_id;
689 return current_y_rank;
691 std::vector<bool> get_bit_sequence(uint64_t h, uint64_t node_id)
const{
692 uint64_t x_pos = this->get_node_x_pos_in_bit_sequence(h, node_id);
693 uint64_t node_size = this->get_bit_count_in_node(h, node_id);
695 r.resize(node_size,
false);
696 for(uint64_t i = 0; i < node_size; i++){
697 r[i] = this->bits_seq[h].at(x_pos + i);
705 for (uint64_t h = 0; h < this->bits_seq.size(); h++)
707 uint64_t node_count = 1 << h;
709 if(h + 1 < this->bits_seq.size()){
710 for (uint64_t i = 0; i < node_count; i++)
712 std::vector<bool> bit_sequence = this->get_bit_sequence(h, i);
715 for(uint64_t j = 0; j < bit_sequence.size(); j++){
723 uint64_t left_tree_size = this->get_bit_count_in_node(h+1, 2 * i);
724 uint64_t right_tree_size = this->get_bit_count_in_node(h+1, 2 * i + 1);
726 if(countL != left_tree_size){
728 throw std::runtime_error(
"Error: verify, countL != left_tree_size");
731 if(countR != right_tree_size){
733 throw std::runtime_error(
"Error: verify, countR != right_tree_size");
738 for (uint64_t i = 0; i < node_count; i++)
740 uint64_t bit_size = this->get_bit_count_in_node(h, i);
743 throw std::runtime_error(
"Error: verify function, bit_size > 1");
753 std::vector<uint64_t> to_local_rank_elements_in_y_order(uint64_t h, uint64_t node_id)
const
755 uint64_t height = this->height();
756 uint64_t node_size = this->get_bit_count_in_node(h, node_id);
757 std::vector<uint64_t> r;
758 r.resize(node_size, UINT64_MAX);
759 uint64_t x_pos = this->get_node_x_pos_in_bit_sequence(h, node_id);
763 uint64_t counterL = 0;
764 uint64_t counterR = 0;
765 uint64_t leaf_id_L = 2 * node_id;
766 uint64_t leaf_id_R = 2 * node_id + 1;
767 uint64_t left_tree_size = this->get_bit_count_in_node(h + 1, leaf_id_L);
769 std::vector<uint64_t> left_elements = this->to_local_rank_elements_in_y_order(h + 1, leaf_id_L);
770 std::vector<uint64_t> right_elements = this->to_local_rank_elements_in_y_order(h + 1, leaf_id_R);
772 assert(left_elements.size() + right_elements.size() == node_size);
777 for (uint64_t i = 0; i < node_size; i++)
779 bool b = this->bits_seq[h].at(x_pos + i);
782 assert(counterR < right_elements.size());
784 r[i] = right_elements[counterR++] + left_tree_size;
788 assert(counterL < left_elements.size());
789 r[i] = left_elements[counterL++];
797 }
else if(node_size > 1){
798 for (uint64_t i = 0; i < node_size; i++)
800 r[i] = this->bits_seq[h].at(x_pos + i);
809 std::vector<uint64_t> to_rank_elements_in_y_order()
const
812 if (this->height() > 0)
814 return this->to_local_rank_elements_in_y_order(0, 0);
818 std::vector<uint64_t> r;
822 std::vector<uint64_t> to_rank_elements_in_x_order()
const
824 std::vector<uint64_t> r;
825 r.resize(this->size(), UINT64_MAX);
826 uint64_t size = this->size();
827 for(uint64_t i = 0; i < size; i++){
828 r[i] = this->access_y_rank(i);
835 uint64_t compute_local_x_rank(uint64_t node_y, uint64_t node_id, uint64_t local_y_rank)
const
837 assert(local_y_rank < this->length_seq[node_y].at(node_id));
840 uint64_t h_node_id = node_id;
841 int64_t height = this->height();
842 for (int64_t h = node_y; h + 1 < height; h++)
844 uint64_t node_x_pos = this->get_node_x_pos_in_bit_sequence(h, h_node_id);
846 assert(node_x_pos + local_y_rank < this->bits_seq[h].size());
848 bool b = this->bits_seq[h].at(node_x_pos + local_y_rank);
849 uint64_t next_node_id = (2 * h_node_id) + (uint64_t)b;
852 uint64_t left_tree_size = this->get_bit_count_in_node(h+1, 2 * h_node_id);
853 x_rank += left_tree_size;
854 local_y_rank -= this->rank0_in_bit_sequence_of_node(h, h_node_id, node_x_pos, local_y_rank);
858 local_y_rank -= this->rank1_in_bit_sequence_of_node(h, h_node_id, node_x_pos, local_y_rank);
860 h_node_id = next_node_id;
866 template <
typename APPENDABLE_VECTOR>
867 uint64_t local_range_report_on_internal_node(uint64_t h, uint64_t node_id, uint64_t x_rank_gap, uint64_t hy_min, uint64_t hy_max, APPENDABLE_VECTOR &out)
const
870 for (uint64_t i = hy_min; i <= hy_max; i++)
872 uint64_t x = this->compute_local_x_rank(h, node_id, i) + x_rank_gap;
875 return hy_max - hy_min + 1;
878 template <
typename APPENDABLE_VECTOR>
879 uint64_t recursive_range_report_on_internal_nodes(uint64_t h, uint64_t node_id, uint64_t node_x_pos, int64_t x_min, int64_t x_max, uint64_t hy_min, uint64_t hy_max, APPENDABLE_VECTOR &out)
const
882 uint64_t found_elements_count = 0;
883 int64_t node_size = this->get_bit_count_in_node(h, node_id);
884 if (x_min <= (int64_t)node_x_pos && (int64_t)(node_x_pos + node_size - 1) <= x_max)
886 uint64_t limitR = std::min((int64_t)hy_max, node_size-1);
888 if(hy_min <= limitR){
889 uint64_t _tmp = local_range_report_on_internal_node(h, node_id, node_x_pos, hy_min, limitR, out);
890 found_elements_count += _tmp;
894 else if((int64_t)(h+1) < this->height())
896 uint64_t node_x_pos_L = node_x_pos;
897 uint64_t node_x_pos_R = node_x_pos + this->get_bit_count_in_node(h+1, 2 * node_id);
905 int64_t hy_max_0 = rank0_in_bit_sequence_of_node(h, node_id, node_x_pos_L, hy_max) - 1;
906 int64_t hy_max_1 = rank1_in_bit_sequence_of_node(h, node_id, node_x_pos_L, hy_max) - 1;
909 int64_t hy_min_0 = hy_min > 0 ? rank0_in_bit_sequence_of_node(h, node_id, node_x_pos_L, hy_min - 1) : 0;
910 int64_t hy_min_1 = hy_min > 0 ? rank1_in_bit_sequence_of_node(h, node_id, node_x_pos_L, hy_min - 1) : 0;
913 uint64_t next_node_id_L = 2 * node_id;
914 uint64_t next_node_id_R = next_node_id_L + 1;
916 if (x_min < (int64_t)node_x_pos_R && hy_min_0 <= hy_max_0)
918 found_elements_count += this->recursive_range_report_on_internal_nodes(h + 1, next_node_id_L, node_x_pos_L, x_min, x_max, hy_min_0, hy_max_0, out);
921 if (x_max >= (int64_t)node_x_pos_R && hy_min_1 <= hy_max_1)
923 found_elements_count += this->recursive_range_report_on_internal_nodes(h + 1, next_node_id_R, node_x_pos_R, x_min, x_max, hy_min_1, hy_max_1, out);
927 return found_elements_count;
931 template <
typename APPENDABLE_VECTOR>
932 uint64_t range_report(uint64_t x_min, uint64_t x_max, uint64_t y_min, uint64_t y_max, APPENDABLE_VECTOR &out)
const
934 uint64_t found_elements_count = 0;
935 if (this->height() > 0)
937 found_elements_count = this->recursive_range_report_on_internal_nodes(0, 0, 0, x_min, x_max, y_min, y_max, out);
939 return found_elements_count;
942 XRankIterator x_rank_begin()
const
944 if (this->size() == 0)
946 return XRankIterator(
this, this->size(), this->size());
950 return XRankIterator(
this, 0, this->access_y_rank(0));
953 XRankIterator x_rank_end()
const
955 return XRankIterator(
this, this->size(), this->size());
957 YRankIterator y_rank_begin()
const
959 if (this->size() == 0)
961 return YRankIterator(
this, this->size(), this->size());
965 return YRankIterator(
this, this->access_x_rank(0), 0);
968 YRankIterator y_rank_end()
const
970 return YRankIterator(
this, this->size(), this->size());
973 void print_tree()
const
975 std::vector<std::string> r;
976 for (uint64_t h = 0; h < this->bits_seq.size(); h++)
978 std::vector<uint64_t> tmp_len_veq;
979 uint64_t counter = 0;
980 tmp_len_veq.push_back(0);
981 for(uint64_t i = 0; i < this->length_seq[h].size(); i++){
982 counter += this->length_seq[h].at(i);
983 tmp_len_veq.push_back(counter);
988 for(uint64_t i = 0; i < this->bits_seq[h].size(); i++){
989 while(tmp_len_veq[tmp_p] <= i){
994 bool b = this->bits_seq[h].at(i);
996 s.append(b ?
"1" :
"0");
1004 std::cout <<
"===== TREE =====" << std::endl;
1006 for (uint64_t i = 0; i < r.size(); i++)
1008 std::cout << r[i] << std::endl;
1010 std::cout <<
"===== [END] =====" << std::endl;
1013 static void store_to_file(DynamicWaveletTreeOnGrid &item, std::ofstream &os)
1015 throw std::runtime_error(
"Error: DynamicWaveletTreeOnGrid::store_to_file is not implemented");
1017 static void store_to_bytes(DynamicWaveletTreeOnGrid &item, std::vector<uint8_t> &output, uint64_t &pos)
1019 throw std::runtime_error(
"Error: DynamicWaveletTreeOnGrid::store_to_bytes is not implemented");
1021 uint64_t size_in_bytes(
bool only_extra_bytes =
false)
const
1024 sum +=
sizeof(uint64_t);
1025 for(int64_t h = 0; h < (int64_t)this->height(); h++){
1026 sum += this->bits_seq[h].size_in_bytes(only_extra_bytes);
1027 sum += this->length_seq[h].size_in_bytes(only_extra_bytes);
1034 static DynamicWaveletTreeOnGrid load_from_file(std::ifstream &ifs)
1036 throw std::runtime_error(
"Error: DynamicWaveletTreeOnGrid::load_from_file is not implemented");
1039 static DynamicWaveletTreeOnGrid load_from_bytes(
const std::vector<uint8_t> &data, uint64_t &pos)
1041 throw std::runtime_error(
"Error: DynamicWaveletTreeOnGrid::load_from_bytes is not implemented");
1043 void rebuild_internal_node(uint8_t h, uint64_t h_node_id)
1046 std::vector<uint64_t> rank_elements = this->to_local_rank_elements_in_y_order(h, h_node_id);
1049 uint64_t height = this->height();
1050 uint64_t current_node_id = h_node_id;
1051 uint64_t current_node_count = 1;
1052 for(uint64_t q = h; q < height; q++){
1053 std::vector<uint64_t> next_rank_elements;
1054 std::vector<uint64_t> next_length_seq;
1056 this->rebuild_h_bit_sequence(q, current_node_id, current_node_count, rank_elements, next_rank_elements, next_length_seq);
1058 rank_elements.swap(next_rank_elements);
1060 current_node_count *= 2;
1061 current_node_id *= 2;
1064 this->length_seq[q+1].set_values(current_node_id, next_length_seq);
1069 void add(uint64_t x_rank, uint64_t y_rank)
1073 if(this->size() > 0){
1076 std::vector<uint64_t> output_path(this->height(), UINT64_MAX);
1077 this->recursive_add(0, 0, x_rank, y_rank, output_path);
1078 uint64_t upper_size = this->get_upper_size_of_internal_node(0);
1079 if (this->size() >= upper_size)
1087 std::vector<uint64_t> rank_elements = this->to_rank_elements_in_y_order();
1088 this->build(rank_elements);
1097 uint64_t height = this->height();
1098 for(uint64_t h = 0; h < height; h++){
1099 uint64_t h_node_id = output_path[h];
1100 if (this->is_unbalanced_node(h, h_node_id))
1108 this->rebuild_internal_node(h, h_node_id);
1114 assert(this->verify());
1118 std::vector<uint64_t> rank_elements;
1119 rank_elements.push_back(0);
1120 this->build(rank_elements);
1128 void remove(uint64_t y_rank)
1130 int64_t height = this->height();
1132 throw std::runtime_error(
"Error: DynamicWaveletTreeOnGrid::remove(y_rank)");
1134 uint64_t h_y_rank = y_rank;
1135 uint64_t h_node_id = 0;
1137 for (int64_t h = 0; h < height; h++)
1139 uint64_t node_x_pos = this->get_node_x_pos_in_bit_sequence(h, h_node_id);
1140 bool b = this->bits_seq[h].at(node_x_pos + h_y_rank);
1141 uint64_t next_node_id = (2 * h_node_id) + (uint64_t)b;
1144 uint64_t rmv_y_rank = this->rank0_in_bit_sequence_of_node(h, h_node_id, node_x_pos, h_y_rank);
1145 this->bits_seq[h].remove(node_x_pos + h_y_rank);
1146 this->length_seq[h].decrement(h_node_id, 1);
1147 h_y_rank -= rmv_y_rank;
1151 uint64_t rmv_y_rank = this->rank1_in_bit_sequence_of_node(h, h_node_id, node_x_pos, h_y_rank);
1152 this->bits_seq[h].remove(node_x_pos + h_y_rank);
1153 this->length_seq[h].decrement(h_node_id, 1);
1154 h_y_rank -= rmv_y_rank;
1156 h_node_id = next_node_id;
1159 uint64_t upper_size = this->get_upper_size_of_internal_node(0);
1160 uint64_t size = this->size();
1161 if (size < upper_size / 2)
1163 auto rank_elements = this->to_rank_elements_in_y_order();
1164 this->build(rank_elements);
1167 assert(this->verify());
1173 std::vector<std::string> get_memory_usage_info(
int message_paragraph = stool::Message::SHOW_MESSAGE)
const
1176 std::vector<std::string> r;
1177 uint64_t size_in_bytes = this->size_in_bytes();
1178 uint64_t element_count = this->size();
1180 double bits_per_element = element_count > 0 ? ((double)size_in_bytes / (
double)element_count) : 0;
1182 r.push_back(stool::Message::get_paragraph_string(message_paragraph) +
"=DynamicWaveletTreeOnGrid: " + std::to_string(this->size_in_bytes())
1183 +
" bytes, " + std::to_string(element_count) +
" elements, " + std::to_string(bits_per_element) +
" bytes per element =");
1185 for(uint64_t h = 0; h < this->bits_seq.size(); h++){
1186 uint64_t _sub_size = 0;
1187 _sub_size += this->bits_seq[h].size_in_bytes();
1188 _sub_size += this->length_seq[h].size_in_bytes();
1190 uint64_t _bits_per_element = element_count > 0 ? ((double)_sub_size / (
double)element_count) : 0;
1191 r.push_back(stool::Message::get_paragraph_string(message_paragraph+1) +
"Level " + std::to_string(h) +
" in range tree: " + std::to_string(_sub_size) +
" bytes" +
" (" + std::to_string(_bits_per_element) +
" bytes per element)");
1193 r.push_back(stool::Message::get_paragraph_string(message_paragraph) +
"==");