19 std::vector<BIT_SEQUENCE> bits_seq;
20 std::vector<PREFIX_SUM> length_seq;
31 using iterator_category = std::random_access_iterator_tag;
33 using difference_type = std::ptrdiff_t;
41 uint64_t operator*()
const noexcept {
return this->x_rank; }
49 this->x_rank = this->container->access_x_rank(this->y_rank);
53 this->x_rank = this->container->size();
68 this->x_rank = this->container->access_x_rank(this->y_rank);
72 this->x_rank = this->container->size();
89 this->x_rank = this->container->access_x_rank(this->y_rank);
93 this->x_rank = this->container->size();
102 this->x_rank = this->container->access_x_rank(this->y_rank);
106 this->x_rank = this->container->size();
127 using iterator_category = std::random_access_iterator_tag;
129 using difference_type = std::ptrdiff_t;
134 XRankIterator() : container(
nullptr), x_rank(0), y_rank(0) {}
137 uint64_t operator*()
const noexcept {
return this->y_rank; }
145 this->y_rank = this->container->access_y_rank(this->x_rank);
149 this->y_rank = this->container->size();
161 if (this->x_rank > 0)
164 this->y_rank = this->container->access_y_rank(this->x_rank);
168 this->y_rank = this->container->size();
185 this->y_rank = this->container->access_y_rank(this->x_rank);
189 this->y_rank = this->container->size();
198 this->y_rank = this->container->access_y_rank(this->x_rank);
202 this->y_rank = this->container->size();
229 this->bits_seq[i].clear();
230 this->length_seq[i].clear();
232 this->bits_seq.clear();
233 this->length_seq.clear();
245 uint64_t get_node_x_pos_in_bit_sequence(int64_t h, uint64_t h_node_id)
const{
250 return this->length_seq[h].psum(h_node_id-1);
256 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 {
257 assert(i <= this->length_seq[h].at(h_node_id));
258 assert(node_x_pos_in_bit_sequence == this->get_node_x_pos_in_bit_sequence(h, h_node_id));
259 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);
265 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 {
266 assert(i <= this->length_seq[h].at(h_node_id));
267 assert(node_x_pos_in_bit_sequence == this->get_node_x_pos_in_bit_sequence(h, h_node_id));
268 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);
273 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)
275 output_path[h] = h_node_id;
276 uint64_t node_size = this->length_seq[h].at(h_node_id);
277 uint64_t node_x_pos_in_bit_sequence = this->get_node_x_pos_in_bit_sequence(h, h_node_id);
279 if (h + 1 < this->height())
281 uint64_t left_node_id = 2 * h_node_id;
282 uint64_t right_node_id = 2 * h_node_id + 1;
283 uint64_t left_tree_size = this->length_seq[h + 1].at(left_node_id);
285 if (x_rank <= left_tree_size)
287 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;
288 this->recursive_add(h + 1, left_node_id, x_rank, new_y_rank, output_path);
289 this->bits_seq[h].insert(node_x_pos_in_bit_sequence + y_rank,
false);
290 this->length_seq[h].increment(h_node_id, 1);
294 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;
295 uint64_t new_x_rank = x_rank - left_tree_size;
297 this->recursive_add(h + 1, right_node_id, new_x_rank, new_y_rank, output_path);
298 this->bits_seq[h].insert(node_x_pos_in_bit_sequence + y_rank,
true);
299 this->length_seq[h].increment(h_node_id, 1);
319 assert(this->get_bit_count_in_node(h, h_node_id) <= 1);
321 this->bits_seq[h].insert(node_x_pos_in_bit_sequence + y_rank,
false);
322 this->length_seq[h].increment(h_node_id, 1);
323 }
else if(node_size == 1){
326 this->bits_seq[h].set_bit(node_x_pos_in_bit_sequence,
true);
327 this->bits_seq[h].insert(node_x_pos_in_bit_sequence + y_rank,
false);
331 this->bits_seq[h].insert(node_x_pos_in_bit_sequence + y_rank,
true);
333 this->length_seq[h].increment(h_node_id, 1);
335 throw std::runtime_error(
"node_size > 1");
342 void add(uint64_t x_rank, uint64_t y_rank)
346 if(this->size() > 0){
349 std::vector<uint64_t> output_path(this->height(), UINT64_MAX);
350 this->recursive_add(0, 0, x_rank, y_rank, output_path);
351 uint64_t upper_size = this->get_upper_size_of_internal_node(0);
352 if (this->size() >= upper_size)
360 std::vector<uint64_t> rank_elements = this->to_rank_elements_in_y_order();
361 this->build(rank_elements);
370 uint64_t height = this->height();
371 for(uint64_t h = 0; h < height; h++){
372 uint64_t h_node_id = output_path[h];
373 if (this->is_unbalanced_node(h, h_node_id))
381 this->rebuild_internal_node(h, h_node_id);
387 assert(this->verify());
391 std::vector<uint64_t> rank_elements;
392 rank_elements.push_back(0);
393 this->build(rank_elements);
401 void remove(uint64_t y_rank)
403 int64_t height = this->height();
405 throw std::runtime_error(
"Error: DynamicWaveletTreeForRangeSearch::remove(y_rank)");
407 uint64_t h_y_rank = y_rank;
408 uint64_t h_node_id = 0;
410 for (int64_t h = 0; h < height; h++)
412 uint64_t node_x_pos = this->get_node_x_pos_in_bit_sequence(h, h_node_id);
413 bool b = this->bits_seq[h].at(node_x_pos + h_y_rank);
414 uint64_t next_node_id = (2 * h_node_id) + (uint64_t)b;
417 uint64_t rmv_y_rank = this->rank0_in_bit_sequence_of_node(h, h_node_id, node_x_pos, h_y_rank);
418 this->bits_seq[h].remove(node_x_pos + h_y_rank);
419 this->length_seq[h].decrement(h_node_id, 1);
420 h_y_rank -= rmv_y_rank;
424 uint64_t rmv_y_rank = this->rank1_in_bit_sequence_of_node(h, h_node_id, node_x_pos, h_y_rank);
425 this->bits_seq[h].remove(node_x_pos + h_y_rank);
426 this->length_seq[h].decrement(h_node_id, 1);
427 h_y_rank -= rmv_y_rank;
429 h_node_id = next_node_id;
432 uint64_t upper_size = this->get_upper_size_of_internal_node(0);
433 uint64_t size = this->size();
434 if (size < upper_size / 2)
436 auto rank_elements = this->to_rank_elements_in_y_order();
437 this->build(rank_elements);
440 assert(this->verify());
481 static uint64_t _get_upper_size_of_root(uint64_t H)
483 return _get_upper_size_of_internal_node(0, H);
487 static uint64_t _get_upper_size_of_internal_node(uint64_t h, uint64_t H)
491 for (uint64_t p = h + 1; p < H; p++)
503 uint64_t get_upper_size_of_internal_node(uint64_t h)
const
505 return _get_upper_size_of_internal_node(h, this->height());
507 uint64_t get_lower_size_of_internal_node(uint64_t h)
const
509 uint64_t fsize = _get_upper_size_of_internal_node(h, this->height());
520 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)
523 uint64_t h_node_count = 1ULL << h;
524 uint64_t counter = 0;
525 uint64_t node_x_pos = 0;
526 std::vector<bool> tmp_bit_sequence(rank_elements.size(),
false);
528 if((int64_t)(h + 1) < this->height()){
529 output_next_rank_elements.resize(rank_elements.size(), UINT64_MAX);
530 output_next_length_seq.resize(h_node_count * 2, UINT64_MAX);
535 for(uint64_t i = 0; i < h_node_count; i++){
536 uint64_t bit_size = this->get_bit_count_in_node(h, i);
537 uint64_t half_size = bit_size / 2;
541 if((int64_t)(h + 1) < this->height())
543 uint64_t left_counter = 0;
544 for (uint64_t j = 0; j < bit_size; j++)
546 if (rank_elements[node_x_pos + j] < half_size)
549 output_next_rank_elements[counter++] = rank_elements[node_x_pos + j];
553 output_next_length_seq[i * 2] = left_counter;
558 uint64_t right_counter = 0;
560 if((int64_t)(h + 1) < this->height()){
561 for (uint64_t j = 0; j < bit_size; j++)
563 if (rank_elements[node_x_pos + j] >= half_size)
566 tmp_bit_sequence[node_x_pos + j] =
true;
567 output_next_rank_elements[counter++] = rank_elements[node_x_pos + j] - half_size;
571 output_next_length_seq[(i * 2) + 1] = right_counter;
574 throw std::runtime_error(
"Error in build_h_bit_sequence, bit_size > 1");
580 node_x_pos += bit_size;
582 this->bits_seq[h].clear();
583 this->bits_seq[h].push_many(tmp_bit_sequence);
586 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)
588 assert(first_node_id + local_h_node_count - 1 < this->length_seq[h].size());
591 uint64_t counter = 0;
592 uint64_t node_x_pos = this->get_node_x_pos_in_bit_sequence(h, first_node_id);
593 const uint64_t first_node_x_pos = node_x_pos;
594 std::vector<bool> tmp_bit_sequence(rank_elements.size(),
false);
596 if((int64_t)(h + 1) < this->height()){
597 output_next_rank_elements.resize(rank_elements.size(), UINT64_MAX);
598 output_next_length_seq.resize(local_h_node_count * 2, UINT64_MAX);
603 for(uint64_t node_id = first_node_id; node_id <= first_node_id + local_h_node_count - 1; node_id++){
604 uint64_t bit_size = this->get_bit_count_in_node(h, node_id);
605 uint64_t half_size = bit_size / 2;
609 if((int64_t)(h + 1) < this->height())
611 uint64_t left_counter = 0;
612 for (uint64_t j = 0; j < bit_size; j++)
614 if (rank_elements[(node_x_pos - first_node_x_pos) + j] < half_size)
616 output_next_rank_elements[counter++] = rank_elements[(node_x_pos - first_node_x_pos) + j];
620 output_next_length_seq[(node_id - first_node_id) * 2] = left_counter;
625 uint64_t right_counter = 0;
627 if((int64_t)(h + 1) < this->height()){
628 for (uint64_t j = 0; j < bit_size; j++)
630 if (rank_elements[(node_x_pos - first_node_x_pos) + j] >= half_size)
633 tmp_bit_sequence[(node_x_pos - first_node_x_pos) + j] =
true;
634 output_next_rank_elements[counter++] = rank_elements[(node_x_pos - first_node_x_pos) + j] - half_size;
638 output_next_length_seq[(node_id - first_node_id) * 2 + 1] = right_counter;
641 throw std::runtime_error(
"Error in rebuild_h_bit_sequence, bit_size > 1");
647 node_x_pos += bit_size;
649 this->bits_seq[h].set_bits(first_node_x_pos, tmp_bit_sequence);
653 void build(
const std::vector<uint64_t> &rank_elements,
int message_paragraph = stool::Message::NO_MESSAGE)
662 uint64_t fsize = _get_upper_size_of_root(height);
663 if (rank_elements.size() < fsize)
673 if(message_paragraph != stool::Message::NO_MESSAGE){
674 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;
676 std::chrono::system_clock::time_point st1, st2;
677 st1 = std::chrono::system_clock::now();
680 this->bits_seq.resize(height);
681 this->length_seq.resize(height);
682 for(uint64_t h = 0; h < height; h++){
683 this->bits_seq[h].clear();
684 this->length_seq[h].clear();
688 this->length_seq[0].push_back(rank_elements.size());
689 std::vector<uint64_t> tmp_rank_elements = rank_elements;
691 for(uint64_t h = 0; h < height; h++){
693 if(message_paragraph != stool::Message::NO_MESSAGE){
694 std::cout << stool::Message::get_paragraph_string(message_paragraph+1) <<
"Building the " << h <<
"-th bit sequence in the wavelet tree... " << std::endl;
696 std::vector<uint64_t> next_rank_elements;
697 std::vector<uint64_t> next_length_seq;
699 this->build_h_bit_sequence(h, tmp_rank_elements, next_rank_elements, next_length_seq);
702 tmp_rank_elements.swap(next_rank_elements);
704 this->length_seq[h+1].push_many(next_length_seq);
709 assert(this->verify());
711 st2 = std::chrono::system_clock::now();
712 uint64_t sec_time = std::chrono::duration_cast<std::chrono::seconds>(st2 - st1).count();
714 if(message_paragraph != stool::Message::NO_MESSAGE){
715 std::cout << stool::Message::get_paragraph_string(message_paragraph) <<
"[DONE] Elapsed Time: " << sec_time <<
" sec" << std::endl;
719 uint64_t get_bit_count_in_node(uint64_t h, uint64_t h_node_id)
const {
720 assert(h < this->length_seq.size());
721 assert(h_node_id < this->length_seq[h].size());
722 return this->length_seq[h].at(h_node_id);
726 bool is_unbalanced_node(uint8_t h, uint64_t h_node_id)
const
728 if (h + 1 < this->height())
730 uint64_t left_node_id = 2 * h_node_id;
731 uint64_t right_node_id = 2 * h_node_id + 1;
732 uint64_t left_tree_size = this->get_bit_count_in_node(h + 1, left_node_id);
733 uint64_t right_tree_size = this->get_bit_count_in_node(h + 1, right_node_id);
734 bool unbalance_flag_LR = left_tree_size > (right_tree_size * 2) || right_tree_size > (left_tree_size * 2);
735 uint64_t child_upper_size = this->get_upper_size_of_internal_node(h + 1);
736 bool full_flag_L = left_tree_size >= child_upper_size;
737 bool full_flag_R = right_tree_size >= child_upper_size;
738 return unbalance_flag_LR || full_flag_L || full_flag_R;
742 return this->length_seq[h].at(h_node_id) >= 2;
761 void rebuild_internal_node(uint8_t h, uint64_t h_node_id)
764 std::vector<uint64_t> rank_elements = this->to_local_rank_elements_in_y_order(h, h_node_id);
767 uint64_t height = this->height();
768 uint64_t current_node_id = h_node_id;
769 uint64_t current_node_count = 1;
770 for(uint64_t q = h; q < height; q++){
771 std::vector<uint64_t> next_rank_elements;
772 std::vector<uint64_t> next_length_seq;
774 this->rebuild_h_bit_sequence(q, current_node_id, current_node_count, rank_elements, next_rank_elements, next_length_seq);
776 rank_elements.swap(next_rank_elements);
778 current_node_count *= 2;
779 current_node_id *= 2;
782 this->length_seq[q+1].set_values(current_node_id, next_length_seq);
860 void swap(DynamicWaveletTreeForRangeSearch &item)
862 this->length_seq.swap(item.length_seq);
863 this->bits_seq.swap(item.bits_seq);
906 int64_t height()
const
908 return this->bits_seq.size();
910 uint64_t size()
const
912 if (this->height() > 0)
914 return this->bits_seq[0].size();
922 uint64_t access_x_rank(uint64_t y_rank)
const
924 assert(y_rank < this->size());
925 uint64_t x_rank = this->compute_local_x_rank(0, 0, y_rank);
928 uint64_t find_leaf_index(uint64_t x_rank)
const
931 if (x_rank >= this->size())
933 throw std::runtime_error(
"ERROR in find_leaf_index: x_rank is out of range");
936 uint64_t current_x_rank = x_rank;
937 uint64_t current_node_id = 0;
938 uint64_t height = this->height();
940 for (uint64_t h = 0; h + 1 < height; h++)
942 uint64_t left_tree_size = this->get_bit_count_in_node(h+1, 2 * current_node_id);
944 if (current_x_rank < left_tree_size)
946 current_node_id = 2 * current_node_id;
950 current_x_rank = current_x_rank - left_tree_size;
951 current_node_id = 2 * current_node_id + 1;
954 return current_node_id;
957 uint64_t access_y_rank(uint64_t x_rank)
const
959 uint64_t leaf_index = this->find_leaf_index(x_rank);
960 uint64_t current_y_rank = 0;
961 uint64_t prev_node_id = leaf_index;
962 int64_t height = this->height();
963 for (int64_t h = height - 2; h >= 0; h--)
965 uint64_t next_node_id = prev_node_id / 2;
966 uint64_t next_x_pos = this->get_node_x_pos_in_bit_sequence(h, next_node_id);
968 if (prev_node_id % 2 == 0)
970 uint64_t count_zero_offset = this->bits_seq[h].rank0(next_x_pos);
971 uint64_t next_y_rank = this->bits_seq[h].select0(current_y_rank + count_zero_offset) - next_x_pos;
972 current_y_rank = next_y_rank;
973 prev_node_id = next_node_id;
979 uint64_t count_one_offset = this->bits_seq[h].rank1(next_x_pos);
980 int64_t select_result = this->bits_seq[h].select1(current_y_rank + count_one_offset);
981 assert(select_result >= 0);
982 uint64_t next_y_rank = select_result - next_x_pos;
986 current_y_rank = next_y_rank;
987 prev_node_id = next_node_id;
992 return current_y_rank;
994 std::vector<bool> get_bit_sequence(uint64_t h, uint64_t node_id)
const{
995 uint64_t x_pos = this->get_node_x_pos_in_bit_sequence(h, node_id);
996 uint64_t node_size = this->get_bit_count_in_node(h, node_id);
998 r.resize(node_size,
false);
999 for(uint64_t i = 0; i < node_size; i++){
1000 r[i] = this->bits_seq[h].at(x_pos + i);
1008 for (uint64_t h = 0; h < this->bits_seq.size(); h++)
1010 uint64_t node_count = 1 << h;
1012 if(h + 1 < this->bits_seq.size()){
1013 for (uint64_t i = 0; i < node_count; i++)
1015 std::vector<bool> bit_sequence = this->get_bit_sequence(h, i);
1016 uint64_t countL = 0;
1017 uint64_t countR = 0;
1018 for(uint64_t j = 0; j < bit_sequence.size(); j++){
1019 if(bit_sequence[j]){
1026 uint64_t left_tree_size = this->get_bit_count_in_node(h+1, 2 * i);
1027 uint64_t right_tree_size = this->get_bit_count_in_node(h+1, 2 * i + 1);
1029 if(countL != left_tree_size){
1031 throw std::runtime_error(
"Error: verify, countL != left_tree_size");
1034 if(countR != right_tree_size){
1036 throw std::runtime_error(
"Error: verify, countR != right_tree_size");
1041 for (uint64_t i = 0; i < node_count; i++)
1043 uint64_t bit_size = this->get_bit_count_in_node(h, i);
1046 throw std::runtime_error(
"Error: verify function, bit_size > 1");
1056 std::vector<uint64_t> to_local_rank_elements_in_y_order(uint64_t h, uint64_t node_id)
const
1058 uint64_t height = this->height();
1059 uint64_t node_size = this->get_bit_count_in_node(h, node_id);
1060 std::vector<uint64_t> r;
1061 r.resize(node_size, UINT64_MAX);
1062 uint64_t x_pos = this->get_node_x_pos_in_bit_sequence(h, node_id);
1066 uint64_t counterL = 0;
1067 uint64_t counterR = 0;
1068 uint64_t leaf_id_L = 2 * node_id;
1069 uint64_t leaf_id_R = 2 * node_id + 1;
1070 uint64_t left_tree_size = this->get_bit_count_in_node(h + 1, leaf_id_L);
1072 std::vector<uint64_t> left_elements = this->to_local_rank_elements_in_y_order(h + 1, leaf_id_L);
1073 std::vector<uint64_t> right_elements = this->to_local_rank_elements_in_y_order(h + 1, leaf_id_R);
1075 assert(left_elements.size() + right_elements.size() == node_size);
1080 for (uint64_t i = 0; i < node_size; i++)
1082 bool b = this->bits_seq[h].at(x_pos + i);
1085 assert(counterR < right_elements.size());
1087 r[i] = right_elements[counterR++] + left_tree_size;
1091 assert(counterL < left_elements.size());
1092 r[i] = left_elements[counterL++];
1100 }
else if(node_size > 1){
1101 for (uint64_t i = 0; i < node_size; i++)
1103 r[i] = this->bits_seq[h].at(x_pos + i);
1112 std::vector<uint64_t> to_rank_elements_in_y_order()
const
1115 if (this->height() > 0)
1117 return this->to_local_rank_elements_in_y_order(0, 0);
1121 std::vector<uint64_t> r;
1125 std::vector<uint64_t> to_rank_elements_in_x_order()
const
1127 std::vector<uint64_t> r;
1128 r.resize(this->size(), UINT64_MAX);
1129 uint64_t size = this->size();
1130 for(uint64_t i = 0; i < size; i++){
1131 r[i] = this->access_y_rank(i);
1151 uint64_t compute_local_x_rank(uint64_t node_y, uint64_t node_id, uint64_t local_y_rank)
const
1153 assert(local_y_rank < this->length_seq[node_y].at(node_id));
1155 uint64_t x_rank = 0;
1156 uint64_t h_node_id = node_id;
1157 int64_t height = this->height();
1158 for (int64_t h = node_y; h + 1 < height; h++)
1160 uint64_t node_x_pos = this->get_node_x_pos_in_bit_sequence(h, h_node_id);
1162 assert(node_x_pos + local_y_rank < this->bits_seq[h].size());
1164 bool b = this->bits_seq[h].at(node_x_pos + local_y_rank);
1165 uint64_t next_node_id = (2 * h_node_id) + (uint64_t)b;
1168 uint64_t left_tree_size = this->get_bit_count_in_node(h+1, 2 * h_node_id);
1169 x_rank += left_tree_size;
1170 local_y_rank -= this->rank0_in_bit_sequence_of_node(h, h_node_id, node_x_pos, local_y_rank);
1174 local_y_rank -= this->rank1_in_bit_sequence_of_node(h, h_node_id, node_x_pos, local_y_rank);
1176 h_node_id = next_node_id;
1182 template <
typename APPENDABLE_VECTOR>
1183 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
1186 for (uint64_t i = hy_min; i <= hy_max; i++)
1188 uint64_t x = this->compute_local_x_rank(h, node_id, i) + x_rank_gap;
1191 return hy_max - hy_min + 1;
1211 template <
typename APPENDABLE_VECTOR>
1212 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
1215 uint64_t found_elements_count = 0;
1216 int64_t node_size = this->get_bit_count_in_node(h, node_id);
1217 if (x_min <= (int64_t)node_x_pos && (int64_t)(node_x_pos + node_size - 1) <= x_max)
1219 uint64_t limitR = std::min((int64_t)hy_max, node_size-1);
1221 if(hy_min <= limitR){
1222 uint64_t _tmp = local_range_report_on_internal_node(h, node_id, node_x_pos, hy_min, limitR, out);
1223 found_elements_count += _tmp;
1227 else if((int64_t)(h+1) < this->height())
1229 uint64_t node_x_pos_L = node_x_pos;
1230 uint64_t node_x_pos_R = node_x_pos + this->get_bit_count_in_node(h+1, 2 * node_id);
1238 int64_t hy_max_0 = rank0_in_bit_sequence_of_node(h, node_id, node_x_pos_L, hy_max) - 1;
1239 int64_t hy_max_1 = rank1_in_bit_sequence_of_node(h, node_id, node_x_pos_L, hy_max) - 1;
1245 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;
1246 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;
1249 uint64_t next_node_id_L = 2 * node_id;
1250 uint64_t next_node_id_R = next_node_id_L + 1;
1252 if (x_min < (int64_t)node_x_pos_R && hy_min_0 <= hy_max_0)
1254 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);
1257 if (x_max >= (int64_t)node_x_pos_R && hy_min_1 <= hy_max_1)
1259 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);
1263 return found_elements_count;
1267 template <
typename APPENDABLE_VECTOR>
1268 uint64_t range_report(uint64_t x_min, uint64_t x_max, uint64_t y_min, uint64_t y_max, APPENDABLE_VECTOR &out)
const
1270 uint64_t found_elements_count = 0;
1271 if (this->height() > 0)
1273 found_elements_count = this->recursive_range_report_on_internal_nodes(0, 0, 0, x_min, x_max, y_min, y_max, out);
1275 return found_elements_count;
1278 XRankIterator x_rank_begin()
const
1280 if (this->size() == 0)
1282 return XRankIterator(
this, this->size(), this->size());
1286 return XRankIterator(
this, 0, this->access_y_rank(0));
1289 XRankIterator x_rank_end()
const
1291 return XRankIterator(
this, this->size(), this->size());
1293 YRankIterator y_rank_begin()
const
1295 if (this->size() == 0)
1297 return YRankIterator(
this, this->size(), this->size());
1301 return YRankIterator(
this, this->access_x_rank(0), 0);
1304 YRankIterator y_rank_end()
const
1306 return YRankIterator(
this, this->size(), this->size());
1309 void print_tree()
const
1311 std::vector<std::string> r;
1312 for (uint64_t h = 0; h < this->bits_seq.size(); h++)
1314 std::vector<uint64_t> tmp_len_veq;
1315 uint64_t counter = 0;
1316 tmp_len_veq.push_back(0);
1317 for(uint64_t i = 0; i < this->length_seq[h].size(); i++){
1318 counter += this->length_seq[h].at(i);
1319 tmp_len_veq.push_back(counter);
1324 for(uint64_t i = 0; i < this->bits_seq[h].size(); i++){
1325 while(tmp_len_veq[tmp_p] <= i){
1330 bool b = this->bits_seq[h].at(i);
1332 s.append(b ?
"1" :
"0");
1363 std::cout <<
"===== TREE =====" << std::endl;
1365 for (uint64_t i = 0; i < r.size(); i++)
1367 std::cout << r[i] << std::endl;
1369 std::cout <<
"===== [END] =====" << std::endl;
1372 static void store_to_file(DynamicWaveletTreeForRangeSearch &item, std::ofstream &os)
1374 uint64_t height = item.height();
1375 os.write(
reinterpret_cast<const char *
>(&height),
sizeof(uint64_t));
1377 for (uint64_t h = 0; h < height; h++)
1383 static void store_to_bytes(DynamicWaveletTreeForRangeSearch &item, std::vector<uint8_t> &output, uint64_t &pos)
1385 uint64_t bytes = item.size_in_bytes();
1386 if(output.size() < pos + bytes){
1387 output.resize(pos + bytes);
1389 uint64_t height = item.height();
1391 std::memcpy(output.data() + pos, &height,
sizeof(height));
1392 pos +=
sizeof(height);
1394 for (int64_t h = 0; h < (int64_t)height; h++)
1400 uint64_t size_in_bytes(
bool only_extra_bytes =
false)
const
1403 sum +=
sizeof(uint64_t);
1404 for(int64_t h = 0; h < (int64_t)this->height(); h++){
1405 sum += this->bits_seq[h].size_in_bytes(only_extra_bytes);
1406 sum += this->length_seq[h].size_in_bytes(only_extra_bytes);
1411 static DynamicWaveletTreeForRangeSearch load_from_file(std::ifstream &ifs)
1413 DynamicWaveletTreeForRangeSearch r;
1414 uint64_t _height = 0;
1415 ifs.read(
reinterpret_cast<char *
>(&_height),
sizeof(uint64_t));
1417 r.bits_seq.resize(_height);
1418 r.length_seq.resize(_height);
1419 for (uint64_t h = 0; h < _height; h++)
1422 r.bits_seq[h].swap(bits);
1425 r.length_seq[h].swap(length_seq);
1430 static DynamicWaveletTreeForRangeSearch load_from_bytes(
const std::vector<uint8_t> &data, uint64_t &pos)
1432 DynamicWaveletTreeForRangeSearch r;
1434 std::memcpy(&_height, data.data() + pos,
sizeof(_height));
1435 pos +=
sizeof(_height);
1437 r.bits_seq.resize(_height);
1438 r.length_seq.resize(_height);
1439 for (uint64_t h = 0; h < _height; h++)
1442 r.bits_seq[h].swap(bits);
1445 r.length_seq[h].swap(length_seq);
1451 std::vector<std::string> get_memory_usage_info(
int message_paragraph = stool::Message::SHOW_MESSAGE)
const
1454 std::vector<std::string> r;
1455 uint64_t size_in_bytes = this->size_in_bytes();
1456 uint64_t element_count = this->size();
1458 double bits_per_element = element_count > 0 ? ((double)size_in_bytes / (
double)element_count) : 0;
1460 r.push_back(stool::Message::get_paragraph_string(message_paragraph) +
"=DynamicWaveletTreeForRangeSearch: " + std::to_string(this->size_in_bytes())
1461 +
" bytes, " + std::to_string(element_count) +
" elements, " + std::to_string(bits_per_element) +
" bytes per element =");
1463 for(uint64_t h = 0; h < this->bits_seq.size(); h++){
1464 uint64_t _sub_size = 0;
1465 _sub_size += this->bits_seq[h].size_in_bytes();
1466 _sub_size += this->length_seq[h].size_in_bytes();
1468 uint64_t _bits_per_element = element_count > 0 ? ((double)_sub_size / (
double)element_count) : 0;
1469 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)");
1471 r.push_back(stool::Message::get_paragraph_string(message_paragraph) +
"==");