Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:57:16

0001 //FJSTARTHEADER
0002 // $Id$
0003 //
0004 // Copyright (c) 2005-2021, Matteo Cacciari, Gavin P. Salam and Gregory Soyez
0005 //
0006 //----------------------------------------------------------------------
0007 // This file is part of FastJet.
0008 //
0009 //  FastJet is free software; you can redistribute it and/or modify
0010 //  it under the terms of the GNU General Public License as published by
0011 //  the Free Software Foundation; either version 2 of the License, or
0012 //  (at your option) any later version.
0013 //
0014 //  The algorithms that underlie FastJet have required considerable
0015 //  development. They are described in the original FastJet paper,
0016 //  hep-ph/0512210 and in the manual, arXiv:1111.6097. If you use
0017 //  FastJet as part of work towards a scientific publication, please
0018 //  quote the version you use and include a citation to the manual and
0019 //  optionally also to hep-ph/0512210.
0020 //
0021 //  FastJet is distributed in the hope that it will be useful,
0022 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
0023 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0024 //  GNU General Public License for more details.
0025 //
0026 //  You should have received a copy of the GNU General Public License
0027 //  along with FastJet. If not, see <http://www.gnu.org/licenses/>.
0028 //----------------------------------------------------------------------
0029 //FJENDHEADER
0030 
0031 
0032 #ifndef __FASTJET_SEARCHTREE_HH__
0033 #define __FASTJET_SEARCHTREE_HH__
0034 
0035 #include<vector>
0036 #include<cassert>
0037 #include<cstddef>
0038 #include "fastjet/internal/base.hh"
0039 
0040 FASTJET_BEGIN_NAMESPACE      // defined in fastjet/internal/base.hh
0041 
0042 
0043 //======================================================================
0044 /// \if internal_doc
0045 /// @ingroup internal
0046 /// \class SearchTree
0047 /// Efficient class for a search tree
0048 ///
0049 /// This is the class for a search tree designed to be especially efficient
0050 /// when looking for successors and predecessors (to be used in Chan's
0051 /// CP algorithm). It has the requirement that the maximum size of the
0052 /// search tree must be known in advance.
0053 /// \endif
0054 template<class T> class SearchTree {
0055 public:
0056 
0057   class Node;
0058   class circulator;
0059   class const_circulator;
0060 
0061   /// constructor for a search tree from an ordered vector
0062   SearchTree(const std::vector<T> & init);
0063 
0064   /// constructor for a search tree from an ordered vector allowing
0065   /// for future growth beyond the current size, up to max_size
0066   SearchTree(const std::vector<T> & init, unsigned int max_size);
0067 
0068   /// remove the node corresponding to node_index from the search tree
0069   void remove(unsigned node_index);
0070   void remove(typename SearchTree::Node * node);
0071   void remove(typename SearchTree::circulator & circ);
0072 
0073   /// insert the supplied value into the tree and return a pointer to
0074   /// the relevant SearchTreeNode.
0075   //Node * insert(const T & value);
0076   circulator insert(const T & value);
0077 
0078   const Node & operator[](int i) const {return _nodes[i];};
0079 
0080   /// return the number of elements currently in the search tree
0081   unsigned int size() const {return _nodes.size() - _available_nodes.size();}
0082 
0083   /// check that the structure we've obtained makes sense...
0084   void verify_structure();
0085   void verify_structure_linear() const;
0086   void verify_structure_recursive(const Node * , const Node * , const Node * ) const;
0087 
0088   /// print out all elements...
0089   void print_elements();
0090 
0091   // tracking the depth may have some speed overhead -- so leave it 
0092   // out for the time being...
0093 #ifdef __FASTJET_SEARCHTREE_TRACK_DEPTH
0094   /// the max depth the tree has ever reached
0095   inline unsigned int max_depth() const {return _max_depth;};
0096 #else
0097   inline unsigned int max_depth() const {return 0;};
0098 #endif
0099 
0100   int loc(const Node * node) const ;
0101 
0102   /// return predecessor by walking through the tree
0103   Node * _find_predecessor(const Node *);
0104   /// return successor by walking through the tree
0105   Node * _find_successor(const Node *);
0106 
0107   const Node & operator[](unsigned int i) const {return _nodes[i];};
0108 
0109   /// return a circulator to some place in the tree (with a circulator
0110   /// you don't care where...)
0111   const_circulator somewhere() const;
0112   circulator somewhere();
0113 
0114 private:
0115   
0116   void _initialize(const std::vector<T> & init);
0117 
0118   std::vector<Node> _nodes;
0119   std::vector<Node *> _available_nodes;
0120   Node * _top_node;
0121   unsigned int _n_removes;
0122 
0123   
0124   /// recursive routine for doing the initial connections assuming things
0125   /// are ordered. Assumes this_one's parent is labelled, and was
0126   /// generated at a scale "scale" -- connections will be carried out
0127   /// including left edge and excluding right edge
0128   void _do_initial_connections(unsigned int this_one, unsigned int scale,
0129                    unsigned int left_edge, unsigned int right_edge,
0130                    unsigned int depth);
0131 
0132   
0133 #ifdef __FASTJET_SEARCHTREE_TRACK_DEPTH
0134   unsigned int _max_depth;
0135 #endif
0136 
0137 };
0138 
0139 
0140 //======================================================================
0141 /// \if internal_doc
0142 /// @ingroup internal
0143 /// \class SearchTree::Node
0144 /// A node in the search tree
0145 /// \endif
0146 template<class T> class SearchTree<T>::Node{
0147 public:
0148   Node() {}; /// default constructor
0149   
0150   
0151   /// returns tree if all the tree-related links are set to null for this node
0152   bool treelinks_null() const {
0153     return ((parent==0) && (left==0) && (right==0));};
0154   
0155   /// set all the tree-related links are set to null for this node
0156   inline void nullify_treelinks() {
0157     parent = NULL; 
0158     left   = NULL; 
0159     right  = NULL;
0160   };
0161   
0162   /// if my parent exists, determine whether I am it's left or right
0163   /// node and set the relevant link equal to XX.
0164   void reset_parents_link_to_me(Node * XX);
0165   
0166   T      value;
0167   Node * left;
0168   Node * right;
0169   Node * parent;
0170   Node * successor;
0171   Node * predecessor;
0172 };
0173 
0174 //----------------------------------------------------------------------
0175 template<class T> void SearchTree<T>::Node::reset_parents_link_to_me(typename SearchTree<T>::Node * XX) {
0176   if (parent == NULL) {return;}
0177   if (parent->right == this) {parent->right = XX;}
0178   else {parent->left = XX;}
0179 }
0180 
0181 
0182 
0183 //======================================================================
0184 /// \if internal_doc
0185 /// @ingroup internal
0186 /// \class SearchTree::circulator
0187 /// circulator for the search tree
0188 /// \endif
0189 template<class T> class SearchTree<T>::circulator{
0190 public:
0191 
0192   // so that it can access our _node object;
0193   // note: "class U" needed for clang (v1.1 branches/release_27) compilation
0194   // 2014-07-22: as reported by Torbjorn Sjostrand,
0195   // the next line was giving a warning with Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
0196   // (dependent nested name specifier 'SearchTree<U>::' for friend class declaration is not supported)
0197   // Just commenting it out, things still seem to work; same with a template of type T
0198   //template<class U> friend class SearchTree<U>::const_circulator;
0199   friend class SearchTree<T>::const_circulator;
0200   friend class SearchTree<T>;
0201 
0202   circulator() : _node(NULL) {}
0203 
0204   circulator(Node * node) : _node(node) {}
0205 
0206   const T * operator->() const {return &(_node->value);}
0207   T * operator->() {return &(_node->value);}
0208   const T & operator*() const {return _node->value;}
0209   T & operator*() {return _node->value;}
0210 
0211   /// prefix increment (structure copied from stl_bvector.h)
0212   circulator & operator++() {
0213     _node = _node->successor; 
0214     return *this;}
0215 
0216   /// postfix increment ["int" argument tells compiler it's postfix]
0217   /// (structure copied from stl_bvector.h)
0218   circulator operator++(int) {
0219     circulator tmp = *this;
0220     _node = _node->successor; 
0221     return tmp;}
0222 
0223   /// prefix decrement (structure copied from stl_bvector.h)
0224   circulator & operator--() {
0225     _node = _node->predecessor; 
0226     return *this;}
0227 
0228   /// postfix decrement ["int" argument tells compiler it's postfix]
0229   /// (structure copied from stl_bvector.h)
0230   circulator operator--(int) {
0231     circulator tmp = *this;
0232     _node = _node->predecessor; 
0233     return tmp;}
0234 
0235   /// return a circulator referring to the next node
0236   circulator next() const {
0237     return circulator(_node->successor);}
0238 
0239   /// return a circulator referring to the previous node
0240   circulator previous() const {
0241     return circulator(_node->predecessor);}
0242 
0243   bool operator!=(const circulator & other) const {return other._node != _node;}
0244   bool operator==(const circulator & other) const {return other._node == _node;}
0245 
0246 private:
0247   Node * _node;
0248 };
0249 
0250 
0251 //======================================================================
0252 /// \if internal_doc
0253 /// @ingroup internal
0254 /// \class SearchTree::const_circulator
0255 /// A const_circulator for the search tree
0256 /// \endif
0257 template<class T> class SearchTree<T>::const_circulator{
0258 public:
0259 
0260   const_circulator() : _node(NULL) {}
0261 
0262   const_circulator(const Node * node) : _node(node) {}
0263   const_circulator(const circulator & circ) :_node(circ._node) {}
0264 
0265   const T * operator->() {return &(_node->value);}
0266   const T & operator*() const {return _node->value;}
0267 
0268   /// prefix increment (structure copied from stl_bvector.h)
0269   const_circulator & operator++() {
0270     _node = _node->successor; 
0271     return *this;}
0272 
0273   /// postfix increment ["int" argument tells compiler it's postfix]
0274   /// (structure copied from stl_bvector.h)
0275   const_circulator operator++(int) {
0276     const_circulator tmp = *this;
0277     _node = _node->successor; 
0278     return tmp;}
0279 
0280 
0281   /// prefix decrement (structure copied from stl_bvector.h)
0282   const_circulator & operator--() {
0283     _node = _node->predecessor; 
0284     return *this;}
0285 
0286   /// postfix decrement ["int" argument tells compiler it's postfix]
0287   /// (structure copied from stl_bvector.h)
0288   const_circulator operator--(int) {
0289     const_circulator tmp = *this;
0290     _node = _node->predecessor; 
0291     return tmp;}
0292 
0293   /// return a circulator referring to the next node
0294   const_circulator next() const {
0295     return const_circulator(_node->successor);}
0296 
0297   /// return a circulator referring to the previous node
0298   const_circulator previous() const {
0299     return const_circulator(_node->predecessor);}
0300 
0301 
0302 
0303   bool operator!=(const const_circulator & other) const {return other._node != _node;}
0304   bool operator==(const const_circulator & other) const {return other._node == _node;}
0305 
0306 private:
0307   const Node * _node;
0308 };
0309 
0310 
0311 
0312 
0313 //----------------------------------------------------------------------
0314 /// initialise from a sorted initial array allowing for a larger
0315 /// maximum size of the array...
0316 template<class T> SearchTree<T>::SearchTree(const std::vector<T> & init,
0317                         unsigned int max_size) :
0318   _nodes(max_size) {
0319 
0320   _available_nodes.reserve(max_size);
0321   _available_nodes.resize(max_size - init.size());
0322   for (unsigned int i = init.size(); i < max_size; i++) {
0323     _available_nodes[i-init.size()] = &(_nodes[i]);
0324   }
0325 
0326   _initialize(init);
0327 }
0328 
0329 //----------------------------------------------------------------------
0330 /// initialise from a sorted initial array
0331 template<class T> SearchTree<T>::SearchTree(const std::vector<T> & init) :
0332   _nodes(init.size()), _available_nodes(0) {
0333 
0334   // reserve space for the list of available nodes
0335   _available_nodes.reserve(init.size());
0336   _initialize(init);
0337 }
0338 
0339 //----------------------------------------------------------------------
0340 /// do the actual hard work of initialization
0341 template<class T> void SearchTree<T>::_initialize(const std::vector<T> & init) {
0342 
0343   _n_removes = 0;
0344   unsigned n = init.size();
0345   assert(n>=1);
0346 
0347   // reserve space for the list of available nodes
0348   //_available_nodes.reserve();
0349 
0350 #ifdef __FASTJET_SEARCHTREE_TRACK_DEPTH
0351   _max_depth     = 0;
0352 #endif
0353 
0354 
0355   // validate the input
0356   for (unsigned int i = 1; i<n; i++) {
0357     assert(!(init[i] < init[i-1]));
0358   }
0359   
0360   // now initialise the vector; link neighbours in the sequence
0361   for(unsigned int i = 0; i < n; i++) {
0362     _nodes[i].value = init[i];
0363     _nodes[i].predecessor = (& (_nodes[i])) - 1;
0364     _nodes[i].successor   = (& (_nodes[i])) + 1;
0365     _nodes[i].nullify_treelinks();
0366   }
0367   // make a loop structure so that we can circulate...
0368   _nodes[0].predecessor = (& (_nodes[n-1]));
0369   _nodes[n-1].successor = (& (_nodes[0]));
0370 
0371   // now label the rest of the nodes
0372   unsigned int scale = (n+1)/2;
0373   unsigned int top   = std::min(n-1,scale);
0374   _nodes[top].parent = NULL;
0375   _top_node = &(_nodes[top]);
0376   _do_initial_connections(top, scale, 0, n, 0);
0377 
0378   // make sure things are sensible...
0379   //verify_structure();
0380 }
0381 
0382 
0383 
0384 //----------------------------------------------------------------------
0385 template<class T> inline  int SearchTree<T>::loc(const Node * node) const {return node == NULL? 
0386       -999 : node - &(_nodes[0]);}
0387 
0388 
0389 //----------------------------------------------------------------------
0390 /// Recursive creation of connections, assuming the _nodes vector is
0391 /// completely filled and ordered
0392 template<class T> void SearchTree<T>::_do_initial_connections(
0393                                          unsigned int this_one, 
0394                      unsigned int scale,
0395                      unsigned int left_edge,
0396                      unsigned int right_edge,
0397                      unsigned int depth
0398                      ) {
0399 
0400 #ifdef __FASTJET_SEARCHTREE_TRACK_DEPTH
0401   // keep track of tree depth for checking things stay reasonable...
0402   _max_depth = max(depth, _max_depth);
0403 #endif
0404 
0405   //std::cout << this_one << " "<< scale<< std::endl;
0406   unsigned int ref_new_scale = (scale+1)/2;
0407 
0408   // work through children to our left
0409   unsigned new_scale = ref_new_scale;
0410   bool     did_child  = false;
0411   while(true) {
0412     int left = this_one - new_scale; // be careful here to use signed int...
0413     // if there is something unitialised to our left, link to it
0414     if (left >= static_cast<int>(left_edge) 
0415                     && _nodes[left].treelinks_null() ) {
0416       _nodes[left].parent = &(_nodes[this_one]);
0417       _nodes[this_one].left = &(_nodes[left]);
0418       // create connections between left_edge and this_one
0419       _do_initial_connections(left, new_scale, left_edge, this_one, depth+1);
0420       did_child = true;
0421       break;
0422     }
0423     // reduce the scale so as to try again
0424     unsigned int old_new_scale = new_scale;
0425     new_scale = (old_new_scale + 1)/2;
0426     // unless we've reached end of tree
0427     if (new_scale == old_new_scale) break;
0428   }
0429   if (!did_child) {_nodes[this_one].left = NULL;}
0430 
0431 
0432   // work through children to our right
0433   new_scale = ref_new_scale;
0434   did_child  = false;
0435   while(true) {
0436     unsigned int right = this_one + new_scale;
0437     if (right < right_edge  && _nodes[right].treelinks_null()) {
0438       _nodes[right].parent = &(_nodes[this_one]);
0439       _nodes[this_one].right = &(_nodes[right]);
0440       // create connections between this_one+1 and right_edge
0441       _do_initial_connections(right, new_scale, this_one+1,right_edge,depth+1);
0442       did_child = true;
0443       break;
0444     }
0445     // reduce the scale so as to try again
0446     unsigned int old_new_scale = new_scale;
0447     new_scale = (old_new_scale + 1)/2;
0448     // unless we've reached end of tree
0449     if (new_scale == old_new_scale) break;
0450   }
0451   if (!did_child) {_nodes[this_one].right = NULL;}
0452 
0453 }
0454 
0455 
0456 
0457 //----------------------------------------------------------------------
0458 template<class T> void SearchTree<T>::remove(unsigned int node_index) {
0459   remove(&(_nodes[node_index]));
0460 }
0461 
0462 //----------------------------------------------------------------------
0463 template<class T> void SearchTree<T>::remove(circulator & circ) {
0464   remove(circ._node);
0465 }
0466 
0467 //----------------------------------------------------------------------
0468 // Useful reference for this:
0469 //   http://en.wikipedia.org/wiki/Binary_search_tree#Deletion
0470 template<class T> void SearchTree<T>::remove(typename SearchTree<T>::Node * node) {
0471 
0472   // we don't remove things from the tree if we've reached the last
0473   // elements... (is this wise?)
0474   assert(size() > 1); // switch this to throw...?
0475   assert(!node->treelinks_null());
0476 
0477   // deal with relinking predecessor and successor
0478   node->predecessor->successor = node->successor;
0479   node->successor->predecessor = node->predecessor;
0480 
0481   if (node->left == NULL && node->right == NULL) {
0482     // node has no children, so remove it by nullifying the pointer 
0483     // from the parent
0484     node->reset_parents_link_to_me(NULL); 
0485 
0486   } else if (node->left != NULL && node->right == NULL){
0487     // make parent point to my child
0488     node->reset_parents_link_to_me(node->left);
0489     // and child to parent
0490     node->left->parent = node->parent;         
0491     // sort out the top node...
0492     if (_top_node == node) {_top_node = node->left;}
0493 
0494   } else if (node->left == NULL && node->right != NULL){
0495     // make parent point to my child
0496     node->reset_parents_link_to_me(node->right);
0497     // and child to parent
0498     node->right->parent = node->parent;   
0499     // sort out the top node...
0500     if (_top_node == node) {_top_node = node->right;}
0501 
0502   } else {
0503     // we have two children; we will put a replacement in our place
0504     Node * replacement;
0505     //SearchTree<T>::Node * replacements_child;
0506     // chose predecessor or successor (one, then other, then first, etc...)
0507     bool use_predecessor = (_n_removes % 2 == 1);
0508     if (use_predecessor) {
0509       // Option 1: put predecessor in our place, and have its parent
0510       // point to its left child (as a predecessor it has no right child)
0511       replacement = node->predecessor;
0512       assert(replacement->right == NULL); // guaranteed if it's our predecessor
0513       // we have to be careful of replacing certain links when the 
0514       // replacement is this node's child
0515       if (replacement != node->left) {
0516     if (replacement->left != NULL) {
0517       replacement->left->parent = replacement->parent;}
0518     replacement->reset_parents_link_to_me(replacement->left);
0519     replacement->left   = node->left;
0520       }
0521       replacement->parent = node->parent;
0522       replacement->right  = node->right;
0523     } else {
0524       // Option 2: put successor in our place, and have its parent
0525       // point to its right child (as a successor it has no left child)
0526       replacement = node->successor;
0527       assert(replacement->left == NULL); // guaranteed if it's our successor
0528       if (replacement != node->right) {
0529     if (replacement->right != NULL) {
0530       replacement->right->parent = replacement->parent;}
0531     replacement->reset_parents_link_to_me(replacement->right);
0532     replacement->right  = node->right;
0533       }
0534       replacement->parent = node->parent;
0535       replacement->left   = node->left;
0536     }
0537     node->reset_parents_link_to_me(replacement);
0538 
0539     // make sure node's original children now point to the replacement
0540     if (node->left  != replacement) {node->left->parent  = replacement;}
0541     if (node->right != replacement) {node->right->parent = replacement;}
0542 
0543     // sort out the top node...
0544     if (_top_node == node) {_top_node = replacement;}
0545   }
0546 
0547   // make sure we leave something nice and clean...
0548   node->nullify_treelinks();
0549   node->predecessor = NULL;
0550   node->successor   = NULL;
0551 
0552   // for bookkeeping (and choosing whether to use pred. or succ.)
0553   _n_removes++;
0554   // for when we next need access to a free node...
0555   _available_nodes.push_back(node);
0556 }
0557 
0558 
0559 //----------------------------------------------------------------------
0560 //template<class T> typename SearchTree<T>::Node * SearchTree<T>::insert(const T & value) {
0561 
0562 //----------------------------------------------------------------------
0563 template<class T> typename SearchTree<T>::circulator SearchTree<T>::insert(const T & value) {
0564   // make sure we don't exceed allowed number of nodes...
0565   assert(_available_nodes.size() > 0);
0566 
0567   Node * node = _available_nodes.back();
0568   _available_nodes.pop_back();
0569   node->value = value;
0570 
0571   Node * location = _top_node;
0572   Node * old_location = NULL;
0573   bool             on_left = true; // (init not needed -- but soothes g++4)
0574   // work through tree until we reach its end
0575 #ifdef __FASTJET_SEARCHTREE_TRACK_DEPTH
0576   unsigned int depth = 0;
0577 #endif
0578   while(location != NULL) {
0579 #ifdef __FASTJET_SEARCHTREE_TRACK_DEPTH
0580     depth++;
0581 #endif
0582     old_location = location;
0583     on_left = value < location->value;
0584     if (on_left) {location = location->left;}
0585     else {location = location->right;}
0586   }
0587 #ifdef __FASTJET_SEARCHTREE_TRACK_DEPTH
0588   _max_depth = max(depth, _max_depth);
0589 #endif
0590   // now create tree links
0591   node->parent = old_location;
0592   if (on_left) {node->parent->left = node;} 
0593   else {node->parent->right = node;}
0594   node->left = NULL;
0595   node->right = NULL;
0596   // and create predecessor / successor links
0597   node->predecessor = _find_predecessor(node);
0598   if (node->predecessor != NULL) {
0599     // it exists, so make use of its info (will include a cyclic case,
0600     // when successor is round the bend)
0601     node->successor = node->predecessor->successor;
0602     node->predecessor->successor = node;
0603     node->successor->predecessor = node;
0604   } else {
0605     // deal with case when we are left-most edge of tree (then successor
0606     // will exist...)
0607     node->successor = _find_successor(node);
0608     assert(node->successor != NULL); // can only happen if we're sole element 
0609                                      // (but not allowed, since tree size>=1)
0610     node->predecessor = node->successor->predecessor;
0611     node->successor->predecessor = node;
0612     node->predecessor->successor = node;
0613   }
0614 
0615   return circulator(node);
0616 }
0617 
0618 
0619 //----------------------------------------------------------------------
0620 template<class T> void SearchTree<T>::verify_structure() {
0621   
0622   // do a check running through all elements
0623   verify_structure_linear();
0624 
0625   // do a recursive check down tree from top
0626 
0627   // first establish the extremities
0628   const Node * left_limit = _top_node;
0629   while (left_limit->left != NULL) {left_limit = left_limit->left;}
0630   const Node * right_limit = _top_node;
0631   while (right_limit->right != NULL) {right_limit = right_limit->right;}
0632 
0633   // then actually do recursion
0634   verify_structure_recursive(_top_node, left_limit, right_limit);
0635 }
0636 
0637 
0638 //----------------------------------------------------------------------
0639 template<class T> void SearchTree<T>::verify_structure_recursive(
0640               const typename SearchTree<T>::Node * element, 
0641               const typename SearchTree<T>::Node * left_limit,
0642               const typename SearchTree<T>::Node * right_limit)  const {
0643 
0644   assert(!(element->value < left_limit->value));
0645   assert(!(right_limit->value < element->value));
0646 
0647   const Node * left = element->left;
0648   if (left != NULL) {
0649     assert(!(element->value < left->value));
0650     if (left != left_limit) {
0651       // recurse down the tree with this element as the right-hand limit
0652       verify_structure_recursive(left, left_limit, element);}
0653   }
0654   
0655   const Node * right = element->right;
0656   if (right != NULL) {
0657     assert(!(right->value < element->value));
0658     if (right != right_limit) {
0659       // recurse down the tree with this element as the left-hand limit
0660       verify_structure_recursive(right, element, right_limit);}
0661   }
0662 }
0663 
0664 //----------------------------------------------------------------------
0665 template<class T> void SearchTree<T>::verify_structure_linear() const {
0666 
0667   //print_elements();
0668 
0669   unsigned n_top = 0;
0670   unsigned n_null = 0;
0671   for(unsigned i = 0; i < _nodes.size(); i++) {
0672     const typename SearchTree<T>::Node * node = &(_nodes[i]);
0673     // make sure node is defined
0674     if (node->treelinks_null()) {n_null++; continue;}
0675 
0676     // make sure of the number of "top" nodes 
0677     if (node->parent == NULL) {
0678       n_top++;
0679       //assert(node->left != NULL);
0680       //assert(node->right != NULL);
0681     } else {
0682       // make sure that I am a child of my parent...
0683       //assert((node->parent->left == node) || (node->parent->right == node));
0684       assert((node->parent->left == node) ^ (node->parent->right == node));
0685     }
0686 
0687     // when there is a left child make sure it's value is ordered
0688     // (note use of !(b<a), to allow for a<=b while using just the <
0689     // operator)
0690     if (node->left != NULL) {
0691       assert(!(node->value < node->left->value ));}
0692 
0693     // when there is a right child make sure it's value is ordered
0694     if (node->right != NULL) {
0695       assert(!(node->right->value < node->value ));}
0696 
0697   }
0698   assert(n_top == 1 || (n_top == 0 && size() <= 1) );
0699   assert(n_null == _available_nodes.size() ||
0700      (n_null == _available_nodes.size() + 1 && size() == 1));
0701 }
0702 
0703 
0704 //----------------------------------------------------------------------
0705 template<class T> typename SearchTree<T>::Node * SearchTree<T>::_find_predecessor(const typename SearchTree<T>::Node * node) {
0706 
0707   typename SearchTree<T>::Node * newnode;
0708   if (node->left != NULL) {
0709     // go down left, and then down right as far as possible.
0710     newnode = node->left;
0711     while(newnode->right != NULL) {newnode = newnode->right;}
0712     return newnode;
0713   } else {
0714     const typename SearchTree<T>::Node * lastnode = node;
0715     newnode = node->parent;
0716     // go up the tree as long as we're going right (when we go left then
0717     // we've found something smaller, so stop)
0718     while(newnode != NULL) {
0719       if (newnode->right == lastnode) {return newnode;}
0720       lastnode = newnode;
0721       newnode = newnode->parent;
0722     }
0723     return newnode;
0724   }
0725 }
0726 
0727 
0728 //----------------------------------------------------------------------
0729 template<class T> typename SearchTree<T>::Node * SearchTree<T>::_find_successor(const typename SearchTree<T>::Node * node) {
0730 
0731   typename SearchTree<T>::Node * newnode;
0732   if (node->right != NULL) {
0733     // go down right, and then down left as far as possible.
0734     newnode = node->right;
0735     while(newnode->left != NULL) {newnode = newnode->left;}
0736     return newnode;
0737   } else {
0738     const typename SearchTree<T>::Node * lastnode = node;
0739     newnode = node->parent;
0740     // go up the tree as long as we're going left (when we go right then
0741     // we've found something larger, so stop)
0742     while(newnode != NULL) {
0743       if (newnode->left == lastnode) {return newnode;}
0744       lastnode = newnode;
0745       newnode = newnode->parent;
0746     }
0747     return newnode;
0748   }
0749 }
0750 
0751 
0752 //----------------------------------------------------------------------
0753 // print out all the elements for visual checking...
0754 template<class T> void SearchTree<T>::print_elements() {
0755   typename SearchTree<T>::Node * base_node = &(_nodes[0]);
0756   typename SearchTree<T>::Node * node = base_node;
0757   
0758   int n = _nodes.size();
0759   for(; node - base_node < n ; node++) {
0760     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);
0761   }
0762 }
0763 
0764 //----------------------------------------------------------------------
0765 template<class T> typename SearchTree<T>::circulator SearchTree<T>::somewhere() {
0766   return circulator(_top_node);
0767 }
0768 
0769 
0770 //----------------------------------------------------------------------
0771 template<class T> typename SearchTree<T>::const_circulator SearchTree<T>::somewhere() const {
0772   return const_circulator(_top_node);
0773 }
0774 
0775 
0776 FASTJET_END_NAMESPACE
0777 
0778 #endif // __FASTJET_SEARCHTREE_HH__