b-tree-plus-alpha
Loading...
Searching...
No Matches
dynamic_sequence64.hpp
Go to the documentation of this file.
1
6#pragma once
7#include "../bp_tree/bp_tree.hpp"
8
9namespace stool
10{
11 namespace bptree
12 {
19 template <typename LEAF_CONTAINER = stool::NaiveFLCVector<>, uint64_t TREE_DEGREE = 62, uint64_t LEAF_CONTAINER_MAX_SIZE = 256>
21 {
22 public:
25
26 private:
27 Tree tree;
28
29 public:
35 {
36 return this->tree.get_value_forward_iterator_begin();
37 }
43 {
44 return this->tree.get_value_forward_iterator_end();
45 }
46
51 {
52 this->tree.initialize();
53 }
59 {
60 this->tree.swap(item.tree);
61 }
62
63 DynamicSequence64 &operator=(const DynamicSequence64 &) = delete;
66
67 public:
71 void clear()
72 {
73 this->tree.clear();
74 }
78 void verify() const
79 {
80 this->tree.verify();
81 }
87 {
88 return this->tree;
89 }
95 {
96 return this->tree.get_max_degree_of_internal_node();
97 }
98
105 static DynamicSequence64 build(const std::vector<uint64_t> &items)
106 {
108 r.tree.initialize();
109 r.tree.build(items);
110 return r;
111 }
112
118 {
119 return this->tree.size();
120 }
126 {
127 this->tree.push_back(value);
128 }
129
130
137 {
139 this->tree.insert(pos, value, value);
140 }
146 {
147 this->tree.remove(pos);
148 }
153 std::vector<uint64_t> to_vector() const
154 {
155 return this->tree.to_value_vector();
156 }
157
164 {
165 return this->tree.at(pos);
166 }
173 {
174 uint64_t v = this->at(pos);
175 if (v < value)
176 {
177 this->increment(pos, value - v);
178 }
179 else if (v > value)
180 {
181 this->decrement(pos, v - value);
182 }
183 }
190 {
191 this->tree.increment(i, delta);
192 }
199 {
200 this->tree.increment(i, -delta);
201 }
202
207 static std::string name()
208 {
209 std::string s;
210 s += "SPSI(";
211 s += VLCDeque::name();
212 s += ")";
213 return s;
214 }
215 void push_front(uint64_t value)
216 {
217 this->tree.push_front(value);
218 }
219 void pop_back()
220 {
221 this->tree.remove(this->size() - 1);
222 }
223 void pop_front()
224 {
225 this->tree.remove(0);
226 }
227
228
233 void push_many(const std::vector<uint64_t> &items)
234 {
235 this->tree.push_many(items);
236 }
242 {
243 return this->tree.size_in_bytes(only_extra_bytes);
244 }
250 std::vector<std::string> get_memory_usage_info(int message_paragraph = stool::Message::SHOW_MESSAGE) const
251 {
252 std::vector<std::string> log1 = this->tree.get_memory_usage_info(message_paragraph + 1);
253
254 std::vector<std::string> r;
255 r.push_back(stool::Message::get_paragraph_string(message_paragraph) + "=DynamicSequence64: " + std::to_string(this->size_in_bytes()) + " bytes =");
256 for (std::string &s : log1)
257 {
258 r.push_back(s);
259 }
260 r.push_back(stool::Message::get_paragraph_string(message_paragraph) + "==");
261 return r;
262 }
263 void print_info() const {
264 this->print_statistics();
265 }
270 void print_memory_usage(int message_paragraph = stool::Message::SHOW_MESSAGE) const
271 {
272 std::vector<std::string> log = this->get_memory_usage_info(message_paragraph);
273 for (std::string &s : log)
274 {
275 std::cout << s << std::endl;
276 }
277 }
282 void print_statistics(int message_paragraph = stool::Message::SHOW_MESSAGE) const
283 {
284 std::cout << stool::Message::get_paragraph_string(message_paragraph) << "Statistics(DynamicSequence64):" << std::endl;
285 this->tree.print_statistics(message_paragraph + 1);
286 std::cout << stool::Message::get_paragraph_string(message_paragraph) << "[END]" << std::endl;
287 }
288
295 static void store_to_bytes(DynamicSequence64 &item, std::vector<uint8_t> &output, uint64_t &pos)
296 {
298 }
304 static void store_to_file(DynamicSequence64 &item, std::ofstream &os)
305 {
307 }
308
315 static DynamicSequence64 load_from_bytes(const std::vector<uint8_t> &data, uint64_t &pos)
316 {
318 r.tree.load_from_bytes(data, pos);
319 return r;
320 }
321
327 static DynamicSequence64 load_from_file(std::ifstream &ifs)
328 {
330 r.tree.load_from_file(ifs);
331 return r;
332 }
333 static DynamicSequence64 load(std::ifstream &ifs)
334 {
336 }
343 {
344 return this->tree.at(n);
345 }
350 std::string to_string() const
351 {
352 std::stringstream ss;
353 auto vec = this->to_vector();
354 ss << stool::DebugPrinter::to_integer_string(vec);
355 return ss.str();
356 }
357
361 void print() const
362 {
363 auto vec = this->to_vector();
364 std::cout << "================== SPSI ==================" << std::endl;
365 stool::DebugPrinter::print_integers(vec, "values");
366
367 std::cout << "================== SPSI[END] =============" << std::endl;
368 }
369 };
370
371
372 using SimpleDynamicSequence64 = DynamicSequence64<stool::NaiveFLCVector<>, 62, 256>;
373 }
374}
An implementation of B+-tree [Unchecked AI's Comment].
Definition bp_tree.hpp:24
std::vector< VALUE > to_value_vector() const
Converts the B+ tree to a vector of values.
Definition bp_tree.hpp:684
void build(const std::vector< VALUE > &_values)
Builds a complete B+ tree from a vector of values.
Definition bp_tree.hpp:2858
ValueForwardIterator get_value_forward_iterator_end() const
Returns an iterator pointing to the end of forward value traversal.
Definition bp_tree.hpp:423
void print_statistics(int message_paragraph=stool::Message::SHOW_MESSAGE) const
Prints statistical information about the B+ tree.
Definition bp_tree.hpp:3038
void increment(uint64_t i, int64_t delta)
Increments a value at a given position in the B+ tree.
Definition bp_tree.hpp:2411
ValueForwardIterator get_value_forward_iterator_begin() const
Returns an iterator pointing to the first value in forward traversal.
Definition bp_tree.hpp:404
uint64_t size_in_bytes(bool only_extra_bytes=false) const
Returns the total memory size of the B+ tree.
Definition bp_tree.hpp:930
void push_back(VALUE value)
Pushes a single value to the B+ tree.
Definition bp_tree.hpp:1851
void insert(uint64_t pos, VALUE value, uint64_t sum_delta)
Inserts a value at a specific position in the B+ tree.
Definition bp_tree.hpp:2122
void initialize()
Initializes the B+ tree with specified maximum degrees for internal nodes and leaf nodes.
Definition bp_tree.hpp:150
std::vector< std::string > get_memory_usage_info(int message_paragraph=stool::Message::SHOW_MESSAGE) const
Returns a vector of strings containing memory usage information about the B+ tree.
Definition bp_tree.hpp:1070
static void store_to_bytes(BPTree &bp, std::vector< uint8_t > &output, uint64_t &pos)
Saves the B+ tree structure to a byte vector.
Definition bp_tree.hpp:2913
uint64_t size() const
Return the number of values stored in this tree.
Definition bp_tree.hpp:316
bool verify() const
Verifies the integrity of the B+ tree.
Definition bp_tree.hpp:787
VALUE at(uint64_t pos) const
Returns the value at position pos in the B+ tree.
Definition bp_tree.hpp:574
void push_front(VALUE value)
Inserts a value at the front of the B+ tree.
Definition bp_tree.hpp:1868
void remove(uint64_t pos)
Removes a value at a specific position in the B+ tree.
Definition bp_tree.hpp:2088
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 push_many(std::vector< LEAF_CONTAINER > &containers)
Pushes multiple leaf containers into the B+ tree.
Definition bp_tree.hpp:1978
void clear()
Clears all contents of the B+ tree and resets it to an empty state.
Definition bp_tree.hpp:219
void load_from_file(std::ifstream &ifs)
Builds a B+ tree from serialized data in a file stream.
Definition bp_tree.hpp:2993
void load_from_bytes(const std::vector< uint8_t > &data, uint64_t &pos)
Builds a B+ tree from serialized data in a byte vector.
Definition bp_tree.hpp:2969
static void store_to_file(BPTree &bp, std::ofstream &os)
Saves the B+ tree structure to a file stream.
Definition bp_tree.hpp:2945
uint64_t get_max_degree_of_internal_node() const
Return the max degree of this tree. The number of children of every internal node does not exceed the...
Definition bp_tree.hpp:278
The forward iterator of the values stored in BPTree [Unchecked AI's Comment].
Definition bp_value_forward_iterator.hpp:17
A dynamic data structure that maintains a sequence of 64-bit non-negative integers....
Definition dynamic_sequence64.hpp:21
Tree::ValueForwardIterator begin() const
Returns an iterator to the beginning of the sequence.
Definition dynamic_sequence64.hpp:34
static DynamicSequence64 load_from_file(std::ifstream &ifs)
Builds a sequence from a file.
Definition dynamic_sequence64.hpp:327
std::string to_string() const
Converts the sequence to a string.
Definition dynamic_sequence64.hpp:350
Tree::ValueForwardIterator end() const
Returns an iterator to the end of the sequence.
Definition dynamic_sequence64.hpp:42
uint64_t size() const
Returns the number of elements in the sequence.
Definition dynamic_sequence64.hpp:117
uint64_t operator[](uint64_t n) const
Returns the value at a specified position in the sequence.
Definition dynamic_sequence64.hpp:342
static DynamicSequence64 load_from_bytes(const std::vector< uint8_t > &data, uint64_t &pos)
Builds a sequence from a vector of bytes.
Definition dynamic_sequence64.hpp:315
static DynamicSequence64 build(const std::vector< uint64_t > &items)
Builds a new sequence from a vector of items.
Definition dynamic_sequence64.hpp:105
void push_back(uint64_t value)
Adds an element to the end of the sequence.
Definition dynamic_sequence64.hpp:125
void remove(uint64_t pos)
Removes the element at a specified position from the sequence.
Definition dynamic_sequence64.hpp:145
void push_many(const std::vector< uint64_t > &items)
Adds multiple elements to the end of the sequence.
Definition dynamic_sequence64.hpp:233
DynamicSequence64()
Default constructor initializes the tree with a default degree.
Definition dynamic_sequence64.hpp:50
void set_value(uint64_t pos, uint64_t value)
Sets the value at a specified position in the sequence.
Definition dynamic_sequence64.hpp:172
void verify() const
Verifies the integrity of the sequence.
Definition dynamic_sequence64.hpp:78
void clear()
Clears all elements from the sequence.
Definition dynamic_sequence64.hpp:71
uint64_t size_in_bytes(bool only_extra_bytes=false) const
Returns the size of the sequence in bytes.
Definition dynamic_sequence64.hpp:241
void swap(DynamicSequence64 &item)
Swaps the contents of this sequence with another.
Definition dynamic_sequence64.hpp:58
uint64_t get_degree() const
Returns the degree of the tree.
Definition dynamic_sequence64.hpp:94
void decrement(uint64_t i, int64_t delta)
Decrements the value at a specified position by a given delta.
Definition dynamic_sequence64.hpp:198
void increment(uint64_t i, int64_t delta)
Increments the value at a specified position by a given delta.
Definition dynamic_sequence64.hpp:189
uint64_t at(uint64_t pos) const
Returns the value at a specified position in the sequence.
Definition dynamic_sequence64.hpp:163
void insert(uint64_t pos, uint64_t value)
Inserts a value at a specified position in the sequence.
Definition dynamic_sequence64.hpp:136
void print_memory_usage(int message_paragraph=stool::Message::SHOW_MESSAGE) const
Prints the memory usage of the sequence.
Definition dynamic_sequence64.hpp:270
static void store_to_file(DynamicSequence64 &item, std::ofstream &os)
Saves the sequence to a file.
Definition dynamic_sequence64.hpp:304
void print() const
Prints the sequence to the console.
Definition dynamic_sequence64.hpp:361
void print_statistics(int message_paragraph=stool::Message::SHOW_MESSAGE) const
Prints statistics about the sequence.
Definition dynamic_sequence64.hpp:282
std::vector< uint64_t > to_vector() const
Converts the sequence to a vector.
Definition dynamic_sequence64.hpp:153
static std::string name()
Returns the name of the sequence.
Definition dynamic_sequence64.hpp:207
std::vector< std::string > get_memory_usage_info(int message_paragraph=stool::Message::SHOW_MESSAGE) const
Returns information about the memory usage of the sequence.
Definition dynamic_sequence64.hpp:250
static void store_to_bytes(DynamicSequence64 &item, std::vector< uint8_t > &output, uint64_t &pos)
Saves the sequence to a vector of bytes.
Definition dynamic_sequence64.hpp:295
Tree & get_tree()
Returns a reference to the underlying tree.
Definition dynamic_sequence64.hpp:86