Pixie
Loading...
Searching...
No Matches
bp.h
1#pragma once
2
3#include <pixie/rmm/tree.h>
4#include <pixie/tree.h>
5
6#include <cstddef>
7#include <cstdint>
8#include <utility>
9#include <vector>
10
11namespace pixie {
12
17template <typename RMMTree>
18class BPTree : public TreeBase<BPTree<RMMTree>> {
19 private:
20 const size_t num_bits_;
21 RMMTree rmm_;
22
23 public:
27 using Node = TreeNode;
28
32 explicit BPTree(const std::vector<std::uint64_t>& words, size_t tree_size)
33 : num_bits_(2 * tree_size), rmm_(words, 2 * tree_size) {}
34
38 Node root_impl() const { return Node(0, 0); }
39
43 size_t size_impl() const { return num_bits_ / 2; }
44
48 bool is_leaf_impl(const Node& node) const {
49 return (node.pos + 2 == num_bits_) or rmm_.bit(node.pos + 1) == 0;
50 }
51
55 bool is_root_impl(const Node& node) const { return node.number == 0; }
56
63 size_t degree_impl(const Node& node) const {
64 if (is_leaf_impl(node)) {
65 return 0;
66 }
68 size_t child_count = 1;
69 while (true) {
71 return child_count;
72 }
74 child_count++;
75 }
76 }
77
81 Node first_child_impl(const Node& node) const {
82 size_t pos = node.pos + 1;
83 size_t num = node.number + 1;
84 return Node(num, pos);
85 }
86
94 Node child_impl(const Node& node, size_t i) const {
96 while (i--) {
98 }
99 return child;
100 }
101
106 Node parent_impl(const Node& node) const {
107 if (node.number == 0) {
108 return root_impl();
109 }
110 size_t pos = rmm_.enclose(node.pos);
111 size_t num = rmm_.rank1(pos);
112 return Node(num, pos);
113 }
114
118 bool is_last_child_impl(const Node& node) const {
119 size_t end = rmm_.close(node.pos);
120
121 return end + 2 >= num_bits_ or rmm_.bit(end + 1) == 0;
122 }
123
127 Node next_sibling_impl(const Node& node) const {
128 size_t pos = rmm_.close(node.pos) + 1;
129 size_t num = rmm_.rank1(pos + 1) - 1;
130 return Node(num, pos);
131 }
132};
133
134std::vector<uint64_t> adj_to_bp(size_t tree_size,
135 const std::vector<std::vector<size_t>>& adj) {
136 size_t bp_size = tree_size * 2;
137 std::vector<uint64_t> bp((bp_size + 63) / 64, 0);
138 std::vector<std::pair<size_t, size_t>> stack;
139 stack.push_back(std::make_pair(0, 0));
140 size_t pos = 0;
141 bp[pos >> 6] = bp[pos >> 6] | (1ULL << (pos & 63));
142 while (!stack.empty()) {
143 auto& [v, p] = stack.back();
144 p++;
145 if (p >= adj[v].size()) {
146 pos++;
147 stack.pop_back();
148 continue;
149 }
150 pos++;
151 bp[pos >> 6] = bp[pos >> 6] | (1ULL << (pos & 63));
152 stack.push_back(std::make_pair(adj[v][p], 0));
153 }
154 return bp;
155}
156} // namespace pixie
BPTree(const std::vector< std::uint64_t > &words, size_t tree_size)
Constructor from an external array of uint64_t.
Definition bp.h:32
size_t size_impl() const
Returns the size of the tree.
Definition bp.h:43
Node first_child_impl(const Node &node) const
Returns first child of a node.
Definition bp.h:81
bool is_last_child_impl(const Node &node) const
Indicates if node is last child.
Definition bp.h:118
Node parent_impl(const Node &node) const
Returns the parent of a node if node is not root, else returns root.
Definition bp.h:106
bool is_root_impl(const Node &node) const
Indicates if node is a root.
Definition bp.h:55
Node next_sibling_impl(const Node &node) const
Returns next sibling of a node.
Definition bp.h:127
size_t degree_impl(const Node &node) const
Returns the number of children of a node this method has O(d) time complexity!
Definition bp.h:63
TreeNode Node
A node class of BP tree.
Definition bp.h:27
bool is_leaf_impl(const Node &node) const
Indicates if node is a leaf.
Definition bp.h:48
Node root_impl() const
Returns the root node.
Definition bp.h:38
Node child_impl(const Node &node, size_t i) const
Returns the i-th child of node Indexing starts at 0 this method has O(i) time complexity!
Definition bp.h:94
CRTP facade for rooted ordered trees.
Definition tree.h:43
Node child(const Node &node, std::size_t index) const
Definition tree.h:103
Logical node handle shared by succinct rooted-tree encodings.
Definition tree.h:18
std::size_t number
Logical node number in the encoding's traversal order.
Definition tree.h:20
std::size_t pos
Bit position representing the node in the succinct encoding.
Definition tree.h:23
Common interface and node handle for rooted ordered trees.