40 std::vector<LEAF_CONTAINER> leaf_container_vec;
41 std::vector<Node *> parent_vec;
42 std::stack<uint64_t> unused_leaf_container_indexes;
43 std::vector<Node *> unused_node_pointers;
44 std::vector<NodePointer> tmp_path;
51 BPTree *linked_tree_ =
nullptr;
52 double density_threshold = 0.5;
53 uint64_t density1 = 0;
55 bool root_is_leaf_ =
false;
57 uint64_t split_process_counter = 0;
58 uint64_t insert_operation_counter = 0;
59 uint64_t merge_process_counter = 0;
60 uint64_t remove_operation_counter = 0;
80 this->leaf_container_vec = std::move(other.leaf_container_vec);
81 this->parent_vec = std::move(other.parent_vec);
82 this->unused_leaf_container_indexes = std::move(other.unused_leaf_container_indexes);
83 this->unused_node_pointers = std::move(other.unused_node_pointers);
84 this->tmp_path = std::move(other.tmp_path);
88 this->root = other.root;
89 this->linked_tree_ = other.linked_tree_;
90 this->density_threshold = other.density_threshold;
91 this->density1 = other.density1;
92 this->height_ = other.height_;
93 this->root_is_leaf_ = other.root_is_leaf_;
95 other.leaf_container_vec.clear();
96 other.parent_vec.clear();
97 other.unused_node_pointers.clear();
98 other.tmp_path.clear();
99 while (other.unused_leaf_container_indexes.size() > 0)
101 other.unused_leaf_container_indexes.pop();
104 other.root =
nullptr;
105 other.linked_tree_ =
nullptr;
107 other.root_is_leaf_ =
false;
135 this->leaf_container_vec = std::move(other.leaf_container_vec);
136 this->parent_vec = std::move(other.parent_vec);
137 this->unused_leaf_container_indexes = std::move(other.unused_leaf_container_indexes);
138 this->unused_node_pointers = std::move(other.unused_node_pointers);
139 this->tmp_path = std::move(other.tmp_path);
143 this->root = other.root;
144 this->linked_tree_ = other.linked_tree_;
145 this->density_threshold = other.density_threshold;
146 this->density1 = other.density1;
147 this->height_ = other.height_;
148 this->root_is_leaf_ = other.root_is_leaf_;
150 other.leaf_container_vec.clear();
151 other.parent_vec.clear();
152 other.unused_node_pointers.clear();
153 other.tmp_path.clear();
154 while (other.unused_leaf_container_indexes.size() > 0)
156 other.unused_leaf_container_indexes.pop();
159 other.root =
nullptr;
160 other.linked_tree_ =
nullptr;
162 other.root_is_leaf_ =
false;
185 if (this->root_is_leaf_)
261 return this->tmp_path;
277 return this->split_process_counter;
290 if (this->root_is_leaf_)
292 return this->leaf_container_vec[(uint64_t)this->root].
size();
296 return this->root->psum_on_count_deque();
323 return LEAF_CONTAINER_MAX_SIZE;
331 return this->height_;
339 return this->leaf_container_vec.size() - this->unused_leaf_container_indexes.size();
347 return this->leaf_container_vec.size();
355 return this->
height() == 0;
364 return this->leaf_container_vec[i];
371 return this->leaf_container_vec[i];
377 uint64_t
size_in_bytes([[maybe_unused]]
bool only_dynamic_memory =
false)
const
379 uint64_t _max_degree = MAX_DEGREE;
380 uint64_t _max_count_of_values_in_leaf = LEAF_CONTAINER_MAX_SIZE;
381 uint64_t _size =
sizeof(_max_degree) +
sizeof(_max_count_of_values_in_leaf) + LEAF_CONTAINER::get_byte_size(this->leaf_container_vec);
390 return this->linked_tree_;
408 if (this->root_is_leaf_)
410 std::vector<VALUE> r;
411 this->leaf_container_vec[(uint64_t)this->root].to_values(r);
417 std::vector<VALUE> r;
418 BPFunctions::to_value_vector(*this->root, r, this->leaf_container_vec);
424 std::vector<VALUE> r;
440 VALUE
at(uint64_t i)
const
444 assert(i < this->
size());
445 std::vector<NodePointer> path;
449 assert(this->tmp_path.size() > 0);
451 uint64_t leaf = this->tmp_path[this->tmp_path.size() - 1].get_leaf_container_index();
454 auto st3 = std::chrono::system_clock::now();
456 uint64_t result = this->leaf_container_vec[leaf].at(idx);
459 auto st4 = std::chrono::system_clock::now();
460 time_count2 += std::chrono::duration_cast<std::chrono::nanoseconds>(st4 - st3).count();
467 throw std::invalid_argument(
"Error: BPTree::at()");
476 uint64_t
get_value_index(uint64_t leaf_index_j, uint64_t position_in_leaf_container_p)
const
478 if (USE_PARENT_FIELD)
480 assert(leaf_index_j < this->parent_vec.size());
481 Node *parent = this->parent_vec[leaf_index_j];
482 uint64_t dist = position_in_leaf_container_p;
484 if (parent ==
nullptr)
492 while (parent !=
nullptr)
495 int64_t idx = parent->get_index(node);
500 uint64_t sum = parent->psum_on_count_deque(idx - 1);
506 parent = node->get_parent();
513 throw std::runtime_error(
"BPTree::get_value_index(): this function is not supported.");
525 if (this->root_is_leaf_)
527 return this->leaf_container_vec[(uint64_t)this->root].
psum();
531 return BPFunctions::psum(*this->root);
544 uint64_t
psum(uint64_t i)
const
546 if (i >= this->
size())
548 throw std::invalid_argument(
"Error: BPTree::psum(i). The i must be less than the size of the tree.");
550 assert(!this->
empty());
553 if (this->root_is_leaf_)
555 return this->leaf_container_vec[(uint64_t)this->root].
psum(i);
559 return BPFunctions::psum(*this->root, i, this->leaf_container_vec);
564 throw std::runtime_error(
"Error:psum(i)");
576 if (this->root_is_leaf_)
578 return this->leaf_container_vec[(uint64_t)this->root].
search(u);
582 return BPFunctions::search(*this->root, u, this->leaf_container_vec);
600 if (this->root_is_leaf_)
602 return this->leaf_container_vec[(uint64_t)this->root].
select0(i);
606 return BPFunctions::select0(*this->root, i, this->leaf_container_vec);
623 Node *current_node = this->root;
624 bool is_leaf = this->root_is_leaf_;
628 output_path.push_back(NodePointer::build_internal_node_pointer(this->root, -1));
632 output_path.push_back(NodePointer::build_leaf_pointer((uint64_t)this->root, -1));
637 is_leaf = current_node->is_parent_of_leaves();
638 current_node = current_node->get_child(0);
641 output_path.push_back(NodePointer::build_internal_node_pointer(current_node, 0));
645 output_path.push_back(NodePointer::build_leaf_pointer((uint64_t)current_node, 0));
659 Node *current_node = this->root;
660 bool is_leaf = this->root_is_leaf_;
664 output_path.push_back(NodePointer::build_internal_node_pointer(this->root, -1));
668 output_path.push_back(NodePointer::build_leaf_pointer((uint64_t)this->root, -1));
673 is_leaf = current_node->is_parent_of_leaves();
674 uint64_t idx = current_node->get_degree() - 1;
675 current_node = current_node->get_child(idx);
678 output_path.push_back(NodePointer::build_internal_node_pointer(current_node, idx));
682 output_path.push_back(NodePointer::build_leaf_pointer((uint64_t)current_node, idx));
694 std::vector<NodePointer> &path =
const_cast<std::vector<NodePointer> &
>(this->tmp_path);
704 if (output_path.size() != this->height_)
706 output_path.resize(this->height_);
713 Node *current_node = this->root;
714 uint64_t current_i = i;
715 bool is_leaf = this->root_is_leaf_;
719 output_path[y++] = is_leaf ? NodePointer::build_leaf_pointer((uint64_t)this->root, -1) : NodePointer::build_internal_node_pointer(this->root, -1);
724 assert(current_i <= current_node->psum_on_count_deque());
727 std::pair<int64_t, uint64_t> result = BPFunctions::access_child_index_by_value_index(*current_node, current_i);
731 if (result.first != -1)
733 is_leaf = current_node->is_parent_of_leaves();
734 current_node = current_node->get_child(result.first);
735 current_i = result.second;
736 output_path[y++] = is_leaf ? NodePointer::build_leaf_pointer((uint64_t)current_node, result.first) : NodePointer::build_internal_node_pointer(current_node, result.first);
740 throw std::runtime_error(
"Error: get_path_from_root_to_leaf2(1)");
746 assert(output_path.size() == y);
765 if (prev + 1 != (int64_t)(*it))
791 std::vector<std::string> log;
796 log.push_back(stool::Message::get_paragraph_string(message_paragraph) +
"=BP Tree: " + std::to_string(
size_in_bytes) +
" bytes, " + std::to_string(
size) +
" values, " + std::to_string(bytes_per_value) +
" bytes per value =");
797 log.push_back(stool::Message::get_paragraph_string(message_paragraph) +
" Max Degree of Internal Node: " + std::to_string(MAX_DEGREE));
798 log.push_back(stool::Message::get_paragraph_string(message_paragraph) +
" Max Count of Values in Leaf: " + std::to_string(LEAF_CONTAINER_MAX_SIZE));
801 log.push_back(stool::Message::get_paragraph_string(message_paragraph) +
" Internal Nodes: " + std::to_string(this->get_internal_node_memory()) +
" bytes");
802 log.push_back(stool::Message::get_paragraph_string(message_paragraph) +
" Ununsed Leaf Pointers: " + std::to_string(this->get_unused_leaf_container_vector_memory()) +
" bytes");
803 log.push_back(stool::Message::get_paragraph_string(message_paragraph) +
" Ununsed Node Pointers: " + std::to_string(this->get_unused_node_pointers_memory()) +
" bytes");
804 log.push_back(stool::Message::get_paragraph_string(message_paragraph) +
" Leaf Vector: " + std::to_string(this->get_leaf_container_vector_memory()) +
" bytes (vector capacity: " + std::to_string(this->leaf_container_vec.capacity()) +
")");
806 uint64_t value_count = this->
size();
807 uint64_t leaf_total_memory = this->get_leaf_container_memory();
808 uint64_t leaf_total_unused_memory = this->get_leaf_container_unused_memory();
810 double byte_per_value = value_count > 0 ? (double)leaf_total_memory / (
double)value_count : 0;
811 double unused_byte_per_value = value_count > 0 ? (double)leaf_total_unused_memory / (
double)value_count : 0;
813 log.push_back(stool::Message::get_paragraph_string(message_paragraph) +
" Leaves : " + std::to_string(leaf_total_memory) +
" bytes (" + std::to_string(byte_per_value) +
" bytes per value, leaf count: " + std::to_string(this->leaf_container_vec.size()) +
")");
814 log.push_back(stool::Message::get_paragraph_string(message_paragraph) +
" Unused memory for leaves : " + std::to_string(leaf_total_unused_memory) +
" bytes (" + std::to_string(unused_byte_per_value) +
" bytes per value)");
816 log.push_back(stool::Message::get_paragraph_string(message_paragraph) +
" Parent Vector : " + std::to_string(this->get_parent_vector_memory()) +
" bytes");
817 log.push_back(stool::Message::get_paragraph_string(message_paragraph) +
"==");
828 for (std::string &s : log)
830 std::cout << s << std::endl;
838 std::vector<std::string> tree;
841 if (this->root_is_leaf_)
843 tree.push_back(this->leaf_container_vec[(uint64_t)this->root].to_string());
847 BPFunctions::get_tree_string(*this->root, tree, this->leaf_container_vec);
852 tree.push_back(
"[]");
855 for (int64_t i = tree.size() - 1; i >= 0; i--)
857 std::cout << tree[i] << std::endl;
864 void print_info(
int message_paragraph = stool::Message::SHOW_MESSAGE)
const
866 std::cout << stool::Message::get_paragraph_string(message_paragraph) <<
"========= BPTREE =========" << std::endl;
867 std::cout << stool::Message::get_paragraph_string(message_paragraph) <<
"Height: " << this->height_ << std::endl;
876 std::cout << stool::Message::get_paragraph_string(message_paragraph + 1) <<
"Leaf: id = " << pt.get_leaf_container_index() <<
", content = " << this->leaf_container_vec[pt.get_leaf_container_index()].to_string() << std::endl;
880 pt.get_node()->print_info(message_paragraph + 1);
885 if (USE_PARENT_FIELD)
887 std::vector<uint64_t> r;
888 for (
Node *x : this->parent_vec)
890 r.push_back((uint64_t)x);
892 stool::DebugPrinter::print_integers(r);
894 std::cout <<
"==========================" << std::endl;
908 std::cout <<
"leaf_index: " << pt.get_leaf_container_index() <<
", " << this->leaf_container_vec[pt.get_leaf_container_index()].to_string() << std::endl;
918 std::cout <<
"INTERNAL NODES: " << std::endl;
925 std::cout <<
" " << node->to_string() << std::endl;
936 std::cout <<
"============ LEAVES ============" << std::endl;
937 for (uint64_t i = 0; i < this->leaf_container_vec.size(); i++)
939 std::cout <<
"leaf_container_vec[" << i <<
"] = " << this->leaf_container_vec[i].to_string() << std::endl;
941 std::cout <<
"============ LEAVES[END] ============" << std::endl;
949 std::cout << stool::Message::get_paragraph_string(message_paragraph) <<
"Performance Information (BPTree)[" << std::endl;
950 std::cout << stool::Message::get_paragraph_string(message_paragraph) <<
"The number of insertion operations: " << this->insert_operation_counter << std::endl;
951 std::cout << stool::Message::get_paragraph_string(message_paragraph) <<
"The number of split process in insertion operations: " << this->split_process_counter << std::endl;
952 if (this->insert_operation_counter > 0)
954 std::cout << stool::Message::get_paragraph_string(message_paragraph) <<
"The number of split process per insertion: " << (double)this->split_process_counter / (
double)this->insert_operation_counter << std::endl;
957 std::cout << stool::Message::get_paragraph_string(message_paragraph) <<
"The number of removal operations: " << this->remove_operation_counter << std::endl;
958 std::cout << stool::Message::get_paragraph_string(message_paragraph) <<
"The number of merge process in removal operations: " << this->merge_process_counter << std::endl;
959 if (this->remove_operation_counter > 0)
961 std::cout << stool::Message::get_paragraph_string(message_paragraph) <<
"The number of merge process per removal: " << (double)this->merge_process_counter / (
double)this->remove_operation_counter << std::endl;
964 std::cout << stool::Message::get_paragraph_string(message_paragraph) <<
"]" << std::endl;
973 uint64_t internal_node_count = 0;
974 uint64_t leaf_count = 0;
975 uint64_t total_degree_of_internal_node = 0;
982 internal_node_count++;
983 total_degree_of_internal_node += np.get_node()->children_count();
991 std::cout << stool::Message::get_paragraph_string(message_paragraph) <<
"Statistics(BPTree):" << std::endl;
992 std::cout << stool::Message::get_paragraph_string(message_paragraph + 1) <<
"The number of internal nodes: " << internal_node_count << std::endl;
993 std::cout << stool::Message::get_paragraph_string(message_paragraph + 1) <<
"The number of used leaf containers: " << leaf_count << std::endl;
994 std::cout << stool::Message::get_paragraph_string(message_paragraph + 1) <<
"The height of this tree: " << this->height_ << std::endl;
995 std::cout << stool::Message::get_paragraph_string(message_paragraph + 1) <<
"The number of stored values: " << this->
size() << std::endl;
997 std::cout << stool::Message::get_paragraph_string(message_paragraph + 1) <<
"The max degree of internal node (parameter): " << MAX_DEGREE << std::endl;
998 if (internal_node_count > 0)
1000 std::cout << stool::Message::get_paragraph_string(message_paragraph + 1) <<
"The average degree of internal node: " << (total_degree_of_internal_node / internal_node_count) << std::endl;
1002 std::cout << stool::Message::get_paragraph_string(message_paragraph + 1) <<
"The max count of values in leaf (parameter): " << LEAF_CONTAINER_MAX_SIZE << std::endl;
1003 if (this->
size() > 0)
1005 std::cout << stool::Message::get_paragraph_string(message_paragraph + 1) <<
"The average count of values in leaf (parameter): " << (this->
size() / leaf_count) << std::endl;
1008 std::cout << stool::Message::get_paragraph_string(message_paragraph + 1) <<
"The number of unused nodes: " << this->unused_node_pointers.size() << std::endl;
1009 std::cout << stool::Message::get_paragraph_string(message_paragraph + 1) <<
"The number of unused leaf containers: " << this->unused_leaf_container_indexes.size() << std::endl;
1010 std::cout << stool::Message::get_paragraph_string(message_paragraph + 1) <<
"The number of stored parent pointers: " << this->parent_vec.size() << std::endl;
1013 uint64_t num = this->
size();
1014 if (USE_PARENT_FIELD)
1016 total_memory_usage += this->parent_vec.size() *
sizeof(
Node *);
1018 std::cout << stool::Message::get_paragraph_string(message_paragraph + 1) <<
"Estimated memory usage: " << total_memory_usage <<
" bytes (Avg: " << (total_memory_usage / num) <<
" bytes)" << std::endl;
1019 std::cout << stool::Message::get_paragraph_string(message_paragraph) <<
"[END]" << std::endl;
1026 std::cout <<
"BPTree::print_debug_info()" << std::endl;
1034 std::cout <<
"BPTree::time_count: " << (double)bptree::time_count / 1000000.0 <<
" ms" << std::endl;
1035 std::cout <<
"BPTree::time_count2: " << (double)bptree::time_count2 / 1000000.0 <<
" ms" << std::endl;
1042 bool b1 = this->verify_sub();
1045 if (!this->root_is_leaf_ && this->
size() > 0)
1047 this->verify_sum_deque(this->root);
1065 uint64_t __max_degree_of_internal_node = MAX_DEGREE;
1066 uint64_t __max_count_of_values_in_leaf = LEAF_CONTAINER_MAX_SIZE;
1067 if (__max_degree_of_internal_node < 4)
1069 throw std::runtime_error(
"Error: BPTree::initialize(__max_degree_of_internal_node, __max_count_of_values_in_leaf). The __max_degree_of_internal_node must be larger than 3.");
1071 if (__max_count_of_values_in_leaf < 4)
1073 throw std::runtime_error(
"Error: BPTree::initialize(__max_degree_of_internal_node, __max_count_of_values_in_leaf). The __max_count_of_values_in_leaf must be larger than 3.");
1079 this->root =
nullptr;
1080 this->root_is_leaf_ =
false;
1089 this->leaf_container_vec.swap(_tree.leaf_container_vec);
1090 this->parent_vec.swap(_tree.parent_vec);
1091 this->unused_node_pointers.swap(_tree.unused_node_pointers);
1092 this->unused_leaf_container_indexes.swap(_tree.unused_leaf_container_indexes);
1093 this->tmp_path.swap(_tree.tmp_path);
1096 std::swap(this->root, _tree.root);
1097 if (swap_linked_tree)
1099 std::swap(this->linked_tree_, _tree.linked_tree_);
1101 std::swap(this->density_threshold, _tree.density_threshold);
1102 std::swap(this->density1, _tree.density1);
1103 std::swap(this->height_, _tree.height_);
1104 std::swap(this->root_is_leaf_, _tree.root_is_leaf_);
1112 std::stack<Node *> nodes;
1113 if (!this->
empty() && !this->root_is_leaf_)
1115 nodes.push(this->root);
1117 while (nodes.size() > 0)
1119 Node *top = nodes.top();
1121 if (!top->is_parent_of_leaves())
1123 const stool::SimpleDeque16<Node *> &children = top->get_children();
1124 for (
Node *child : children)
1131 this->root =
nullptr;
1132 this->root_is_leaf_ =
false;
1135 while (unused_node_pointers.size() > 0)
1137 Node *top = unused_node_pointers[unused_node_pointers.size() - 1];
1139 unused_node_pointers.pop_back();
1142 this->leaf_container_vec.resize(0);
1143 this->parent_vec.resize(0);
1145 while (this->unused_leaf_container_indexes.size() > 0)
1147 this->unused_leaf_container_indexes.pop();
1156 std::vector<VALUE> r;
1167 std::vector<NodePointer> path;
1169 while (i < values_Q.size())
1173 if (path.size() > 0)
1175 i += this->push_many_to_leaf(values_Q, i, path);
1179 this->create_root_leaf(values_Q[i]);
1191 std::vector<NodePointer> path;
1193 if (path.size() > 0)
1195 uint64_t leaf = path[path.size() - 1].get_leaf_container_index();
1196 this->leaf_container_vec[leaf].push_front(value);
1198 uint64_t sum = this->leaf_container_vec[leaf].psum(0);
1200 for (int64_t i = path.size() - 2; i >= 0; --i)
1202 Node *parent = path[i].get_node();
1203 uint64_t parent_edge_index = path[i + 1].get_parent_edge_index();
1204 parent->increment(parent_edge_index, 1, sum);
1206 this->balance_for_insertion(path,
true);
1210 this->create_root_leaf(value);
1219 uint64_t
remove_using_path(
const std::vector<NodePointer> &path, uint64_t position_in_leaf_container_q)
1221 uint64_t merge_counter = 0;
1222 uint64_t x = path[path.size() - 1].get_leaf_container_index();
1223 int64_t delta = leaf_container_vec[x].psum(position_in_leaf_container_q, position_in_leaf_container_q);
1224 leaf_container_vec[x].remove(position_in_leaf_container_q);
1225 for (int64_t j = path.size() - 2; j >= 0; j--)
1227 Node *node = path[j].get_node();
1228 uint64_t child_index = path[j + 1].get_parent_edge_index();
1230 node->increment(child_index, -1, -delta);
1233 if (path.size() > 1)
1235 merge_counter += this->balance_for_removal(this->tmp_path);
1239 assert(path.size() == 1);
1240 if (leaf_container_vec[x].
size() == 0)
1242 this->remove_empty_leaf((uint64_t)this->root,
nullptr, -1);
1243 this->root =
nullptr;
1244 this->root_is_leaf_ =
false;
1248 return merge_counter;
1259 assert(i < this->
size());
1260 this->remove_operation_counter++;
1263 this->merge_process_counter += this->
remove_using_path(this->tmp_path, position_to_remove);
1267 throw std::invalid_argument(
"Error in BPTree::remove(i). This tree is empty. i = " + std::to_string(i));
1275 void insert(uint64_t i, VALUE v, uint64_t weight_w)
1277 if (i < this->
size())
1281 assert(i <= this->
size());
1285 if (position_to_insert != -1)
1287 uint64_t x = this->tmp_path[this->tmp_path.size() - 1].get_leaf_container_index();
1289 this->leaf_container_vec[x].insert(position_to_insert, v);
1291 for (int64_t j = this->tmp_path.size() - 2; j >= 0; j--)
1293 Node *node = this->tmp_path[j].get_node();
1294 uint64_t child_index = this->tmp_path[j + 1].get_parent_edge_index();
1296 assert(child_index < node->children_count());
1298 node->increment(child_index, 1, weight_w);
1300 this->split_process_counter += this->balance_for_insertion(this->tmp_path);
1304 throw std::runtime_error(
"Error: insert");
1312 this->create_root_leaf(v);
1316 throw std::invalid_argument(
"Error(BPTree::insert)");
1322 assert(i == this->
size());
1325 this->insert_operation_counter++;
1335 NodePointer &leaf_pointer = this->tmp_path[this->tmp_path.size() - 1];
1336 LEAF_CONTAINER &leaf = this->leaf_container_vec[leaf_pointer.get_leaf_container_index()];
1337 leaf.increment(pos, delta);
1338 for (int64_t i = this->tmp_path.size() - 2; i >= 0; --i)
1340 uint64_t idx = this->tmp_path[i + 1].get_parent_edge_index();
1341 Node *parent = this->tmp_path[i].get_node();
1342 parent->increment(idx, 0, delta);
1349 void resize(uint64_t _size, VALUE default_value)
1351 if (this->
size() < _size)
1354 std::vector<NodePointer> path;
1355 uint64_t diff = _size - this->
size();
1361 if (path.size() > 0)
1363 uint64_t leaf = path[path.size() - 1].get_leaf_container_index();
1367 this->leaf_container_vec[leaf].push_back(default_value);
1371 uint64_t sum = len > 0 ? this->leaf_container_vec[leaf].reverse_psum(len - 1) : 0;
1373 for (int64_t j = path.size() - 2; j >= 0; --j)
1375 Node *parent = path[j].get_node();
1376 uint64_t parent_edge_index = path[j + 1].get_parent_edge_index();
1377 parent->increment(parent_edge_index, len, sum);
1379 this->balance_for_insertion(path,
true);
1383 this->create_root_leaf(default_value);
1390 while (this->
size() > _size)
1403 this->linked_tree_ = _tree;
1412 if (this->
size() == 0 || this->root_is_leaf_)
1419 if (!USE_PARENT_FIELD)
1421 this->parent_vec.resize(this->leaf_container_vec.size(),
nullptr);
1428 Node *node = np.get_node();
1429 if (node->is_parent_of_leaves())
1431 auto &children = node->get_children();
1432 for (
Node *v : children)
1434 uint64_t leaf = (uint64_t)v;
1435 this->parent_vec[leaf] = node;
1441 uint64_t counter = 0;
1448 Node *node = np.get_node();
1449 if (node->is_parent_of_leaves())
1451 uint64_t _size = node->children_count();
1452 auto &children1 = node->get_children();
1454 for (uint64_t j = 0; j < _size; j++)
1456 this->exchange(children1, j, counter);
1463 while (this->leaf_container_vec.size() > counter)
1465 this->leaf_container_vec.pop_back();
1466 this->parent_vec.pop_back();
1468 while (this->unused_leaf_container_indexes.size() > 0)
1470 this->unused_leaf_container_indexes.pop();
1473 if (!USE_PARENT_FIELD)
1475 std::vector<Node *> tmp;
1476 this->parent_vec.swap(tmp);
1479 if (this->root_is_leaf_)
1481 this->root = (
Node *)0;
1491 void build(
const std::vector<VALUE> &_values)
1494 this->build_sub(_values);
1496 std::vector<Node *> layer = this->build_from_leaf_containers_sub1();
1497 uint64_t current_height = 2;
1498 while (layer.size() > 0)
1501 std::vector<Node *> next_layer = this->build_from_leaf_containers_sub2(layer, current_height);
1502 layer.swap(next_layer);
1519 uint64_t _max_degree;
1520 uint64_t _max_count_of_values_in_leaf;
1522 std::memcpy(&_max_degree, data.data() + pos,
sizeof(uint64_t));
1523 pos +=
sizeof(uint64_t);
1524 std::memcpy(&_max_count_of_values_in_leaf, data.data() + pos,
sizeof(uint64_t));
1525 pos +=
sizeof(uint64_t);
1527 auto tmp = LEAF_CONTAINER::load_vector_from_bytes(data, pos);
1529 r.build_from_leaf_containers(tmp);
1541 uint64_t _max_degree;
1542 uint64_t _max_count_of_values_in_leaf;
1543 ifs.read((
char *)(&_max_degree),
sizeof(uint64_t));
1544 ifs.read((
char *)(&_max_count_of_values_in_leaf),
sizeof(uint64_t));
1546 auto tmp = LEAF_CONTAINER::load_vector_from_file(ifs);
1548 r.build_from_leaf_containers(tmp);
1561 uint64_t _max_degree = MAX_DEGREE;
1562 uint64_t _max_count_of_values_in_leaf = LEAF_CONTAINER_MAX_SIZE;
1564 uint64_t _size =
sizeof(_max_degree) +
sizeof(_max_count_of_values_in_leaf) + LEAF_CONTAINER::get_byte_size(item.leaf_container_vec);
1565 if (pos + _size > output.size())
1567 output.resize(pos + _size);
1570 std::memcpy(output.data() + pos, &_max_degree,
sizeof(uint64_t));
1571 pos +=
sizeof(uint64_t);
1572 std::memcpy(output.data() + pos, &_max_count_of_values_in_leaf,
sizeof(uint64_t));
1573 pos +=
sizeof(uint64_t);
1575 LEAF_CONTAINER::store_to_bytes(item.leaf_container_vec, output, pos);
1587 uint64_t _max_degree = MAX_DEGREE;
1588 uint64_t _max_count_of_values_in_leaf = LEAF_CONTAINER_MAX_SIZE;
1590 os.write((
const char *)(&_max_degree),
sizeof(uint64_t));
1591 os.write((
const char *)(&_max_count_of_values_in_leaf),
sizeof(uint64_t));
1592 LEAF_CONTAINER::store_to_file(item.leaf_container_vec, os);
1608 void defragmentation()
1610 std::vector<uint64_t> tmp;
1611 while (this->unused_leaf_container_indexes.size() > 0)
1613 tmp.push_back(this->unused_leaf_container_indexes.top());
1614 this->unused_leaf_container_indexes.pop();
1617 std::sort(tmp.begin(), tmp.end(), [](
const uint64_t &lhs,
const uint64_t &rhs)
1618 { return lhs > rhs; });
1620 std::vector<std::pair<Node *, uint64_t>> tmp_nodes;
1622 std::unordered_set<uint64_t> checker2;
1624 std::unordered_set<uint64_t> checker1;
1625 for (uint64_t p : tmp)
1630 for (int64_t i = this->leaf_container_vec.size() - 1; i >= 0; --i)
1632 auto xs = checker1.find(i);
1633 if (xs == checker1.end())
1637 if (checker2.size() > tmp.size())
1646 NodePointer pt = *it;
1647 if (!pt.is_leaf() && pt.get_node()->is_parent_of_leaves())
1649 Node *node = pt.get_node();
1650 for (int64_t i = 0; i < node->children_count(); i++)
1652 uint64_t x = (uint64_t)node->get_child(i);
1653 auto xs = checker2.find(x);
1654 if (xs != checker2.end())
1656 tmp_nodes.push_back(std::pair<Node *, uint64_t>(node, i));
1661 assert(tmp_nodes.size() == checker2.size());
1664 std::sort(tmp_nodes.begin(), tmp_nodes.end(), [](
const std::pair<Node *, uint64_t> &lhs,
const std::pair<Node *, uint64_t> &rhs)
1666 uint64_t x = (uint64_t)lhs.first->get_child(lhs.second);
1667 uint64_t y = (uint64_t)rhs.first->get_child(rhs.second);
1671 while (tmp.size() > 0 && tmp_nodes.size() > 0)
1673 std::pair<Node *, uint64_t> top = tmp_nodes[tmp_nodes.size() - 1];
1674 uint64_t idx = (uint64_t)top.first->get_child(top.second);
1675 uint64_t x = tmp[tmp.size() - 1];
1678 top.first->move_container_index(top.second, x, this->leaf_container_vec);
1679 tmp_nodes.pop_back();
1689 this->leaf_container_vec.resize(this->leaf_container_vec.size() - k);
1702 void clear_unused_node_pointers()
1704 for (uint64_t i = 0; i < this->unused_node_pointers.size(); i++)
1706 delete this->unused_node_pointers[i];
1708 this->unused_node_pointers.clear();
1709 this->unused_node_pointers.shrink_to_fit();
1717 void create_root_leaf(VALUE value)
1719 uint64_t p = this->get_new_container_index();
1720 this->leaf_container_vec[p].push_back(value);
1721 this->root = (Node *)p;
1722 this->root_is_leaf_ =
true;
1724 if (USE_PARENT_FIELD)
1726 this->parent_vec[p] =
nullptr;
1741 void remove_empty_leaf(uint64_t idx, Node *parent, int64_t child_index)
1743 if (parent !=
nullptr)
1745 parent->remove_child(child_index);
1747 if (USE_PARENT_FIELD)
1749 this->parent_vec[idx] =
nullptr;
1752 this->unused_leaf_container_indexes.push(idx);
1753 this->leaf_container_vec[idx].clear();
1768 void remove_empty_node(Node *node, Node *parent, int64_t child_index)
1771 if (parent !=
nullptr)
1773 parent->remove_child(child_index);
1778 if (this->root == node)
1780 this->root =
nullptr;
1784 if (this->unused_node_pointers.size() <= 4096)
1786 this->unused_node_pointers.push_back(node);
1800 uint64_t get_new_container_index()
1802 if (this->unused_leaf_container_indexes.size() == 0)
1804 this->leaf_container_vec.push_back(LEAF_CONTAINER());
1805 if (USE_PARENT_FIELD)
1807 this->parent_vec.push_back(
nullptr);
1809 this->unused_leaf_container_indexes.push(this->leaf_container_vec.size() - 1);
1811 assert(this->unused_leaf_container_indexes.size() > 0);
1812 uint64_t idx = this->unused_leaf_container_indexes.top();
1813 this->unused_leaf_container_indexes.pop();
1824 Node *get_new_node_pointer()
1826 Node *new_node =
nullptr;
1827 if (this->unused_node_pointers.size() > 0)
1829 new_node = this->unused_node_pointers[this->unused_node_pointers.size() - 1];
1830 this->unused_node_pointers.pop_back();
1834 new_node =
new Node();
1851 void split_process(
const std::vector<NodePointer> &path, uint64_t t)
1853 const NodePointer &top = path[t];
1855 Node *node = top.get_node();
1856 Node *_new_right_node =
nullptr;
1857 uint64_t parent_edge_index = 0;
1860 _new_right_node = this->get_new_node_pointer();
1861 _new_right_node->initialize(top.get_node()->is_parent_of_leaves(), this->leaf_container_vec);
1865 _new_right_node = (Node *)this->get_new_container_index();
1868 Node *_parent =
nullptr;
1871 _parent = path[t - 1].get_node();
1872 _parent->insert_child(top.get_parent_edge_index() + 1, _new_right_node, 0, 0);
1873 parent_edge_index = top.get_parent_edge_index();
1877 _parent = this->get_new_node_pointer();
1880 _parent->initialize((uint64_t)node, (uint64_t)_new_right_node, this->leaf_container_vec);
1884 _parent->initialize(node, _new_right_node, this->leaf_container_vec);
1886 parent_edge_index = 0;
1888 this->root = _parent;
1889 this->root_is_leaf_ =
false;
1892 if (USE_PARENT_FIELD)
1896 this->parent_vec[(uint64_t)node] = _parent;
1900 node->set_parent(_parent);
1904 if (USE_PARENT_FIELD)
1908 this->parent_vec[(uint64_t)_new_right_node] = _parent;
1912 _new_right_node->set_parent(_parent);
1916 this->split_node(node, _new_right_node, top.is_leaf(), _parent, parent_edge_index, this->parent_vec);
1929 uint64_t balance_for_insertion(
const std::vector<NodePointer> &path,
bool superLeftPushMode =
false)
1931 uint64_t split_counter = 0;
1932 for (int64_t t = path.size() - 1; t >= 0; --t)
1934 const NodePointer &top = path[t];
1935 uint64_t degree = top.get_degree(this->leaf_container_vec);
1936 uint64_t threshold = top.is_leaf() ? LEAF_CONTAINER_MAX_SIZE : MAX_DEGREE;
1937 uint64_t LR_threshold = threshold;
1938 if (superLeftPushMode)
1940 LR_threshold = (threshold * 3) / 4;
1943 if (degree > threshold)
1949 Node *parent = path[t - 1].get_node();
1950 uint64_t parent_edge_index = top.get_parent_edge_index();
1951 Node *leftSibling = parent_edge_index > 0 ? parent->get_child(parent_edge_index - 1) :
nullptr;
1952 Node *rightSibling = parent_edge_index + 1 < parent->children_count() ? parent->get_child(parent_edge_index + 1) :
nullptr;
1953 uint64_t leftSiblingDegree = UINT64_MAX;
1954 if (parent_edge_index > 0)
1956 leftSiblingDegree = top.is_leaf() ? this->leaf_container_vec[(uint64_t)leftSibling].
size() : leftSibling->get_degree();
1961 uint64_t rightSiblingDegree = UINT64_MAX;
1962 if (parent_edge_index + 1 < parent->children_count())
1964 rightSiblingDegree = top.is_leaf() ? this->leaf_container_vec[(uint64_t)rightSibling].
size() : rightSibling->get_degree();
1968 if (leftSiblingDegree < LR_threshold || rightSiblingDegree < LR_threshold)
1970 if (leftSiblingDegree < LR_threshold)
1972 if (superLeftPushMode)
1974 uint64_t diff = LR_threshold - leftSiblingDegree;
1975 this->move_values_left(leftSibling, top.get_node(), diff, top.is_leaf(), parent, parent_edge_index);
1979 this->move_values_left(leftSibling, top.get_node(), 1, top.is_leaf(), parent, parent_edge_index);
1984 this->move_values_right(top.get_node(), rightSibling, 1, top.is_leaf(), parent, parent_edge_index);
1990 this->split_process(path, t);
1996 this->split_process(path, t);
2005 return split_counter;
2018 uint64_t balance_for_removal(
const std::vector<NodePointer> &path)
2022 uint64_t merge_counter = 0;
2024 for (int64_t t = path.size() - 1; t >= 0; --t)
2026 const NodePointer &top = path[t];
2027 uint64_t max_size = top.is_leaf() ? LEAF_CONTAINER_MAX_SIZE : MAX_DEGREE;
2028 uint64_t threshold = max_size / 2;
2030 uint64_t degree = top.get_degree(this->leaf_container_vec);
2031 if (degree >= threshold)
2039 Node *parent = path[t - 1].get_node();
2040 uint64_t parent_edge_index = top.get_parent_edge_index();
2041 Node *leftSibling = parent_edge_index > 0 ? parent->get_child(parent_edge_index - 1) :
nullptr;
2042 Node *rightSibling = parent_edge_index + 1 < parent->children_count() ? parent->get_child(parent_edge_index + 1) :
nullptr;
2043 uint64_t leftSiblingDegree = 0;
2044 if (parent_edge_index > 0)
2046 leftSiblingDegree = top.is_leaf() ? this->leaf_container_vec[(uint64_t)leftSibling].
size() : leftSibling->get_degree();
2049 uint64_t rightSiblingDegree = 0;
2050 if (parent_edge_index + 1 < parent->children_count())
2052 rightSiblingDegree = top.is_leaf() ? this->leaf_container_vec[(uint64_t)rightSibling].
size() : rightSibling->get_degree();
2055 if (leftSiblingDegree != 0 || rightSiblingDegree != 0)
2057 if (leftSiblingDegree > threshold)
2059 this->move_values_right(leftSibling, top.get_node(), 1, top.is_leaf(), parent, parent_edge_index - 1);
2062 else if (rightSiblingDegree > threshold)
2064 this->move_values_left(top.get_node(), rightSibling, 1, top.is_leaf(), parent, parent_edge_index + 1);
2069 assert(leftSiblingDegree == threshold || rightSiblingDegree == threshold);
2070 if (leftSiblingDegree == threshold)
2072 this->move_values_left(leftSibling, top.get_node(), degree, top.is_leaf(), parent, parent_edge_index);
2075 this->remove_empty_leaf(top.get_leaf_container_index(), parent, parent_edge_index);
2079 this->remove_empty_node(top.get_node(), parent, parent_edge_index);
2084 this->move_values_right(top.get_node(), rightSibling, degree, top.is_leaf(), parent, parent_edge_index);
2087 this->remove_empty_leaf(top.get_leaf_container_index(), parent, parent_edge_index);
2091 this->remove_empty_node(top.get_node(), parent, parent_edge_index);
2099 throw std::logic_error(
"Error(BPTree::balance_for_removal(1))");
2107 throw std::logic_error(
"Error(BPTree::balance_for_removal(2))");
2109 else if (degree == 1)
2111 if (!this->root_is_leaf_)
2114 bool b = this->root->is_parent_of_leaves();
2115 Node *new_root = this->root->get_child(0);
2116 this->root->remove_child(0);
2117 this->remove_empty_node(this->root,
nullptr, -1);
2118 this->root = new_root;
2119 this->root_is_leaf_ = b;
2121 if (USE_PARENT_FIELD)
2125 this->parent_vec[(uint64_t)new_root] =
nullptr;
2129 new_root->set_parent(
nullptr);
2143 return merge_counter;
2150 uint64_t compute_internal_node_count()
const
2152 uint64_t counter = 0;
2156 NodePointer np = *it;
2169 uint64_t compute_leaf_container_count()
const
2171 uint64_t counter = 0;
2175 NodePointer np = *it;
2196 void move_values_right(Node *left_node, Node *right_node, uint64_t len,
bool is_leaf, Node *parent, int64_t parent_edge_index_of_left_node)
2200 uint64_t left_leaf = (uint64_t)left_node;
2201 uint64_t right_leaf = (uint64_t)right_node;
2202 assert(left_leaf < this->leaf_container_vec.size());
2203 assert(right_leaf < this->leaf_container_vec.size());
2205 if constexpr (USE_PSUM)
2207 if (parent !=
nullptr)
2209 assert(this->leaf_container_vec[left_leaf].
psum() == parent->access_sum_deque(parent_edge_index_of_left_node));
2210 int64_t sum = len != 0 ? this->leaf_container_vec[left_leaf].reverse_psum(len - 1) : 0;
2212 assert(sum <= (int64_t)this->leaf_container_vec[left_leaf].
psum());
2214 parent->decrement_on_sum_deque(parent_edge_index_of_left_node, sum);
2215 parent->increment_on_sum_deque(parent_edge_index_of_left_node + 1, sum);
2219 if (parent !=
nullptr)
2221 parent->decrement_on_count_deque(parent_edge_index_of_left_node, len);
2222 parent->increment_on_count_deque(parent_edge_index_of_left_node + 1, len);
2224 auto items = this->leaf_container_vec[left_leaf].pop_back_many(len);
2225 this->leaf_container_vec[right_leaf].push_front_many(items);
2229 BPFunctions::move_right(*left_node, *right_node, len, parent, parent_edge_index_of_left_node, this->parent_vec);
2245 void move_values_left(Node *left_node, Node *right_node, uint64_t len,
bool is_leaf, Node *parent, int64_t parent_edge_index_of_right_node)
2249 uint64_t left_leaf = (uint64_t)left_node;
2250 uint64_t right_leaf = (uint64_t)right_node;
2251 assert(left_leaf < this->leaf_container_vec.size());
2253 if (USE_PSUM && parent !=
nullptr)
2255 uint64_t sum = len != 0 ? this->leaf_container_vec[right_leaf].psum(len - 1) : 0;
2256 parent->decrement_on_sum_deque(parent_edge_index_of_right_node, sum);
2257 parent->increment_on_sum_deque(parent_edge_index_of_right_node - 1, sum);
2260 if (parent !=
nullptr)
2262 parent->decrement_on_count_deque(parent_edge_index_of_right_node, len);
2263 parent->increment_on_count_deque(parent_edge_index_of_right_node - 1, len);
2265 auto items = this->leaf_container_vec[right_leaf].pop_front_many(len);
2266 this->leaf_container_vec[left_leaf].push_back_many(items);
2270 BPFunctions::move_left(*left_node, *right_node, len, parent, parent_edge_index_of_right_node, this->parent_vec);
2288 void split_node(Node *left_node, Node *right_node,
bool is_leaf, Node *parent, int64_t parent_edge_index_of_left_node, [[maybe_unused]] std::vector<Node *> &parent_vec)
2291 uint64_t degree = 0;
2294 degree = leaf_container_vec[(uint64_t)left_node].
size();
2295 assert((uint64_t)left_node < this->leaf_container_vec.size());
2296 assert((uint64_t)right_node < this->leaf_container_vec.size());
2301 degree = left_node->get_degree();
2303 uint64_t left_len = degree / 2;
2304 uint64_t right_len = degree - left_len;
2305 this->move_values_right(left_node, right_node, right_len, is_leaf, parent, parent_edge_index_of_left_node);
2321 uint64_t push_many_to_leaf(
const std::vector<VALUE> &values, uint64_t value_pos,
const std::vector<NodePointer> &path)
2324 uint64_t leaf = path[path.size() - 1].get_leaf_container_index();
2326 while ((value_pos + x) < values.size() && leaf_container_vec[leaf].size() <= this->get_max_count_of_values_in_leaf())
2329 this->leaf_container_vec[leaf].push_back(values[value_pos + x]);
2334 uint64_t sum = len > 0 ? this->leaf_container_vec[leaf].reverse_psum(len - 1) : 0;
2336 for (int64_t j = path.size() - 2; j >= 0; --j)
2338 Node *parent = path[j].get_node();
2339 uint64_t parent_edge_index = path[j + 1].get_parent_edge_index();
2340 assert(parent_edge_index < parent->children_count());
2342 parent->increment(parent_edge_index, len, sum);
2345 this->balance_for_insertion(path,
true);
2359 void preprocess_for_the_exchange_of_two_leaves([[maybe_unused]] uint64_t leaf_index1, [[maybe_unused]] uint64_t leaf_index2)
2375 void exchange(stool::SimpleDeque16<Node *> &children1, uint64_t j, uint64_t leaf_index2)
2377 uint64_t leaf_index1 = (uint64_t)children1[j];
2378 assert(this->parent_vec[leaf_index1] !=
nullptr);
2380 if (leaf_index1 != leaf_index2)
2382 this->preprocess_for_the_exchange_of_two_leaves(leaf_index1, leaf_index2);
2384 if (this->parent_vec[leaf_index2] !=
nullptr)
2386 auto &children2 = this->parent_vec[leaf_index2]->get_children();
2387 uint64_t idx = this->parent_vec[leaf_index2]->get_index((Node *)leaf_index2);
2388 this->leaf_container_vec[leaf_index2].swap(this->leaf_container_vec[leaf_index1]);
2389 children1[j] = (Node *)leaf_index2;
2390 children2[idx] = (Node *)leaf_index1;
2394 this->leaf_container_vec[leaf_index2].swap(this->leaf_container_vec[leaf_index1]);
2395 children1[j] = (Node *)leaf_index2;
2398 Node *tmp_n = this->parent_vec[leaf_index1];
2399 this->parent_vec[leaf_index1] = this->parent_vec[leaf_index2];
2400 this->parent_vec[leaf_index2] = tmp_n;
2403 bool verify_sub()
const
2409 NodePointer pt = *it;
2412 assert(pt.get_leaf_container_index() < this->leaf_container_vec.size());
2413 uint64_t
size = this->leaf_container_vec[pt.get_leaf_container_index()].size();
2414 if (this->root_is_leaf_ && pt.get_leaf_container_index() == (uint64_t)this->root)
2416 b = b && (
size > 0);
2429 Node *_node = pt.get_node();
2430 b = b && BPFunctions::verify(*_node, this->leaf_container_vec, MAX_DEGREE, this->root == pt.get_node());
2435 if (p != this->
size())
2437 throw std::logic_error(
"Error(BPTree::verify)");
2445 uint64_t get_internal_node_memory()
const
2451 NodePointer pt = *it;
2454 sum1 += pt.get_node()->size_in_bytes();
2455 sum1 +=
sizeof(Node *);
2465 uint64_t get_unused_leaf_container_vector_memory()
const
2467 return sizeof(std::stack<uint64_t>) + (this->unused_leaf_container_indexes.size() *
sizeof(uint64_t));
2474 uint64_t get_leaf_container_vector_memory()
const
2476 return sizeof(std::vector<uint64_t>) + (this->leaf_container_vec.capacity() *
sizeof(LEAF_CONTAINER));
2483 uint64_t get_leaf_container_memory()
const
2486 for (
const LEAF_CONTAINER &leaf : this->leaf_container_vec)
2488 sum2 += leaf.size_in_bytes(
true);
2493 uint64_t get_leaf_container_unused_memory()
const
2496 for (
const LEAF_CONTAINER &leaf : this->leaf_container_vec)
2498 sum2 += leaf.unused_size_in_bytes();
2507 uint64_t get_unused_node_pointers_memory()
const
2510 for (Node *node : this->unused_node_pointers)
2512 sum6 += node->size_in_bytes();
2515 sum6 += (
sizeof(Node *) * this->unused_node_pointers.capacity()) +
sizeof(std::vector<Node *>);
2523 uint64_t get_parent_vector_memory()
const
2525 return (
sizeof(Node *) * this->parent_vec.capacity()) +
sizeof(std::vector<Node *>);
2527 void verify_sum_deque(Node *node)
const
2529 if (node->is_parent_of_leaves())
2531 uint64_t true_sum = 0;
2532 auto &children = node->get_children();
2533 for (uint64_t i = 0; i < children.size(); i++)
2535 uint64_t
id = (uint64_t)children[i];
2536 true_sum += this->leaf_container_vec[id].psum();
2538 if (true_sum != node->psum_on_sum_deque())
2540 throw std::runtime_error(
"Error: verify_sum_deque");
2545 uint64_t true_sum = 0;
2546 auto &children = node->get_children();
2547 for (uint64_t i = 0; i < children.size(); i++)
2549 Node *child = children[i];
2550 this->verify_sum_deque(child);
2551 true_sum += child->psum_on_sum_deque();
2554 if (true_sum != node->psum_on_sum_deque())
2556 throw std::runtime_error(
"Error: verify_sum_deque");
2572 std::vector<Node *> build_from_leaf_containers_sub2(std::vector<Node *> &nodes, uint64_t current_height)
2574 assert(nodes.size() > 0);
2575 std::vector<Node *> r;
2576 if (nodes.size() == 1)
2578 this->root = nodes[0];
2579 this->root_is_leaf_ =
false;
2580 this->height_ = current_height;
2582 else if (nodes.size() <= MAX_DEGREE)
2585 Node *root = this->get_new_node_pointer();
2586 root->initialize(
false, this->leaf_container_vec);
2587 for (uint64_t i = 0; i < nodes.size(); i++)
2589 if (USE_PARENT_FIELD)
2591 nodes[i]->set_parent(root);
2593 uint64_t count = nodes[i]->psum_on_count_deque();
2595 if constexpr (USE_PSUM)
2597 sum = nodes[i]->psum_on_sum_deque();
2599 root->append_child(nodes[i], count, sum);
2606 uint64_t counter = nodes.size();
2607 uint64_t threshold = MAX_DEGREE * 2;
2611 if (counter >= threshold)
2615 else if (counter > MAX_DEGREE)
2623 Node *node = this->get_new_node_pointer();
2624 node->initialize(
false, this->leaf_container_vec);
2626 for (uint64_t j = 0; j < d; j++)
2628 if (USE_PARENT_FIELD)
2630 nodes[i]->set_parent(node);
2632 uint64_t count = nodes[i]->psum_on_count_deque();
2634 if constexpr (USE_PSUM)
2636 sum = nodes[i]->psum_on_sum_deque();
2638 node->append_child(nodes[i], count, sum);
2657 std::vector<Node *> build_from_leaf_containers_sub1()
2659 std::vector<Node *> r;
2661 if (this->leaf_container_vec.size() == 0)
2663 this->root =
nullptr;
2664 this->root_is_leaf_ =
false;
2667 else if (this->leaf_container_vec.size() == 1)
2669 this->root = (Node *)0;
2670 this->root_is_leaf_ =
true;
2673 else if (this->leaf_container_vec.size() <= MAX_DEGREE)
2675 Node *root = this->get_new_node_pointer();
2676 root->initialize(
true, this->leaf_container_vec);
2677 for (uint64_t i = 0; i < this->leaf_container_vec.size(); i++)
2680 if constexpr (USE_PSUM)
2682 psum += this->leaf_container_vec[i].psum();
2684 if (USE_PARENT_FIELD)
2686 this->parent_vec[i] = root;
2689 root->append_child((Node *)i, this->leaf_container_vec[i].size(),
psum);
2697 uint64_t counter = this->leaf_container_vec.size();
2698 uint64_t threshold = MAX_DEGREE * 2;
2702 if (counter >= threshold)
2706 else if (counter > MAX_DEGREE)
2714 Node *node = this->get_new_node_pointer();
2715 node->initialize(
true, this->leaf_container_vec);
2717 for (uint64_t j = 0; j < d; j++)
2720 if constexpr (USE_PSUM)
2722 psum += this->leaf_container_vec[i].psum();
2724 if (USE_PARENT_FIELD)
2726 this->parent_vec[i] = node;
2728 node->append_child((Node *)i, this->leaf_container_vec[i].size(),
psum);
2747 void build_sub(
const std::vector<VALUE> &_values)
2750 if (_values.size() == 0)
2753 else if (_values.size() <= LEAF_CONTAINER_MAX_SIZE)
2755 uint64_t leaf_index = this->get_new_container_index();
2757 while (i < _values.size())
2759 this->leaf_container_vec[leaf_index].push_back(_values[i]);
2765 uint64_t threshold = LEAF_CONTAINER_MAX_SIZE * 2;
2766 uint64_t counter = _values.size();
2768 while (i < _values.size())
2771 if (counter >= threshold)
2773 d = LEAF_CONTAINER_MAX_SIZE;
2775 else if (counter > LEAF_CONTAINER_MAX_SIZE)
2777 d = LEAF_CONTAINER_MAX_SIZE / 2;
2783 assert(counter >= (LEAF_CONTAINER_MAX_SIZE / 2));
2785 uint64_t leaf_index = this->get_new_container_index();
2786 for (uint64_t j = 0; j < d; j++)
2788 this->leaf_container_vec[leaf_index].push_back(_values[i]);
2805 void build_from_leaf_containers(std::vector<LEAF_CONTAINER> &_leaf_containers)
2809 this->leaf_container_vec.swap(_leaf_containers);
2810 if (USE_PARENT_FIELD)
2812 this->parent_vec.resize(this->leaf_container_vec.size(),
nullptr);
2815 std::vector<Node *> layer = this->build_from_leaf_containers_sub1();
2816 uint64_t current_height = 2;
2817 while (layer.size() > 0)
2819 std::vector<Node *> next_layer = this->build_from_leaf_containers_sub2(layer, current_height);
2820 layer.swap(next_layer);