2#include "../dynamic_bit_sequence.hpp"
3#include "..//dynamic_prefix_sum.hpp"
23 std::vector<BIT_SEQUENCE> bits_seq;
24 std::vector<PREFIX_SUM> length_seq;
39 using iterator_category = std::random_access_iterator_tag;
40 using value_type = uint64_t;
41 using difference_type = std::ptrdiff_t;
59 uint64_t
operator*() const noexcept {
return this->x_rank; }
65 if (this->y_rank < this->container->
size())
71 this->x_rank = this->container->
size();
92 this->x_rank = this->container->
size();
111 if (this->y_rank < this->container->
size())
117 this->x_rank = this->container->
size();
128 if (this->y_rank < this->container->
size())
134 this->x_rank = this->container->
size();
169 using iterator_category = std::random_access_iterator_tag;
170 using value_type = uint64_t;
171 using difference_type = std::ptrdiff_t;
189 uint64_t
operator*() const noexcept {
return this->y_rank; }
195 if (this->x_rank < this->container->
size())
201 this->y_rank = this->container->
size();
215 if (this->x_rank > 0)
222 this->y_rank = this->container->
size();
241 if (this->x_rank < this->container->
size())
247 this->y_rank = this->container->
size();
258 if (this->x_rank < this->container->
size())
264 this->y_rank = this->container->
size();
305 uint64_t get_node_x_pos_in_bit_sequence(int64_t h, uint64_t h_node_id)
const
313 return this->length_seq[h].psum(h_node_id - 1);
326 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
328 assert(i <= this->length_seq[h].at(h_node_id));
329 assert(node_x_pos_in_bit_sequence == this->get_node_x_pos_in_bit_sequence(h, h_node_id));
330 return this->bits_seq[h].one_based_rank0(node_x_pos_in_bit_sequence + i + 1) - this->bits_seq[h].one_based_rank0(node_x_pos_in_bit_sequence);
342 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
344 assert(i <= this->length_seq[h].at(h_node_id));
345 assert(node_x_pos_in_bit_sequence == this->get_node_x_pos_in_bit_sequence(h, h_node_id));
346 return this->bits_seq[h].one_based_rank1(node_x_pos_in_bit_sequence + i + 1) - this->bits_seq[h].one_based_rank1(node_x_pos_in_bit_sequence);
358 void recursive_insert(int64_t h, uint64_t h_node_id, uint64_t x_rank, uint64_t y_rank, std::vector<uint64_t> &output_path)
360 output_path[h] = h_node_id;
361 uint64_t node_size = this->length_seq[h].at(h_node_id);
362 uint64_t node_x_pos_in_bit_sequence = this->get_node_x_pos_in_bit_sequence(h, h_node_id);
364 if (h + 1 < this->
height())
366 uint64_t left_node_id = 2 * h_node_id;
367 uint64_t right_node_id = 2 * h_node_id + 1;
368 uint64_t left_tree_size = this->length_seq[h + 1].at(left_node_id);
370 if (x_rank <= left_tree_size)
372 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;
373 this->recursive_insert(h + 1, left_node_id, x_rank, new_y_rank, output_path);
374 this->bits_seq[h].insert(node_x_pos_in_bit_sequence + y_rank,
false);
375 this->length_seq[h].increment(h_node_id, 1);
379 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;
380 uint64_t new_x_rank = x_rank - left_tree_size;
382 this->recursive_insert(h + 1, right_node_id, new_x_rank, new_y_rank, output_path);
383 this->bits_seq[h].insert(node_x_pos_in_bit_sequence + y_rank,
true);
384 this->length_seq[h].increment(h_node_id, 1);
389 assert(this->get_bit_count_in_node(h, h_node_id) <= 1);
392 this->bits_seq[h].insert(node_x_pos_in_bit_sequence + y_rank,
false);
393 this->length_seq[h].increment(h_node_id, 1);
395 else if (node_size == 1)
400 this->bits_seq[h].set_bit(node_x_pos_in_bit_sequence,
true);
401 this->bits_seq[h].insert(node_x_pos_in_bit_sequence + y_rank,
false);
406 this->bits_seq[h].insert(node_x_pos_in_bit_sequence + y_rank,
true);
408 this->length_seq[h].increment(h_node_id, 1);
412 throw std::runtime_error(
"node_size > 1");
421 static uint64_t _get_upper_size_of_root(uint64_t H)
423 return _get_upper_size_of_internal_node(0, H);
432 static uint64_t _get_upper_size_of_internal_node(uint64_t h, uint64_t H)
436 for (uint64_t p = h + 1; p < H; p++)
456 uint64_t get_upper_size_of_internal_node(uint64_t h)
const
458 return _get_upper_size_of_internal_node(h, this->
height());
466 uint64_t get_lower_size_of_internal_node(uint64_t h)
const
468 uint64_t fsize = _get_upper_size_of_internal_node(h, this->
height());
478 bool is_unbalanced_node(uint8_t h, uint64_t h_node_id)
const
480 if (h + 1 < this->
height())
482 uint64_t left_node_id = 2 * h_node_id;
483 uint64_t right_node_id = 2 * h_node_id + 1;
484 uint64_t left_tree_size = this->get_bit_count_in_node(h + 1, left_node_id);
485 uint64_t right_tree_size = this->get_bit_count_in_node(h + 1, right_node_id);
486 bool unbalance_flag_LR = left_tree_size > (right_tree_size * 2) || right_tree_size > (left_tree_size * 2);
487 uint64_t child_upper_size = this->get_upper_size_of_internal_node(h + 1);
488 bool full_flag_L = left_tree_size >= child_upper_size;
489 bool full_flag_R = right_tree_size >= child_upper_size;
490 return unbalance_flag_LR || full_flag_L || full_flag_R;
494 return this->length_seq[h].at(h_node_id) >= 2;
511 return this->bits_seq.size();
523 return this->bits_seq[0].size();
538 uint64_t get_bit_count_in_node(uint64_t h, uint64_t h_node_id)
const
540 assert(h < this->length_seq.size());
541 assert(h_node_id < this->length_seq[h].
size());
542 return this->length_seq[h].at(h_node_id);
563 template <
typename APPENDABLE_VECTOR>
564 uint64_t
range_report(uint64_t x_min, uint64_t x_max, uint64_t y_min, uint64_t y_max, APPENDABLE_VECTOR &out)
const
566 uint64_t found_elements_count = 0;
569 found_elements_count = this->recursive_range_report_on_internal_nodes(0, 0, 0, x_min, x_max, y_min, y_max, out);
571 return found_elements_count;
581 assert(y_rank < this->
size());
582 uint64_t x_rank = this->compute_local_x_rank(0, 0, y_rank);
596 if (x_rank >= this->
size())
598 throw std::runtime_error(
"ERROR in find_leaf_index: x_rank is out of range");
601 uint64_t current_x_rank = x_rank;
602 uint64_t current_node_id = 0;
605 for (uint64_t h = 0; h + 1 <
height; h++)
607 uint64_t left_tree_size = this->get_bit_count_in_node(h + 1, 2 * current_node_id);
609 if (current_x_rank < left_tree_size)
611 current_node_id = 2 * current_node_id;
615 current_x_rank = current_x_rank - left_tree_size;
616 current_node_id = 2 * current_node_id + 1;
619 return current_node_id;
631 uint64_t current_y_rank = 0;
632 uint64_t prev_node_id = leaf_index;
634 for (int64_t h =
height - 2; h >= 0; h--)
636 uint64_t next_node_id = prev_node_id / 2;
637 uint64_t next_x_pos = this->get_node_x_pos_in_bit_sequence(h, next_node_id);
639 if (prev_node_id % 2 == 0)
641 uint64_t count_zero_offset = this->bits_seq[h].one_based_rank0(next_x_pos);
642 uint64_t next_y_rank = this->bits_seq[h].select0(current_y_rank + count_zero_offset) - next_x_pos;
643 current_y_rank = next_y_rank;
644 prev_node_id = next_node_id;
649 uint64_t count_one_offset = this->bits_seq[h].one_based_rank1(next_x_pos);
650 int64_t select_result = this->bits_seq[h].select1(current_y_rank + count_one_offset);
651 assert(select_result >= 0);
652 uint64_t next_y_rank = select_result - next_x_pos;
654 current_y_rank = next_y_rank;
655 prev_node_id = next_node_id;
658 return current_y_rank;
670 uint64_t compute_local_x_rank(uint64_t node_y, uint64_t node_id, uint64_t local_y_rank)
const
672 assert(local_y_rank < this->length_seq[node_y].at(node_id));
675 uint64_t h_node_id = node_id;
677 for (int64_t h = node_y; h + 1 <
height; h++)
679 uint64_t node_x_pos = this->get_node_x_pos_in_bit_sequence(h, h_node_id);
681 assert(node_x_pos + local_y_rank < this->bits_seq[h].
size());
683 bool b = this->bits_seq[h].at(node_x_pos + local_y_rank);
684 uint64_t next_node_id = (2 * h_node_id) + (uint64_t)b;
687 uint64_t left_tree_size = this->get_bit_count_in_node(h + 1, 2 * h_node_id);
688 x_rank += left_tree_size;
689 local_y_rank -= this->rank0_in_bit_sequence_of_node(h, h_node_id, node_x_pos, local_y_rank);
693 local_y_rank -= this->rank1_in_bit_sequence_of_node(h, h_node_id, node_x_pos, local_y_rank);
695 h_node_id = next_node_id;
713 template <
typename APPENDABLE_VECTOR>
714 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
717 for (uint64_t i = hy_min; i <= hy_max; i++)
719 uint64_t x = this->compute_local_x_rank(h, node_id, i) + x_rank_gap;
722 return hy_max - hy_min + 1;
737 template <
typename APPENDABLE_VECTOR>
738 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
741 uint64_t found_elements_count = 0;
742 int64_t node_size = this->get_bit_count_in_node(h, node_id);
743 if (x_min <= (int64_t)node_x_pos && (int64_t)(node_x_pos + node_size - 1) <= x_max)
745 uint64_t limitR = std::min((int64_t)hy_max, node_size - 1);
747 if (hy_min <= limitR)
749 uint64_t _tmp = local_range_report_on_internal_node(h, node_id, node_x_pos, hy_min, limitR, out);
750 found_elements_count += _tmp;
753 else if ((int64_t)(h + 1) < this->
height())
755 uint64_t node_x_pos_L = node_x_pos;
756 uint64_t node_x_pos_R = node_x_pos + this->get_bit_count_in_node(h + 1, 2 * node_id);
758 int64_t hy_max_0 = rank0_in_bit_sequence_of_node(h, node_id, node_x_pos_L, hy_max) - 1;
759 int64_t hy_max_1 = rank1_in_bit_sequence_of_node(h, node_id, node_x_pos_L, hy_max) - 1;
761 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;
762 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;
764 uint64_t next_node_id_L = 2 * node_id;
765 uint64_t next_node_id_R = next_node_id_L + 1;
767 if (x_min < (int64_t)node_x_pos_R && hy_min_0 <= hy_max_0)
769 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);
772 if (x_max >= (int64_t)node_x_pos_R && hy_min_1 <= hy_max_1)
774 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);
777 return found_elements_count;
795 uint64_t x_pos = this->get_node_x_pos_in_bit_sequence(h, node_id);
796 uint64_t node_size = this->get_bit_count_in_node(h, node_id);
798 r.resize(node_size,
false);
799 for (uint64_t i = 0; i < node_size; i++)
801 r[i] = this->bits_seq[h].at(x_pos + i);
815 uint64_t node_size = this->get_bit_count_in_node(h, node_id);
816 std::vector<uint64_t> r;
817 r.resize(node_size, UINT64_MAX);
818 uint64_t x_pos = this->get_node_x_pos_in_bit_sequence(h, node_id);
822 uint64_t counterL = 0;
823 uint64_t counterR = 0;
824 uint64_t leaf_id_L = 2 * node_id;
825 uint64_t leaf_id_R = 2 * node_id + 1;
826 uint64_t left_tree_size = this->get_bit_count_in_node(h + 1, leaf_id_L);
831 assert(left_elements.size() + right_elements.size() == node_size);
833 for (uint64_t i = 0; i < node_size; i++)
835 bool b = this->bits_seq[h].at(x_pos + i);
838 assert(counterR < right_elements.size());
840 r[i] = right_elements[counterR++] + left_tree_size;
844 assert(counterL < left_elements.size());
845 r[i] = left_elements[counterL++];
855 else if (node_size > 1)
857 for (uint64_t i = 0; i < node_size; i++)
859 r[i] = this->bits_seq[h].at(x_pos + i);
880 std::vector<uint64_t> r;
891 std::vector<uint64_t> r;
892 r.resize(this->
size(), UINT64_MAX);
894 for (uint64_t i = 0; i <
size; i++)
902 std::string to_string()
const{
905 for(uint64_t i = 0; i < this->
size(); i++){
907 if(i < this->
size() - 1){
929 this->length_seq.swap(item.length_seq);
930 this->bits_seq.swap(item.bits_seq);
935 for (uint64_t i = 0; i < this->bits_seq.size(); i++)
937 this->bits_seq[i].clear();
938 this->length_seq[i].clear();
940 this->bits_seq.clear();
941 this->length_seq.clear();
944 void rebuild(
const std::vector<uint64_t> &rank_elements,
int message_paragraph = stool::Message::NO_MESSAGE)
959 void insert(uint64_t x_rank, uint64_t y_rank)
962 if (this->
size() > 0)
964 std::vector<uint64_t> output_path(this->
height(), UINT64_MAX);
965 this->recursive_insert(0, 0, x_rank, y_rank, output_path);
966 uint64_t upper_size = this->get_upper_size_of_internal_node(0);
967 if (this->
size() >= upper_size)
971 this->rebuild(rank_elements);
976 for (uint64_t h = 0; h <
height; h++)
978 uint64_t h_node_id = output_path[h];
979 if (this->is_unbalanced_node(h, h_node_id))
981 this->rebuild_internal_node(h, h_node_id);
991 std::vector<uint64_t> rank_elements;
992 rank_elements.push_back(0);
993 this->rebuild(rank_elements);
1011 throw std::runtime_error(
"Error: DynamicWaveletMatrixForRangeSearch::remove(y_rank)");
1015 uint64_t h_y_rank = y_rank;
1016 uint64_t h_node_id = 0;
1018 for (int64_t h = 0; h <
height; h++)
1020 uint64_t node_x_pos = this->get_node_x_pos_in_bit_sequence(h, h_node_id);
1021 bool b = this->bits_seq[h].at(node_x_pos + h_y_rank);
1022 uint64_t next_node_id = (2 * h_node_id) + (uint64_t)b;
1025 uint64_t rmv_y_rank = this->rank0_in_bit_sequence_of_node(h, h_node_id, node_x_pos, h_y_rank);
1026 this->bits_seq[h].remove(node_x_pos + h_y_rank);
1027 this->length_seq[h].decrement(h_node_id, 1);
1028 h_y_rank -= rmv_y_rank;
1032 uint64_t rmv_y_rank = this->rank1_in_bit_sequence_of_node(h, h_node_id, node_x_pos, h_y_rank);
1033 this->bits_seq[h].remove(node_x_pos + h_y_rank);
1034 this->length_seq[h].decrement(h_node_id, 1);
1035 h_y_rank -= rmv_y_rank;
1037 h_node_id = next_node_id;
1040 uint64_t upper_size = this->get_upper_size_of_internal_node(0);
1042 if (size < upper_size / 2)
1045 this->rebuild(rank_elements);
1061 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)
1064 uint64_t h_node_count = 1ULL << h;
1065 uint64_t counter = 0;
1066 uint64_t node_x_pos = 0;
1067 std::vector<bool> tmp_bit_sequence(rank_elements.size(),
false);
1069 if ((int64_t)(h + 1) < this->
height())
1071 output_next_rank_elements.resize(rank_elements.size(), UINT64_MAX);
1072 output_next_length_seq.resize(h_node_count * 2, UINT64_MAX);
1075 for (uint64_t i = 0; i < h_node_count; i++)
1077 uint64_t bit_size = this->get_bit_count_in_node(h, i);
1078 uint64_t half_size = bit_size / 2;
1081 if ((int64_t)(h + 1) < this->
height())
1083 uint64_t left_counter = 0;
1084 for (uint64_t j = 0; j < bit_size; j++)
1086 if (rank_elements[node_x_pos + j] < half_size)
1089 output_next_rank_elements[counter++] = rank_elements[node_x_pos + j];
1093 output_next_length_seq[i * 2] = left_counter;
1098 uint64_t right_counter = 0;
1100 if ((int64_t)(h + 1) < this->
height())
1102 for (uint64_t j = 0; j < bit_size; j++)
1104 if (rank_elements[node_x_pos + j] >= half_size)
1107 tmp_bit_sequence[node_x_pos + j] =
true;
1108 output_next_rank_elements[counter++] = rank_elements[node_x_pos + j] - half_size;
1112 output_next_length_seq[(i * 2) + 1] = right_counter;
1118 throw std::runtime_error(
"Error in build_h_bit_sequence, bit_size > 1");
1123 node_x_pos += bit_size;
1125 this->bits_seq[h].clear();
1126 this->bits_seq[h].push_many(tmp_bit_sequence);
1139 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)
1141 assert(first_node_id + local_h_node_count - 1 < this->length_seq[h].
size());
1143 uint64_t counter = 0;
1144 uint64_t node_x_pos = this->get_node_x_pos_in_bit_sequence(h, first_node_id);
1145 const uint64_t first_node_x_pos = node_x_pos;
1146 std::vector<bool> tmp_bit_sequence(rank_elements.size(),
false);
1148 if ((int64_t)(h + 1) < this->
height())
1150 output_next_rank_elements.resize(rank_elements.size(), UINT64_MAX);
1151 output_next_length_seq.resize(local_h_node_count * 2, UINT64_MAX);
1154 for (uint64_t node_id = first_node_id; node_id <= first_node_id + local_h_node_count - 1; node_id++)
1156 uint64_t bit_size = this->get_bit_count_in_node(h, node_id);
1157 uint64_t half_size = bit_size / 2;
1160 if ((int64_t)(h + 1) < this->
height())
1162 uint64_t left_counter = 0;
1163 for (uint64_t j = 0; j < bit_size; j++)
1165 if (rank_elements[(node_x_pos - first_node_x_pos) + j] < half_size)
1167 output_next_rank_elements[counter++] = rank_elements[(node_x_pos - first_node_x_pos) + j];
1171 output_next_length_seq[(node_id - first_node_id) * 2] = left_counter;
1176 uint64_t right_counter = 0;
1178 if ((int64_t)(h + 1) < this->
height())
1180 for (uint64_t j = 0; j < bit_size; j++)
1182 if (rank_elements[(node_x_pos - first_node_x_pos) + j] >= half_size)
1185 tmp_bit_sequence[(node_x_pos - first_node_x_pos) + j] =
true;
1186 output_next_rank_elements[counter++] = rank_elements[(node_x_pos - first_node_x_pos) + j] - half_size;
1190 output_next_length_seq[(node_id - first_node_id) * 2 + 1] = right_counter;
1196 throw std::runtime_error(
"Error in rebuild_h_bit_sequence, bit_size > 1");
1201 node_x_pos += bit_size;
1203 this->bits_seq[h].set_bits(first_node_x_pos, tmp_bit_sequence);
1211 void rebuild_internal_node(uint8_t h, uint64_t h_node_id)
1217 uint64_t current_node_id = h_node_id;
1218 uint64_t current_node_count = 1;
1219 for (uint64_t q = h; q <
height; q++)
1221 std::vector<uint64_t> next_rank_elements;
1222 std::vector<uint64_t> next_length_seq;
1224 this->rebuild_h_bit_sequence(q, current_node_id, current_node_count, rank_elements, next_rank_elements, next_length_seq);
1226 rank_elements.swap(next_rank_elements);
1228 current_node_count *= 2;
1229 current_node_id *= 2;
1233 this->length_seq[q + 1].set_values(current_node_id, next_length_seq);
1256 for (uint64_t h = 0; h < this->bits_seq.size(); h++)
1258 uint64_t node_count = 1 << h;
1260 if (h + 1 < this->bits_seq.size())
1262 for (uint64_t i = 0; i < node_count; i++)
1265 uint64_t countL = 0;
1266 uint64_t countR = 0;
1267 for (uint64_t j = 0; j < bit_sequence.size(); j++)
1269 if (bit_sequence[j])
1279 uint64_t left_tree_size = this->get_bit_count_in_node(h + 1, 2 * i);
1280 uint64_t right_tree_size = this->get_bit_count_in_node(h + 1, 2 * i + 1);
1282 if (countL != left_tree_size)
1285 throw std::runtime_error(
"Error: verify, countL != left_tree_size");
1288 if (countR != right_tree_size)
1291 throw std::runtime_error(
"Error: verify, countR != right_tree_size");
1297 for (uint64_t i = 0; i < node_count; i++)
1299 uint64_t bit_size = this->get_bit_count_in_node(h, i);
1303 throw std::runtime_error(
"Error: verify function, bit_size > 1");
1313 std::vector<std::string> r;
1314 for (uint64_t h = 0; h < this->bits_seq.size(); h++)
1316 std::vector<uint64_t> tmp_len_veq;
1317 uint64_t counter = 0;
1318 tmp_len_veq.push_back(0);
1319 for (uint64_t i = 0; i < this->length_seq[h].size(); i++)
1321 counter += this->length_seq[h].at(i);
1322 tmp_len_veq.push_back(counter);
1327 for (uint64_t i = 0; i < this->bits_seq[h].size(); i++)
1329 while (tmp_len_veq[tmp_p] <= i)
1335 bool b = this->bits_seq[h].at(i);
1337 s.append(b ?
"1" :
"0");
1344 std::cout <<
"===== TREE =====" << std::endl;
1346 for (uint64_t i = 0; i < r.size(); i++)
1348 std::cout << r[i] << std::endl;
1350 std::cout <<
"===== [END] =====" << std::endl;
1362 if (this->
size() == 0)
1381 if (this->
size() == 0)
1413 std::vector<bool> y_rank_checker;
1414 y_rank_checker.resize(rank_elements.size(),
false);
1415 for(uint64_t i = 0; i < rank_elements.size(); i++){
1416 if(y_rank_checker[rank_elements[i]]){
1417 throw std::runtime_error(
"Error in build function, at least two elements have the same y-rank");
1419 y_rank_checker[rank_elements[i]] =
true;
1431 uint64_t fsize = r._get_upper_size_of_root(
height);
1432 if (rank_elements.size() < fsize)
1442 if (message_paragraph != stool::Message::NO_MESSAGE)
1444 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;
1446 std::chrono::system_clock::time_point st1, st2;
1447 st1 = std::chrono::system_clock::now();
1449 r.bits_seq.resize(
height);
1450 r.length_seq.resize(
height);
1451 for (uint64_t h = 0; h <
height; h++)
1453 r.bits_seq[h].clear();
1454 r.length_seq[h].clear();
1459 r.length_seq[0].push_back(rank_elements.size());
1460 std::vector<uint64_t> tmp_rank_elements = rank_elements;
1462 for (uint64_t h = 0; h <
height; h++)
1465 if (message_paragraph != stool::Message::NO_MESSAGE)
1467 std::cout << stool::Message::get_paragraph_string(message_paragraph + 1) <<
"Building the " << h <<
"-th bit sequence in the wavelet tree... " << std::endl;
1469 std::vector<uint64_t> next_rank_elements;
1470 std::vector<uint64_t> next_length_seq;
1472 r.build_h_bit_sequence(h, tmp_rank_elements, next_rank_elements, next_length_seq);
1474 tmp_rank_elements.swap(next_rank_elements);
1477 r.length_seq[h + 1].push_many(next_length_seq);
1484 st2 = std::chrono::system_clock::now();
1485 uint64_t sec_time = std::chrono::duration_cast<std::chrono::seconds>(st2 - st1).count();
1487 if (message_paragraph != stool::Message::NO_MESSAGE)
1489 std::cout << stool::Message::get_paragraph_string(message_paragraph) <<
"[DONE] Elapsed Time: " << sec_time <<
" sec" << std::endl;
1502 os.write(
reinterpret_cast<const char *
>(&
height),
sizeof(uint64_t));
1504 for (uint64_t h = 0; h <
height; h++)
1520 if (output.size() < pos + bytes)
1522 output.resize(pos + bytes);
1526 std::memcpy(output.data() + pos, &
height,
sizeof(
height));
1529 for (int64_t h = 0; h < (int64_t)
height; h++)
1544 sum +=
sizeof(uint64_t);
1545 for (int64_t h = 0; h < (int64_t)this->
height(); h++)
1547 sum += this->bits_seq[h].size_in_bytes(only_extra_bytes);
1548 sum += this->length_seq[h].size_in_bytes(only_extra_bytes);
1561 uint64_t _height = 0;
1562 ifs.read(
reinterpret_cast<char *
>(&_height),
sizeof(uint64_t));
1564 r.bits_seq.resize(_height);
1565 r.length_seq.resize(_height);
1566 for (uint64_t h = 0; h < _height; h++)
1569 r.bits_seq[h].swap(bits);
1572 r.length_seq[h].swap(length_seq);
1586 std::memcpy(&_height, data.data() + pos,
sizeof(_height));
1587 pos +=
sizeof(_height);
1589 r.bits_seq.resize(_height);
1590 r.length_seq.resize(_height);
1591 for (uint64_t h = 0; h < _height; h++)
1594 r.bits_seq[h].swap(bits);
1597 r.length_seq[h].swap(length_seq);
1610 std::vector<std::string> r;
1612 uint64_t element_count = this->
size();
1614 double bits_per_element = element_count > 0 ? ((double)
size_in_bytes / (
double)element_count) : 0;
1616 r.push_back(stool::Message::get_paragraph_string(message_paragraph) +
"=DynamicWaveletMatrixForRangeSearch: " + std::to_string(this->
size_in_bytes()) +
" bytes, " + std::to_string(element_count) +
" elements, " + std::to_string(bits_per_element) +
" bytes per element =");
1618 for (uint64_t h = 0; h < this->bits_seq.size(); h++)
1620 uint64_t _sub_size = 0;
1621 _sub_size += this->bits_seq[h].size_in_bytes();
1622 _sub_size += this->length_seq[h].size_in_bytes();
1624 uint64_t _bits_per_element = element_count > 0 ? ((double)_sub_size / (
double)element_count) : 0;
1625 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)");
1627 r.push_back(stool::Message::get_paragraph_string(message_paragraph) +
"==");