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, true, false, bptree::DEFAULT_MAX_DEGREE_OF_INTERNAL_NODE, bptree::PermutationContainer::___PermutationLeafSize>;
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, true, false, bptree::DEFAULT_MAX_DEGREE_OF_INTERNAL_NODE, bptree::DEFAULT_MAX_COUNT_OF_VALUES_IN_LEAF>;
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 auto &parent_count_deq = parent->get_value_count_deque();
26
27 parent_count_deq.decrement(parent_edge_index_of_left_node, len);
28 parent_count_deq.increment(parent_edge_index_of_left_node + 1, len);
29 }
30
31 /*
32 this->print_leaves();
33 if(this->linked_tree_ != nullptr){
34 this->linked_tree_->print_leaves();
35 }
36 */
37 for (uint64_t i = 0; i < len; i++)
38 {
39 bptree::PermutationItem item = this->leaf_container_vec[left_leaf].pop_back();
40 this->leaf_container_vec[right_leaf].push_front(item);
41 if (this->linked_tree_ != nullptr && item.pointer < this->linked_tree_->get_leaf_container_vector_size())
42 {
43 leaf_container_vec[right_leaf].update_linked_tree(0, right_leaf, left_leaf, *this->linked_tree_);
44 }
45 }
46 /*
47 std::vector<PermutationItem> items = this->leaf_container_vec[left_leaf].pop_back(len);
48 this->leaf_container_vec[right_leaf].push_front(items);
49 */
50 }
51 else
52 {
53 BPTree::BPFunctions::move_right(*left_node, *right_node, len, parent, parent_edge_index_of_left_node, this->parent_vec);
54 }
55 }
56 template <>
57 void ___PermutationTree::preprocess_for_the_exchange_of_two_leaves(uint64_t leaf_index1, uint64_t leaf_index2)
58 {
59 //std::cout << "Exchange: " << leaf_index1 << " / " << leaf_index2 << std::endl;
60
61 uint64_t dummy_index = this->leaf_container_vec.size();
62 if (this->parent_vec[leaf_index1] != nullptr)
63 {
64 bptree::PermutationContainer &leaf1 = this->leaf_container_vec[leaf_index1];
65
66 uint64_t x_idnex = this->parent_vec[leaf_index2] == nullptr ? leaf_index2 : dummy_index;
67 for (const bptree::PermutationItem &it : leaf1)
68 {
69 //std::cout << "1 Process: leaf_index1 = " << leaf_index1 << " = [" << it.pointer << ", " << (uint64_t)it.key << "]" << std::endl;
70
71 bptree::PermutationContainer &leaf3 = this->linked_tree_->get_leaf_container(it.pointer);
72 int64_t idx = leaf3.get_index(bptree::PermutationItem(leaf_index1, it.key));
73 assert(idx != -1);
74 stool::bptree::PermutationItem tmp = leaf3.at(idx);
75
76 //std::cout << "---[" << tmp.pointer << ", " << (uint64_t)tmp.key << "]" << " -> [" << x_idnex << ", " << (uint64_t)tmp.key << "]" << std::endl;
77 assert(tmp.pointer == leaf_index1);
78
79 leaf3.set_value(idx, stool::bptree::PermutationItem(x_idnex, tmp.key));
80 }
81
82 if (this->parent_vec[leaf_index2] != nullptr)
83 {
84 bptree::PermutationContainer &leaf2 = this->leaf_container_vec[leaf_index2];
85 for (const bptree::PermutationItem &it : leaf2)
86 {
87 //std::cout << "2 Process: leaf_index2 = " << leaf_index2 << " = [" << it.pointer << ", " << (uint64_t)it.key << "]" << std::endl;
88
89 bptree::PermutationContainer &leaf3 = this->linked_tree_->get_leaf_container(it.pointer);
90 int64_t idx = leaf3.get_index(bptree::PermutationItem(leaf_index2, it.key));
91 if(idx == -1){
92 this->linked_tree_->print_leaves();
93 }
94 assert(idx != -1);
95
96 stool::bptree::PermutationItem tmp = leaf3.at(idx);
97 //std::cout << "---[" << tmp.pointer << ", " << (uint64_t)tmp.key << "]" << " -> [" << leaf_index1 << ", " << (uint64_t)tmp.key << "]" << std::endl;
98
99 assert(tmp.pointer == leaf_index2);
100
101 leaf3.set_value(idx, stool::bptree::PermutationItem(leaf_index1, tmp.key));
102 }
103
104 for (const bptree::PermutationItem &it : leaf1)
105 {
106 //std::cout << "3 Process: leaf_index1 = " << leaf_index1 << " = [" << it.pointer << ", " << (uint64_t)it.key << "]" << std::endl;
107 bptree::PermutationContainer &leaf3 = this->linked_tree_->get_leaf_container(it.pointer);
108 int64_t idx = leaf3.get_index(bptree::PermutationItem(dummy_index, it.key));
109 assert(idx != -1);
110 stool::bptree::PermutationItem tmp = leaf3.at(idx);
111 //std::cout << "---[" << tmp.pointer << ", " << (uint64_t)tmp.key << "]" << " -> [" << leaf_index2 << ", " << (uint64_t)tmp.key << "]" << std::endl;
112 assert(tmp.pointer == dummy_index);
113 leaf3.set_value(idx, stool::bptree::PermutationItem(leaf_index2, tmp.key));
114 }
115 }
116 }
117 else
118 {
119 if (this->parent_vec[leaf_index2] != nullptr)
120 {
121 ___PermutationTree::preprocess_for_the_exchange_of_two_leaves(leaf_index2, leaf_index1);
122 }
123 }
124
125 }
126
127 template <>
128 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)
129 {
130 if (is_leaf)
131 {
132 uint64_t left_leaf = (uint64_t)left_node;
133 uint64_t right_leaf = (uint64_t)right_node;
134 assert(left_leaf < this->leaf_container_vec.size());
135
136 if (parent != nullptr)
137 {
138 auto &parent_count_deq = parent->get_value_count_deque();
139 parent_count_deq.decrement(parent_edge_index_of_right_node, len);
140 parent_count_deq.increment(parent_edge_index_of_right_node - 1, len);
141 }
142
143 for (uint64_t i = 0; i < len; i++)
144 {
145 bptree::PermutationItem item = this->leaf_container_vec[right_leaf].pop_front();
146 this->leaf_container_vec[left_leaf].push_back(item);
147 uint64_t _size = leaf_container_vec[left_leaf].size();
148 if (this->linked_tree_ != nullptr && item.pointer < this->linked_tree_->get_leaf_container_vector_size())
149 {
150 leaf_container_vec[left_leaf].update_linked_tree(_size - 1, left_leaf, right_leaf, *this->linked_tree_);
151 }
152 }
153
154 /*
155 std::vector<PermutationItem> items = this->leaf_container_vec[right_leaf].pop_front(len);
156 this->leaf_container_vec[left_leaf].push_back(items);
157 */
158 }
159 else
160 {
161 BPTree::BPFunctions::move_left(*left_node, *right_node, len, parent, parent_edge_index_of_right_node, this->parent_vec);
162 }
163 }
164
165}
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:2334
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:2278
The value stored in the BPTree of DynamicPermutation.
Definition permutation_item.hpp:15