30 #ifndef __FASTJET_SEARCHTREE_HH__
31 #define __FASTJET_SEARCHTREE_HH__
36 #include "fastjet/internal/base.hh"
38 FASTJET_BEGIN_NAMESPACE
52 template<
class T>
class SearchTree {
57 class const_circulator;
60 SearchTree(
const std::vector<T> & init);
64 SearchTree(
const std::vector<T> & init,
unsigned int max_size);
67 void remove(
unsigned node_index);
68 void remove(
typename SearchTree::Node * node);
69 void remove(
typename SearchTree::circulator & circ);
74 circulator insert(
const T & value);
76 const Node & operator[](
int i)
const {
return _nodes[i];};
79 unsigned int size()
const {
return _nodes.size() - _available_nodes.size();}
82 void verify_structure();
83 void verify_structure_linear()
const;
84 void verify_structure_recursive(
const Node * ,
const Node * ,
const Node * )
const;
87 void print_elements();
93 inline unsigned int max_depth()
const {
return _max_depth;};
95 inline unsigned int max_depth()
const {
return 0;};
98 int loc(
const Node * node)
const ;
101 Node * _find_predecessor(
const Node *);
103 Node * _find_successor(
const Node *);
105 const Node & operator[](
unsigned int i)
const {
return _nodes[i];};
109 const_circulator somewhere()
const;
110 circulator somewhere();
114 void _initialize(
const std::vector<T> & init);
116 std::vector<Node> _nodes;
117 std::vector<Node *> _available_nodes;
119 unsigned int _n_removes;
126 void _do_initial_connections(
unsigned int this_one,
unsigned int scale,
127 unsigned int left_edge,
unsigned int right_edge,
132 unsigned int _max_depth;
144 template<
class T>
class SearchTree<T>::Node{
150 bool treelinks_null()
const {
151 return ((parent==0) && (left==0) && (right==0));};
154 inline void nullify_treelinks() {
162 void reset_parents_link_to_me(Node * XX);
173 template<
class T>
void SearchTree<T>::Node::reset_parents_link_to_me(
typename SearchTree<T>::Node * XX) {
174 if (parent == NULL) {
return;}
175 if (parent->right ==
this) {parent->right = XX;}
176 else {parent->left = XX;}
187 template<
class T>
class SearchTree<T>::circulator{
191 friend class SearchTree<T>::const_circulator;
192 friend class SearchTree<T>;
194 circulator() : _node(NULL) {}
196 circulator(Node * node) : _node(node) {}
198 const T * operator->()
const {
return &(_node->value);}
199 T * operator->() {
return &(_node->value);}
200 const T &
operator*()
const {
return _node->value;}
204 circulator & operator++() {
205 _node = _node->successor;
210 circulator operator++(
int) {
211 circulator tmp = *
this;
212 _node = _node->successor;
216 circulator & operator--() {
217 _node = _node->predecessor;
222 circulator operator--(
int) {
223 circulator tmp = *
this;
224 _node = _node->predecessor;
228 circulator next()
const {
229 return circulator(_node->successor);}
232 circulator previous()
const {
233 return circulator(_node->predecessor);}
235 bool operator!=(
const circulator & other)
const {
return other._node != _node;}
236 bool operator==(
const circulator & other)
const {
return other._node == _node;}
249 template<
class T>
class SearchTree<T>::const_circulator{
252 const_circulator() : _node(NULL) {}
254 const_circulator(
const Node * node) : _node(node) {}
255 const_circulator(
const circulator & circ) :_node(circ._node) {}
257 const T * operator->() {
return &(_node->value);}
258 const T &
operator*()
const {
return _node->value;}
261 const_circulator & operator++() {
262 _node = _node->successor;
267 const_circulator operator++(
int) {
268 const_circulator tmp = *
this;
269 _node = _node->successor;
274 const_circulator & operator--() {
275 _node = _node->predecessor;
280 const_circulator operator--(
int) {
281 const_circulator tmp = *
this;
282 _node = _node->predecessor;
286 const_circulator next()
const {
287 return const_circulator(_node->successor);}
290 const_circulator previous()
const {
291 return const_circulator(_node->predecessor);}
295 bool operator!=(
const const_circulator & other)
const {
return other._node != _node;}
296 bool operator==(
const const_circulator & other)
const {
return other._node == _node;}
308 template<
class T> SearchTree<T>::SearchTree(
const std::vector<T> & init,
309 unsigned int max_size) :
312 _available_nodes.reserve(max_size);
313 _available_nodes.resize(max_size - init.size());
314 for (
unsigned int i = init.size(); i < max_size; i++) {
315 _available_nodes[i-init.size()] = &(_nodes[i]);
323 template<
class T> SearchTree<T>::SearchTree(
const std::vector<T> & init) :
324 _nodes(init.size()), _available_nodes(0) {
327 _available_nodes.reserve(init.size());
333 template<
class T>
void SearchTree<T>::_initialize(
const std::vector<T> & init) {
336 unsigned n = init.size();
348 for (
unsigned int i = 1; i<n; i++) {
349 assert(!(init[i] < init[i-1]));
353 for(
unsigned int i = 0; i < n; i++) {
354 _nodes[i].value = init[i];
355 _nodes[i].predecessor = (& (_nodes[i])) - 1;
356 _nodes[i].successor = (& (_nodes[i])) + 1;
357 _nodes[i].nullify_treelinks();
360 _nodes[0].predecessor = (& (_nodes[n-1]));
361 _nodes[n-1].successor = (& (_nodes[0]));
364 unsigned int scale = (n+1)/2;
365 unsigned int top = std::min(n-1,scale);
366 _nodes[top].parent = NULL;
367 _top_node = &(_nodes[top]);
368 _do_initial_connections(top, scale, 0, n, 0);
377 template<
class T>
inline int SearchTree<T>::loc(
const Node * node)
const {
return node == NULL?
378 -999 : node - &(_nodes[0]);}
384 template<
class T>
void SearchTree<T>::_do_initial_connections(
385 unsigned int this_one,
387 unsigned int left_edge,
388 unsigned int right_edge,
394 _max_depth = max(depth, _max_depth);
398 unsigned int ref_new_scale = (scale+1)/2;
401 unsigned new_scale = ref_new_scale;
402 bool did_child =
false;
404 int left = this_one - new_scale;
406 if (left >= static_cast<int>(left_edge)
407 && _nodes[left].treelinks_null() ) {
408 _nodes[left].parent = &(_nodes[this_one]);
409 _nodes[this_one].left = &(_nodes[left]);
411 _do_initial_connections(left, new_scale, left_edge, this_one, depth+1);
416 unsigned int old_new_scale = new_scale;
417 new_scale = (old_new_scale + 1)/2;
419 if (new_scale == old_new_scale)
break;
421 if (!did_child) {_nodes[this_one].left = NULL;}
425 new_scale = ref_new_scale;
428 unsigned int right = this_one + new_scale;
429 if (right < right_edge && _nodes[right].treelinks_null()) {
430 _nodes[right].parent = &(_nodes[this_one]);
431 _nodes[this_one].right = &(_nodes[right]);
433 _do_initial_connections(right, new_scale, this_one+1,right_edge,depth+1);
438 unsigned int old_new_scale = new_scale;
439 new_scale = (old_new_scale + 1)/2;
441 if (new_scale == old_new_scale)
break;
443 if (!did_child) {_nodes[this_one].right = NULL;}
450 template<
class T>
void SearchTree<T>::remove(
unsigned int node_index) {
451 remove(&(_nodes[node_index]));
455 template<
class T>
void SearchTree<T>::remove(circulator & circ) {
462 template<
class T>
void SearchTree<T>::remove(
typename SearchTree<T>::Node * node) {
467 assert(!node->treelinks_null());
470 node->predecessor->successor = node->successor;
471 node->successor->predecessor = node->predecessor;
473 if (node->left == NULL && node->right == NULL) {
476 node->reset_parents_link_to_me(NULL);
478 }
else if (node->left != NULL && node->right == NULL){
480 node->reset_parents_link_to_me(node->left);
482 node->left->parent = node->parent;
484 if (_top_node == node) {_top_node = node->left;}
486 }
else if (node->left == NULL && node->right != NULL){
488 node->reset_parents_link_to_me(node->right);
490 node->right->parent = node->parent;
492 if (_top_node == node) {_top_node = node->right;}
499 bool use_predecessor = (_n_removes % 2 == 1);
500 if (use_predecessor) {
503 replacement = node->predecessor;
504 assert(replacement->right == NULL);
507 if (replacement != node->left) {
508 if (replacement->left != NULL) {
509 replacement->left->parent = replacement->parent;}
510 replacement->reset_parents_link_to_me(replacement->left);
511 replacement->left = node->left;
513 replacement->parent = node->parent;
514 replacement->right = node->right;
518 replacement = node->successor;
519 assert(replacement->left == NULL);
520 if (replacement != node->right) {
521 if (replacement->right != NULL) {
522 replacement->right->parent = replacement->parent;}
523 replacement->reset_parents_link_to_me(replacement->right);
524 replacement->right = node->right;
526 replacement->parent = node->parent;
527 replacement->left = node->left;
529 node->reset_parents_link_to_me(replacement);
532 if (node->left != replacement) {node->left->parent = replacement;}
533 if (node->right != replacement) {node->right->parent = replacement;}
536 if (_top_node == node) {_top_node = replacement;}
540 node->nullify_treelinks();
541 node->predecessor = NULL;
542 node->successor = NULL;
547 _available_nodes.push_back(node);
555 template<
class T>
typename SearchTree<T>::circulator SearchTree<T>::insert(
const T & value) {
557 assert(_available_nodes.size() > 0);
559 Node * node = _available_nodes.back();
560 _available_nodes.pop_back();
563 Node * location = _top_node;
564 Node * old_location = NULL;
568 unsigned int depth = 0;
570 while(location != NULL) {
574 old_location = location;
575 on_left = value < location->value;
576 if (on_left) {location = location->left;}
577 else {location = location->right;}
580 _max_depth = max(depth, _max_depth);
583 node->parent = old_location;
584 if (on_left) {node->parent->left = node;}
585 else {node->parent->right = node;}
589 node->predecessor = _find_predecessor(node);
590 if (node->predecessor != NULL) {
593 node->successor = node->predecessor->successor;
594 node->predecessor->successor = node;
595 node->successor->predecessor = node;
599 node->successor = _find_successor(node);
600 assert(node->successor != NULL);
602 node->predecessor = node->successor->predecessor;
603 node->successor->predecessor = node;
604 node->predecessor->successor = node;
607 return circulator(node);
612 template<
class T>
void SearchTree<T>::verify_structure() {
615 verify_structure_linear();
620 const Node * left_limit = _top_node;
621 while (left_limit->left != NULL) {left_limit = left_limit->left;}
622 const Node * right_limit = _top_node;
623 while (right_limit->right != NULL) {right_limit = right_limit->right;}
626 verify_structure_recursive(_top_node, left_limit, right_limit);
631 template<
class T>
void SearchTree<T>::verify_structure_recursive(
632 const typename SearchTree<T>::Node * element,
633 const typename SearchTree<T>::Node * left_limit,
634 const typename SearchTree<T>::Node * right_limit)
const {
636 assert(!(element->value < left_limit->value));
637 assert(!(right_limit->value < element->value));
639 const Node * left = element->left;
641 assert(!(element->value < left->value));
642 if (left != left_limit) {
644 verify_structure_recursive(left, left_limit, element);}
647 const Node * right = element->right;
649 assert(!(right->value < element->value));
650 if (right != right_limit) {
652 verify_structure_recursive(right, element, right_limit);}
657 template<
class T>
void SearchTree<T>::verify_structure_linear()
const {
663 for(
unsigned i = 0; i < _nodes.size(); i++) {
664 const typename SearchTree<T>::Node * node = &(_nodes[i]);
666 if (node->treelinks_null()) {n_null++;
continue;}
669 if (node->parent == NULL) {
676 assert((node->parent->left == node) ^ (node->parent->right == node));
682 if (node->left != NULL) {
683 assert(!(node->value < node->left->value ));}
686 if (node->right != NULL) {
687 assert(!(node->right->value < node->value ));}
690 assert(n_top == 1 || (n_top == 0 && size() <= 1) );
691 assert(n_null == _available_nodes.size() ||
692 (n_null == _available_nodes.size() + 1 && size() == 1));
697 template<
class T>
typename SearchTree<T>::Node * SearchTree<T>::_find_predecessor(
const typename SearchTree<T>::Node * node) {
699 typename SearchTree<T>::Node * newnode;
700 if (node->left != NULL) {
702 newnode = node->left;
703 while(newnode->right != NULL) {newnode = newnode->right;}
706 const typename SearchTree<T>::Node * lastnode = node;
707 newnode = node->parent;
710 while(newnode != NULL) {
711 if (newnode->right == lastnode) {
return newnode;}
713 newnode = newnode->parent;
721 template<
class T>
typename SearchTree<T>::Node * SearchTree<T>::_find_successor(
const typename SearchTree<T>::Node * node) {
723 typename SearchTree<T>::Node * newnode;
724 if (node->right != NULL) {
726 newnode = node->right;
727 while(newnode->left != NULL) {newnode = newnode->left;}
730 const typename SearchTree<T>::Node * lastnode = node;
731 newnode = node->parent;
734 while(newnode != NULL) {
735 if (newnode->left == lastnode) {
return newnode;}
737 newnode = newnode->parent;
746 template<
class T>
void SearchTree<T>::print_elements() {
747 typename SearchTree<T>::Node * base_node = &(_nodes[0]);
748 typename SearchTree<T>::Node * node = base_node;
750 int n = _nodes.size();
751 for(; node - base_node < n ; node++) {
752 printf(
"%4d parent:%4d left:%4d right:%4d pred:%4d succ:%4d value:%10.6f\n",loc(node), loc(node->parent), loc(node->left), loc(node->right), loc(node->predecessor),loc(node->successor),node->value);
757 template<
class T>
typename SearchTree<T>::circulator SearchTree<T>::somewhere() {
758 return circulator(_top_node);
763 template<
class T>
typename SearchTree<T>::const_circulator SearchTree<T>::somewhere()
const {
764 return const_circulator(_top_node);
768 FASTJET_END_NAMESPACE
770 #endif // __FASTJET_SEARCHTREE_HH__