b-tree-plus-alpha
Loading...
Searching...
No Matches
dynamic_permutation_builder.hpp
1#pragma once
2#include "./dynamic_permutation.hpp"
3
4namespace stool
5{
6 namespace bptree
7 {
14 {
15 using PostorderIterator = DynamicPermutation::Tree::PostorderIterator;
16 // PostorderIterator postorder_iterator_beg;
17 // PostorderIterator postorder_iterator_end;
18 int64_t i;
19 int64_t j;
20 int64_t inverse_leaf_index;
22 DynamicPermutation::Tree *pi_tree;
23 DynamicPermutation::Tree *inverse_pi_tree;
24 std::unordered_map<uint64_t, uint64_t> mapper;
25
26 public:
28 {
29 DynamicPermutation::Tree& pi_tree = perm.get_pi_tree();
30 DynamicPermutation::Tree& inverse_pi_tree = perm.get_inverse_pi_tree();
31
32 pi_tree.initialize();
33 inverse_pi_tree.initialize();
34 pi_tree.set_linked_tree(nullptr);
35 inverse_pi_tree.set_linked_tree(nullptr);
36
38 default_item.key = 0;
39 default_item.pointer = 0;
41 inverse_pi_tree.resize(pi_vector_size, default_item);
42
43 pi_tree.set_linked_tree(&inverse_pi_tree);
44 inverse_pi_tree.set_linked_tree(&pi_tree);
45
46 this->i = 0;
47 this->dp = &perm;
48 this->pi_tree = &perm.get_pi_tree();
49 this->inverse_pi_tree = &perm.get_inverse_pi_tree();
50
51 if (this->inverse_pi_tree->size() > 0)
52 {
53 this->j = this->inverse_pi_tree->compute_path_from_root_to_leaf(this->inverse_pi_tree->size() - 1);
54 const auto &path = this->inverse_pi_tree->get_temporary_path();
55 this->inverse_leaf_index = path[path.size() - 1].get_leaf_container_index();
56 this->mapper.clear();
57 }
58 else
59 {
60 this->j = -1;
61 }
62 }
63 void push_front(uint64_t inverse_pi_value)
64 {
65 if (this->j == -1)
66 {
67 this->j = this->inverse_pi_tree->compute_path_from_root_to_leaf(this->inverse_pi_tree->size() - 1 - this->i);
68 const auto &path = this->inverse_pi_tree->get_temporary_path();
69 this->inverse_leaf_index = path[path.size() - 1].get_leaf_container_index();
70 this->mapper.clear();
71 }
72
74 const auto &path = this->pi_tree->get_temporary_path();
75 uint64_t pi_leaf_idx = path[path.size() - 1].get_leaf_container_index();
77
78 auto f = mapper.find(pi_leaf_idx);
79 if (f == mapper.end())
80 {
81 mapper[pi_leaf_idx] = 0;
82 }
83 uint64_t key = mapper[pi_leaf_idx];
84 PermutationContainer &inv_leaf = this->inverse_pi_tree->get_leaf_container(this->inverse_leaf_index);
85
86 pi_leaf.set_value(idx, PermutationItem(this->inverse_leaf_index, key));
87 inv_leaf.set_value(this->j, PermutationItem(pi_leaf_idx, key));
88
89 mapper[pi_leaf_idx] = key + 1;
90 this->i++;
91 this->j--;
92 }
93 void finish()
94 {
95 if (this->i != (int64_t)this->pi_tree->size())
96 {
97 std::cout << "Error: i: " << (this->i) << "!= pi_tree_size: " << this->pi_tree->size() << std::endl;
98 throw std::runtime_error("Error: DeynamicPermutationBuilder");
99 }
100 }
101 };
102 }
103}
An implementation of B+-tree.
Definition bp_tree.hpp:24
const LEAF_CONTAINER & get_leaf_container(uint64_t leaf_index) const
Returns the leaf container at position leaf_index.
Definition bp_tree.hpp:656
void initialize()
Initializes the B+ tree with specified maximum degrees for internal nodes and leaf nodes.
Definition bp_tree.hpp:150
const std::vector< NodePointer > & get_temporary_path() const
Returns the temporary path used during tree operations.
Definition bp_tree.hpp:270
int64_t compute_path_from_root_to_leaf(uint64_t i) const
Computes the path from the root to the leaf at position i.
Definition bp_tree.hpp:1207
void resize(uint64_t _size, VALUE default_value)
Resizes the B+ tree to contain a specified number of elements.
Definition bp_tree.hpp:2190
uint64_t size() const
Return the number of values stored in this tree.
Definition bp_tree.hpp:316
void clear()
Clears all contents of the B+ tree and resets it to an empty state.
Definition bp_tree.hpp:219
void set_linked_tree(BPTree *_tree)
Sets the linked B+ tree.
Definition bp_tree.hpp:2243
A builder of DynamicPermutation.
Definition dynamic_permutation_builder.hpp:14
A dynamic data structure storing a permutation.
Definition dynamic_permutation.hpp:14
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