32#ifndef __FASTJET_SEARCHTREE_HH__
33#define __FASTJET_SEARCHTREE_HH__
38#include "fastjet/internal/base.hh"
40FASTJET_BEGIN_NAMESPACE
66 SearchTree(
const std::vector<T> & init,
unsigned int max_size);
78 const Node & operator[](
int i)
const {
return _nodes[i];};
81 unsigned int size()
const {
return _nodes.size() - _available_nodes.size();}
84 void verify_structure();
85 void verify_structure_linear()
const;
86 void verify_structure_recursive(
const Node * ,
const Node * ,
const Node * )
const;
89 void print_elements();
93#ifdef __FASTJET_SEARCHTREE_TRACK_DEPTH
95 inline unsigned int max_depth()
const {
return _max_depth;};
97 inline unsigned int max_depth()
const {
return 0;};
100 int loc(
const Node * node)
const ;
103 Node * _find_predecessor(
const Node *);
105 Node * _find_successor(
const Node *);
107 const Node & operator[](
unsigned int i)
const {
return _nodes[i];};
111 const_circulator somewhere()
const;
112 circulator somewhere();
116 void _initialize(
const std::vector<T> & init);
118 std::vector<Node> _nodes;
119 std::vector<Node *> _available_nodes;
121 unsigned int _n_removes;
128 void _do_initial_connections(
unsigned int this_one,
unsigned int scale,
129 unsigned int left_edge,
unsigned int right_edge,
133#ifdef __FASTJET_SEARCHTREE_TRACK_DEPTH
134 unsigned int _max_depth;
153 return ((parent==0) && (left==0) && (right==0));};
164 void reset_parents_link_to_me(
Node * XX);
176 if (parent == NULL) {
return;}
177 if (parent->right ==
this) {parent->right = XX;}
178 else {parent->left = XX;}
206 const T * operator->()
const {
return &(_node->value);}
207 T * operator->() {
return &(_node->value);}
208 const T &
operator*()
const {
return _node->value;}
213 _node = _node->successor;
220 _node = _node->successor;
225 _node = _node->predecessor;
232 _node = _node->predecessor;
244 bool operator==(
const circulator & other)
const {
return other._node == _node;}
265 const T * operator->() {
return &(_node->value);}
266 const T &
operator*()
const {
return _node->value;}
270 _node = _node->successor;
277 _node = _node->successor;
283 _node = _node->predecessor;
290 _node = _node->predecessor;
304 bool operator==(
const const_circulator & other)
const {
return other._node == _node;}
317 unsigned int max_size) :
320 _available_nodes.reserve(max_size);
321 _available_nodes.resize(max_size - init.size());
322 for (
unsigned int i = init.size(); i < max_size; i++) {
323 _available_nodes[i-init.size()] = &(_nodes[i]);
332 _nodes(init.size()), _available_nodes(0) {
335 _available_nodes.reserve(init.size());
344 unsigned n = init.
size();
350#ifdef __FASTJET_SEARCHTREE_TRACK_DEPTH
356 for (
unsigned int i = 1; i<n; i++) {
357 assert(!(init[i] < init[i-1]));
361 for(
unsigned int i = 0; i < n; i++) {
362 _nodes[i].value = init[i];
363 _nodes[i].predecessor = (& (_nodes[i])) - 1;
364 _nodes[i].successor = (& (_nodes[i])) + 1;
365 _nodes[i].nullify_treelinks();
368 _nodes[0].predecessor = (& (_nodes[n-1]));
369 _nodes[n-1].successor = (& (_nodes[0]));
372 unsigned int scale = (n+1)/2;
373 unsigned int top = std::min(n-1,scale);
374 _nodes[top].parent = NULL;
375 _top_node = &(_nodes[top]);
376 _do_initial_connections(top, scale, 0, n, 0);
385template<
class T>
inline int SearchTree<T>::loc(
const Node * node)
const {
return node == NULL?
386 -999 : node - &(_nodes[0]);}
392template<
class T>
void SearchTree<T>::_do_initial_connections(
393 unsigned int this_one,
395 unsigned int left_edge,
396 unsigned int right_edge,
400#ifdef __FASTJET_SEARCHTREE_TRACK_DEPTH
402 _max_depth = max(depth, _max_depth);
406 unsigned int ref_new_scale = (scale+1)/2;
409 unsigned new_scale = ref_new_scale;
410 bool did_child =
false;
412 int left = this_one - new_scale;
414 if (left >=
static_cast<int>(left_edge)
415 && _nodes[left].treelinks_null() ) {
416 _nodes[left].parent = &(_nodes[this_one]);
417 _nodes[this_one].left = &(_nodes[left]);
419 _do_initial_connections(left, new_scale, left_edge, this_one, depth+1);
424 unsigned int old_new_scale = new_scale;
425 new_scale = (old_new_scale + 1)/2;
427 if (new_scale == old_new_scale)
break;
429 if (!did_child) {_nodes[this_one].left = NULL;}
433 new_scale = ref_new_scale;
436 unsigned int right = this_one + new_scale;
437 if (right < right_edge && _nodes[right].treelinks_null()) {
438 _nodes[right].parent = &(_nodes[this_one]);
439 _nodes[this_one].right = &(_nodes[right]);
441 _do_initial_connections(right, new_scale, this_one+1,right_edge,depth+1);
446 unsigned int old_new_scale = new_scale;
447 new_scale = (old_new_scale + 1)/2;
449 if (new_scale == old_new_scale)
break;
451 if (!did_child) {_nodes[this_one].right = NULL;}
459 remove(&(_nodes[node_index]));
475 assert(!node->treelinks_null());
478 node->predecessor->successor = node->successor;
479 node->successor->predecessor = node->predecessor;
481 if (node->left == NULL && node->right == NULL) {
484 node->reset_parents_link_to_me(NULL);
486 }
else if (node->left != NULL && node->right == NULL){
488 node->reset_parents_link_to_me(node->left);
490 node->left->parent = node->parent;
492 if (_top_node == node) {_top_node = node->left;}
494 }
else if (node->left == NULL && node->right != NULL){
496 node->reset_parents_link_to_me(node->right);
498 node->right->parent = node->parent;
500 if (_top_node == node) {_top_node = node->right;}
507 bool use_predecessor = (_n_removes % 2 == 1);
508 if (use_predecessor) {
511 replacement = node->predecessor;
512 assert(replacement->right == NULL);
515 if (replacement != node->left) {
516 if (replacement->left != NULL) {
517 replacement->left->parent = replacement->parent;}
518 replacement->reset_parents_link_to_me(replacement->left);
519 replacement->left = node->left;
521 replacement->parent = node->parent;
522 replacement->right = node->right;
526 replacement = node->successor;
527 assert(replacement->left == NULL);
528 if (replacement != node->right) {
529 if (replacement->right != NULL) {
530 replacement->right->parent = replacement->parent;}
531 replacement->reset_parents_link_to_me(replacement->right);
532 replacement->right = node->right;
534 replacement->parent = node->parent;
535 replacement->left = node->left;
537 node->reset_parents_link_to_me(replacement);
540 if (node->left != replacement) {node->left->parent = replacement;}
541 if (node->right != replacement) {node->right->parent = replacement;}
544 if (_top_node == node) {_top_node = replacement;}
548 node->nullify_treelinks();
549 node->predecessor = NULL;
550 node->successor = NULL;
555 _available_nodes.push_back(node);
565 assert(_available_nodes.size() > 0);
567 Node * node = _available_nodes.back();
568 _available_nodes.pop_back();
571 Node * location = _top_node;
572 Node * old_location = NULL;
575#ifdef __FASTJET_SEARCHTREE_TRACK_DEPTH
576 unsigned int depth = 0;
578 while(location != NULL) {
579#ifdef __FASTJET_SEARCHTREE_TRACK_DEPTH
582 old_location = location;
583 on_left = value < location->value;
584 if (on_left) {location = location->left;}
585 else {location = location->right;}
587#ifdef __FASTJET_SEARCHTREE_TRACK_DEPTH
588 _max_depth = max(depth, _max_depth);
591 node->parent = old_location;
592 if (on_left) {node->parent->left = node;}
593 else {node->parent->right = node;}
597 node->predecessor = _find_predecessor(node);
598 if (node->predecessor != NULL) {
601 node->successor = node->predecessor->successor;
602 node->predecessor->successor = node;
603 node->successor->predecessor = node;
607 node->successor = _find_successor(node);
608 assert(node->successor != NULL);
610 node->predecessor = node->successor->predecessor;
611 node->successor->predecessor = node;
612 node->predecessor->successor = node;
623 verify_structure_linear();
628 const Node * left_limit = _top_node;
629 while (left_limit->left != NULL) {left_limit = left_limit->left;}
630 const Node * right_limit = _top_node;
631 while (right_limit->right != NULL) {right_limit = right_limit->right;}
634 verify_structure_recursive(_top_node, left_limit, right_limit);
644 assert(!(element->value < left_limit->value));
645 assert(!(right_limit->value < element->value));
647 const Node * left = element->left;
649 assert(!(element->value < left->value));
650 if (left != left_limit) {
652 verify_structure_recursive(left, left_limit, element);}
655 const Node * right = element->right;
657 assert(!(right->value < element->value));
658 if (right != right_limit) {
660 verify_structure_recursive(right, element, right_limit);}
665template<
class T>
void SearchTree<T>::verify_structure_linear()
const {
671 for(
unsigned i = 0; i < _nodes.size(); i++) {
672 const typename SearchTree<T>::Node * node = &(_nodes[i]);
674 if (node->treelinks_null()) {n_null++;
continue;}
677 if (node->parent == NULL) {
684 assert((node->parent->left == node) ^ (node->parent->right == node));
690 if (node->left != NULL) {
691 assert(!(node->value < node->left->value ));}
694 if (node->right != NULL) {
695 assert(!(node->right->value < node->value ));}
698 assert(n_top == 1 || (n_top == 0 && size() <= 1) );
699 assert(n_null == _available_nodes.size() ||
700 (n_null == _available_nodes.size() + 1 && size() == 1));
708 if (node->left != NULL) {
710 newnode = node->left;
711 while(newnode->right != NULL) {newnode = newnode->right;}
715 newnode = node->parent;
718 while(newnode != NULL) {
719 if (newnode->right == lastnode) {
return newnode;}
721 newnode = newnode->parent;
732 if (node->right != NULL) {
734 newnode = node->right;
735 while(newnode->left != NULL) {newnode = newnode->left;}
739 newnode = node->parent;
742 while(newnode != NULL) {
743 if (newnode->left == lastnode) {
return newnode;}
745 newnode = newnode->parent;
758 int n = _nodes.size();
759 for(; node - base_node < n ; node++) {
760 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);
766 return circulator(_top_node);
void nullify_treelinks()
set all the tree-related links are set to null for this node
bool treelinks_null() const
default constructor
circulator & operator++()
prefix increment (structure copied from stl_bvector.h)
circulator operator--(int)
postfix decrement ["int" argument tells compiler it's postfix] (structure copied from stl_bvector....
circulator next() const
return a circulator referring to the next node
circulator previous() const
return a circulator referring to the previous node
circulator operator++(int)
postfix increment ["int" argument tells compiler it's postfix] (structure copied from stl_bvector....
circulator & operator--()
prefix decrement (structure copied from stl_bvector.h)
const_circulator previous() const
return a circulator referring to the previous node
const_circulator operator--(int)
postfix decrement ["int" argument tells compiler it's postfix] (structure copied from stl_bvector....
const_circulator operator++(int)
postfix increment ["int" argument tells compiler it's postfix] (structure copied from stl_bvector....
const_circulator & operator++()
prefix increment (structure copied from stl_bvector.h)
const_circulator next() const
return a circulator referring to the next node
const_circulator & operator--()
prefix decrement (structure copied from stl_bvector.h)
void print_elements()
print out all elements...
Node * _find_predecessor(const Node *)
return predecessor by walking through the tree
const_circulator somewhere() const
return a circulator to some place in the tree (with a circulator you don't care where....
unsigned int size() const
return the number of elements currently in the search tree
circulator insert(const T &value)
insert the supplied value into the tree and return a pointer to the relevant SearchTreeNode.
void verify_structure()
check that the structure we've obtained makes sense...
Node * _find_successor(const Node *)
return successor by walking through the tree
SearchTree(const std::vector< T > &init)
constructor for a search tree from an ordered vector
void remove(unsigned node_index)
remove the node corresponding to node_index from the search tree
Selector operator*(const Selector &s1, const Selector &s2)
successive application of 2 selectors
bool operator!=(const PseudoJet &a, const PseudoJet &b)
inequality test which is exact opposite of operator==
bool operator==(const PseudoJet &a, const PseudoJet &b)
returns true if the 4 momentum components of the two PseudoJets are identical and all the internal in...