28 #ifndef INCLUDED_MDDS_SEGMENTTREE_HPP 29 #define INCLUDED_MDDS_SEGMENTTREE_HPP 31 #include "mdds/node.hpp" 32 #include "mdds/global.hpp" 38 #include <unordered_map> 47 template<
typename _Key,
typename _Value>
50 template<
typename _Key,
typename _Value>
55 typedef _Key key_type;
56 typedef _Value value_type;
57 typedef size_t size_type;
58 typedef ::std::vector<value_type> search_result_type;
67 segment_data(key_type _beg, key_type _end, value_type p) :
68 begin_key(_beg), end_key(_end), pdata(p) {}
72 return begin_key == r.begin_key && end_key == r.end_key && pdata == r.pdata;
75 bool operator!=(
const segment_data& r)
const 81 struct segment_map_printer :
public ::std::unary_function< ::std::pair<value_type, ::std::pair<key_type, key_type> >, void>
83 void operator() (const ::std::pair<value_type, ::std::pair<key_type, key_type> >& r)
const 86 cout << r.second.first <<
"-" << r.second.second <<
": " << r.first->name << endl;
92 typedef ::std::vector<value_type> data_chain_type;
93 typedef std::unordered_map<value_type, ::std::pair<key_type, key_type> > segment_map_type;
94 typedef ::std::map<value_type, ::std::pair<key_type, key_type> > sorted_segment_map_type;
104 return low == r.low && high == r.
high && data_chain == r.
data_chain;
111 data_chain_type* data_chain;
115 return key == r.key && data_chain == r.data_chain;
122 #ifdef MDDS_UNIT_TEST 123 struct to_string_handler;
127 typedef typename node::node_ptr node_ptr;
138 _self.value_nonleaf.low = left_node->
is_leaf ?
139 static_cast<const node*
>(left_node)->value_leaf.key :
140 static_cast<const nonleaf_node*>(left_node)->value_nonleaf.low;
145 throw general_error(
"segment_tree::fill_nonleaf_value_handler: Having a left node is prerequisite.");
156 const node* p =
static_cast<const node*
>(right_node);
158 _self.value_nonleaf.high = p->
next->value_leaf.key;
160 _self.value_nonleaf.high = p->value_leaf.key;
164 _self.value_nonleaf.high =
static_cast<const nonleaf_node*
>(right_node)->value_nonleaf.high;
169 _self.value_nonleaf.high = left_node->
is_leaf ?
170 static_cast<const node*
>(left_node)->value_leaf.key :
171 static_cast<const nonleaf_node*>(left_node)->value_nonleaf.high;
176 #ifdef MDDS_UNIT_TEST 177 struct to_string_handler
179 std::string operator() (
const node& _self)
const 181 std::ostringstream os;
182 os <<
"[" << _self.value_leaf.key <<
"] ";
188 std::ostringstream os;
189 os <<
"[" << _self.value_nonleaf.low <<
"-" << _self.value_nonleaf.high <<
")";
190 if (_self.value_nonleaf.data_chain)
193 typename data_chain_type::const_iterator
195 itr_beg = _self.value_nonleaf.data_chain->begin(),
196 itr_end = _self.value_nonleaf.data_chain->end();
197 for (itr = itr_beg; itr != itr_end; ++itr)
213 void operator() (node& _self)
215 _self.value_leaf.data_chain =
nullptr;
220 _self.value_nonleaf.data_chain =
nullptr;
226 void operator() (node& _self)
228 delete _self.value_leaf.data_chain;
233 delete _self.value_nonleaf.data_chain;
237 #ifdef MDDS_UNIT_TEST 238 struct node_printer :
public ::std::unary_function<const __st::node_base*, void>
243 std::cout << static_cast<const node*>(p)->to_string() <<
" ";
245 std::cout << static_cast<const nonleaf_node*>(p)->to_string() <<
" ";
256 class search_result_base
259 typedef std::vector<data_chain_type*> res_chains_type;
260 typedef std::shared_ptr<res_chains_type> res_chains_ptr;
263 search_result_base() :
264 mp_res_chains(static_cast<res_chains_type*>(
nullptr)) {}
266 search_result_base(
const search_result_base& r) :
267 mp_res_chains(r.mp_res_chains) {}
275 typename res_chains_type::const_iterator
276 itr = mp_res_chains->begin(), itr_end = mp_res_chains->end();
277 for (; itr != itr_end; ++itr)
278 combined += (*itr)->size();
282 void push_back_chain(data_chain_type* chain)
284 if (!chain || chain->empty())
288 mp_res_chains.reset(
new res_chains_type);
289 mp_res_chains->push_back(chain);
292 res_chains_ptr& get_res_chains() {
return mp_res_chains; }
295 res_chains_ptr mp_res_chains;
301 typedef typename search_result_base::res_chains_type res_chains_type;
302 typedef typename search_result_base::res_chains_ptr res_chains_ptr;
304 iterator_base(
const res_chains_ptr& p) :
305 mp_res_chains(p), m_end_pos(
true) {}
308 typedef ::std::bidirectional_iterator_tag iterator_category;
309 typedef typename data_chain_type::value_type value_type;
310 typedef typename data_chain_type::pointer pointer;
311 typedef typename data_chain_type::reference reference;
312 typedef typename data_chain_type::difference_type difference_type;
315 mp_res_chains(static_cast<res_chains_type*>(
nullptr)), m_end_pos(
true) {}
317 iterator_base(
const iterator_base& r) :
318 mp_res_chains(r.mp_res_chains),
319 m_cur_chain(r.m_cur_chain),
320 m_cur_pos_in_chain(r.m_cur_pos_in_chain),
321 m_end_pos(r.m_end_pos) {}
323 iterator_base& operator= (
const iterator_base& r)
325 mp_res_chains = r.mp_res_chains;
326 m_cur_chain = r.m_cur_chain;
327 m_cur_pos_in_chain = r.m_cur_pos_in_chain;
328 m_end_pos = r.m_end_pos;
332 typename data_chain_type::value_type* operator++ ()
343 typename data_chain_type::iterator cur_pos_in_chain = m_cur_pos_in_chain;
345 if (++cur_pos_in_chain == (*m_cur_chain)->end())
348 typename res_chains_type::iterator cur_chain = m_cur_chain;
350 if (cur_chain == mp_res_chains->end())
355 m_cur_chain = cur_chain;
356 m_cur_pos_in_chain = (*m_cur_chain)->begin();
359 ++m_cur_pos_in_chain;
364 typename data_chain_type::value_type* operator-- ()
372 return &(*m_cur_pos_in_chain);
375 if (m_cur_pos_in_chain == (*m_cur_chain)->begin())
377 if (m_cur_chain == mp_res_chains->begin())
383 m_cur_pos_in_chain = (*m_cur_chain)->end();
385 --m_cur_pos_in_chain;
389 bool operator== (
const iterator_base& r)
const 391 if (mp_res_chains.get())
394 return mp_res_chains.get() == r.mp_res_chains.get() &&
395 m_cur_chain == r.m_cur_chain && m_cur_pos_in_chain == r.m_cur_pos_in_chain &&
396 m_end_pos == r.m_end_pos;
400 if (r.mp_res_chains.get())
402 return m_end_pos == r.m_end_pos;
405 bool operator!= (
const iterator_base& r)
const {
return !
operator==(r); }
407 typename data_chain_type::value_type& operator*()
409 return *m_cur_pos_in_chain;
412 typename data_chain_type::value_type* operator->()
414 return &(*m_cur_pos_in_chain);
429 m_cur_chain = mp_res_chains->begin();
430 m_cur_pos_in_chain = (*m_cur_chain)->begin();
441 m_cur_chain = mp_res_chains->end();
443 m_cur_pos_in_chain = (*m_cur_chain)->end();
444 --m_cur_pos_in_chain;
448 res_chains_ptr mp_res_chains;
449 typename res_chains_type::iterator m_cur_chain;
450 typename data_chain_type::iterator m_cur_pos_in_chain;
458 typedef typename search_result_base::res_chains_type res_chains_type;
459 typedef typename search_result_base::res_chains_ptr res_chains_ptr;
466 iterator(const res_chains_ptr& p) : iterator_base(p) {}
490 void operator() (data_chain_type* node_data)
495 typename data_chain_type::const_iterator itr = node_data->begin(), itr_end = node_data->end();
496 for (; itr != itr_end; ++itr)
497 m_result.push_back(*itr);
500 search_result_type& m_result;
507 void operator() (data_chain_type* node_data)
512 m_result.push_back_chain(node_data);
515 search_result_base& m_result;
552 bool insert(key_type begin_key, key_type end_key, value_type pdata);
570 bool search(key_type point, search_result_type& result)
const;
581 search_result
search(key_type point)
const;
590 void remove(value_type value);
614 #ifdef MDDS_UNIT_TEST 615 void dump_tree()
const;
616 void dump_leaf_nodes()
const;
617 void dump_segment_data()
const;
618 bool verify_node_lists()
const;
620 struct leaf_node_check
623 data_chain_type data_chain;
626 bool verify_leaf_nodes(const ::std::vector<leaf_node_check>& checks)
const;
634 bool verify_segment_data(
const segment_map_type& checks)
const;
641 void search(key_type point, search_result_base& result)
const;
643 typedef std::vector<__st::node_base*> node_list_type;
644 typedef std::map<value_type, std::unique_ptr<node_list_type>> data_node_map_type;
646 static void create_leaf_node_instances(const ::std::vector<key_type>& keys, node_ptr& left, node_ptr& right);
653 void descend_tree_and_mark(
654 __st::node_base* pnode, value_type pdata, key_type begin_key, key_type end_key, node_list_type* plist);
656 void build_leaf_nodes();
662 void remove_data_from_nodes(node_list_type* plist,
const value_type pdata);
663 void remove_data_from_chain(data_chain_type& chain,
const value_type pdata);
665 void clear_all_nodes();
667 #ifdef MDDS_UNIT_TEST 668 static bool has_data_pointer(
const node_list_type& node_list,
const value_type pdata);
669 static void print_leaf_value(
const leaf_value_type& v);
673 std::vector<nonleaf_node> m_nonleaf_node_pool;
675 segment_map_type m_segment_data;
682 data_node_map_type m_tagged_node_map;
684 nonleaf_node* m_root_node;
685 node_ptr m_left_leaf;
686 node_ptr m_right_leaf;
692 #include "segment_tree_def.inl" Definition: segment_tree.hpp:108
bool is_leaf
parent nonleaf_node
Definition: node.hpp:46
Definition: segment_tree.hpp:51
Definition: segment_tree.hpp:462
bool insert(key_type begin_key, key_type end_key, value_type pdata)
bool operator==(const segment_tree &r) const
bool is_tree_valid() const
Definition: segment_tree.hpp:536
Definition: rectangle_set.hpp:37
Definition: global.hpp:58
Definition: segment_tree.hpp:211
Definition: segment_tree.hpp:456
Definition: segment_tree.hpp:224
Definition: default_deleter.hpp:33
data_chain_type * data_chain
high range value (non-inclusive)
Definition: segment_tree.hpp:100
bool search(key_type point, search_result_type &result) const
Definition: segment_tree.hpp:486
Definition: segment_tree.hpp:503
Definition: segment_tree.hpp:96
key_type high
low range value (inclusive)
Definition: segment_tree.hpp:99
node_ptr next
previous sibling leaf node.
Definition: node.hpp:166
Definition: segment_tree.hpp:131