b-tree-plus-alpha
Loading...
Searching...
No Matches
bp_postorder_iterator.hpp
1#pragma once
2#include "./stack_node.hpp"
3namespace stool
4{
5 namespace bptree
6 {
11 template <typename LEAF_CONTAINER, typename VALUE, uint64_t MAX_DEGREE, bool USE_PSUM>
13 {
14
15 public:
19 std::vector<SNode> _st;
20 uint64_t idx = 0;
21
22 using iterator_category = std::bidirectional_iterator_tag;
23 using difference_type = std::ptrdiff_t;
24
26 {
27 this->idx = UINT64_MAX;
28 }
29
30 BPPostorderIterator(uint64_t leaf)
31 {
32 this->_st.push_back(SNode(NodePointer::build_leaf_pointer(leaf, -1), 0, false));
33 this->idx = 0;
34 }
35
37 {
38 if (_root != nullptr)
39 {
40 BPPostorderIterator::initialize_stack(this->_st, *_root);
41 this->idx = 0;
42 }
43 else
44 {
45 this->idx = UINT64_MAX;
46 }
47 }
48
53
54
55 NodePointer get_current_node() const
56 {
57 assert(this->_st.size() != 0);
58 SNode top = this->_st[this->_st.size() - 1];
59 return top.pointer;
60 }
61
62 bool is_end() const
63 {
64 return this->idx == UINT64_MAX;
65 }
66
67 void print() const
68 {
69
70 SNode top = this->_st[this->_st.size() - 1];
71 std::cout << "[" << (uint64_t)top.pointer.get_node() << "/" << top.position << "]" << std::endl;
72 }
73
74 void copy_to(BPPostorderIterator &item) const
75 {
76 item._st.clear();
77 for (auto &it : this->_st)
78 {
79 item._st.push_back(it.copy());
80 }
81 item.idx = this->idx;
82 }
84
88
89 NodePointer operator*() const
90 {
91 assert(this->_st.size() != 0);
92 SNode top = this->_st[this->_st.size() - 1];
93 return top.pointer;
94 }
95
96 static void initialize_stack(std::vector<SNode> &st, Node &_root)
97 {
98 st.push_back(SNode(NodePointer::build_internal_node_pointer(&_root, -1), 0, false));
99 while (st.size() > 0)
100 {
101 SNode top = st[st.size() - 1];
102 st.pop_back();
103 st.push_back(SNode(top.pointer, top.position, true));
104
105 NodePointer &np = top.pointer;
106 if (np.is_leaf())
107 {
108 break;
109 }
110 else
111 {
112 Node *node = np.get_node();
113 if (node->is_parent_of_leaves())
114 {
115 NodePointer child = NodePointer::build_leaf_pointer((uint64_t)node->get_child(0), 0);
116 st.push_back(SNode(child, 0, false));
117 }
118 else
119 {
120 NodePointer child = NodePointer::build_internal_node_pointer(node->get_child(0), 0);
121 st.push_back(SNode(child, 0, false));
122 }
123 }
124 }
125 }
126 static bool proceed(std::vector<SNode> &st)
127 {
128 if (st.size() > 0)
129 {
130 st.pop_back();
131 while (st.size() > 0)
132 {
133 SNode top = st[st.size() - 1];
134
135 NodePointer &np = top.pointer;
136 if (np.is_leaf())
137 {
138 break;
139 }
140 else
141 {
142 Node *node = np.get_node();
143 if (top.checked)
144 {
145 if (top.position + 1 == node->children_count())
146 {
147 break;
148 }
149 else
150 {
151 st.pop_back();
152 st.push_back(SNode(np, top.position + 1, false));
153 }
154 }
155 else
156 {
157 st.pop_back();
158 st.push_back(SNode(np, top.position, true));
159 if (node->is_parent_of_leaves())
160 {
161 NodePointer child = NodePointer::build_leaf_pointer((uint64_t)node->get_child(top.position), top.position);
162 st.push_back(SNode(child, 0, false));
163 }
164 else
165 {
166 NodePointer child = NodePointer::build_internal_node_pointer(node->get_child(top.position), top.position);
167 st.push_back(SNode(child, 0, false));
168 }
169 }
170 }
171 }
172 if (st.size() > 0)
173 {
174 return true;
175 }
176 else
177 {
178 return false;
179 }
180 }
181 else
182 {
183 return false;
184 }
185 }
186
187 BPPostorderIterator &operator++()
188 {
189 assert(this->_st.size() > 0);
190 bool b = proceed(this->_st);
191 if (b)
192 {
193 this->idx++;
194 }
195 else
196 {
197 this->idx = UINT64_MAX;
198 }
199 return *this;
200 }
201 bool operator==(const BPPostorderIterator &other) const { return this->idx == other.idx; }
202 bool operator!=(const BPPostorderIterator &other) const { return this->idx != other.idx; }
203 bool operator<(const BPPostorderIterator &other) const { return this->idx < other.idx; }
204 bool operator>(const BPPostorderIterator &other) const { return this->idx > other.idx; }
205 bool operator<=(const BPPostorderIterator &other) const { return this->idx <= other.idx; }
206 bool operator>=(const BPPostorderIterator &other) const { return this->idx >= other.idx; }
207
209 };
210 }
211}
The internal node of BPTree [Unchecked AI's Comment].
Definition bp_internal_node.hpp:15
A pointer to a node of BPTree [Unchecked AI's Comment].
Definition bp_node_pointer.hpp:14
The iterator of a post-order traversal on BPTree [Unchecked AI's Comment].
Definition bp_postorder_iterator.hpp:13
The item of the stack for traversing BPTree [Unchecked AI's Comment].
Definition stack_node.hpp:13