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 {
13 {
14 using PostorderIterator = DynamicPermutation::Tree::PostorderIterator;
15 // PostorderIterator postorder_iterator_beg;
16 // PostorderIterator postorder_iterator_end;
17 int64_t i;
18 int64_t j;
19 int64_t inverse_leaf_index;
21 DynamicPermutation::Tree *pi_tree;
22 DynamicPermutation::Tree *inverse_pi_tree;
23 std::unordered_map<uint64_t, uint64_t> mapper;
24
25 public:
26 void initialize(DynamicPermutation &perm, uint64_t pi_vector_size)
27 {
28 DynamicPermutation::Tree& pi_tree = perm.get_pi_tree();
29 DynamicPermutation::Tree& inverse_pi_tree = perm.get_inverse_pi_tree();
30
31 pi_tree.initialize();
32 inverse_pi_tree.initialize();
33 pi_tree.set_linked_tree(nullptr);
34 inverse_pi_tree.set_linked_tree(nullptr);
35
36 PermutationItem default_item;
37 default_item.key = 0;
38 default_item.pointer = 0;
39 pi_tree.resize(pi_vector_size, default_item);
40 inverse_pi_tree.resize(pi_vector_size, default_item);
41
42 pi_tree.set_linked_tree(&inverse_pi_tree);
43 inverse_pi_tree.set_linked_tree(&pi_tree);
44
45 this->i = 0;
46 this->dp = &perm;
47 this->pi_tree = &perm.get_pi_tree();
48 this->inverse_pi_tree = &perm.get_inverse_pi_tree();
49
50 if (this->inverse_pi_tree->size() > 0)
51 {
52 this->j = this->inverse_pi_tree->compute_path_from_root_to_leaf(this->inverse_pi_tree->size() - 1);
53 const auto &path = this->inverse_pi_tree->get_temporary_path();
54 this->inverse_leaf_index = path[path.size() - 1].get_leaf_container_index();
55 this->mapper.clear();
56 }
57 else
58 {
59 this->j = -1;
60 }
61 }
62 void push_front(uint64_t inverse_pi_value)
63 {
64 if (this->j == -1)
65 {
66 this->j = this->inverse_pi_tree->compute_path_from_root_to_leaf(this->inverse_pi_tree->size() - 1 - this->i);
67 const auto &path = this->inverse_pi_tree->get_temporary_path();
68 this->inverse_leaf_index = path[path.size() - 1].get_leaf_container_index();
69 this->mapper.clear();
70 }
71
72 uint64_t idx = this->pi_tree->compute_path_from_root_to_leaf(inverse_pi_value);
73 const auto &path = this->pi_tree->get_temporary_path();
74 uint64_t pi_leaf_idx = path[path.size() - 1].get_leaf_container_index();
75 PermutationContainer &pi_leaf = this->pi_tree->get_leaf_container(pi_leaf_idx);
76
77 auto f = mapper.find(pi_leaf_idx);
78 if (f == mapper.end())
79 {
80 mapper[pi_leaf_idx] = 0;
81 }
82 uint64_t key = mapper[pi_leaf_idx];
83 PermutationContainer &inv_leaf = this->inverse_pi_tree->get_leaf_container(this->inverse_leaf_index);
84
85 pi_leaf.set_value(idx, PermutationItem(this->inverse_leaf_index, key));
86 inv_leaf.set_value(this->j, PermutationItem(pi_leaf_idx, key));
87
88 mapper[pi_leaf_idx] = key + 1;
89 this->i++;
90 this->j--;
91 }
92 void finish()
93 {
94 if (this->i != (int64_t)this->pi_tree->size())
95 {
96 std::cout << "Error: i: " << (this->i) << "!= pi_tree_size: " << this->pi_tree->size() << std::endl;
97 throw std::runtime_error("Error: DeynamicPermutationBuilder");
98 }
99 }
100 };
101 }
102}
A builder of DynamicPermutation [Unchecked AI's Comment].
Definition dynamic_permutation_builder.hpp:13
A dynamic data structure to store a permutation Π[0..n-1] of integers 0, 1, ..., n-1.
Definition dynamic_permutation.hpp:14
Tree & get_pi_tree()
Return the internal tree storing the permutation Π.
Definition dynamic_permutation.hpp:94
Tree & get_inverse_pi_tree()
Get the internal tree storing the inverse permutation Π^{-1}.
Definition dynamic_permutation.hpp:101
The container stored in the BPTree of DynamicPermutation [Unchecked AI's Comment].
Definition permutation_container.hpp:13
The value stored in the BPTree of DynamicPermutation [Unchecked AI's Comment].
Definition permutation_item.hpp:14