29 #ifndef INCLUDED_MDDS_FLAT_SEGMENT_TREE_HPP
30 #define INCLUDED_MDDS_FLAT_SEGMENT_TREE_HPP
37 #include "mdds/node.hpp"
38 #include "mdds/flat_segment_tree_itr.hpp"
39 #include "mdds/global.hpp"
48 template<
typename Key,
typename Value>
53 typedef Value value_type;
54 typedef size_t size_type;
66 return value == r.value;
71 return !operator==(r);
79 using node_ptr =
typename node::node_ptr;
83 friend struct ::mdds::fst::detail::forward_itr_handler<flat_segment_tree>;
84 friend struct ::mdds::fst::detail::reverse_itr_handler<flat_segment_tree>;
90 flat_segment_tree, ::mdds::fst::detail::forward_itr_handler<flat_segment_tree>>
92 typedef ::mdds::fst::detail::const_iterator_base<
95 friend class flat_segment_tree;
97 using base_type::get_parent;
98 using base_type::get_pos;
99 using base_type::is_end_pos;
120 flat_segment_tree, ::mdds::fst::detail::reverse_itr_handler<flat_segment_tree>>
122 typedef ::mdds::fst::detail::const_iterator_base<
125 friend class flat_segment_tree;
138 node_ptr m_left_leaf;
139 node_ptr m_right_leaf;
304 std::pair<const_iterator, bool>
insert_front(key_type start_key, key_type end_key, value_type val)
306 return insert_segment_impl(std::move(start_key), std::move(end_key), std::move(val),
true);
324 std::pair<const_iterator, bool>
insert_back(key_type start_key, key_type end_key, value_type val)
326 return insert_segment_impl(std::move(start_key), std::move(end_key), std::move(val),
false);
344 std::pair<const_iterator, bool>
insert(
345 const const_iterator& pos, key_type start_key, key_type end_key, value_type val);
356 void shift_left(key_type start_key, key_type end_key);
370 void shift_right(key_type pos, key_type size,
bool skip_start_node);
389 std::pair<const_iterator, bool>
search(
390 key_type key, value_type& value, key_type* start_key =
nullptr, key_type* end_key =
nullptr)
const;
410 std::pair<const_iterator, bool>
search(
411 const const_iterator& pos, key_type key, value_type& value, key_type* start_key =
nullptr,
412 key_type* end_key =
nullptr)
const;
423 const_iterator
search(key_type key)
const;
437 const_iterator
search(
const const_iterator& pos, key_type key)
const;
459 key_type key, value_type& value, key_type* start_key =
nullptr, key_type* end_key =
nullptr)
const;
501 key_type min_key()
const
503 return m_left_leaf->key;
506 key_type max_key()
const
508 return m_right_leaf->key;
511 value_type default_value()
const
523 #ifdef MDDS_UNIT_TEST
524 const nonleaf_node* get_root_node()
const
529 struct tree_dumper_traits
531 using leaf_type = node;
532 using nonleaf_type = nonleaf_node;
533 using tree_type = flat_segment_tree;
537 to_string(
const tree_type&)
540 std::string operator()(
const leaf_type& leaf)
const
542 return leaf.to_string();
545 std::string operator()(
const nonleaf_type& nonleaf)
const
547 return nonleaf.to_string();
552 void dump_tree()
const
558 assert(!
"attempted to dump an invalid tree!");
561 size_t node_instance_count = node::get_instance_count();
564 cout <<
"tree node count = " << node_count <<
"; node instance count = " << node_instance_count
565 <<
"; leaf node count = " << leaf_count << endl;
567 assert(leaf_count == node_instance_count);
570 void dump_leaf_nodes()
const
575 cout <<
"------------------------------------------" << endl;
577 node_ptr cur_node = m_left_leaf;
581 cout <<
" node " << node_id++ <<
": key = " << cur_node->key <<
"; value = " << cur_node->value_leaf.value
583 cur_node = cur_node->next;
585 cout << endl <<
" node instance count = " << node::get_instance_count() << endl;
595 bool verify_keys(const ::std::vector<key_type>& key_values)
const
599 node* cur_node = m_left_leaf.get();
600 typename ::std::vector<key_type>::const_iterator itr = key_values.begin(), itr_end = key_values.end();
601 for (; itr != itr_end; ++itr)
607 if (cur_node->key != *itr)
611 cur_node = cur_node->next.get();
622 node* cur_node = m_right_leaf.get();
623 typename ::std::vector<key_type>::const_reverse_iterator itr = key_values.rbegin(),
624 itr_end = key_values.rend();
625 for (; itr != itr_end; ++itr)
631 if (cur_node->key != *itr)
635 cur_node = cur_node->prev.get();
654 bool verify_values(const ::std::vector<value_type>& values)
const
656 node* cur_node = m_left_leaf.get();
657 node* end_node = m_right_leaf.get();
658 typename ::std::vector<value_type>::const_iterator itr = values.begin(), itr_end = values.end();
659 for (; itr != itr_end; ++itr)
661 if (cur_node == end_node || !cur_node)
664 if (cur_node->value_leaf.value != *itr)
668 cur_node = cur_node->next.get();
671 if (cur_node != end_node)
681 const_iterator search_by_key_impl(
const node* start_pos, key_type key)
const;
683 const node* search_tree_for_leaf_node(key_type key)
const;
685 void append_new_segment(key_type start_key)
687 if (m_right_leaf->prev->key == start_key)
689 m_right_leaf->prev->value_leaf.value = m_init_val;
693 #ifdef MDDS_UNIT_TEST
696 assert(m_right_leaf->prev->key < start_key);
699 if (m_right_leaf->prev->value_leaf.value == m_init_val)
704 node_ptr new_node(
new node);
705 new_node->key = start_key;
706 new_node->value_leaf.value = m_init_val;
707 new_node->prev = m_right_leaf->prev;
708 new_node->next = m_right_leaf;
709 m_right_leaf->prev->next = new_node;
710 m_right_leaf->prev = std::move(new_node);
711 m_valid_tree =
false;
714 ::std::pair<const_iterator, bool> insert_segment_impl(
715 key_type start_key, key_type end_key, value_type val,
bool forward);
717 ::std::pair<const_iterator, bool> insert_to_pos(
718 node_ptr start_pos, key_type start_key, key_type end_key, value_type val);
720 ::std::pair<const_iterator, bool> search_impl(
721 const node* pos, key_type key, value_type& value, key_type* start_key, key_type* end_key)
const;
723 const node* get_insertion_pos_leaf_reverse(
const key_type& key,
const node* start_pos)
const;
735 const node* get_insertion_pos_leaf(
const key_type& key,
const node* start_pos)
const;
737 static void shift_leaf_key_left(node_ptr& begin_node, node_ptr& end_node, key_type shift_value)
739 node* cur_node_p = begin_node.get();
740 node* end_node_p = end_node.get();
741 while (cur_node_p != end_node_p)
743 cur_node_p->key -= shift_value;
744 cur_node_p = cur_node_p->next.get();
748 static void shift_leaf_key_right(node_ptr& cur_node, node_ptr& end_node, key_type shift_value)
750 key_type end_node_key = end_node->key;
751 while (cur_node.get() != end_node.get())
753 cur_node->key += shift_value;
754 if (cur_node->key < end_node_key)
757 cur_node = cur_node->next;
765 node_ptr last_node = cur_node->prev;
766 while (cur_node.get() != end_node.get())
768 node_ptr next_node = cur_node->next;
769 disconnect_all_nodes(cur_node.get());
770 cur_node = std::move(next_node);
772 last_node->next = end_node;
773 end_node->prev = std::move(last_node);
787 bool adjust_segment_range(key_type& start_key, key_type& end_key)
const;
790 std::vector<nonleaf_node> m_nonleaf_node_pool;
792 const nonleaf_node* m_root_node;
793 node_ptr m_left_leaf;
794 node_ptr m_right_leaf;
795 value_type m_init_val;
799 template<
typename Key,
typename Value>
800 void swap(flat_segment_tree<Key, Value>& left, flat_segment_tree<Key, Value>& right)
807 #include "flat_segment_tree_def.inl"
Definition: flat_segment_tree.hpp:136
Definition: flat_segment_tree_itr.hpp:67
Definition: flat_segment_tree.hpp:119
bool valid_tree() const noexcept
Definition: flat_segment_tree.hpp:484
const_reverse_iterator rbegin() const
Definition: flat_segment_tree.hpp:181
size_type leaf_size() const
const_segment_range_type segment_range() const
Definition: flat_segment_tree.hpp:60
const_segment_iterator end_segment() const
std::pair< const_iterator, bool > insert(const const_iterator &pos, key_type start_key, key_type end_key, value_type val)
bool operator==(const flat_segment_tree &other) const
const_reverse_iterator rend() const
Definition: flat_segment_tree.hpp:195
const_iterator end() const
Definition: flat_segment_tree.hpp:168
std::pair< const_iterator, bool > insert_front(key_type start_key, key_type end_key, value_type val)
Definition: flat_segment_tree.hpp:304
std::pair< const_iterator, bool > search_tree(key_type key, value_type &value, key_type *start_key=nullptr, key_type *end_key=nullptr) const
Definition: flat_segment_tree_itr.hpp:94
Definition: flat_segment_tree_itr.hpp:37
const_segment_iterator begin_segment() const
Definition: flat_segment_tree_itr.hpp:198
void shift_right(key_type pos, key_type size, bool skip_start_node)
Definition: flat_segment_tree.hpp:49
Definition: cref_wrapper.hpp:34
Definition: flat_segment_tree.hpp:56
flat_segment_tree< Key, Value > & operator=(const flat_segment_tree &other)
std::pair< const_iterator, bool > insert_back(key_type start_key, key_type end_key, value_type val)
Definition: flat_segment_tree.hpp:324
std::pair< const_iterator, bool > search(key_type key, value_type &value, key_type *start_key=nullptr, key_type *end_key=nullptr) const
void shift_left(key_type start_key, key_type end_key)
Definition: flat_segment_tree.hpp:89
void swap(flat_segment_tree &other)
const_segment_iterator to_segment() const
const_iterator begin() const
Definition: flat_segment_tree.hpp:155