b-tree-plus-alpha
Loading...
Searching...
No Matches
bp_internal_node.hpp
1#pragma once
2#include "stool/include/light_stool.hpp"
3
4namespace stool
5{
6 namespace bptree
7 {
8
14 template <typename LEAF_CONTAINER, typename VALUE, uint64_t MAX_DEGREE>
16 {
17
18 #if DEBUG
19 public:
20 static inline int ID_COUNTER = 0;
21 uint64_t id;
22 #endif
23
24 using DEQUE_TYPE = stool::NaiveArray<MAX_DEGREE+2>;
25 //using DEQUE_TYPE = stool::StaticArrayDeque<(MAX_DEGREE+2)*2, false>;
26 //using DEQUE_TYPE = stool::StaticArrayDeque<MAX_DEGREE+2, true>;
27 //using DEQUE_TYPE = stool::FasterStaticArrayDeque<MAX_DEGREE+2>;
28
29 private:
31 stool::SimpleDeque16<InternalNode *> children_;
32 DEQUE_TYPE children_value_count_deque_;
33 DEQUE_TYPE children_value_sum_deque_;
34 bool is_parent_of_leaves_ = false;
35
36
37 public:
39 {
40 #if DEBUG
41 this->id = ID_COUNTER++;
42 #endif
43 }
44
49
50 void initialize(const std::vector<InternalNode *> &_children, bool _is_parent_of_leaves, const std::vector<LEAF_CONTAINER> &_leaf_container_vec)
51 {
52 this->is_parent_of_leaves_ = _is_parent_of_leaves;
53 this->children_.clear();
54 for (InternalNode *node : _children)
55 {
56 this->children_.push_back(node);
57 }
58
59 this->children_value_count_deque_.clear();
60 this->children_value_sum_deque_.clear();
61 if (this->is_parent_of_leaves_)
62 {
63 for (InternalNode *child : this->children_)
64 {
65 this->children_value_count_deque_.push_back(_leaf_container_vec[(uint64_t)child].size());
66
67 uint64_t psum = _leaf_container_vec[(uint64_t)child].psum();
68 this->children_value_sum_deque_.push_back(psum);
69 }
70 }
71 else
72 {
73 for (InternalNode *child : this->children_)
74 {
75 this->children_value_count_deque_.push_back(child->get_value_count());
76
77 this->children_value_sum_deque_.push_back(child->get_value_sum());
78 }
79 }
80 }
81 void initialize(BPInternalNode *_left_node, BPInternalNode *_right_node, const std::vector<LEAF_CONTAINER> &_leaf_container_vec)
82 {
83 std::vector<InternalNode *> _children;
84 _children.push_back(_left_node);
85 _children.push_back(_right_node);
86 this->initialize(_children, false, _leaf_container_vec);
87 }
88 void initialize(uint64_t _left_node, uint64_t _right_node, const std::vector<LEAF_CONTAINER> &leaf_container_vec)
89 {
90 std::vector<InternalNode *> _children;
91 _children.push_back((InternalNode *)_left_node);
92 _children.push_back((InternalNode *)_right_node);
93 this->initialize(_children, true, leaf_container_vec);
94 }
95 void initialize(bool _is_parent_of_leaves, const std::vector<LEAF_CONTAINER> &_leaf_container_vec)
96 {
97 std::vector<InternalNode *> _children;
98 this->initialize(_children, _is_parent_of_leaves, _leaf_container_vec);
99 }
100
101 void set_parent([[maybe_unused]] InternalNode *_parent)
102 {
103 throw std::runtime_error("This function is not suppported");
104 }
106
111
112
113 const stool::SimpleDeque16<InternalNode *> &get_children() const
114 {
115 return this->children_;
116 }
117 stool::SimpleDeque16<InternalNode *> &get_children()
118 {
119 return this->children_;
120 }
121 const DEQUE_TYPE &get_value_count_deque() const
122 {
123 return this->children_value_count_deque_;
124 }
125 DEQUE_TYPE &get_value_count_deque()
126 {
127 return this->children_value_count_deque_;
128 }
129 const DEQUE_TYPE &get_value_sum_deque() const
130 {
131 return this->children_value_sum_deque_;
132 }
133 /*
134 stool::SimpleDeque16<uint64_t> &get_value_sum_deque()
135 {
136 return this->children_value_sum_deque_;
137 }
138 */
139 bool use_psum() const
140 {
141 return true;
142 }
143 bool has_parent_pointer_field() const
144 {
145 return false;
146 }
147
148 bool is_parent_of_leaves() const
149 {
150 return this->is_parent_of_leaves_;
151 }
152 uint64_t children_count() const
153 {
154 return this->children_.size();
155 }
156 InternalNode *get_child(uint64_t ith) const
157 {
158 return this->children_[ith];
159 }
160
161 uint64_t get_degree() const
162 {
163 return this->children_.size();
164 }
165 uint64_t get_height() const
166 {
167 if (this->is_parent_of_leaves())
168 {
169 return 1;
170 }
171 else
172 {
173 return this->children_[0]->get_height() + 1;
174 }
175 }
176 uint64_t size_in_bytes() const
177 {
178 return sizeof(BPInternalNode) + (this->children_.size_in_bytes(true) + this->children_value_count_deque_.size_in_bytes(true) + this->children_value_sum_deque_.size_in_bytes(true));
179 }
180 uint64_t get_value_count() const
181 {
182 return this->children_value_count_deque_.psum();
183 /*
184 uint64_t sum = 0;
185 for (uint64_t p : this->children_value_count_deque_)
186 {
187 sum += p;
188 }
189 return sum;
190 */
191 }
192 uint64_t get_value_sum() const
193 {
194 return this->children_value_sum_deque_.psum();
195
196 /*
197 uint64_t sum = 0;
198 for (uint64_t p : this->children_value_sum_deque_)
199 {
200 sum += p;
201 }
202 return sum;
203 */
204 }
205 void __increment_a_value_of_sum_deque(uint64_t pos, int64_t value){
206 #if DEBUG
207 if(value < 0){
208 if((int64_t)this->children_value_sum_deque_.at(pos) < -value){
209 std::cout << "pos: " << pos << ", value: " << value << ", sum: " << this->children_value_sum_deque_.at(pos) << std::endl;
210 throw std::runtime_error("Error: __increment_a_value_of_sum_deque");
211 }
212 }
213 #endif
214
215 this->children_value_sum_deque_.increment(pos, value);
216 }
217 void __push_back_on_sum_deque(uint64_t value){
218 this->children_value_sum_deque_.push_back(value);
219 }
220 void __push_front_on_sum_deque(uint64_t value){
221 this->children_value_sum_deque_.push_front(value);
222 }
223 void __pop_front_on_sum_deque(){
224 this->children_value_sum_deque_.pop_front();
225 }
226 void __pop_back_on_sum_deque(){
227 this->children_value_sum_deque_.pop_back();
228 }
229 uint64_t __last_item_on_sum_deque() const {
230 return this->children_value_sum_deque_[this->children_value_sum_deque_.size()-1];
231 }
232
233
234 int64_t get_index(InternalNode *node) const
235 {
236 uint64_t i = 0;
237 for (InternalNode *child : this->children_)
238 {
239 if (child == node)
240 {
241 return i;
242 }
243 else
244 {
245 i++;
246 }
247 }
248 return -1;
249 }
250 InternalNode *get_parent() const
251 {
252 throw std::runtime_error("BPInternalNode::get_parent(): this function is not supported");
253 }
254
256
261
262
263 void print_info(int message_paragraph = stool::Message::SHOW_MESSAGE) const
264 {
265 std::cout << stool::Message::get_paragraph_string(message_paragraph) << "================= NODE =================" << std::endl;
266 #if DEBUG
267 std::cout << stool::Message::get_paragraph_string(message_paragraph) << "InternalNode ID: " << this->id ;
268 #else
269 std::cout << stool::Message::get_paragraph_string(message_paragraph) << "InternalNode ID: " << (uint64_t)this;
270 #endif
271 std::cout << ", is_parent_of_leaves: " << this->is_parent_of_leaves();
272 std::cout << ", count: " << this->get_value_count();
273 std::cout << ", sum: " << this->get_value_sum() << std::endl;
274
275
276 auto count_deq_str = this->children_value_count_deque_.to_string();
277 std::cout << stool::Message::get_paragraph_string(message_paragraph) << "count_deque: " << count_deq_str << std::endl;
278 auto sum_deq_str = this->children_value_sum_deque_.to_string();
279 std::cout << stool::Message::get_paragraph_string(message_paragraph) << "sum_deque: " << sum_deq_str << std::endl;
280
281 // std::cout << "Parent: " << (uint64_t)this->parent << std::endl;
282
283 std::vector<uint64_t> vec;
284 for (uint64_t i = 0; i < this->children_.size(); i++)
285 {
286 vec.push_back((uint64_t)this->children_[i]);
287 }
288 std::cout << stool::Message::get_paragraph_string(message_paragraph) << "children: " << stool::DebugPrinter::to_integer_string(vec) << std::endl;
289
290 std::cout << stool::Message::get_paragraph_string(message_paragraph)<< "==================================" << std::endl;
291 }
292
294
299
300
301 void clear()
302 {
303 this->children_.clear();
304 this->children_value_count_deque_.clear();
305 this->children_value_sum_deque_.clear();
306 }
307
308 void increment(uint64_t child_index, int64_t count_delta, int64_t sum_delta)
309 {
310 assert(child_index < this->children_count());
311 if (count_delta != 0)
312 {
313 assert(child_index < this->children_value_count_deque_.size());
314 this->children_value_count_deque_.increment(child_index, count_delta);
315 //this->children_value_count_deque_[child_index] += count_delta;
316 }
317
318 if (sum_delta != 0)
319 {
320 assert(child_index < this->children_value_sum_deque_.size());
321 this->children_value_sum_deque_.increment(child_index, sum_delta);
322
323
324 }
325
326 }
327
328 void move_container_index(uint64_t child_index, uint64_t new_leaf_index, std::vector<LEAF_CONTAINER> &leaf_container_vec)
329 {
330 assert(this->is_parent_of_leaves_);
331 uint64_t old_leaf = (uint64_t)this->children_[child_index];
332 leaf_container_vec[new_leaf_index].swap(leaf_container_vec[old_leaf]);
333 this->children_[child_index] = (InternalNode *)new_leaf_index;
334 }
335 void insert_child(uint64_t pos, InternalNode *child, uint64_t child_count, uint64_t child_sum)
336 {
337 this->children_.insert(this->children_.begin() + pos, child);
338 this->children_value_count_deque_.insert(pos, child_count);
339 this->children_value_sum_deque_.insert(pos, child_sum);
340 }
341 void append_child(InternalNode *child, uint64_t child_count, uint64_t child_sum)
342 {
343 this->children_.push_back(child);
344 this->children_value_count_deque_.push_back(child_count);
345 this->children_value_sum_deque_.push_back(child_sum);
346 }
347
348 void remove_child(uint64_t pos)
349 {
350 this->children_.erase(this->children_.begin() + pos);
351 this->children_value_count_deque_.erase(pos);
352 this->children_value_sum_deque_.erase(pos);
353 }
354
355 std::string to_string() const{
356 std::string s;
357 #if DEBUG
358 s += "InternalNode ID: " + std::to_string(this->id);
359 #else
360 s += "InternalNode ID: " + std::to_string((uint64_t)this);
361 #endif
362 s += ", is_parent_of_leaves: " + std::to_string(this->is_parent_of_leaves());
363 s += ", count: " + std::to_string(this->get_value_count());
364 s += ", sum: " + std::to_string(this->get_value_sum());
365 return s;
366 }
367
369 };
370 }
371}
The internal node of BPTree.
Definition bp_internal_node.hpp:16