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;
490 [[deprecated(
"Use valid_tree() instead")]]
501 bool operator==(
const flat_segment_tree& other)
const noexcept(nothrow_eq_comparable_v);
503 bool operator!=(
const flat_segment_tree& other)
const noexcept(nothrow_eq_comparable_v)
508 const key_type& min_key() const noexcept
510 return m_left_leaf->key;
513 const key_type& max_key() const noexcept
515 return m_right_leaf->key;
518 const value_type& default_value() const noexcept
529 noexcept(noexcept(st::detail::count_leaf_nodes<size_type>(m_left_leaf.get(), m_right_leaf.get())))
531 return st::detail::count_leaf_nodes<size_type>(m_left_leaf.get(), m_right_leaf.get());
535 const nonleaf_node* get_root_node()
const
540 struct tree_dumper_traits
542 using leaf_type = node;
543 using nonleaf_type = nonleaf_node;
544 using tree_type = flat_segment_tree;
548 to_string(
const tree_type&)
551 std::string operator()(
const leaf_type& leaf)
const
553 return leaf.to_string();
556 std::string operator()(
const nonleaf_type& nonleaf)
const
558 return nonleaf.to_string();
563 void dump_tree()
const
569 assert(!
"attempted to dump an invalid tree!");
571 size_t node_count = mdds::st::detail::tree_dumper<tree_dumper_traits>::dump(std::cout, *
this, m_root_node);
572 size_t node_instance_count = node::get_instance_count();
575 cout <<
"tree node count = " << node_count <<
"; node instance count = " << node_instance_count
576 <<
"; leaf node count = " << leaf_count << endl;
578 assert(leaf_count == node_instance_count);
581 void dump_leaf_nodes()
const
586 cout <<
"------------------------------------------" << endl;
588 node_ptr cur_node = m_left_leaf;
592 cout <<
" node " << node_id++ <<
": key = " << cur_node->key <<
"; value = " << cur_node->value_leaf.value
594 cur_node = cur_node->next;
596 cout << endl <<
" node instance count = " << node::get_instance_count() << endl;
606 bool verify_keys(const ::std::vector<key_type>& key_values)
const
610 node* cur_node = m_left_leaf.get();
611 typename ::std::vector<key_type>::const_iterator itr = key_values.begin(), itr_end = key_values.end();
612 for (; itr != itr_end; ++itr)
618 if (cur_node->key != *itr)
622 cur_node = cur_node->next.get();
633 node* cur_node = m_right_leaf.get();
634 typename ::std::vector<key_type>::const_reverse_iterator itr = key_values.rbegin(),
635 itr_end = key_values.rend();
636 for (; itr != itr_end; ++itr)
642 if (cur_node->key != *itr)
646 cur_node = cur_node->prev.get();
665 bool verify_values(const ::std::vector<value_type>& values)
const
667 node* cur_node = m_left_leaf.get();
668 node* end_node = m_right_leaf.get();
669 typename ::std::vector<value_type>::const_iterator itr = values.begin(), itr_end = values.end();
670 for (; itr != itr_end; ++itr)
672 if (cur_node == end_node || !cur_node)
675 if (cur_node->value_leaf.value != *itr)
679 cur_node = cur_node->next.get();
682 if (cur_node != end_node)
692 const_iterator search_by_key_impl(
const node* start_pos, key_type key)
const;
694 const node* search_tree_for_leaf_node(key_type key)
const;
696 void append_new_segment(key_type start_key)
698 if (m_right_leaf->prev->key == start_key)
700 m_right_leaf->prev->value_leaf.value = m_init_val;
707 assert(m_right_leaf->prev->key < start_key);
710 if (m_right_leaf->prev->value_leaf.value == m_init_val)
715 node_ptr new_node(
new node);
716 new_node->key = start_key;
717 new_node->value_leaf.value = m_init_val;
718 new_node->prev = m_right_leaf->prev;
719 new_node->next = m_right_leaf;
720 m_right_leaf->prev->next = new_node;
721 m_right_leaf->prev = std::move(new_node);
722 m_valid_tree =
false;
725 ::std::pair<const_iterator, bool> insert_segment_impl(
726 key_type start_key, key_type end_key, value_type val,
bool forward);
728 ::std::pair<const_iterator, bool> insert_to_pos(
729 node_ptr start_pos, key_type start_key, key_type end_key, value_type val);
731 ::std::pair<const_iterator, bool> search_impl(
732 const node* pos, key_type key, value_type& value, key_type* start_key, key_type* end_key)
const;
734 const node* get_insertion_pos_leaf_reverse(
const key_type& key,
const node* start_pos)
const;
746 const node* get_insertion_pos_leaf(
const key_type& key,
const node* start_pos)
const;
748 static void shift_leaf_key_left(node_ptr& begin_node, node_ptr& end_node, key_type shift_value)
750 node* cur_node_p = begin_node.get();
751 node* end_node_p = end_node.get();
752 while (cur_node_p != end_node_p)
754 cur_node_p->key -= shift_value;
755 cur_node_p = cur_node_p->next.get();
759 static void shift_leaf_key_right(node_ptr& cur_node, node_ptr& end_node, key_type shift_value)
761 key_type end_node_key = end_node->key;
762 while (cur_node.get() != end_node.get())
764 cur_node->key += shift_value;
765 if (cur_node->key < end_node_key)
768 cur_node = cur_node->next;
776 node_ptr last_node = cur_node->prev;
777 while (cur_node.get() != end_node.get())
779 node_ptr next_node = cur_node->next;
780 disconnect_all_nodes(cur_node.get());
781 cur_node = std::move(next_node);
783 last_node->next = end_node;
784 end_node->prev = std::move(last_node);
798 bool adjust_segment_range(key_type& start_key, key_type& end_key)
const;
801 std::vector<nonleaf_node> m_nonleaf_node_pool;
803 const nonleaf_node* m_root_node;
804 node_ptr m_left_leaf;
805 node_ptr m_right_leaf;
806 value_type m_init_val;