/usr/include/mdds/segment_tree.hpp is in libmdds-dev 0.12.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 | /*************************************************************************
*
* Copyright (c) 2010-2014 Kohei Yoshida
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
************************************************************************/
#ifndef __MDDS_SEGMENTTREE_HPP__
#define __MDDS_SEGMENTTREE_HPP__
#include "mdds/node.hpp"
#include "mdds/hash_container/map.hpp"
#include "mdds/global.hpp"
#include <vector>
#include <list>
#include <iostream>
#include <map>
#include <boost/shared_ptr.hpp>
#include <boost/ptr_container/ptr_map.hpp>
#ifdef MDDS_UNIT_TEST
#include <sstream>
#endif
namespace mdds {
template<typename _Key, typename _Data>
class rectangle_set;
namespace __st {
template<typename T, typename _Inserter>
void descend_tree_for_search(
typename T::key_type point, const __st::node_base* pnode, _Inserter& result)
{
typedef typename T::node leaf_node;
typedef typename T::nonleaf_node nonleaf_node;
typedef typename T::key_type key_type;
typedef typename T::nonleaf_value_type nonleaf_value_type;
typedef typename T::leaf_value_type leaf_value_type;
typedef _Inserter inserter_type;
if (!pnode)
// This should never happen, but just in case.
return;
if (pnode->is_leaf)
{
result(static_cast<const leaf_node*>(pnode)->value_leaf.data_chain);
return;
}
const nonleaf_node* pnonleaf = static_cast<const nonleaf_node*>(pnode);
const nonleaf_value_type& v = pnonleaf->value_nonleaf;
if (point < v.low || v.high <= point)
// Query point is out-of-range.
return;
result(v.data_chain);
// Check the left child node first, then the right one.
__st::node_base* pchild = pnonleaf->left;
if (!pchild)
return;
assert(pnonleaf->right ? pchild->is_leaf == pnonleaf->right->is_leaf : true);
if (pchild->is_leaf)
{
// The child node are leaf nodes.
const leaf_value_type& vleft = static_cast<const leaf_node*>(pchild)->value_leaf;
if (point < vleft.key)
{
// Out-of-range. Nothing more to do.
return;
}
if (pnonleaf->right)
{
assert(pnonleaf->right->is_leaf);
const leaf_value_type& vright = static_cast<const leaf_node*>(pnonleaf->right)->value_leaf;
if (vright.key <= point)
// Follow the right node.
pchild = pnonleaf->right;
}
}
else
{
// This child nodes are non-leaf nodes.
const nonleaf_value_type& vleft =
static_cast<const nonleaf_node*>(pchild)->value_nonleaf;
if (point < vleft.low)
{
// Out-of-range. Nothing more to do.
return;
}
if (vleft.high <= point && pnonleaf->right)
// Follow the right child.
pchild = pnonleaf->right;
assert(static_cast<const nonleaf_node*>(pchild)->value_nonleaf.low <= point &&
point < static_cast<const nonleaf_node*>(pchild)->value_nonleaf.high);
}
descend_tree_for_search<T,_Inserter>(point, pchild, result);
}
} // namespace __st
template<typename _Key, typename _Data>
class segment_tree
{
friend class rectangle_set<_Key, _Data>;
public:
typedef _Key key_type;
typedef _Data data_type;
typedef size_t size_type;
typedef ::std::vector<data_type> search_result_type;
#ifdef MDDS_UNIT_TEST
struct segment_data
{
key_type begin_key;
key_type end_key;
data_type pdata;
segment_data(key_type _beg, key_type _end, data_type p) :
begin_key(_beg), end_key(_end), pdata(p) {}
bool operator==(const segment_data& r) const
{
return begin_key == r.begin_key && end_key == r.end_key && pdata == r.pdata;
}
bool operator!=(const segment_data& r) const
{
return !operator==(r);
}
};
struct segment_map_printer : public ::std::unary_function< ::std::pair<data_type, ::std::pair<key_type, key_type> >, void>
{
void operator() (const ::std::pair<data_type, ::std::pair<key_type, key_type> >& r) const
{
using namespace std;
cout << r.second.first << "-" << r.second.second << ": " << r.first->name << endl;
}
};
#endif
public:
typedef ::std::vector<data_type> data_chain_type;
typedef _mdds_unordered_map_type<data_type, ::std::pair<key_type, key_type> > segment_map_type;
typedef ::std::map<data_type, ::std::pair<key_type, key_type> > sorted_segment_map_type;
struct nonleaf_value_type
{
key_type low; /// low range value (inclusive)
key_type high; /// high range value (non-inclusive)
data_chain_type* data_chain;
bool operator== (const nonleaf_value_type& r) const
{
return low == r.low && high == r.high && data_chain == r.data_chain;
}
};
struct leaf_value_type
{
key_type key;
data_chain_type* data_chain;
bool operator== (const leaf_value_type& r) const
{
return key == r.key && data_chain == r.data_chain;
}
};
struct fill_nonleaf_value_handler;
struct init_handler;
struct dispose_handler;
#ifdef MDDS_UNIT_TEST
struct to_string_handler;
#endif
typedef __st::node<segment_tree> node;
typedef typename node::node_ptr node_ptr;
typedef typename __st::nonleaf_node<segment_tree> nonleaf_node;
struct fill_nonleaf_value_handler
{
void operator() (__st::nonleaf_node<segment_tree>& _self, const __st::node_base* left_node, const __st::node_base* right_node)
{
// Parent node should carry the range of all of its child nodes.
if (left_node)
{
_self.value_nonleaf.low = left_node->is_leaf ?
static_cast<const node*>(left_node)->value_leaf.key :
static_cast<const nonleaf_node*>(left_node)->value_nonleaf.low;
}
else
{
// Having a left node is prerequisite.
throw general_error("segment_tree::fill_nonleaf_value_handler: Having a left node is prerequisite.");
}
if (right_node)
{
if (right_node->is_leaf)
{
// When the child nodes are leaf nodes, the upper bound
// must be the value of the node that comes after the
// right leaf node (if such node exists).
const node* p = static_cast<const node*>(right_node);
if (p->next)
_self.value_nonleaf.high = p->next->value_leaf.key;
else
_self.value_nonleaf.high = p->value_leaf.key;
}
else
{
_self.value_nonleaf.high = static_cast<const nonleaf_node*>(right_node)->value_nonleaf.high;
}
}
else
{
_self.value_nonleaf.high = left_node->is_leaf ?
static_cast<const node*>(left_node)->value_leaf.key :
static_cast<const nonleaf_node*>(left_node)->value_nonleaf.high;
}
}
};
#ifdef MDDS_UNIT_TEST
struct to_string_handler
{
std::string operator() (const node& _self) const
{
std::ostringstream os;
os << "[" << _self.value_leaf.key << "] ";
return os.str();
}
std::string operator() (const __st::nonleaf_node<segment_tree>& _self) const
{
std::ostringstream os;
os << "[" << _self.value_nonleaf.low << "-" << _self.value_nonleaf.high << ")";
if (_self.value_nonleaf.data_chain)
{
os << " { ";
typename data_chain_type::const_iterator
itr,
itr_beg = _self.value_nonleaf.data_chain->begin(),
itr_end = _self.value_nonleaf.data_chain->end();
for (itr = itr_beg; itr != itr_end; ++itr)
{
if (itr != itr_beg)
os << ", ";
os << (*itr)->name;
}
os << " }";
}
os << " ";
return os.str();
}
};
#endif
struct init_handler
{
void operator() (node& _self)
{
_self.value_leaf.data_chain = NULL;
}
void operator() (__st::nonleaf_node<segment_tree>& _self)
{
_self.value_nonleaf.data_chain = NULL;
}
};
struct dispose_handler
{
void operator() (node& _self)
{
delete _self.value_leaf.data_chain;
}
void operator() (__st::nonleaf_node<segment_tree>& _self)
{
delete _self.value_nonleaf.data_chain;
}
};
#ifdef MDDS_UNIT_TEST
struct node_printer : public ::std::unary_function<const __st::node_base*, void>
{
void operator() (const __st::node_base* p) const
{
if (p->is_leaf)
std::cout << static_cast<const node*>(p)->to_string() << " ";
else
std::cout << static_cast<const nonleaf_node*>(p)->to_string() << " ";
}
};
#endif
private:
/**
* This base class takes care of collecting data chain pointers during
* tree descend for search.
*/
class search_result_base
{
public:
typedef ::std::vector<data_chain_type*> res_chains_type;
typedef ::boost::shared_ptr<res_chains_type> res_chains_ptr;
public:
search_result_base() :
mp_res_chains(static_cast<res_chains_type*>(NULL)) {}
search_result_base(const search_result_base& r) :
mp_res_chains(r.mp_res_chains) {}
size_t size() const
{
size_t combined = 0;
if (!mp_res_chains)
return combined;
typename res_chains_type::const_iterator
itr = mp_res_chains->begin(), itr_end = mp_res_chains->end();
for (; itr != itr_end; ++itr)
combined += (*itr)->size();
return combined;
}
void push_back_chain(data_chain_type* chain)
{
if (!chain || chain->empty())
return;
if (!mp_res_chains)
mp_res_chains.reset(new res_chains_type);
mp_res_chains->push_back(chain);
}
res_chains_ptr& get_res_chains() { return mp_res_chains; }
private:
res_chains_ptr mp_res_chains;
};
class iterator_base
{
protected:
typedef typename search_result_base::res_chains_type res_chains_type;
typedef typename search_result_base::res_chains_ptr res_chains_ptr;
iterator_base(const res_chains_ptr& p) :
mp_res_chains(p), m_end_pos(true) {}
public:
typedef ::std::bidirectional_iterator_tag iterator_category;
typedef typename data_chain_type::value_type value_type;
typedef typename data_chain_type::pointer pointer;
typedef typename data_chain_type::reference reference;
typedef typename data_chain_type::difference_type difference_type;
iterator_base() :
mp_res_chains(static_cast<res_chains_type*>(NULL)), m_end_pos(true) {}
iterator_base(const iterator_base& r) :
mp_res_chains(r.mp_res_chains),
m_cur_chain(r.m_cur_chain),
m_cur_pos_in_chain(r.m_cur_pos_in_chain),
m_end_pos(r.m_end_pos) {}
iterator_base& operator= (const iterator_base& r)
{
mp_res_chains = r.mp_res_chains;
m_cur_chain = r.m_cur_chain;
m_cur_pos_in_chain = r.m_cur_pos_in_chain;
m_end_pos = r.m_end_pos;
return *this;
}
typename data_chain_type::value_type* operator++ ()
{
// We don't check for end position flag for performance reasons.
// The caller is responsible for making sure not to increment past
// end position.
// When reaching the end position, the internal iterators still
// need to be pointing at the last item before the end position.
// This is why we need to make copies of the iterators, and copy
// them back once done.
typename data_chain_type::iterator cur_pos_in_chain = m_cur_pos_in_chain;
if (++cur_pos_in_chain == (*m_cur_chain)->end())
{
// End of current chain. Inspect the next chain if exists.
typename res_chains_type::iterator cur_chain = m_cur_chain;
++cur_chain;
if (cur_chain == mp_res_chains->end())
{
m_end_pos = true;
return NULL;
}
m_cur_chain = cur_chain;
m_cur_pos_in_chain = (*m_cur_chain)->begin();
}
else
++m_cur_pos_in_chain;
return operator->();
}
typename data_chain_type::value_type* operator-- ()
{
if (!mp_res_chains)
return NULL;
if (m_end_pos)
{
m_end_pos = false;
return &(*m_cur_pos_in_chain);
}
if (m_cur_pos_in_chain == (*m_cur_chain)->begin())
{
if (m_cur_chain == mp_res_chains->begin())
{
// Already at the first data chain. Don't move the iterator position.
return NULL;
}
--m_cur_chain;
m_cur_pos_in_chain = (*m_cur_chain)->end();
}
--m_cur_pos_in_chain;
return operator->();
}
bool operator== (const iterator_base& r) const
{
if (mp_res_chains.get())
{
// non-empty result set.
return mp_res_chains.get() == r.mp_res_chains.get() &&
m_cur_chain == r.m_cur_chain && m_cur_pos_in_chain == r.m_cur_pos_in_chain &&
m_end_pos == r.m_end_pos;
}
// empty result set.
if (r.mp_res_chains.get())
return false;
return m_end_pos == r.m_end_pos;
}
bool operator!= (const iterator_base& r) const { return !operator==(r); }
typename data_chain_type::value_type& operator*()
{
return *m_cur_pos_in_chain;
}
typename data_chain_type::value_type* operator->()
{
return &(*m_cur_pos_in_chain);
}
protected:
void move_to_front()
{
if (!mp_res_chains)
{
// Empty data set.
m_end_pos = true;
return;
}
// We assume that there is at least one chain list, and no
// empty chain list exists. So, skip the check.
m_cur_chain = mp_res_chains->begin();
m_cur_pos_in_chain = (*m_cur_chain)->begin();
m_end_pos = false;
}
void move_to_end()
{
m_end_pos = true;
if (!mp_res_chains)
// Empty data set.
return;
m_cur_chain = mp_res_chains->end();
--m_cur_chain;
m_cur_pos_in_chain = (*m_cur_chain)->end();
--m_cur_pos_in_chain;
}
private:
res_chains_ptr mp_res_chains;
typename res_chains_type::iterator m_cur_chain;
typename data_chain_type::iterator m_cur_pos_in_chain;
bool m_end_pos:1;
};
public:
class search_result : public search_result_base
{
typedef typename search_result_base::res_chains_type res_chains_type;
typedef typename search_result_base::res_chains_ptr res_chains_ptr;
public:
class iterator : public iterator_base
{
friend class segment_tree<_Key,_Data>::search_result;
private:
iterator(const res_chains_ptr& p) : iterator_base(p) {}
public:
iterator() : iterator_base() {}
};
typename search_result::iterator begin()
{
typename search_result::iterator itr(search_result_base::get_res_chains());
itr.move_to_front();
return itr;
}
typename search_result::iterator end()
{
typename search_result::iterator itr(search_result_base::get_res_chains());
itr.move_to_end();
return itr;
}
};
class search_result_vector_inserter : public ::std::unary_function<data_chain_type*, void>
{
public:
search_result_vector_inserter(search_result_type& result) : m_result(result) {}
void operator() (data_chain_type* node_data)
{
if (!node_data)
return;
typename data_chain_type::const_iterator itr = node_data->begin(), itr_end = node_data->end();
for (; itr != itr_end; ++itr)
m_result.push_back(*itr);
}
private:
search_result_type& m_result;
};
class search_result_inserter : public ::std::unary_function<data_chain_type*, void>
{
public:
search_result_inserter(search_result_base& result) : m_result(result) {}
void operator() (data_chain_type* node_data)
{
if (!node_data)
return;
m_result.push_back_chain(node_data);
}
private:
search_result_base& m_result;
};
segment_tree();
segment_tree(const segment_tree& r);
~segment_tree();
/**
* Equality between two segment_tree instances is evaluated by comparing
* the segments that they store. The trees are not compared.
*/
bool operator==(const segment_tree& r) const;
bool operator!=(const segment_tree& r) const { return !operator==(r); }
/**
* Check whether or not the internal tree is in a valid state. The tree
* must be valid in order to perform searches.
*
* @return true if the tree is valid, false otherwise.
*/
bool is_tree_valid() const { return m_valid_tree; }
/**
* Build or re-build tree based on the current set of segments.
*/
void build_tree();
/**
* Insert a new segment.
*
* @param begin_key begin point of the segment. The value is inclusive.
* @param end_key end point of the segment. The value is non-inclusive.
* @param pdata pointer to the data instance associated with this segment.
* Note that <i>the caller must manage the life cycle of the
* data instance</i>.
*/
bool insert(key_type begin_key, key_type end_key, data_type pdata);
/**
* Search the tree and collect all segments that include a specified
* point.
*
* @param point specified point value
* @param result doubly-linked list of data instances associated with
* the segments that include the specified point.
* <i>Note that the search result gets appended to the
* list; the list will not get emptied on each
* search.</i> It is caller's responsibility to empty
* the list before passing it to this method in case the
* caller so desires.
*
* @return true if the search is performed successfully, false if the
* search has ended prematurely due to error conditions.
*/
bool search(key_type point, search_result_type& result) const;
/**
* Search the tree and collect all segments that include a specified
* point.
*
* @param point specified point value
*
* @return object containing the result of the search, which can be
* accessed via iterator.
*/
search_result search(key_type point) const;
/**
* Remove a segment by the data pointer. This will <i>not</i> invalidate
* the tree; however, if you have removed lots of segments, you might want
* to re-build the tree to shrink its size.
*/
void remove(data_type pdata);
/**
* Remove all segments data.
*/
void clear();
/**
* Return the number of segments currently stored in this container.
*/
size_t size() const;
/**
* Return whether or not the container stores any segments or none at all.
*/
bool empty() const;
/**
* Return the number of leaf nodes.
*
* @return number of leaf nodes.
*/
size_t leaf_size() const;
#ifdef MDDS_UNIT_TEST
void dump_tree() const;
void dump_leaf_nodes() const;
void dump_segment_data() const;
bool verify_node_lists() const;
struct leaf_node_check
{
key_type key;
data_chain_type data_chain;
};
bool verify_leaf_nodes(const ::std::vector<leaf_node_check>& checks) const;
/**
* Verify the validity of the segment data array.
*
* @param checks null-terminated array of expected values. The last item
* must have a NULL pdata value to terminate the array.
*/
bool verify_segment_data(const segment_map_type& checks) const;
#endif
private:
/**
* To be called from rectangle_set.
*/
void search(key_type point, search_result_base& result) const;
typedef ::std::vector<__st::node_base*> node_list_type;
typedef ::boost::ptr_map<data_type, node_list_type> data_node_map_type;
static void create_leaf_node_instances(const ::std::vector<key_type>& keys, node_ptr& left, node_ptr& right);
/**
* Descend the tree from the root node, and mark appropriate nodes, both
* leaf and non-leaf, based on segment's end points. When marking nodes,
* record their positions as a list of node pointers.
*/
void descend_tree_and_mark(
__st::node_base* pnode, data_type pdata, key_type begin_key, key_type end_key, node_list_type* plist);
void build_leaf_nodes();
/**
* Go through the list of nodes, and remove the specified data pointer
* value from the nodes.
*/
void remove_data_from_nodes(node_list_type* plist, const data_type pdata);
void remove_data_from_chain(data_chain_type& chain, const data_type pdata);
void clear_all_nodes();
#ifdef MDDS_UNIT_TEST
static bool has_data_pointer(const node_list_type& node_list, const data_type pdata);
static void print_leaf_value(const leaf_value_type& v);
#endif
private:
std::vector<nonleaf_node> m_nonleaf_node_pool;
segment_map_type m_segment_data;
/**
* For each data pointer, it keeps track of all nodes, leaf or non-leaf,
* that stores the data pointer label. This data is used when removing
* segments by the data pointer value.
*/
data_node_map_type m_tagged_node_map;
nonleaf_node* m_root_node;
node_ptr m_left_leaf;
node_ptr m_right_leaf;
bool m_valid_tree:1;
};
}
#include "segment_tree_def.inl"
#endif
|