28 #ifndef INCLUDED_MDDS_SEGMENTTREE_HPP 29 #define INCLUDED_MDDS_SEGMENTTREE_HPP 31 #include "mdds/node.hpp" 32 #include "mdds/global.hpp" 37 #include <unordered_map> 46 template<
typename _Key,
typename _Value>
49 template<
typename _Key,
typename _Value>
54 typedef _Key key_type;
55 typedef _Value value_type;
56 typedef size_t size_type;
57 typedef ::std::vector<value_type> search_result_type;
66 segment_data(key_type _beg, key_type _end, value_type p) :
67 begin_key(_beg), end_key(_end), pdata(p) {}
71 return begin_key == r.begin_key && end_key == r.end_key && pdata == r.pdata;
74 bool operator!=(
const segment_data& r)
const 80 struct segment_map_printer :
public ::std::unary_function< ::std::pair<value_type, ::std::pair<key_type, key_type> >, void>
82 void operator() (const ::std::pair<value_type, ::std::pair<key_type, key_type> >& r)
const 85 cout << r.second.first <<
"-" << r.second.second <<
": " << r.first->name << endl;
91 typedef ::std::vector<value_type> data_chain_type;
92 typedef std::unordered_map<value_type, ::std::pair<key_type, key_type> > segment_map_type;
93 typedef ::std::map<value_type, ::std::pair<key_type, key_type> > sorted_segment_map_type;
103 return low == r.low && high == r.
high && data_chain == r.
data_chain;
110 data_chain_type* data_chain;
114 return key == r.key && data_chain == r.data_chain;
121 #ifdef MDDS_UNIT_TEST 122 struct to_string_handler;
126 typedef typename node::node_ptr node_ptr;
137 _self.value_nonleaf.low = left_node->
is_leaf ?
138 static_cast<const node*
>(left_node)->value_leaf.key :
139 static_cast<const nonleaf_node*>(left_node)->value_nonleaf.low;
144 throw general_error(
"segment_tree::fill_nonleaf_value_handler: Having a left node is prerequisite.");
155 const node* p =
static_cast<const node*
>(right_node);
157 _self.value_nonleaf.high = p->
next->value_leaf.key;
159 _self.value_nonleaf.high = p->value_leaf.key;
163 _self.value_nonleaf.high =
static_cast<const nonleaf_node*
>(right_node)->value_nonleaf.high;
168 _self.value_nonleaf.high = left_node->
is_leaf ?
169 static_cast<const node*
>(left_node)->value_leaf.key :
170 static_cast<const nonleaf_node*>(left_node)->value_nonleaf.high;
175 #ifdef MDDS_UNIT_TEST 176 struct to_string_handler
178 std::string operator() (
const node& _self)
const 180 std::ostringstream os;
181 os <<
"[" << _self.value_leaf.key <<
"] ";
187 std::ostringstream os;
188 os <<
"[" << _self.value_nonleaf.low <<
"-" << _self.value_nonleaf.high <<
")";
189 if (_self.value_nonleaf.data_chain)
192 typename data_chain_type::const_iterator
194 itr_beg = _self.value_nonleaf.data_chain->begin(),
195 itr_end = _self.value_nonleaf.data_chain->end();
196 for (itr = itr_beg; itr != itr_end; ++itr)
212 void operator() (node& _self)
214 _self.value_leaf.data_chain =
nullptr;
219 _self.value_nonleaf.data_chain =
nullptr;
225 void operator() (node& _self)
227 delete _self.value_leaf.data_chain;
232 delete _self.value_nonleaf.data_chain;
236 #ifdef MDDS_UNIT_TEST 237 struct node_printer :
public ::std::unary_function<const __st::node_base*, void>
242 std::cout << static_cast<const node*>(p)->to_string() <<
" ";
244 std::cout << static_cast<const nonleaf_node*>(p)->to_string() <<
" ";
255 class search_result_base
258 typedef std::vector<data_chain_type*> res_chains_type;
259 typedef std::shared_ptr<res_chains_type> res_chains_ptr;
262 search_result_base() :
263 mp_res_chains(static_cast<res_chains_type*>(
nullptr)) {}
265 search_result_base(
const search_result_base& r) :
266 mp_res_chains(r.mp_res_chains) {}
274 typename res_chains_type::const_iterator
275 itr = mp_res_chains->begin(), itr_end = mp_res_chains->end();
276 for (; itr != itr_end; ++itr)
277 combined += (*itr)->size();
281 void push_back_chain(data_chain_type* chain)
283 if (!chain || chain->empty())
287 mp_res_chains.reset(
new res_chains_type);
288 mp_res_chains->push_back(chain);
291 res_chains_ptr& get_res_chains() {
return mp_res_chains; }
294 res_chains_ptr mp_res_chains;
300 typedef typename search_result_base::res_chains_type res_chains_type;
301 typedef typename search_result_base::res_chains_ptr res_chains_ptr;
303 iterator_base(
const res_chains_ptr& p) :
304 mp_res_chains(p), m_end_pos(
true) {}
307 typedef ::std::bidirectional_iterator_tag iterator_category;
308 typedef typename data_chain_type::value_type value_type;
309 typedef typename data_chain_type::pointer pointer;
310 typedef typename data_chain_type::reference reference;
311 typedef typename data_chain_type::difference_type difference_type;
314 mp_res_chains(static_cast<res_chains_type*>(
nullptr)), m_end_pos(
true) {}
316 iterator_base(
const iterator_base& r) :
317 mp_res_chains(r.mp_res_chains),
318 m_cur_chain(r.m_cur_chain),
319 m_cur_pos_in_chain(r.m_cur_pos_in_chain),
320 m_end_pos(r.m_end_pos) {}
322 iterator_base& operator= (
const iterator_base& r)
324 mp_res_chains = r.mp_res_chains;
325 m_cur_chain = r.m_cur_chain;
326 m_cur_pos_in_chain = r.m_cur_pos_in_chain;
327 m_end_pos = r.m_end_pos;
331 typename data_chain_type::value_type* operator++ ()
342 typename data_chain_type::iterator cur_pos_in_chain = m_cur_pos_in_chain;
344 if (++cur_pos_in_chain == (*m_cur_chain)->end())
347 typename res_chains_type::iterator cur_chain = m_cur_chain;
349 if (cur_chain == mp_res_chains->end())
354 m_cur_chain = cur_chain;
355 m_cur_pos_in_chain = (*m_cur_chain)->begin();
358 ++m_cur_pos_in_chain;
363 typename data_chain_type::value_type* operator-- ()
371 return &(*m_cur_pos_in_chain);
374 if (m_cur_pos_in_chain == (*m_cur_chain)->begin())
376 if (m_cur_chain == mp_res_chains->begin())
382 m_cur_pos_in_chain = (*m_cur_chain)->end();
384 --m_cur_pos_in_chain;
388 bool operator== (
const iterator_base& r)
const 390 if (mp_res_chains.get())
393 return mp_res_chains.get() == r.mp_res_chains.get() &&
394 m_cur_chain == r.m_cur_chain && m_cur_pos_in_chain == r.m_cur_pos_in_chain &&
395 m_end_pos == r.m_end_pos;
399 if (r.mp_res_chains.get())
401 return m_end_pos == r.m_end_pos;
404 bool operator!= (
const iterator_base& r)
const {
return !
operator==(r); }
406 typename data_chain_type::value_type& operator*()
408 return *m_cur_pos_in_chain;
411 typename data_chain_type::value_type* operator->()
413 return &(*m_cur_pos_in_chain);
428 m_cur_chain = mp_res_chains->begin();
429 m_cur_pos_in_chain = (*m_cur_chain)->begin();
440 m_cur_chain = mp_res_chains->end();
442 m_cur_pos_in_chain = (*m_cur_chain)->end();
443 --m_cur_pos_in_chain;
447 res_chains_ptr mp_res_chains;
448 typename res_chains_type::iterator m_cur_chain;
449 typename data_chain_type::iterator m_cur_pos_in_chain;
457 typedef typename search_result_base::res_chains_type res_chains_type;
458 typedef typename search_result_base::res_chains_ptr res_chains_ptr;
465 iterator(const res_chains_ptr& p) : iterator_base(p) {}
489 void operator() (data_chain_type* node_data)
494 typename data_chain_type::const_iterator itr = node_data->begin(), itr_end = node_data->end();
495 for (; itr != itr_end; ++itr)
496 m_result.push_back(*itr);
499 search_result_type& m_result;
506 void operator() (data_chain_type* node_data)
511 m_result.push_back_chain(node_data);
514 search_result_base& m_result;
551 bool insert(key_type begin_key, key_type end_key, value_type pdata);
569 bool search(key_type point, search_result_type& result)
const;
580 search_result
search(key_type point)
const;
589 void remove(value_type value);
613 #ifdef MDDS_UNIT_TEST 614 void dump_tree()
const;
615 void dump_leaf_nodes()
const;
616 void dump_segment_data()
const;
617 bool verify_node_lists()
const;
619 struct leaf_node_check
622 data_chain_type data_chain;
625 bool verify_leaf_nodes(const ::std::vector<leaf_node_check>& checks)
const;
633 bool verify_segment_data(
const segment_map_type& checks)
const;
640 void search(key_type point, search_result_base& result)
const;
642 typedef std::vector<__st::node_base*> node_list_type;
643 typedef std::map<value_type, std::unique_ptr<node_list_type>> data_node_map_type;
645 static void create_leaf_node_instances(const ::std::vector<key_type>& keys, node_ptr& left, node_ptr& right);
652 void descend_tree_and_mark(
653 __st::node_base* pnode, value_type pdata, key_type begin_key, key_type end_key, node_list_type* plist);
655 void build_leaf_nodes();
661 void remove_data_from_nodes(node_list_type* plist,
const value_type pdata);
662 void remove_data_from_chain(data_chain_type& chain,
const value_type pdata);
664 void clear_all_nodes();
666 #ifdef MDDS_UNIT_TEST 667 static bool has_data_pointer(
const node_list_type& node_list,
const value_type pdata);
668 static void print_leaf_value(
const leaf_value_type& v);
672 std::vector<nonleaf_node> m_nonleaf_node_pool;
674 segment_map_type m_segment_data;
681 data_node_map_type m_tagged_node_map;
683 nonleaf_node* m_root_node;
684 node_ptr m_left_leaf;
685 node_ptr m_right_leaf;
691 #include "segment_tree_def.inl" Definition: segment_tree.hpp:107
bool is_leaf
parent nonleaf_node
Definition: node.hpp:46
Definition: segment_tree.hpp:50
Definition: segment_tree.hpp:461
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:535
Definition: rectangle_set.hpp:37
Definition: global.hpp:58
Definition: segment_tree.hpp:210
Definition: segment_tree.hpp:455
Definition: segment_tree.hpp:223
Definition: flat_segment_tree.hpp:46
data_chain_type * data_chain
high range value (non-inclusive)
Definition: segment_tree.hpp:99
bool search(key_type point, search_result_type &result) const
Definition: segment_tree.hpp:485
Definition: segment_tree.hpp:502
Definition: segment_tree.hpp:95
key_type high
low range value (inclusive)
Definition: segment_tree.hpp:98
node_ptr next
previous sibling leaf node.
Definition: node.hpp:166
Definition: segment_tree.hpp:130