b-tree-plus-alpha
Loading...
Searching...
No Matches
bp_tree_template.hpp
1#pragma once
2#include "./permutation_container.hpp"
3#include "./bp_internal_node_template.hpp"
4
5namespace stool
6{
7
8 using ___PermutationTree = bptree::BPTree<bptree::PermutationContainer, bptree::PermutationItem, bptree::DEFAULT_MAX_DEGREE_OF_INTERNAL_NODE, bptree::PermutationContainer::___PermutationLeafSize, true, false>;
9 using ___PermutationNode = ___PermutationTree::Node;
10
11 template <>
12 void ___PermutationTree::move_values_right(___PermutationNode *left_node, ___PermutationNode *right_node, uint64_t len, bool is_leaf, ___PermutationNode *parent, int64_t parent_edge_index_of_left_node)
13 {
14 using BPTree = BPTree<bptree::PermutationContainer, bptree::PermutationItem, bptree::DEFAULT_MAX_DEGREE_OF_INTERNAL_NODE, bptree::DEFAULT_MAX_COUNT_OF_VALUES_IN_LEAF, true, false>;
15 if (is_leaf)
16 {
17 uint64_t left_leaf = (uint64_t)left_node;
18 uint64_t right_leaf = (uint64_t)right_node;
19 assert(left_leaf < this->leaf_container_vec.size());
20 assert(right_leaf < this->leaf_container_vec.size());
21
22 if (parent != nullptr)
23 {
24
25 parent->decrement_on_count_deque(parent_edge_index_of_left_node, len);
26 parent->increment_on_count_deque(parent_edge_index_of_left_node + 1, len);
27 }
28
29 /*
30 this->print_leaves();
31 if(this->linked_tree_ != nullptr){
32 this->linked_tree_->print_leaves();
33 }
34 */
35 for (uint64_t i = 0; i < len; i++)
36 {
37 bptree::PermutationItem item = this->leaf_container_vec[left_leaf].pop_back();
38 this->leaf_container_vec[right_leaf].push_front(item);
39 if (this->linked_tree_ != nullptr && item.pointer < this->linked_tree_->get_leaf_container_vector_size())
40 {
41 leaf_container_vec[right_leaf].update_linked_tree(0, right_leaf, left_leaf, *this->linked_tree_);
42 }
43 }
44 /*
45 std::vector<PermutationItem> items = this->leaf_container_vec[left_leaf].pop_back(len);
46 this->leaf_container_vec[right_leaf].push_front(items);
47 */
48 }
49 else
50 {
51 BPTree::BPFunctions::move_right(*left_node, *right_node, len, parent, parent_edge_index_of_left_node, this->parent_vec);
52 }
53 }
54 template <>
55 void ___PermutationTree::preprocess_for_the_exchange_of_two_leaves(uint64_t leaf_index1, uint64_t leaf_index2)
56 {
57 //std::cout << "Exchange: " << leaf_index1 << " / " << leaf_index2 << std::endl;
58
59 uint64_t dummy_index = this->leaf_container_vec.size();
60 if (this->parent_vec[leaf_index1] != nullptr)
61 {
62 bptree::PermutationContainer &leaf1 = this->leaf_container_vec[leaf_index1];
63
64 uint64_t x_idnex = this->parent_vec[leaf_index2] == nullptr ? leaf_index2 : dummy_index;
65 for (const bptree::PermutationItem &it : leaf1)
66 {
67 //std::cout << "1 Process: leaf_index1 = " << leaf_index1 << " = [" << it.pointer << ", " << (uint64_t)it.key << "]" << std::endl;
68
69 bptree::PermutationContainer &leaf3 = this->linked_tree_->get_leaf_container(it.pointer);
70 int64_t idx = leaf3.get_index(bptree::PermutationItem(leaf_index1, it.key));
71 assert(idx != -1);
72 stool::bptree::PermutationItem tmp = leaf3.at(idx);
73
74 //std::cout << "---[" << tmp.pointer << ", " << (uint64_t)tmp.key << "]" << " -> [" << x_idnex << ", " << (uint64_t)tmp.key << "]" << std::endl;
75 assert(tmp.pointer == leaf_index1);
76
77 leaf3.set_value(idx, stool::bptree::PermutationItem(x_idnex, tmp.key));
78 }
79
80 if (this->parent_vec[leaf_index2] != nullptr)
81 {
82 bptree::PermutationContainer &leaf2 = this->leaf_container_vec[leaf_index2];
83 for (const bptree::PermutationItem &it : leaf2)
84 {
85 //std::cout << "2 Process: leaf_index2 = " << leaf_index2 << " = [" << it.pointer << ", " << (uint64_t)it.key << "]" << std::endl;
86
87 bptree::PermutationContainer &leaf3 = this->linked_tree_->get_leaf_container(it.pointer);
88 int64_t idx = leaf3.get_index(bptree::PermutationItem(leaf_index2, it.key));
89 if(idx == -1){
90 this->linked_tree_->print_leaves();
91 }
92 assert(idx != -1);
93
94 stool::bptree::PermutationItem tmp = leaf3.at(idx);
95 //std::cout << "---[" << tmp.pointer << ", " << (uint64_t)tmp.key << "]" << " -> [" << leaf_index1 << ", " << (uint64_t)tmp.key << "]" << std::endl;
96
97 assert(tmp.pointer == leaf_index2);
98
99 leaf3.set_value(idx, stool::bptree::PermutationItem(leaf_index1, tmp.key));
100 }
101
102 for (const bptree::PermutationItem &it : leaf1)
103 {
104 //std::cout << "3 Process: leaf_index1 = " << leaf_index1 << " = [" << it.pointer << ", " << (uint64_t)it.key << "]" << std::endl;
105 bptree::PermutationContainer &leaf3 = this->linked_tree_->get_leaf_container(it.pointer);
106 int64_t idx = leaf3.get_index(bptree::PermutationItem(dummy_index, it.key));
107 assert(idx != -1);
108 stool::bptree::PermutationItem tmp = leaf3.at(idx);
109 //std::cout << "---[" << tmp.pointer << ", " << (uint64_t)tmp.key << "]" << " -> [" << leaf_index2 << ", " << (uint64_t)tmp.key << "]" << std::endl;
110 assert(tmp.pointer == dummy_index);
111 leaf3.set_value(idx, stool::bptree::PermutationItem(leaf_index2, tmp.key));
112 }
113 }
114 }
115 else
116 {
117 if (this->parent_vec[leaf_index2] != nullptr)
118 {
119 ___PermutationTree::preprocess_for_the_exchange_of_two_leaves(leaf_index2, leaf_index1);
120 }
121 }
122
123 }
124
125 template <>
126 void ___PermutationTree::move_values_left(___PermutationNode *left_node, ___PermutationNode *right_node, uint64_t len, bool is_leaf, ___PermutationNode *parent, int64_t parent_edge_index_of_right_node)
127 {
128 if (is_leaf)
129 {
130 uint64_t left_leaf = (uint64_t)left_node;
131 uint64_t right_leaf = (uint64_t)right_node;
132 assert(left_leaf < this->leaf_container_vec.size());
133
134 if (parent != nullptr)
135 {
136 parent->decrement_on_count_deque(parent_edge_index_of_right_node, len);
137 parent->increment_on_count_deque(parent_edge_index_of_right_node - 1, len);
138 }
139
140 for (uint64_t i = 0; i < len; i++)
141 {
142 bptree::PermutationItem item = this->leaf_container_vec[right_leaf].pop_front();
143 this->leaf_container_vec[left_leaf].push_back(item);
144 uint64_t _size = leaf_container_vec[left_leaf].size();
145 if (this->linked_tree_ != nullptr && item.pointer < this->linked_tree_->get_leaf_container_vector_size())
146 {
147 leaf_container_vec[left_leaf].update_linked_tree(_size - 1, left_leaf, right_leaf, *this->linked_tree_);
148 }
149 }
150
151 /*
152 std::vector<PermutationItem> items = this->leaf_container_vec[right_leaf].pop_front(len);
153 this->leaf_container_vec[left_leaf].push_back(items);
154 */
155 }
156 else
157 {
158 BPTree::BPFunctions::move_left(*left_node, *right_node, len, parent, parent_edge_index_of_right_node, this->parent_vec);
159 }
160 }
161
162}
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)
Moves values from a right node to a left node in the B+ tree.
Definition bp_tree.hpp:2336
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)
Moves values from a left node to a right node in the B+ tree.
Definition bp_tree.hpp:2282
The value stored in the BPTree of DynamicPermutation [Unchecked AI's Comment].
Definition permutation_item.hpp:15