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"
48template<
typename Key,
typename Value>
53 typedef Value value_type;
54 typedef size_t size_type;
63 return low == r.low &&
high == r.
high;
77 return key == r.key && value == r.value;
89 struct to_string_handler;
93 typedef typename node::node_ptr node_ptr;
106 _self.value_nonleaf.low = left_node->
is_leaf
107 ?
static_cast<const node*
>(left_node)->value_leaf.key
108 :
static_cast<const nonleaf_node*
>(left_node)->value_nonleaf.low;
114 "flat_segment_tree::fill_nonleaf_value_handler: Having a left node is prerequisite.");
124 const node* p =
static_cast<const node*
>(right_node);
126 _self.value_nonleaf.high = p->
next->value_leaf.key;
128 _self.value_nonleaf.high = p->value_leaf.key;
132 _self.value_nonleaf.high =
static_cast<const nonleaf_node*
>(right_node)->value_nonleaf.high;
137 _self.value_nonleaf.high = left_node->
is_leaf
138 ?
static_cast<const node*
>(left_node)->value_leaf.key
139 :
static_cast<const nonleaf_node*
>(left_node)->value_nonleaf.high;
145 struct to_string_handler
147 std::string operator()(
const node& _self)
const
149 std::ostringstream os;
150 os <<
"(" << _self.value_leaf.key <<
") ";
156 std::ostringstream os;
157 os <<
"(" << _self.value_nonleaf.low <<
"-" << _self.value_nonleaf.high <<
") ";
165 void operator()(
node& )
173 void operator()(
node& )
180 friend struct ::mdds::__fst::itr_forward_handler<flat_segment_tree>;
181 friend struct ::mdds::__fst::itr_reverse_handler<flat_segment_tree>;
185 flat_segment_tree, ::mdds::__fst::itr_forward_handler<flat_segment_tree>>
205 flat_segment_tree, ::mdds::__fst::itr_reverse_handler<flat_segment_tree>>
353 std::pair<const_iterator, bool>
insert_front(key_type start_key, key_type end_key, value_type val)
355 return insert_segment_impl(start_key, end_key, val,
true);
373 std::pair<const_iterator, bool>
insert_back(key_type start_key, key_type end_key, value_type val)
375 return insert_segment_impl(start_key, end_key, val,
false);
394 const const_iterator& pos, key_type start_key, key_type end_key, value_type val);
419 void shift_right(key_type pos, key_type size,
bool skip_start_node);
439 key_type key, value_type& value, key_type* start_key =
nullptr, key_type* end_key =
nullptr)
const;
460 const const_iterator& pos, key_type key, value_type& value, key_type* start_key =
nullptr,
461 key_type* end_key =
nullptr)
const;
483 key_type key, value_type& value, key_type* start_key =
nullptr, key_type* end_key =
nullptr)
const;
513 key_type min_key()
const
515 return m_left_leaf->value_leaf.key;
518 key_type max_key()
const
520 return m_right_leaf->value_leaf.key;
523 value_type default_value()
const
541 void dump_tree()
const
547 assert(!
"attempted to dump an invalid tree!");
549 size_t node_count = mdds::__st::tree_dumper<node, nonleaf_node>::dump(m_root_node);
550 size_t node_instance_count = node::get_instance_count();
553 cout <<
"tree node count = " << node_count <<
"; node instance count = " << node_instance_count
554 <<
"; leaf node count = " << leaf_count << endl;
556 assert(leaf_count == node_instance_count);
559 void dump_leaf_nodes()
const
564 cout <<
"------------------------------------------" << endl;
566 node_ptr cur_node = m_left_leaf;
570 cout <<
" node " << node_id++ <<
": key = " << cur_node->value_leaf.key
571 <<
"; value = " << cur_node->value_leaf.value << endl;
572 cur_node = cur_node->next;
574 cout << endl <<
" node instance count = " << node::get_instance_count() << endl;
584 bool verify_keys(const ::std::vector<key_type>& key_values)
const
588 node* cur_node = m_left_leaf.get();
589 typename ::std::vector<key_type>::const_iterator itr = key_values.begin(), itr_end = key_values.end();
590 for (; itr != itr_end; ++itr)
596 if (cur_node->value_leaf.key != *itr)
600 cur_node = cur_node->next.get();
611 node* cur_node = m_right_leaf.get();
612 typename ::std::vector<key_type>::const_reverse_iterator itr = key_values.rbegin(),
613 itr_end = key_values.rend();
614 for (; itr != itr_end; ++itr)
620 if (cur_node->value_leaf.key != *itr)
624 cur_node = cur_node->prev.get();
643 bool verify_values(const ::std::vector<value_type>& values)
const
645 node* cur_node = m_left_leaf.get();
646 node* end_node = m_right_leaf.get();
647 typename ::std::vector<value_type>::const_iterator itr = values.begin(), itr_end = values.end();
648 for (; itr != itr_end; ++itr)
650 if (cur_node == end_node || !cur_node)
653 if (cur_node->value_leaf.value != *itr)
657 cur_node = cur_node->next.get();
660 if (cur_node != end_node)
672 void append_new_segment(key_type start_key)
674 if (m_right_leaf->prev->value_leaf.key == start_key)
676 m_right_leaf->prev->value_leaf.value = m_init_val;
683 assert(m_right_leaf->prev->value_leaf.key < start_key);
686 if (m_right_leaf->prev->value_leaf.value == m_init_val)
691 node_ptr new_node(
new node);
692 new_node->value_leaf.key = start_key;
693 new_node->value_leaf.value = m_init_val;
694 new_node->prev = m_right_leaf->prev;
695 new_node->next = m_right_leaf;
696 m_right_leaf->prev->next = new_node;
697 m_right_leaf->prev = new_node;
698 m_valid_tree =
false;
701 ::std::pair<const_iterator, bool> insert_segment_impl(
702 key_type start_key, key_type end_key, value_type val,
bool forward);
704 ::std::pair<const_iterator, bool> insert_to_pos(
705 node_ptr& start_pos, key_type start_key, key_type end_key, value_type val);
707 ::std::pair<const_iterator, bool> search_impl(
708 const node* pos, key_type key, value_type& value, key_type* start_key, key_type* end_key)
const;
710 const node* get_insertion_pos_leaf_reverse(key_type key,
const node* start_pos)
const;
712 const node* get_insertion_pos_leaf(key_type key,
const node* start_pos)
const;
714 static void shift_leaf_key_left(node_ptr& begin_node, node_ptr& end_node, key_type shift_value)
716 node* cur_node_p = begin_node.get();
717 node* end_node_p = end_node.get();
718 while (cur_node_p != end_node_p)
720 cur_node_p->value_leaf.key -= shift_value;
721 cur_node_p = cur_node_p->next.get();
725 static void shift_leaf_key_right(node_ptr& cur_node, node_ptr& end_node, key_type shift_value)
727 key_type end_node_key = end_node->value_leaf.key;
728 while (cur_node.get() != end_node.get())
730 cur_node->value_leaf.key += shift_value;
731 if (cur_node->value_leaf.key < end_node_key)
734 cur_node = cur_node->next;
742 node_ptr last_node = cur_node->prev;
743 while (cur_node.get() != end_node.get())
745 node_ptr next_node = cur_node->next;
746 disconnect_all_nodes(cur_node.get());
747 cur_node = next_node;
749 last_node->next = end_node;
750 end_node->prev = last_node;
764 bool adjust_segment_range(key_type& start_key, key_type& end_key)
const;
767 std::vector<nonleaf_node> m_nonleaf_node_pool;
769 nonleaf_node* m_root_node;
770 node_ptr m_left_leaf;
771 node_ptr m_right_leaf;
772 value_type m_init_val;
776template<
typename Key,
typename Value>
777void swap(flat_segment_tree<Key, Value>& left, flat_segment_tree<Key, Value>& right)
784#include "flat_segment_tree_def.inl"
Definition: flat_segment_tree_itr.hpp:95
Definition: flat_segment_tree_itr.hpp:193
Definition: flat_segment_tree.hpp:186
Definition: flat_segment_tree.hpp:206
Definition: flat_segment_tree.hpp:50
flat_segment_tree(const flat_segment_tree< key_type, value_type > &r)
size_type leaf_size() const
void swap(flat_segment_tree< key_type, value_type > &other)
void shift_left(key_type start_key, key_type end_key)
void shift_right(key_type pos, key_type size, bool skip_start_node)
const_segment_iterator end_segment() const
const_reverse_iterator rbegin() const
Definition: flat_segment_tree.hpp:256
std::pair< const_iterator, bool > insert_front(key_type start_key, key_type end_key, value_type val)
Definition: flat_segment_tree.hpp:353
const_iterator begin() const
Definition: flat_segment_tree.hpp:230
bool is_tree_valid() const
Definition: flat_segment_tree.hpp:496
std::pair< const_iterator, bool > search(key_type key, value_type &value, key_type *start_key=nullptr, key_type *end_key=nullptr) const
flat_segment_tree< key_type, value_type > & operator=(const flat_segment_tree< key_type, value_type > &other)
bool operator==(const flat_segment_tree< key_type, value_type > &r) const
std::pair< const_iterator, bool > search_tree(key_type key, value_type &value, key_type *start_key=nullptr, key_type *end_key=nullptr) const
std::pair< const_iterator, bool > insert(const const_iterator &pos, key_type start_key, key_type end_key, value_type val)
std::pair< const_iterator, bool > search(const const_iterator &pos, key_type key, value_type &value, key_type *start_key=nullptr, key_type *end_key=nullptr) const
std::pair< const_iterator, bool > insert_back(key_type start_key, key_type end_key, value_type val)
Definition: flat_segment_tree.hpp:373
const_segment_iterator begin_segment() const
const_iterator end() const
Definition: flat_segment_tree.hpp:243
flat_segment_tree(key_type min_val, key_type max_val, value_type init_val)
const_reverse_iterator rend() const
Definition: flat_segment_tree.hpp:270
Definition: global.hpp:84
Definition: flat_segment_tree_itr.hpp:38
Definition: flat_segment_tree_itr.hpp:68
bool is_leaf
parent nonleaf_node
Definition: node.hpp:46
node_ptr next
previous sibling leaf node.
Definition: node.hpp:162
Definition: flat_segment_tree.hpp:172
Definition: flat_segment_tree.hpp:98
Definition: flat_segment_tree.hpp:164
Definition: flat_segment_tree.hpp:71
Definition: flat_segment_tree.hpp:57
key_type high
low range value (inclusive)
Definition: flat_segment_tree.hpp:59
bool operator==(const nonleaf_value_type &r) const
high range value (non-inclusive)
Definition: flat_segment_tree.hpp:61