b-tree-plus-alpha
Loading...
Searching...
No Matches
bit_forward_iterator.hpp
1#pragma once
2#include "../bp_tree/bp_tree.hpp"
3#include "./bit_container.hpp"
4
5namespace stool
6{
7 namespace bptree
8 {
9
14 template <typename CONTAINER, typename CONTAINER_ITERATOR, uint64_t MAX_TREE_DEGREE, uint64_t MAX_BIT_CONTAINER_SIZE>
16 {
18 using T = uint64_t;
20 using LeafForwardIterator = typename Tree::LeafForwardIterator;
21
22 public:
23 LeafForwardIterator _lf_iterator;
24 Tree *tree = nullptr;
25 uint64_t bits;
26 CONTAINER_ITERATOR container_iterator;
27 bool container_iterator_end_flag = true;
28 // uint64_t bits;
29 // int16_t index_in_container = 0;
30
31 using iterator_category = std::bidirectional_iterator_tag;
32 using difference_type = std::ptrdiff_t;
33
35 {
36 }
37
38 BitForwardIterator(LeafForwardIterator *_root, const Tree *_tree)
39 {
40 this->tree = const_cast<Tree *>(_tree);
41 if (_root != nullptr)
42 {
43 _root->copy_to(this->_lf_iterator);
44
45 if (this->_lf_iterator.is_end())
46 {
47 this->container_iterator_end_flag = true;
48 }
49 else
50 {
51 uint64_t leaf_index = *this->_lf_iterator;
53 this->container_iterator_end_flag = false;
54 this->container_iterator = container.begin();
55 ++(*this);
56 }
57 }
58 else
59 {
60 this->_lf_iterator.idx = UINT64_MAX;
61 }
62 }
63
68
69
70 bool is_end() const
71 {
72 return this->_lf_iterator.is_end() && this->container_iterator_end_flag;
73 }
74 int compare(const BitForwardIterator &other) const
75 {
76
77 if (this->_lf_iterator.idx != other._lf_iterator.idx)
78 {
79 if (this->_lf_iterator.idx < other._lf_iterator.idx)
80 {
81 return -1;
82 }
83 else
84 {
85 return 1;
86 }
87 }
88 else
89 {
90 if (this->container_iterator != other.container_iterator)
91 {
92 if (this->container_iterator < other.container_iterator)
93 {
94 return -1;
95 }
96 else
97 {
98 return 1;
99 }
100 }
101 else
102 {
103 bool b1 = this->container_iterator_end_flag;
104 bool b2 = other.container_iterator_end_flag;
105 if(b1 != b2){
106 if(b1){
107 return -1;
108 }else{
109 return 1;
110 }
111 }else{
112 return 0;
113 }
114 }
115 }
116 }
117
119
123
124 uint64_t operator*() const
125 {
126 return this->bits;
127 }
128
129 BitForwardIterator &operator++()
130 {
133
134
135 if (this->is_end())
136 {
137 throw std::invalid_argument("Error: BitForwardIterator::operator++()");
138 }
139
140
141 if (this->_lf_iterator.is_end() && !this->container_iterator_end_flag)
142 {
143 this->container_iterator_end_flag = true;
144 this->bits = 0;
145 return *this;
146 }
147 else
148 {
150 {
151 if (!this->container_iterator_end_flag)
152 {
153
154 uint64_t xbits_size = std::min((uint64_t)this->container_iterator.get_size() - this->container_iterator.index, 64ULL);
155
156 uint64_t xbits = (this->container_iterator.read_64bits_string() >> (64 - xbits_size)) << (64 - xbits_size);
157 assert(xbits_size >=1);
158
159 assert(xbits_size <= 64);
161
162 if (current_bit_size + xbits_size >= 64)
163 {
165 this->container_iterator += read_bit_size;
167 }
168 else
169 {
170 this->container_iterator += xbits_size;
172 }
173
174 if (this->container_iterator.is_end())
175 {
176 this->container_iterator_end_flag = true;
177 }
178 }
179 else if (!this->_lf_iterator.is_end())
180 {
181
182 ++this->_lf_iterator;
183
184 this->container_iterator_end_flag = false;
185 if (!this->_lf_iterator.is_end())
186 {
187 uint64_t leaf_index = *this->_lf_iterator;
189 this->container_iterator = container.begin();
190
191 }
192 else
193 {
194
195 break;
196 }
197 }
198 else
199 {
200 throw std::invalid_argument("Error: BitForwardIterator::operator++()");
201 }
202 }
203 this->bits = current_bits;
204 return *this;
205 }
206 }
207 bool operator==(const BitForwardIterator &other) const { return this->compare(other) == 0; }
208 bool operator!=(const BitForwardIterator &other) const { return this->compare(other) != 0; }
209 bool operator<(const BitForwardIterator &other) const { return this->compare(other) < 0; }
210 bool operator>(const BitForwardIterator &other) const { return this->compare(other) > 0; }
211 bool operator<=(const BitForwardIterator &other) const { return this->compare(other) <= 0; }
212 bool operator>=(const BitForwardIterator &other) const { return this->compare(other) >= 0; }
213
215 };
216 }
217}
An implementation of B+-tree.
Definition bp_tree.hpp:24
const LEAF_CONTAINER & get_leaf_container(uint64_t leaf_index) const
Returns the leaf container at position leaf_index.
Definition bp_tree.hpp:656
A forward iterator for traversing the bits stored in a BP-tree.
Definition bit_forward_iterator.hpp:16