26template <
typename T,
size_t _SIZE>
29 std::vector<std::pair<T, std::optional<avl_tree<T>>>> list;
36 explicit bubble() noexcept : _size(0) { }
42 template <
size_t _NEW_SIZE>
45 if(_NEW_SIZE != _SIZE) {
46 throw std::logic_error(
"Tried to copy bubbles with different sizes");
49 this->_size = t.
size();
51 for(
size_t i = 0; i<this->_size; i++){
55 catch (std::logic_error &e){
56 std::cerr << e.what() <<
'\n';
65 template <
size_t _NEW_SIZE>
68 if(_NEW_SIZE != _SIZE) {
69 throw std::logic_error(
"Tried to copy two bubbles with different sizes");
71 this->_size = t.
size();
73 for(
size_t i = 0; i<this->_size; i++){
77 catch (std::logic_error &e) {
78 std::cerr << e.what() <<
'\n';
89 template <
typename... Args>
97 template <
typename... Args>
106 bool search(
const T& key);
113 T
get_key(
const size_t& index)
const;
163 std::pair<T, std::vector<T>>
operator[] (
const size_t& index)
const {
164 assert(index < _SIZE && index >= 0);
165 if(this->list[index].second == std::nullopt) {
return {std::make_pair(this->list[index].first, std::vector<T>())}; }
166 return std::make_pair(this->list[index].first, this->list[index].second.value().inorder());
173 if(t._size == 0) {
return out; }
174 for(
auto && x : t.list) {
175 out << x.first <<
": {";
176 if(x.second == std::nullopt){
181 std::vector<T> ino = tmp_tree.
inorder();
182 for(
size_t i = 0; i<ino.size(); i++){
183 if(i == ino.size() - 1) {
187 out << ino[i] <<
" ";
196template <
typename T,
size_t _SIZE>
197template <
typename... Args>
199 auto _insert = [&](
const T&& key) ->
void {
201 this->list.push_back({key, std::nullopt});
206 std::ranges::sort(this->list, [](
const std::pair<T, std::optional<
avl_tree<T>>> &a,
const std::pair<T, std::optional<
avl_tree<T>>> &b){
207 return a.first < b.first;
211 auto it = std::lower_bound(std::ranges::begin(this->list), std::ranges::end(this->list), key, [](
const std::pair<T, std::optional<
avl_tree<T>>> &pair,
const T &key){
return pair.first < key; });
213 if(it != std::ranges::end(this->list)) {
214 int idx = std::distance(std::ranges::begin(this->list), it);
215 if(this->list[idx].first == key) {
return; }
218 if(this->list[0].second == std::nullopt) {
221 this->list[0].second.value().insert(key);
224 if(this->list[idx - 1].second == std::nullopt) {
227 this->list[idx - 1].second.value().insert(key);
231 if(this->list[this->list.size() - 1].second == std::nullopt){
232 this->list[this->list.size() - 1].second =
avl_tree<T>();
234 this->list[this->list.size() - 1].second.value().insert(key);
238 (std::invoke(_insert, std::forward<Args>(keys)), ...);
242template <
typename T,
size_t _SIZE>
243template <
typename... Args>
245 auto _remove = [&](
const T&& key) ->
void{
246 if(this->_size == 0) {
return; }
247 if(this->_size <= _SIZE) {
248 auto [begin, end] = std::ranges::remove_if(this->list, [&](
const std::pair<T, std::optional<
avl_tree<T>>> &t) {
return t.first == key; });
249 list.erase(begin, end);
253 auto it = std::lower_bound(std::ranges::begin(this->list), std::ranges::end(this->list), key, [](
const std::pair<T, std::optional<
avl_tree<T>>> &pair,
const T &key){
return pair.first < key; });
254 if(it != std::ranges::end(this->list)) {
255 size_t idx = std::ranges::distance(std::ranges::begin(this->list), it);
257 if(this->list[idx].first == key && this->list[idx].second != std::nullopt) {
258 T curr_root = this->list[idx].second.value().get_root();
259 this->list[idx].second.value().remove(curr_root);
260 this->list[idx].first = curr_root;
265 if(this->list[0].second == std::nullopt){
268 this->list[0].second.value().remove(key);
271 if(this->list[idx - 1].second == std::nullopt) {
274 this->list[idx - 1].second.value().remove(key);
278 if(this->list[this->list.size() - 1].second == std::nullopt) {
281 this->list[this->list.size() - 1].second.value().remove(key);
286 (std::invoke(_remove, std::forward<Args>(keys)), ...);
289template <
typename T,
size_t _SIZE>
291 if(this->_size == 0) {
return false; }
292 auto it = std::lower_bound(std::ranges::begin(this->list), std::ranges::end(this->list), key, [](
const std::pair<T, std::optional<
avl_tree<T>>> &pair,
const T &key){
return pair.first < key; });
293 if (it != std::ranges::end(this->list)){
294 size_t idx = std::ranges::distance(std::ranges::begin(this->list), it);
296 if(this->list[0].first == key) {
return true; }
297 if(this->list[0].second == std::nullopt) {
return false; }
298 if(this->list[0].second.value().search(key) ==
true) {
return true; }
301 if(this->list[idx].first == key) {
return true; }
302 if(this->list[idx - 1].second == std::nullopt) {
return false; }
303 if(this->list[idx - 1].second.value().search(key) ==
true) {
return true; }
307 if(this->list[this->list.size() - 1].first == key) {
return true; }
308 if(this->list[this->list.size() - 1].second == std::nullopt) {
return false; }
309 if(this->list[this->list.size() - 1].second.value().search(key) ==
true) {
return true; }
314template <
typename T,
size_t _SIZE>
316 assert(index >=0 && index < _SIZE);
317 return this->list[index].first;
320template <
typename T,
size_t _SIZE>
322 assert(index >=0 && index < _SIZE);
323 if(this->list[index].second == std::nullopt) {
326 return avl_tree<T>(this->list[index].second.value());
329template <
typename T,
size_t _SIZE>
334template <
typename T,
size_t _SIZE>
336 return this->_size == 0;
339template <
typename T,
size_t _SIZE>
344template <
typename T,
size_t _SIZE>
347 using bubble = std::vector<std::pair<T, std::optional<avl_tree<T>>>>;
353 explicit iterator(
const bubble& v,
const size_t& _index) noexcept : b(v), index(_index) {}
356 this->_size = current.
size();
358 for(
size_t i = 0; i<this->_size; i++){
365 if(this->index < _SIZE) {
378 if(this->index > 0) {
390 bool operator!=(
const iterator &it) {
391 return it.b[it.index].first != this->b[this->index].first;
394 bool operator==(
const iterator &it) {
395 return it.b[it.index].first == this->b[this->index].first;
398 std::pair<T, std::optional<avl_tree<T>>>& operator*() {
399 return this->b[this->index];
407bool operator==(
const std::pair<T, std::optional<
avl_tree<T>>> &a,
408 const std::pair<T, std::optional<
avl_tree<T>>> &b){
409 if(a.first == b.first){
410 if(a.second == std::nullopt && b.second == std::nullopt){
413 else if((a.second != std::nullopt && b.second == std::nullopt) || (a.second == std::nullopt && b.second != std::nullopt)){
417 std::vector v1 { a.second.value().inorder() };
418 std::vector v2 { b.second.value().inorder() };
426bool operator!=(
const std::pair<T, std::optional<
avl_tree<T>>> &a,
427 const std::pair<T, std::optional<
avl_tree<T>>> &b){
428 if(a.first != b.first) {
return true; }
430 if(a.second == std::nullopt && b.second == std::nullopt) {
433 else if((a.second == std::nullopt && b.second != std::nullopt) || (a.second != std::nullopt && b.second == std::nullopt)){
437 std::vector v1 { a.second.value().inorder() };
438 std::vector v2 { b.second.value().inorder() };
446bool operator<(
const std::pair<T, std::optional<
avl_tree<T>>> &a,
447 const std::pair<T, std::optional<
avl_tree<T>>> &b){
448 if(a.first == b.first) {
449 if(a.second == std::nullopt && b.second != std::nullopt){
452 else if(a.second != std::nullopt && b.second == std::nullopt){
456 std::vector v1 { a.second.value().inorder() };
457 std::vector v2 { b.second.value().inorder() };
461 return a.first < b.first;
465bool operator<=(
const std::pair<T, std::optional<
avl_tree<T>>> &a,
466 const std::pair<T, std::optional<
avl_tree<T>>> &b){
467 if(a.first == b.first) {
468 if(a.second == std::nullopt && b.second != std::nullopt){
471 else if(a.second != std::nullopt && b.second == std::nullopt){
475 std::vector v1 { a.second.value().inorder() };
476 std::vector v2 { b.second.value().inorder() };
480 return a.first <= b.first;
484bool operator>(
const std::pair<T, std::optional<
avl_tree<T>>> &a,
485 const std::pair<T, std::optional<
avl_tree<T>>> &b){
486 if(a.first == b.first) {
487 if(a.second == std::nullopt && b.second != std::nullopt){
490 else if(a.second != std::nullopt && b.second == std::nullopt){
494 std::vector v1 { a.second.value().inorder() };
495 std::vector v2 { b.second.value().inorder() };
499 return a.first > b.first;
504bool operator>=(
const std::pair<T, std::optional<
avl_tree<T>>> &a,
505 const std::pair<T, std::optional<
avl_tree<T>>> &b){
506 if(a.first == b.first) {
507 if(a.second == std::nullopt && b.second != std::nullopt){
510 else if(a.second != std::nullopt && b.second == std::nullopt){
514 std::vector v1 { a.second.value().inorder() };
515 std::vector v2 { b.second.value().inorder() };
519 return a.first >= b.first;
Class for AVL tree.
Definition avl_tree.h:15
std::vector< T > inorder() const
inorder function.
Definition avl_tree.h:130
Implementation of the bubble data structure, a data structure that is highly influenced from the fibo...
Definition bubble.h:27
std::pair< T, std::vector< T > > operator[](const size_t &index) const
operator [] for bubble
Definition bubble.h:163
const bubble operator=(bubble< T, _NEW_SIZE > &t)
operator = for bubble class
Definition bubble.h:66
void insert(Args ...keys)
insert function for bubble
Definition bubble.h:198
bool search(const T &key)
search function for bubble
Definition bubble.h:290
bubble(const bubble< T, _NEW_SIZE > &t)
copy constructor of bubble
Definition bubble.h:43
bool empty() const
empty function for bubble
Definition bubble.h:335
void remove(Args ...keys)
remove function for bubble
Definition bubble.h:244
iterator begin() noexcept
begin iterator
Definition bubble.h:131
T get_key(const size_t &index) const
get_key function
Definition bubble.h:315
size_t size() const
size function for bubble
Definition bubble.h:330
iterator end() noexcept
end iterator
Definition bubble.h:137
bubble() noexcept
default constructor of bubble
Definition bubble.h:36
friend std::ostream & operator<<(std::ostream &out, const bubble< T, _SIZE > &t)
operator << for bubble
Definition bubble.h:172
avl_tree< T > get_tree(const size_t &index) const
get_tree function
Definition bubble.h:321
size_t array_size() const
array_size function for bubble
Definition bubble.h:340