153 return ((parent==0) && (left==0) && (right==0));};
176 if (parent == NULL) {
return;}
177 if (parent->right ==
this) {parent->right = XX;}
178 else {parent->left = XX;}
202 circulator() : _node(NULL) {}
204 circulator(
Node * node) : _node(node) {}
206 const T * operator->()
const {
return &(_node->value);}
207 T * operator->() {
return &(_node->value);}
208 const T & operator*()
const {
return _node->value;}
209 T & operator*() {
return _node->value;}
213 _node = _node->successor;
219 circulator tmp = *
this;
220 _node = _node->successor;
225 _node = _node->predecessor;
231 circulator tmp = *
this;
232 _node = _node->predecessor;
237 return circulator(_node->successor);}
241 return circulator(_node->predecessor);}
244 bool operator==(
const circulator & other)
const {
return other._node == _node;}
260 const_circulator() : _node(NULL) {}
262 const_circulator(
const Node * node) : _node(node) {}
263 const_circulator(
const circulator & circ) :_node(circ._node) {}
265 const T * operator->() {
return &(_node->value);}
266 const T & operator*()
const {
return _node->value;}
270 _node = _node->successor;
276 const_circulator tmp = *
this;
277 _node = _node->successor;
283 _node = _node->predecessor;
289 const_circulator tmp = *
this;
290 _node = _node->predecessor;
294 const_circulator
next()
const {
295 return const_circulator(_node->successor);}
299 return const_circulator(_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());
341template<
class T>
void SearchTree<T>::_initialize(
const std::vector<T> & init) {
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;}
598 if (node->predecessor != NULL) {
601 node->successor = node->predecessor->successor;
602 node->predecessor->successor = node;
603 node->successor->predecessor = 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);
639template<
class T>
void SearchTree<T>::verify_structure_recursive(
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++) {
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);