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