27class flat_segment_tree
31 typedef Value value_type;
32 typedef size_t size_type;
42 struct leaf_value_type
46 bool operator==(
const leaf_value_type& r)
const
47 noexcept(
noexcept(std::declval<value_type>() == std::declval<value_type>()))
49 return value == r.value;
52 bool operator!=(
const leaf_value_type& r)
const noexcept(
noexcept(!operator==(r)))
54 return !operator==(r);
57 leaf_value_type() : value{}
62 using node_ptr =
typename node::node_ptr;
66 friend struct ::mdds::fst::detail::forward_itr_handler<flat_segment_tree>;
67 friend struct ::mdds::fst::detail::reverse_itr_handler<flat_segment_tree>;
69 static constexpr bool nothrow_move_constructible_v =
70 std::is_nothrow_move_constructible_v<std::vector<nonleaf_node>> &&
71 std::is_nothrow_move_constructible_v<value_type>;
73 static constexpr bool nothrow_swappable_v =
74 std::is_nothrow_swappable_v<value_type> && std::is_nothrow_swappable_v<std::vector<nonleaf_node>>;
76 static constexpr bool nothrow_move_assignable_v = nothrow_move_constructible_v && nothrow_swappable_v;
78 static constexpr bool nothrow_eq_comparable_v =
79 noexcept(std::declval<key_type>() == std::declval<key_type>()) &&
80 noexcept(std::declval<leaf_value_type>() == std::declval<leaf_value_type>());
86 flat_segment_tree, ::mdds::fst::detail::forward_itr_handler<flat_segment_tree>>
91 friend class flat_segment_tree;
93 using base_type::get_parent;
94 using base_type::get_pos;
95 using base_type::is_end_pos;
98 const_iterator() : base_type(
nullptr,
false)
108 explicit const_iterator(
const typename base_type::fst_type* _db,
bool _end) : base_type(_db, _end)
111 explicit const_iterator(
const typename base_type::fst_type* _db,
const node* p) : base_type(_db, p)
116 flat_segment_tree, ::mdds::fst::detail::reverse_itr_handler<flat_segment_tree>>
121 friend class flat_segment_tree;
124 const_reverse_iterator() : base_type(
nullptr,
false)
128 explicit const_reverse_iterator(
const typename base_type::fst_type* _db,
bool _end) : base_type(_db, _end)
132 class const_segment_range_type
134 node_ptr m_left_leaf;
135 node_ptr m_right_leaf;
138 const_segment_range_type(node_ptr left_leaf, node_ptr right_leaf);
140 const_segment_iterator
begin()
const;
141 const_segment_iterator
end()
const;
226 flat_segment_tree() =
delete;
254 ~flat_segment_tree();
263 flat_segment_tree<Key, Value>&
operator=(
const flat_segment_tree& other);
270 flat_segment_tree<Key, Value>&
operator=(flat_segment_tree&& other)
noexcept(nothrow_move_assignable_v);
277 void swap(flat_segment_tree& other)
noexcept(nothrow_swappable_v);
300 std::pair<const_iterator, bool>
insert_front(key_type start_key, key_type end_key, value_type val)
302 return insert_segment_impl(std::move(start_key), std::move(end_key), std::move(val),
true);
320 std::pair<const_iterator, bool>
insert_back(key_type start_key, key_type end_key, value_type val)
322 return insert_segment_impl(std::move(start_key), std::move(end_key), std::move(val),
false);
340 std::pair<const_iterator, bool>
insert(
const_iterator pos, key_type start_key, key_type end_key, value_type val);
365 void shift_right(key_type pos, key_type size,
bool skip_start_node);
385 key_type key, value_type& value, key_type* start_key =
nullptr, key_type* end_key =
nullptr)
const;
406 const_iterator pos, key_type key, value_type& value, key_type* start_key =
nullptr,
407 key_type* end_key =
nullptr)
const;
454 key_type key, value_type& value, key_type* start_key =
nullptr, key_type* end_key =
nullptr)
const;
489 bool operator==(
const flat_segment_tree& other)
const noexcept(nothrow_eq_comparable_v);
491 bool operator!=(
const flat_segment_tree& other)
const noexcept(nothrow_eq_comparable_v)
496 const key_type& min_key() const noexcept
498 return m_left_leaf->key;
501 const key_type& max_key() const noexcept
503 return m_right_leaf->key;
506 const value_type& default_value() const noexcept
517 noexcept(noexcept(st::detail::count_leaf_nodes<size_type>(m_left_leaf.get(), m_right_leaf.get())))
519 return st::detail::count_leaf_nodes<size_type>(m_left_leaf.get(), m_right_leaf.get());
523 const nonleaf_node* get_root_node()
const
528 struct tree_dumper_traits
530 using leaf_type = node;
531 using nonleaf_type = nonleaf_node;
532 using tree_type = flat_segment_tree;
536 to_string(
const tree_type&)
539 std::string operator()(
const leaf_type& leaf)
const
541 return leaf.to_string();
544 std::string operator()(
const nonleaf_type& nonleaf)
const
546 return nonleaf.to_string();
551 void dump_tree()
const
557 assert(!
"attempted to dump an invalid tree!");
559 size_t node_count = mdds::st::detail::tree_dumper<tree_dumper_traits>::dump(std::cout, *
this, m_root_node);
560 size_t node_instance_count = node::get_instance_count();
563 cout <<
"tree node count = " << node_count <<
"; node instance count = " << node_instance_count
564 <<
"; leaf node count = " << leaf_count << endl;
566 assert(leaf_count == node_instance_count);
569 void dump_leaf_nodes()
const
574 cout <<
"------------------------------------------" << endl;
576 node_ptr cur_node = m_left_leaf;
580 cout <<
" node " << node_id++ <<
": key = " << cur_node->key <<
"; value = " << cur_node->value_leaf.value
582 cur_node = cur_node->next;
584 cout << endl <<
" node instance count = " << node::get_instance_count() << endl;
594 bool verify_keys(const ::std::vector<key_type>& key_values)
const
598 node* cur_node = m_left_leaf.get();
599 typename ::std::vector<key_type>::const_iterator itr = key_values.begin(), itr_end = key_values.end();
600 for (; itr != itr_end; ++itr)
606 if (cur_node->key != *itr)
610 cur_node = cur_node->next.get();
621 node* cur_node = m_right_leaf.get();
622 typename ::std::vector<key_type>::const_reverse_iterator itr = key_values.rbegin(),
623 itr_end = key_values.rend();
624 for (; itr != itr_end; ++itr)
630 if (cur_node->key != *itr)
634 cur_node = cur_node->prev.get();
653 bool verify_values(const ::std::vector<value_type>& values)
const
655 node* cur_node = m_left_leaf.get();
656 node* end_node = m_right_leaf.get();
657 typename ::std::vector<value_type>::const_iterator itr = values.begin(), itr_end = values.end();
658 for (; itr != itr_end; ++itr)
660 if (cur_node == end_node || !cur_node)
663 if (cur_node->value_leaf.value != *itr)
667 cur_node = cur_node->next.get();
670 if (cur_node != end_node)
680 const_iterator search_by_key_impl(
const node* start_pos, key_type key)
const;
682 const node* search_tree_for_leaf_node(key_type key)
const;
684 void append_new_segment(key_type start_key)
686 if (m_right_leaf->prev->key == start_key)
688 m_right_leaf->prev->value_leaf.value = m_init_val;
695 assert(m_right_leaf->prev->key < start_key);
698 if (m_right_leaf->prev->value_leaf.value == m_init_val)
703 node_ptr new_node(
new node);
704 new_node->key = start_key;
705 new_node->value_leaf.value = m_init_val;
706 new_node->prev = m_right_leaf->prev;
707 new_node->next = m_right_leaf;
708 m_right_leaf->prev->next = new_node;
709 m_right_leaf->prev = std::move(new_node);
710 m_valid_tree =
false;
713 ::std::pair<const_iterator, bool> insert_segment_impl(
714 key_type start_key, key_type end_key, value_type val,
bool forward);
716 ::std::pair<const_iterator, bool> insert_to_pos(
717 node_ptr start_pos, key_type start_key, key_type end_key, value_type val);
719 ::std::pair<const_iterator, bool> search_impl(
720 const node* pos, key_type key, value_type& value, key_type* start_key, key_type* end_key)
const;
722 const node* get_insertion_pos_leaf_reverse(
const key_type& key,
const node* start_pos)
const;
734 const node* get_insertion_pos_leaf(
const key_type& key,
const node* start_pos)
const;
736 static void shift_leaf_key_left(node_ptr& begin_node, node_ptr& end_node, key_type shift_value)
738 node* cur_node_p = begin_node.get();
739 node* end_node_p = end_node.get();
740 while (cur_node_p != end_node_p)
742 cur_node_p->key -= shift_value;
743 cur_node_p = cur_node_p->next.get();
747 static void shift_leaf_key_right(node_ptr& cur_node, node_ptr& end_node, key_type shift_value)
749 key_type end_node_key = end_node->key;
750 while (cur_node.get() != end_node.get())
752 cur_node->key += shift_value;
753 if (cur_node->key < end_node_key)
756 cur_node = cur_node->next;
764 node_ptr last_node = cur_node->prev;
765 while (cur_node.get() != end_node.get())
767 node_ptr next_node = cur_node->next;
768 disconnect_all_nodes(cur_node.get());
769 cur_node = std::move(next_node);
771 last_node->next = end_node;
772 end_node->prev = std::move(last_node);
786 bool adjust_segment_range(key_type& start_key, key_type& end_key)
const;
789 std::vector<nonleaf_node> m_nonleaf_node_pool;
791 const nonleaf_node* m_root_node;
792 node_ptr m_left_leaf;
793 node_ptr m_right_leaf;
794 value_type m_init_val;