00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef __FASTJET_SEARCHTREE_HH__
00033 #define __FASTJET_SEARCHTREE_HH__
00034
00035 #include<vector>
00036 #include<cassert>
00037 #include<cstddef>
00038 #include "fastjet/internal/base.hh"
00039
00040 FASTJET_BEGIN_NAMESPACE
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 template<class T> class SearchTree {
00055 public:
00056
00057 class Node;
00058 class circulator;
00059 class const_circulator;
00060
00061
00062 SearchTree(const std::vector<T> & init);
00063
00064
00065
00066 SearchTree(const std::vector<T> & init, unsigned int max_size);
00067
00068
00069 void remove(unsigned node_index);
00070 void remove(typename SearchTree::Node * node);
00071 void remove(typename SearchTree::circulator & circ);
00072
00073
00074
00075
00076 circulator insert(const T & value);
00077
00078 const Node & operator[](int i) const {return _nodes[i];};
00079
00080
00081 unsigned int size() const {return _nodes.size() - _available_nodes.size();}
00082
00083
00084 void verify_structure();
00085 void verify_structure_linear() const;
00086 void verify_structure_recursive(const Node * , const Node * , const Node * ) const;
00087
00088
00089 void print_elements();
00090
00091
00092
00093 #ifdef TRACK_DEPTH
00094
00095 inline unsigned int max_depth() const {return _max_depth;};
00096 #else
00097 inline unsigned int max_depth() const {return 0;};
00098 #endif
00099
00100 int loc(const Node * node) const ;
00101
00102
00103 Node * _find_predecessor(const Node *);
00104
00105 Node * _find_successor(const Node *);
00106
00107 const Node & operator[](unsigned int i) const {return _nodes[i];};
00108
00109
00110
00111 const_circulator somewhere() const;
00112 circulator somewhere();
00113
00114 private:
00115
00116 void _initialize(const std::vector<T> & init);
00117
00118 std::vector<Node> _nodes;
00119 std::vector<Node *> _available_nodes;
00120 Node * _top_node;
00121 unsigned int _n_removes;
00122
00123
00124
00125
00126
00127
00128 void _do_initial_connections(unsigned int this_one, unsigned int scale,
00129 unsigned int left_edge, unsigned int right_edge,
00130 unsigned int depth);
00131
00132
00133 #ifdef TRACK_DEPTH
00134 unsigned int _max_depth;
00135 #endif
00136
00137 };
00138
00139
00140
00141
00142
00143
00144
00145
00146 template<class T> class SearchTree<T>::Node{
00147 public:
00148 Node() {};
00149
00150
00151
00152 bool treelinks_null() const {
00153 return ((parent==0) && (left==0) && (right==0));};
00154
00155
00156 inline void nullify_treelinks() {
00157 parent = NULL;
00158 left = NULL;
00159 right = NULL;
00160 };
00161
00162
00163
00164 void reset_parents_link_to_me(Node * XX);
00165
00166 T value;
00167 Node * left;
00168 Node * right;
00169 Node * parent;
00170 Node * successor;
00171 Node * predecessor;
00172 };
00173
00174
00175 template<class T> void SearchTree<T>::Node::reset_parents_link_to_me(typename SearchTree<T>::Node * XX) {
00176 if (parent == NULL) {return;}
00177 if (parent->right == this) {parent->right = XX;}
00178 else {parent->left = XX;}
00179 }
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189 template<class T> class SearchTree<T>::circulator{
00190 public:
00191
00192
00193 friend class SearchTree<T>::const_circulator;
00194 friend class SearchTree<T>;
00195
00196 circulator() : _node(NULL) {}
00197
00198 circulator(Node * node) : _node(node) {}
00199
00200 const T * operator->() const {return &(_node->value);}
00201 T * operator->() {return &(_node->value);}
00202 const T & operator*() const {return _node->value;}
00203 T & operator*() {return _node->value;}
00204
00205
00206 circulator & operator++() {
00207 _node = _node->successor;
00208 return *this;}
00209
00210
00211
00212 circulator operator++(int) {
00213 circulator tmp = *this;
00214 _node = _node->successor;
00215 return tmp;}
00216
00217
00218 circulator & operator--() {
00219 _node = _node->predecessor;
00220 return *this;}
00221
00222
00223
00224 circulator operator--(int) {
00225 circulator tmp = *this;
00226 _node = _node->predecessor;
00227 return tmp;}
00228
00229
00230 circulator next() const {
00231 return circulator(_node->successor);}
00232
00233
00234 circulator previous() const {
00235 return circulator(_node->predecessor);}
00236
00237 bool operator!=(const circulator & other) const {return other._node != _node;}
00238 bool operator==(const circulator & other) const {return other._node == _node;}
00239
00240 private:
00241 Node * _node;
00242 };
00243
00244
00245
00246
00247
00248
00249
00250
00251 template<class T> class SearchTree<T>::const_circulator{
00252 public:
00253
00254 const_circulator() : _node(NULL) {}
00255
00256 const_circulator(const Node * node) : _node(node) {}
00257 const_circulator(const circulator & circ) :_node(circ._node) {}
00258
00259 const T * operator->() {return &(_node->value);}
00260 const T & operator*() const {return _node->value;}
00261
00262
00263 const_circulator & operator++() {
00264 _node = _node->successor;
00265 return *this;}
00266
00267
00268
00269 const_circulator operator++(int) {
00270 const_circulator tmp = *this;
00271 _node = _node->successor;
00272 return tmp;}
00273
00274
00275
00276 const_circulator & operator--() {
00277 _node = _node->predecessor;
00278 return *this;}
00279
00280
00281
00282 const_circulator operator--(int) {
00283 const_circulator tmp = *this;
00284 _node = _node->predecessor;
00285 return tmp;}
00286
00287
00288 const_circulator next() const {
00289 return const_circulator(_node->successor);}
00290
00291
00292 const_circulator previous() const {
00293 return const_circulator(_node->predecessor);}
00294
00295
00296
00297 bool operator!=(const const_circulator & other) const {return other._node != _node;}
00298 bool operator==(const const_circulator & other) const {return other._node == _node;}
00299
00300 private:
00301 const Node * _node;
00302 };
00303
00304
00305
00306
00307
00308
00309
00310 template<class T> SearchTree<T>::SearchTree(const std::vector<T> & init,
00311 unsigned int max_size) :
00312 _nodes(max_size) {
00313
00314 _available_nodes.reserve(max_size);
00315 _available_nodes.resize(max_size - init.size());
00316 for (unsigned int i = init.size(); i < max_size; i++) {
00317 _available_nodes[i-init.size()] = &(_nodes[i]);
00318 }
00319
00320 _initialize(init);
00321 }
00322
00323
00324
00325 template<class T> SearchTree<T>::SearchTree(const std::vector<T> & init) :
00326 _nodes(init.size()), _available_nodes(0) {
00327
00328
00329 _available_nodes.reserve(init.size());
00330 _initialize(init);
00331 }
00332
00333
00334
00335 template<class T> void SearchTree<T>::_initialize(const std::vector<T> & init) {
00336
00337 _n_removes = 0;
00338 unsigned n = init.size();
00339 assert(n>=1);
00340
00341
00342
00343
00344 #ifdef TRACK_DEPTH
00345 _max_depth = 0;
00346 #endif
00347
00348
00349
00350 for (unsigned int i = 1; i<n; i++) {
00351 assert(!(init[i] < init[i-1]));
00352 }
00353
00354
00355 for(unsigned int i = 0; i < n; i++) {
00356 _nodes[i].value = init[i];
00357 _nodes[i].predecessor = (& (_nodes[i])) - 1;
00358 _nodes[i].successor = (& (_nodes[i])) + 1;
00359 _nodes[i].nullify_treelinks();
00360 }
00361
00362 _nodes[0].predecessor = (& (_nodes[n-1]));
00363 _nodes[n-1].successor = (& (_nodes[0]));
00364
00365
00366 unsigned int scale = (n+1)/2;
00367 unsigned int top = std::min(n-1,scale);
00368 _nodes[top].parent = NULL;
00369 _top_node = &(_nodes[top]);
00370 _do_initial_connections(top, scale, 0, n, 0);
00371
00372
00373
00374 }
00375
00376
00377
00378
00379 template<class T> inline int SearchTree<T>::loc(const Node * node) const {return node == NULL?
00380 -999 : node - &(_nodes[0]);}
00381
00382
00383
00384
00385
00386 template<class T> void SearchTree<T>::_do_initial_connections(
00387 unsigned int this_one,
00388 unsigned int scale,
00389 unsigned int left_edge,
00390 unsigned int right_edge,
00391 unsigned int depth
00392 ) {
00393
00394 #ifdef TRACK_DEPTH
00395
00396 _max_depth = max(depth, _max_depth);
00397 #endif
00398
00399
00400 unsigned int ref_new_scale = (scale+1)/2;
00401
00402
00403 unsigned new_scale = ref_new_scale;
00404 bool did_child = false;
00405 while(true) {
00406 int left = this_one - new_scale;
00407
00408 if (left >= static_cast<int>(left_edge)
00409 && _nodes[left].treelinks_null() ) {
00410 _nodes[left].parent = &(_nodes[this_one]);
00411 _nodes[this_one].left = &(_nodes[left]);
00412
00413 _do_initial_connections(left, new_scale, left_edge, this_one, depth+1);
00414 did_child = true;
00415 break;
00416 }
00417
00418 unsigned int old_new_scale = new_scale;
00419 new_scale = (old_new_scale + 1)/2;
00420
00421 if (new_scale == old_new_scale) break;
00422 }
00423 if (!did_child) {_nodes[this_one].left = NULL;}
00424
00425
00426
00427 new_scale = ref_new_scale;
00428 did_child = false;
00429 while(true) {
00430 unsigned int right = this_one + new_scale;
00431 if (right < right_edge && _nodes[right].treelinks_null()) {
00432 _nodes[right].parent = &(_nodes[this_one]);
00433 _nodes[this_one].right = &(_nodes[right]);
00434
00435 _do_initial_connections(right, new_scale, this_one+1,right_edge,depth+1);
00436 did_child = true;
00437 break;
00438 }
00439
00440 unsigned int old_new_scale = new_scale;
00441 new_scale = (old_new_scale + 1)/2;
00442
00443 if (new_scale == old_new_scale) break;
00444 }
00445 if (!did_child) {_nodes[this_one].right = NULL;}
00446
00447 }
00448
00449
00450
00451
00452 template<class T> void SearchTree<T>::remove(unsigned int node_index) {
00453 remove(&(_nodes[node_index]));
00454 }
00455
00456
00457 template<class T> void SearchTree<T>::remove(circulator & circ) {
00458 remove(circ._node);
00459 }
00460
00461
00462
00463
00464 template<class T> void SearchTree<T>::remove(typename SearchTree<T>::Node * node) {
00465
00466
00467
00468 assert(size() > 1);
00469 assert(!node->treelinks_null());
00470
00471
00472 node->predecessor->successor = node->successor;
00473 node->successor->predecessor = node->predecessor;
00474
00475 if (node->left == NULL && node->right == NULL) {
00476
00477
00478 node->reset_parents_link_to_me(NULL);
00479
00480 } else if (node->left != NULL && node->right == NULL){
00481
00482 node->reset_parents_link_to_me(node->left);
00483
00484 node->left->parent = node->parent;
00485
00486 if (_top_node == node) {_top_node = node->left;}
00487
00488 } else if (node->left == NULL && node->right != NULL){
00489
00490 node->reset_parents_link_to_me(node->right);
00491
00492 node->right->parent = node->parent;
00493
00494 if (_top_node == node) {_top_node = node->right;}
00495
00496 } else {
00497
00498 Node * replacement;
00499
00500
00501 bool use_predecessor = (_n_removes % 2 == 1);
00502 if (use_predecessor) {
00503
00504
00505 replacement = node->predecessor;
00506 assert(replacement->right == NULL);
00507
00508
00509 if (replacement != node->left) {
00510 if (replacement->left != NULL) {
00511 replacement->left->parent = replacement->parent;}
00512 replacement->reset_parents_link_to_me(replacement->left);
00513 replacement->left = node->left;
00514 }
00515 replacement->parent = node->parent;
00516 replacement->right = node->right;
00517 } else {
00518
00519
00520 replacement = node->successor;
00521 assert(replacement->left == NULL);
00522 if (replacement != node->right) {
00523 if (replacement->right != NULL) {
00524 replacement->right->parent = replacement->parent;}
00525 replacement->reset_parents_link_to_me(replacement->right);
00526 replacement->right = node->right;
00527 }
00528 replacement->parent = node->parent;
00529 replacement->left = node->left;
00530 }
00531 node->reset_parents_link_to_me(replacement);
00532
00533
00534 if (node->left != replacement) {node->left->parent = replacement;}
00535 if (node->right != replacement) {node->right->parent = replacement;}
00536
00537
00538 if (_top_node == node) {_top_node = replacement;}
00539 }
00540
00541
00542 node->nullify_treelinks();
00543 node->predecessor = NULL;
00544 node->successor = NULL;
00545
00546
00547 _n_removes++;
00548
00549 _available_nodes.push_back(node);
00550 }
00551
00552
00553
00554
00555
00556
00557 template<class T> typename SearchTree<T>::circulator SearchTree<T>::insert(const T & value) {
00558
00559 assert(_available_nodes.size() > 0);
00560
00561 Node * node = _available_nodes.back();
00562 _available_nodes.pop_back();
00563 node->value = value;
00564
00565 Node * location = _top_node;
00566 Node * old_location = NULL;
00567 bool on_left = true;
00568
00569 #ifdef TRACK_DEPTH
00570 unsigned int depth = 0;
00571 #endif
00572 while(location != NULL) {
00573 #ifdef TRACK_DEPTH
00574 depth++;
00575 #endif
00576 old_location = location;
00577 on_left = value < location->value;
00578 if (on_left) {location = location->left;}
00579 else {location = location->right;}
00580 }
00581 #ifdef TRACK_DEPTH
00582 _max_depth = max(depth, _max_depth);
00583 #endif
00584
00585 node->parent = old_location;
00586 if (on_left) {node->parent->left = node;}
00587 else {node->parent->right = node;}
00588 node->left = NULL;
00589 node->right = NULL;
00590
00591 node->predecessor = _find_predecessor(node);
00592 if (node->predecessor != NULL) {
00593
00594
00595 node->successor = node->predecessor->successor;
00596 node->predecessor->successor = node;
00597 node->successor->predecessor = node;
00598 } else {
00599
00600
00601 node->successor = _find_successor(node);
00602 assert(node->successor != NULL);
00603
00604 node->predecessor = node->successor->predecessor;
00605 node->successor->predecessor = node;
00606 node->predecessor->successor = node;
00607 }
00608
00609 return circulator(node);
00610 }
00611
00612
00613
00614 template<class T> void SearchTree<T>::verify_structure() {
00615
00616
00617 verify_structure_linear();
00618
00619
00620
00621
00622 const Node * left_limit = _top_node;
00623 while (left_limit->left != NULL) {left_limit = left_limit->left;}
00624 const Node * right_limit = _top_node;
00625 while (right_limit->right != NULL) {right_limit = right_limit->right;}
00626
00627
00628 verify_structure_recursive(_top_node, left_limit, right_limit);
00629 }
00630
00631
00632
00633 template<class T> void SearchTree<T>::verify_structure_recursive(
00634 const typename SearchTree<T>::Node * element,
00635 const typename SearchTree<T>::Node * left_limit,
00636 const typename SearchTree<T>::Node * right_limit) const {
00637
00638 assert(!(element->value < left_limit->value));
00639 assert(!(right_limit->value < element->value));
00640
00641 const Node * left = element->left;
00642 if (left != NULL) {
00643 assert(!(element->value < left->value));
00644 if (left != left_limit) {
00645
00646 verify_structure_recursive(left, left_limit, element);}
00647 }
00648
00649 const Node * right = element->right;
00650 if (right != NULL) {
00651 assert(!(right->value < element->value));
00652 if (right != right_limit) {
00653
00654 verify_structure_recursive(right, element, right_limit);}
00655 }
00656 }
00657
00658
00659 template<class T> void SearchTree<T>::verify_structure_linear() const {
00660
00661
00662
00663 unsigned n_top = 0;
00664 unsigned n_null = 0;
00665 for(unsigned i = 0; i < _nodes.size(); i++) {
00666 const typename SearchTree<T>::Node * node = &(_nodes[i]);
00667
00668 if (node->treelinks_null()) {n_null++; continue;}
00669
00670
00671 if (node->parent == NULL) {
00672 n_top++;
00673
00674
00675 } else {
00676
00677
00678 assert((node->parent->left == node) ^ (node->parent->right == node));
00679 }
00680
00681
00682
00683
00684 if (node->left != NULL) {
00685 assert(!(node->value < node->left->value ));}
00686
00687
00688 if (node->right != NULL) {
00689 assert(!(node->right->value < node->value ));}
00690
00691 }
00692 assert(n_top == 1 || (n_top == 0 && size() <= 1) );
00693 assert(n_null == _available_nodes.size() ||
00694 (n_null == _available_nodes.size() + 1 && size() == 1));
00695 }
00696
00697
00698
00699 template<class T> typename SearchTree<T>::Node * SearchTree<T>::_find_predecessor(const typename SearchTree<T>::Node * node) {
00700
00701 typename SearchTree<T>::Node * newnode;
00702 if (node->left != NULL) {
00703
00704 newnode = node->left;
00705 while(newnode->right != NULL) {newnode = newnode->right;}
00706 return newnode;
00707 } else {
00708 const typename SearchTree<T>::Node * lastnode = node;
00709 newnode = node->parent;
00710
00711
00712 while(newnode != NULL) {
00713 if (newnode->right == lastnode) {return newnode;}
00714 lastnode = newnode;
00715 newnode = newnode->parent;
00716 }
00717 return newnode;
00718 }
00719 }
00720
00721
00722
00723 template<class T> typename SearchTree<T>::Node * SearchTree<T>::_find_successor(const typename SearchTree<T>::Node * node) {
00724
00725 typename SearchTree<T>::Node * newnode;
00726 if (node->right != NULL) {
00727
00728 newnode = node->right;
00729 while(newnode->left != NULL) {newnode = newnode->left;}
00730 return newnode;
00731 } else {
00732 const typename SearchTree<T>::Node * lastnode = node;
00733 newnode = node->parent;
00734
00735
00736 while(newnode != NULL) {
00737 if (newnode->left == lastnode) {return newnode;}
00738 lastnode = newnode;
00739 newnode = newnode->parent;
00740 }
00741 return newnode;
00742 }
00743 }
00744
00745
00746
00747
00748 template<class T> void SearchTree<T>::print_elements() {
00749 typename SearchTree<T>::Node * base_node = &(_nodes[0]);
00750 typename SearchTree<T>::Node * node = base_node;
00751
00752 int n = _nodes.size();
00753 for(; node - base_node < n ; node++) {
00754 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);
00755 }
00756 }
00757
00758
00759 template<class T> typename SearchTree<T>::circulator SearchTree<T>::somewhere() {
00760 return circulator(_top_node);
00761 }
00762
00763
00764
00765 template<class T> typename SearchTree<T>::const_circulator SearchTree<T>::somewhere() const {
00766 return const_circulator(_top_node);
00767 }
00768
00769
00770 FASTJET_END_NAMESPACE
00771
00772 #endif // __FASTJET_SEARCHTREE_HH__