b-tree-plus-alpha
Loading...
Searching...
No Matches
bp_value_forward_iterator.hpp
1#pragma once
2#include "./bp_postorder_iterator.hpp"
3
4
5
6namespace stool
7{
8 namespace bptree
9 {
15 template <typename LEAF_CONTAINER, typename VALUE, uint64_t MAX_DEGREE>
17 {
18 private:
19 void sub_proceed()
20 {
21 while (!this->node_it.is_end() && !this->node_it.get_current_node().is_leaf())
22 {
23 ++this->node_it;
24
25 }
26
27 if (this->node_it.is_end())
28 {
29 this->tmp_values.clear();
30 this->node_it.idx = UINT64_MAX;
31 this->tmp_idx = UINT32_MAX;
32 }
33 else
34 {
35
36 this->tmp_values.clear();
37 LEAF_CONTAINER &cont = (*this->ref)[this->node_it.get_current_node().get_leaf_container_index()];
38
39 for (const VALUE it : cont)
40 {
41 this->tmp_values.push_back(it);
42 }
43 this->tmp_idx = 0;
44 }
45
46 }
47
48 public:
53
54 NodeIterator node_it;
55 std::vector<uint64_t> tmp_values;
56 std::vector<LEAF_CONTAINER> *ref = nullptr;
57 uint32_t tmp_idx;
58
59 using iterator_category = std::bidirectional_iterator_tag;
60 using difference_type = std::ptrdiff_t;
61
62 BPValueForwardIterator(NodeIterator *_root, const std::vector<LEAF_CONTAINER> *_ref)
63 {
64 this->tmp_values.clear();
65 this->tmp_idx = 0;
66 this->ref = const_cast<std::vector<LEAF_CONTAINER> *>(_ref);
67 if (_root != nullptr)
68 {
69 _root->copy_to(this->node_it);
70 if (this->node_it.is_end())
71 {
72 this->node_it.idx = UINT64_MAX;
73 this->tmp_idx = UINT32_MAX;
74 }
75 else
76 {
77 this->sub_proceed();
78 }
79 }
80 else
81 {
82 this->node_it.idx = UINT64_MAX;
83 this->tmp_idx = UINT32_MAX;
84 }
85 }
86 int compare(const BPValueForwardIterator &other) const
87 {
88 if (this->node_it.idx != other.node_it.idx)
89 {
90 if (this->node_it.idx < other.node_it.idx)
91 {
92 return -1;
93 }
94 else
95 {
96 return 1;
97 }
98 }
99 else
100 {
101 if (this->tmp_idx != other.tmp_idx)
102 {
103 if (this->tmp_idx < other.tmp_idx)
104 {
105 return -1;
106 }
107 else
108 {
109 return 1;
110 }
111 }
112 else
113 {
114 return 0;
115 }
116 }
117 }
118
119 VALUE operator*() const
120 {
121 if (this->node_it.is_end())
122 {
123 throw std::runtime_error("Error:BPValueForwardIterator");
124 }
125 else
126 {
127 return this->tmp_values[this->tmp_idx];
128 }
129 }
130 bool is_end() const
131 {
132 return this->tmp_idx == UINT32_MAX;
133 }
134
135 BPValueForwardIterator &operator++()
136 {
137 if (this->tmp_idx + 1 < this->tmp_values.size())
138 {
139 this->tmp_idx++;
140 }
141 else if (this->tmp_idx + 1 == this->tmp_values.size())
142 {
143 ++this->node_it;
144 this->sub_proceed();
145 }
146 else
147 {
148 throw std::runtime_error("Error:BPValueForwardIterator");
149 }
150 return *this;
151 }
152
153 bool operator==(const BPValueForwardIterator &other) const { return this->compare(other) == 0; }
154 bool operator!=(const BPValueForwardIterator &other) const { return this->compare(other) != 0; }
155 bool operator<(const BPValueForwardIterator &other) const { return this->compare(other) < 0; }
156 bool operator>(const BPValueForwardIterator &other) const { return this->compare(other) > 0; }
157 bool operator<=(const BPValueForwardIterator &other) const { return this->compare(other) <= 0; }
158 bool operator>=(const BPValueForwardIterator &other) const { return this->compare(other) >= 0; }
159 };
160 }
161}
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 forward iterator of the values stored in BPTree.
Definition bp_value_forward_iterator.hpp:17
The item of the stack for traversing BPTree.
Definition stack_node.hpp:14