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.hpp"
8
9namespace stool
10{
11 namespace bptree
12 {
13
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:
33
34
38 {
39 this->tree.initialize();
40 }
41
45 DynamicSequence64(DynamicSequence64 &&) noexcept = default;
46
48
52
53
57 typename Tree::ValueForwardIterator begin() const
58 {
59 return this->tree.get_value_forward_iterator_begin();
60 }
65 {
66 return this->tree.get_value_forward_iterator_end();
67 }
68
70
74
75
84
88 uint64_t operator[](uint64_t n) const
89 {
90 return this->tree.at(n);
91 }
93
97
98
103 {
104 return this->tree;
105 }
106
110 uint64_t get_degree() const
111 {
112 return this->tree.get_max_degree_of_internal_node();
113 }
114
118 uint64_t size() const
119 {
120 return this->tree.size();
121 }
126 uint64_t size_in_bytes(bool only_dynamic_memory = false) const
127 {
128 return this->tree.size_in_bytes(only_dynamic_memory);
129 }
130
132
136
137
140 std::vector<uint64_t> to_vector() const
141 {
142 return this->tree.to_value_vector();
143 }
144
148 std::string to_string() const
149 {
150 std::stringstream ss;
151 auto vec = this->to_vector();
152 ss << stool::ConverterToString::to_integer_string(vec);
153 return ss.str();
154 }
156
160
161
166 uint64_t at(uint64_t pos) const
167 {
168 return this->tree.at(pos);
169 }
170
172
173
174
178
179
184 void set_value(uint64_t i, uint64_t v)
185 {
186 uint64_t old_v = this->at(i);
187 if (old_v < v)
188 {
189 this->increment(i, v - old_v);
190 }
191 else if (old_v > v)
192 {
193 this->decrement(i, old_v - v);
194 }
195 }
200 void increment(uint64_t i, int64_t delta)
201 {
202 this->tree.increment(i, delta);
203 }
208 void decrement(uint64_t i, int64_t delta)
209 {
210 this->tree.increment(i, -delta);
211 }
212
217 {
218 this->tree.swap(item.tree);
219 }
223 void clear()
224 {
225 this->tree.clear();
226 }
227
232 void push_back(uint64_t value)
233 {
234 this->tree.push_back(value);
235 }
240 void push_many(const std::vector<uint64_t> &items)
241 {
242 this->tree.push_many(items);
243 }
248 void push_front(uint64_t value)
249 {
250 this->tree.push_front(value);
251 }
256 void pop_back()
257 {
258 this->tree.remove(this->size() - 1);
259 }
265 {
266 this->tree.remove(0);
267 }
268
273 void insert(uint64_t pos, uint64_t value)
274 {
275 assert(pos <= this->size());
276 this->tree.insert(pos, value, value);
277 }
278
283 void remove(uint64_t pos)
284 {
285 this->tree.remove(pos);
286 }
287
289
293
294
298 std::vector<std::string> get_memory_usage_info(int message_paragraph = stool::Message::SHOW_MESSAGE) const
299 {
300 std::vector<std::string> log1 = this->tree.get_memory_usage_info(message_paragraph + 1);
301
302 std::vector<std::string> r;
303 r.push_back(stool::Message::get_paragraph_string(message_paragraph) + "=DynamicSequence64: " + std::to_string(this->size_in_bytes()) + " bytes =");
304 for (std::string &s : log1)
305 {
306 r.push_back(s);
307 }
308 r.push_back(stool::Message::get_paragraph_string(message_paragraph) + "==");
309 return r;
310 }
314 void print_info() const
315 {
316 auto vec = this->to_vector();
317 std::cout << "================== SPSI ==================" << std::endl;
318 stool::DebugPrinter::print_integers(vec, "values");
319
320 std::cout << "================== SPSI[END] =============" << std::endl;
321
322 }
323
328 void print_memory_usage(int message_paragraph = stool::Message::SHOW_MESSAGE) const
329 {
330 std::vector<std::string> log = this->get_memory_usage_info(message_paragraph);
331 for (std::string &s : log)
332 {
333 std::cout << s << std::endl;
334 }
335 }
340 void print_statistics(int message_paragraph = stool::Message::SHOW_MESSAGE) const
341 {
342 std::cout << stool::Message::get_paragraph_string(message_paragraph) << "Statistics(DynamicSequence64):" << std::endl;
343 this->tree.print_statistics(message_paragraph + 1);
344 std::cout << stool::Message::get_paragraph_string(message_paragraph) << "[END]" << std::endl;
345 }
346
347
351 void verify() const
352 {
353 this->tree.verify();
354 }
355
357
361
362
365 static DynamicSequence64 build(const std::vector<uint64_t> &items)
366 {
368 r.tree.initialize();
369 r.tree.build(items);
370 return r;
371 }
375 static DynamicSequence64 load_from_bytes(const std::vector<uint8_t> &data, uint64_t &pos)
376 {
378 Tree tree = Tree::load_from_bytes(data, pos);
379 r.tree.swap(tree, false);
380 return r;
381 }
382
386 static DynamicSequence64 load_from_file(std::ifstream &ifs)
387 {
389 Tree tree = Tree::load_from_file(ifs);
390 r.tree.swap(tree, false);
391 return r;
392 }
393
397 static void store_to_bytes(DynamicSequence64 &item, std::vector<uint8_t> &output, uint64_t &pos)
398 {
399 Tree::store_to_bytes(item.tree, output, pos);
400 }
401
405 static void store_to_file(DynamicSequence64 &item, std::ofstream &os)
406 {
407 Tree::store_to_file(item.tree, os);
408 }
409
411
415
416
419 static std::string name()
420 {
421 std::string s;
422 s += "SPSI(";
423 s += VLCDeque::name();
424 s += ")";
425 return s;
426 }
428 };
429
430 using SimpleDynamicSequence64 = DynamicSequence64<stool::NaiveFLCVector<>, 62, 256>;
431 }
432}
A pointer to a node of BPTree [Unchecked AI's Comment].
Definition bp_node_pointer.hpp:14
An implementation of a B+-tree for storing n values S[0..n-1] of type VALUE in leaves.
Definition bp_tree.hpp:29
std::vector< VALUE > to_value_vector() const
Return the values S[0..n-1] as a vector of VALUE.
Definition bp_tree.hpp:403
void build(const std::vector< VALUE > &_values)
Builds the B+ tree so that S[0..n-1] = _values[0..n-1].
Definition bp_tree.hpp:1491
ValueForwardIterator get_value_forward_iterator_end() const
Return an iterator pointing to the end of the values S[0..n-1].
Definition bp_tree.hpp:223
void insert(uint64_t i, VALUE v, uint64_t weight_w)
Insert a given value v with weight w at position i in the sequence S.
Definition bp_tree.hpp:1275
static void store_to_bytes(BPTree &item, std::vector< uint8_t > &output, uint64_t &pos)
Save the given instance item to a byte vector output at the position pos.
Definition bp_tree.hpp:1554
static void store_to_file(BPTree &item, std::ofstream &os)
Save the given instance item to a file stream os.
Definition bp_tree.hpp:1581
void print_statistics(int message_paragraph=stool::Message::SHOW_MESSAGE) const
Print the statistics of this data structure.
Definition bp_tree.hpp:971
void increment(uint64_t i, int64_t delta)
Update S[i] and its weight using a given value delta and the increment function supported by the LEAF...
Definition bp_tree.hpp:1332
ValueForwardIterator get_value_forward_iterator_begin() const
Return an iterator pointing to the first value in the values S[0..n-1].
Definition bp_tree.hpp:207
void swap(BPTree &_tree, bool swap_linked_tree=true)
Swap operation.
Definition bp_tree.hpp:1087
static BPTree load_from_file(std::ifstream &ifs)
Returns the BPTree instance loaded from a file stream ifs.
Definition bp_tree.hpp:1536
VALUE at(uint64_t i) const
Return B[i].
Definition bp_tree.hpp:440
void push_back(VALUE value)
Push a given value to the end of the sequence S.
Definition bp_tree.hpp:1154
uint64_t size_in_bytes(bool only_dynamic_memory=false) const
Returns the total memory size of this B+-tree.
Definition bp_tree.hpp:377
void initialize()
Initializes this instance.
Definition bp_tree.hpp:1063
std::vector< std::string > get_memory_usage_info(int message_paragraph=stool::Message::SHOW_MESSAGE) const
Return the memory usage information of this data structure as a vector of strings.
Definition bp_tree.hpp:789
uint64_t size() const
Return the number of values stored in this tree (i.e., n).
Definition bp_tree.hpp:282
bool verify() const
Verify the internal consistency of this data structure.
Definition bp_tree.hpp:1040
void push_front(VALUE value)
Push a given value to the beginning of the sequence S.
Definition bp_tree.hpp:1189
static BPTree load_from_bytes(const std::vector< uint8_t > &data, uint64_t &pos)
Returns the BPTree instance loaded from a byte vector data at the position pos.
Definition bp_tree.hpp:1515
void remove(uint64_t i)
Remove S[i] from the sequence S.
Definition bp_tree.hpp:1255
void push_many(const std::vector< VALUE > &values_Q)
Add a given sequence Q[0..k-1] to the end of S[0..n-1] (i.e., S = S[0..n-1]Q[0..k-1])
Definition bp_tree.hpp:1164
void clear()
Clear the B+-tree and reset it to an empty state (i.e., n = 0, x = 0, y = 0).
Definition bp_tree.hpp:1110
uint64_t get_max_degree_of_internal_node() const
Return MAX_DEGREE.
Definition bp_tree.hpp:267
The forward iterator of the values stored in BPTree [Unchecked AI's Comment].
Definition bp_value_forward_iterator.hpp:16
A dynamic data structure that maintains a sequence of 64-bit non-negative integers S[0....
Definition dynamic_sequence64.hpp:21
Tree::ValueForwardIterator begin() const
Returns an iterator to the beginning of the sequence S.
Definition dynamic_sequence64.hpp:57
static DynamicSequence64 load_from_file(std::ifstream &ifs)
Returns the DynamicPrefixSum instance loaded from a file stream ifs.
Definition dynamic_sequence64.hpp:386
std::string to_string() const
Return S as a string.
Definition dynamic_sequence64.hpp:148
Tree::ValueForwardIterator end() const
Returns an iterator to the end of the sequence S.
Definition dynamic_sequence64.hpp:64
uint64_t size() const
Return |S|.
Definition dynamic_sequence64.hpp:118
DynamicSequence64(DynamicSequence64 &&) noexcept=default
Default move constructor.
static DynamicSequence64 load_from_bytes(const std::vector< uint8_t > &data, uint64_t &pos)
Returns the DynamicSequence64 instance loaded from a byte vector data at the position pos.
Definition dynamic_sequence64.hpp:375
static DynamicSequence64 build(const std::vector< uint64_t > &items)
Build a new DynamicSequence64 from a given sequence items.
Definition dynamic_sequence64.hpp:365
void push_back(uint64_t value)
Add a given integer to the end of S.
Definition dynamic_sequence64.hpp:232
void push_front(uint64_t value)
Add a given value to the beginning of S.
Definition dynamic_sequence64.hpp:248
void print_info() const
Print the statistics of this data structure.
Definition dynamic_sequence64.hpp:314
void remove(uint64_t pos)
Remove the element at the position pos from S and return it.
Definition dynamic_sequence64.hpp:283
DynamicSequence64 & operator=(const DynamicSequence64 &)=delete
Deleted copy assignment operator.
void push_many(const std::vector< uint64_t > &items)
Add a given sequence Q[0..k-1] to the end of S[0..n-1] (i.e., S = S[0..n-1]Q[0..k-1])
Definition dynamic_sequence64.hpp:240
DynamicSequence64()
Default constructor with |S| = 0.
Definition dynamic_sequence64.hpp:37
void verify() const
Verify the internal consistency of this data structure.
Definition dynamic_sequence64.hpp:351
void set_value(uint64_t i, uint64_t v)
Set a given value v at a given position i in S.
Definition dynamic_sequence64.hpp:184
DynamicSequence64 & operator=(DynamicSequence64 &&) noexcept=default
Default move assignment operator.
void clear()
Clear the elements in S.
Definition dynamic_sequence64.hpp:223
void swap(DynamicSequence64 &item)
Swap operation.
Definition dynamic_sequence64.hpp:216
uint64_t get_degree() const
Return the maximum degree of internal nodes of the internal tree storing the sequence S.
Definition dynamic_sequence64.hpp:110
void decrement(uint64_t i, int64_t delta)
Set the value S[i-delta] at a given position i in S.
Definition dynamic_sequence64.hpp:208
void increment(uint64_t i, int64_t delta)
Set the value S[i+delta] at a given position i in S.
Definition dynamic_sequence64.hpp:200
uint64_t at(uint64_t pos) const
Return the (i+1)-th element of S[i].
Definition dynamic_sequence64.hpp:166
void insert(uint64_t pos, uint64_t value)
Insert a given integer value into S as the (pos+1)-th element.
Definition dynamic_sequence64.hpp:273
void print_memory_usage(int message_paragraph=stool::Message::SHOW_MESSAGE) const
Print the memory usage information of this data structure.
Definition dynamic_sequence64.hpp:328
static void store_to_file(DynamicSequence64 &item, std::ofstream &os)
Save the given instance item to a file stream os.
Definition dynamic_sequence64.hpp:405
void pop_back()
Remove the last element from S.
Definition dynamic_sequence64.hpp:256
void print_statistics(int message_paragraph=stool::Message::SHOW_MESSAGE) const
Print the statistics of this data structure.
Definition dynamic_sequence64.hpp:340
std::vector< uint64_t > to_vector() const
Return S as a vector.
Definition dynamic_sequence64.hpp:140
Tree & __get_tree()
Get the internal tree storing the sequence S.
Definition dynamic_sequence64.hpp:102
uint64_t size_in_bytes(bool only_dynamic_memory=false) const
Returns the total memory usage in bytes.
Definition dynamic_sequence64.hpp:126
static std::string name()
Return the name of the DynamicPrefixSum for debugging.
Definition dynamic_sequence64.hpp:419
std::vector< std::string > get_memory_usage_info(int message_paragraph=stool::Message::SHOW_MESSAGE) const
Return the memory usage information of this data structure as a vector of strings.
Definition dynamic_sequence64.hpp:298
static void store_to_bytes(DynamicSequence64 &item, std::vector< uint8_t > &output, uint64_t &pos)
Save the given instance item to a byte vector output at the position pos.
Definition dynamic_sequence64.hpp:397
void pop_front()
Remove the first element from S.
Definition dynamic_sequence64.hpp:264