b-tree-plus-alpha
Loading...
Searching...
No Matches
bp_internal_node_template.hpp
1#pragma once
2#include "../bp_tree/bp_internal_node.hpp"
3#include "./permutation_container.hpp"
4
5namespace stool
6{
7 namespace bptree
8 {
9
14 template <uint64_t MAX_DEGREE>
16 {
17 #if DEBUG
18 public:
19 static inline int ID_COUNTER = 0;
20 uint64_t id;
21 #endif
22
23 private:
25 stool::SimpleDeque16<InternalNode *> children_;
26 stool::NaiveArray<MAX_DEGREE+2> children_value_count_deque_;
27 InternalNode *parent_ = nullptr;
28 bool is_parent_of_leaves_ = false;
29
30 public:
32 {
33 #if DEBUG
34 this->id = ID_COUNTER++;
35 #endif
36 }
37
42
43 void initialize(const std::vector<InternalNode *> _children, bool _is_parent_of_leaves, const std::vector<stool::bptree::PermutationContainer> &_leaf_container_vec)
44 {
45 this->is_parent_of_leaves_ = _is_parent_of_leaves;
46 this->children_.clear();
47 for (InternalNode *node : _children)
48 {
49 this->children_.push_back(node);
50 }
51
52 this->children_value_count_deque_.clear();
53 if (this->is_parent_of_leaves_)
54 {
55 for (InternalNode *child : this->children_)
56 {
57 this->children_value_count_deque_.push_back(_leaf_container_vec[(uint64_t)child].size());
58 }
59 }
60 else
61 {
62 for (InternalNode *child : this->children_)
63 {
64 this->children_value_count_deque_.push_back(child->get_value_count());
65 }
66 }
67 }
68 void set_parent(InternalNode *_parent)
69 {
70 this->parent_ = _parent;
71 }
72 void initialize(BPInternalNode *_left_node, BPInternalNode *_right_node, const std::vector<stool::bptree::PermutationContainer> &_leaf_container_vec)
73 {
74 std::vector<InternalNode *> _children;
75 _children.push_back(_left_node);
76 _children.push_back(_right_node);
77 this->initialize(_children, false, _leaf_container_vec);
78 }
79 void initialize(uint64_t _left_node, uint64_t _right_node, const std::vector<stool::bptree::PermutationContainer> &leaf_container_vec)
80 {
81 std::vector<InternalNode *> _children;
82 _children.push_back((InternalNode *)_left_node);
83 _children.push_back((InternalNode *)_right_node);
84 this->initialize(_children, true, leaf_container_vec);
85 }
86 void initialize(bool _is_parent_of_leaves, const std::vector<stool::bptree::PermutationContainer> &_leaf_container_vec)
87 {
88 std::vector<InternalNode *> _children;
89 this->initialize(_children, _is_parent_of_leaves, _leaf_container_vec);
90 }
92
97
98
99 const stool::SimpleDeque16<InternalNode *> &get_children() const
100 {
101 return this->children_;
102 }
103 stool::SimpleDeque16<InternalNode *> &get_children()
104 {
105 return this->children_;
106 }
107 const stool::NaiveArray<MAX_DEGREE+2> &get_value_count_deque() const
108 {
109 return this->children_value_count_deque_;
110 }
111 stool::NaiveArray<MAX_DEGREE+2> &get_value_count_deque()
112 {
113 return this->children_value_count_deque_;
114 }
115 const stool::NaiveArray<MAX_DEGREE+2> &get_value_sum_deque() const
116 {
117 return this->children_value_count_deque_;
118 }
119 stool::NaiveArray<MAX_DEGREE+2> &get_value_sum_deque()
120 {
121 return this->children_value_count_deque_;
122 }
123 void __increment_a_value_of_sum_deque([[maybe_unused]] uint64_t pos, [[maybe_unused]]int64_t value){
124 throw std::runtime_error("BPInternalNode<PermutationContainer, PermutationItem>::__increment_a_value_of_sum_deque(): No Implementation");
125 }
126 void __push_back_on_sum_deque([[maybe_unused]]uint64_t value){
127 throw std::runtime_error("BPInternalNode<PermutationContainer, PermutationItem>::__push_back_on_sum_deque(): No Implementation");
128 }
129 void __push_front_on_sum_deque([[maybe_unused]]uint64_t value){
130 throw std::runtime_error("BPInternalNode<PermutationContainer, PermutationItem>::__push_front_on_sum_deque(): No Implementation");
131 }
132 void __pop_front_on_sum_deque(){
133 throw std::runtime_error("BPInternalNode<PermutationContainer, PermutationItem>::__pop_front_on_sum_deque: No Implementation");
134 }
135 void __pop_back_on_sum_deque(){
136 throw std::runtime_error("BPInternalNode<PermutationContainer, PermutationItem>::__pop_back_on_sum_deque(): No Implementation");
137 }
138 uint64_t __last_item_on_sum_deque() const {
139 throw std::runtime_error("BPInternalNode<PermutationContainer, PermutationItem>::__last_item_on_sum_deque(): No Implementation");
140 }
141
142 bool use_psum() const
143 {
144 return false;
145 }
146 bool has_parent_pointer_field() const
147 {
148 return true;
149 }
150
151 bool is_parent_of_leaves() const
152 {
153 return this->is_parent_of_leaves_;
154 }
155 uint64_t children_count() const
156 {
157 return this->children_.size();
158 }
159 InternalNode *get_child(uint64_t ith) const
160 {
161 return this->children_[ith];
162 }
163
164 uint64_t get_degree() const
165 {
166 return this->children_.size();
167 }
168 uint64_t get_height() const
169 {
170 if (this->is_parent_of_leaves())
171 {
172 return 1;
173 }
174 else
175 {
176 return this->children_[0]->get_height() + 1;
177 }
178 }
179 uint64_t size_in_bytes() const
180 {
181 return sizeof(BPInternalNode<stool::bptree::PermutationContainer, stool::bptree::PermutationItem, MAX_DEGREE>) + this->children_.size_in_bytes(true) + this->children_value_count_deque_.size_in_bytes(true);
182 }
183 int64_t get_index(InternalNode *node) const
184 {
185 uint64_t i = 0;
186 for (InternalNode *child : this->children_)
187 {
188 if (child == node)
189 {
190 return i;
191 }
192 else
193 {
194 i++;
195 }
196 }
197 return -1;
198 }
199 InternalNode *get_parent() const
200 {
201 return this->parent_;
202 }
203
205
210
211
212 uint64_t get_value_count() const
213 {
214 uint64_t sum = this->children_value_count_deque_.psum();
215 /*
216 for (uint64_t p : this->children_value_count_deque_)
217 {
218 sum += p;
219 }
220 */
221 return sum;
222 }
223 uint64_t get_value_sum() const
224 {
225 return 0;
226 }
228
233
234
235 void print_info([[maybe_unused]] int message_paragraph = stool::Message::SHOW_MESSAGE) const
236 {
237 std::cout << "=================" << std::endl;
238 std::cout << "This: " << (uint64_t)this;
239 std::cout << ", parent: " << (uint64_t)this->parent_;
240 std::cout << ", is_parent_of_leaves: " << this->is_parent_of_leaves();
241 std::cout << ", count: " << this->get_value_count();
242 std::cout << ", sum: " << this->get_value_sum() << std::endl;
243
244 // std::cout << "Parent: " << (uint64_t)this->parent << std::endl;
245
246 std::vector<uint64_t> vec;
247 for (uint64_t i = 0; i < this->children_.size(); i++)
248 {
249 vec.push_back((uint64_t)this->children_[i]);
250 }
251 stool::Printer::print("child", vec);
252 std::cout << "=================" << std::endl;
253 }
254
256
261
262
263 void clear()
264 {
265 this->children_.clear();
266 this->children_value_count_deque_.clear();
267 this->parent_ = nullptr;
268 }
269
270 void increment(uint64_t child_index, int64_t count_delta, [[maybe_unused]] int64_t sum_delta)
271 {
272 this->children_value_count_deque_.increment(child_index, count_delta);
273 }
274
275 void move_container_index(uint64_t child_index, uint64_t new_leaf_index, std::vector<stool::bptree::PermutationContainer> &leaf_container_vec)
276 {
277 assert(this->is_parent_of_leaves_);
278 uint64_t old_leaf = (uint64_t)this->children_[child_index];
279 leaf_container_vec[new_leaf_index].swap(leaf_container_vec[old_leaf]);
280 this->children_[child_index] = (InternalNode *)new_leaf_index;
281 }
282 void insert_child(uint64_t pos, InternalNode *child, uint64_t child_count, [[maybe_unused]] uint64_t child_sum)
283 {
284 this->children_.insert(this->children_.begin() + pos, child);
285 this->children_value_count_deque_.insert(pos, child_count);
286 }
287 void remove_child(uint64_t pos)
288 {
289 this->children_.erase(this->children_.begin() + pos);
290 this->children_value_count_deque_.erase(pos);
291 }
292 void append_child(InternalNode *child, uint64_t child_count, [[maybe_unused]] uint64_t child_sum)
293 {
294 this->children_.push_back(child);
295 this->children_value_count_deque_.push_back(child_count);
296 }
297 std::string to_string() const{
298 std::string s;
299 #if DEBUG
300 s += "InternalNode ID: " + std::to_string(this->id);
301 #else
302 s += "InternalNode ID: " + std::to_string((uint64_t)this);
303 #endif
304 s += ", is_parent_of_leaves: " + std::to_string(this->is_parent_of_leaves());
305 s += ", count: " + std::to_string(this->get_value_count());
306
307 s += ", Count Array: ";
308 s += this->children_value_count_deque_.to_string();
309
310 return s;
311 }
312
314 };
315 }
316}
BPInternalNode for dynamic permutations.
Definition bp_internal_node_template.hpp:16
The internal node of BPTree.
Definition bp_internal_node.hpp:16
The container stored in the BPTree of DynamicPermutation.
Definition permutation_container.hpp:14
The value stored in the BPTree of DynamicPermutation.
Definition permutation_item.hpp:15