b-tree-plus-alpha
Loading...
Searching...
No Matches
plain_spsi_container.hpp
1#pragma once
2#include "../bp_tree/bp_tree.hpp"
3
4namespace stool
5{
6 namespace bptree
7 {
14 {
15 std::vector<uint64_t> items;
16
17 public:
19 {
20 }
21 PlainSPSIContainer(std::vector<uint64_t> &_items)
22 {
23 for(uint64_t x : _items){
24 this->items.push_back(x);
25 }
26 }
27
28
29 uint64_t size() const
30 {
31 return this->items.size();
32 }
33 uint64_t size_in_bytes() const
34 {
35 return sizeof(std::vector<uint64_t>) + (this->items.capacity() * sizeof(uint64_t));
36 }
37
38 uint64_t at(uint64_t pos) const
39 {
40 return this->items[pos];
41 }
42 void print() const
43 {
44 }
45
46 void clear()
47 {
48 this->items.clear();
49 this->items.shrink_to_fit();
50 }
51 void swap(PlainSPSIContainer &item)
52 {
53 this->items.swap(item.items);
54 }
55 static std::string name()
56 {
57 return "plain integers";
58 }
59 uint64_t psum(uint64_t i) const noexcept
60 {
61 uint64_t _sum = 0;
62 assert(i < this->items.size());
63
64 for (uint64_t j = 0; j < this->items.size(); j++)
65 {
66 _sum += this->items[j];
67
68 if (j == i)
69 {
70 // stool::psum_count += i;
71 return _sum;
72 }
73 }
74 return _sum;
75 }
76 uint64_t psum() const noexcept
77 {
78 if (this->items.size() == 0)
79 {
80 return 0;
81 }
82 else
83 {
84 return this->psum(this->items.size() - 1);
85 }
86 }
87 int64_t search(uint64_t x) const noexcept
88 {
89 uint64_t _sum = 0;
90 for (uint64_t i = 0; i < this->items.size(); i++)
91 {
92 _sum += this->items[i];
93
94 if (_sum >= x)
95 {
96 // stool::search_count += i;
97
98 return i;
99 }
100 }
101 return -1;
102 }
103
104 std::string to_string() const
105 {
106 std::string s;
107 s.push_back('[');
108 for (uint64_t i = 0; i < this->items.size(); i++)
109 {
110 s += std::to_string(this->items[i]);
111 if (i + 1 < this->items.size())
112 {
113 s += ", ";
114 }
115 }
116 s.push_back(']');
117 return s;
118 }
119 std::vector<uint64_t> to_value_vector() const
120 {
121 std::vector<uint64_t> r;
122 for (uint64_t i : this->items)
123 {
124 r.push_back(i);
125 }
126 return r;
127 }
128 template<typename VEC>
129 void to_values(VEC &output_vec) const{
131 output_vec.resize(this->size());
132 for (uint64_t i = 0; i < this->size(); i++)
133 {
134 output_vec[i] = this->items[i];
135 }
136 }
137
138
139 void insert(uint64_t pos, uint64_t value)
140 {
141 assert(pos <= this->size());
142 this->items.insert(this->items.begin() + pos, value);
143 }
144 void remove(uint64_t pos)
145 {
146 assert(pos < this->size());
147
148 this->items.erase(this->items.begin() + pos);
149 }
150 void push_front(std::vector<uint64_t> &new_items)
151 {
152 std::vector<uint64_t> tmp;
153 for (uint64_t v : new_items)
154 {
155 tmp.push_back(v);
156 }
157 for (uint64_t v : this->items)
158 {
159 tmp.push_back(v);
160 }
161 this->items.swap(tmp);
162 }
163 void push_front(uint64_t new_item)
164 {
165
166 std::vector<uint64_t> tmp;
168 for (uint64_t v : this->items)
169 {
170 tmp.push_back(v);
171 }
172 this->items.swap(tmp);
173 }
174
175 void push_back(std::vector<uint64_t> &new_items)
176 {
177 for (uint64_t v : new_items)
178 {
179 this->items.push_back(v);
180 }
181 }
182 void push_back(uint64_t value)
183 {
184 this->items.push_back(value);
185 }
186 std::vector<uint64_t> pop_front(uint64_t len)
187 {
188
189 std::vector<uint64_t> tmp1, tmp2;
190 for (uint64_t i = 0; i < len; i++)
191 {
192 tmp1.push_back(this->items[i]);
193 }
194 for (uint64_t i = len; i < this->items.size(); i++)
195 {
196 tmp2.push_back(this->items[i]);
197 }
198 this->items.swap(tmp2);
199 return tmp1;
200 }
201 std::vector<uint64_t> pop_back(uint64_t len)
202 {
203
204 std::vector<uint64_t> tmp1;
205 tmp1.resize(len);
206 int64_t k = len - 1;
207 while (k >= 0)
208 {
209 assert(this->items.size() > 0);
210 tmp1[k--] = this->items[this->items.size() - 1];
211 this->items.pop_back();
212 }
213 return tmp1;
214 }
215
216 uint64_t reverse_psum(uint64_t i) const
217 {
218 size_t len = i + 1;
219 uint64_t sum = 0;
220 size_t size = this->items.size();
221 for (size_t x = 0; x < len; x++)
222 {
223 sum += this->items[size - 1 - x];
224 }
225 return sum;
226 }
227
228 uint64_t psum(uint64_t i, uint64_t j) const
229 {
230 if (i == j)
231 {
232 return this->at(i);
233 }
234 else
235 {
236 throw std::runtime_error("No implementation");
237 }
238 }
239
240 void increment(uint64_t i, int64_t delta)
241 {
242 this->items[i] += delta;
243 }
244
245 std::vector<uint64_t>::const_iterator begin() const {
246 return this->items.begin();
247 }
248
249 std::vector<uint64_t>::const_iterator end() const {
250 return this->items.end();
251 }
252
253 int64_t rank(uint64_t i, uint64_t c) const {
254 return stool::StringFunctions::rank_query(this->items, i, c);
255 }
256 int64_t rank0(uint64_t i) const {
257 return this->rank(0, i);
258 }
259 int64_t rank1(uint64_t i) const {
260 return this->rank(1, i);
261 }
262
263 int64_t select(uint64_t i, uint64_t c) const {
264 return stool::StringFunctions::select_query(this->items, i, c);
265 }
266 int64_t select0(uint64_t i) const {
267 return this->select(0, i);
268 }
269 int64_t select1(uint64_t i) const {
270 return this->select(1, i);
271 }
272 void sort_leaf_containers()
273 {
274 }
275 void verify() const{
276
277 }
278
279
280
281 };
282 }
283
284}
An implementation of B+-tree.
Definition bp_tree.hpp:24
void swap(BPTree &_tree, bool swap_linked_tree)
Swaps the contents of this B+ tree with another B+ tree.
Definition bp_tree.hpp:178
void resize(uint64_t _size, VALUE default_value)
Resizes the B+ tree to contain a specified number of elements.
Definition bp_tree.hpp:2190
uint64_t size() const
Return the number of values stored in this tree.
Definition bp_tree.hpp:316
void push_back(VALUE value)
Pushes a single value to the B+ tree.
Definition bp_tree.hpp:1849
void clear()
Clears all contents of the B+ tree and resets it to an empty state.
Definition bp_tree.hpp:219
A container stored in the BPTree of SPSI. The values of this container are stored in a vector.
Definition plain_spsi_container.hpp:14