28 #define compLT(a,b) (a < b) 29 #define compEQ(a,b) (a == b) 33 typedef enum {
BLACK,
RED } nodeColor;
37 typedef struct Node_ {
47 #define parent parent_ 99 for (i=0; i < level; ++
i) {
113 if (left_ && left_ !=
NIL) {
114 left_->t_iterate(f, level+1);
117 if (right_ && right_ !=
NIL) {
118 right_->t_iterate(f, level+1);
132 tpool_ =
new TQItemPool(1000);
137 #if COLLECT_TQueue_STATISTICS 138 nmove = ninsert = nrem = nleast = nbal = ncmplxrem = 0;
139 nfastmove = ncompare = nleastsrch = nfind = nfindsrch = 0;
152 root_->t_iterate(
prnt, 0);
158 root_->t_iterate(f, 0);
166 root_->t_iterate(
chk, 0);
183 #if !FAST_LEAST || DOCHECK 186 if (b !=
NIL)
for (; b->left_ !=
NIL; b = b->left_) {
195 return least_ !=
NIL ? least_ :
nil ;
234 #if COLLECT_TQueue_STATISTICS 235 Printf(
"insertions=%lu moves=%lu fastmoves=%lu removals=%lu calls to least=%lu\n", ninsert, nmove, nfastmove, nrem, nleast);
236 Printf(
"calls to find=%lu balances=%lu complex removals=%lu\n", nfind, nbal, ncmplxrem);
237 Printf(
"comparisons=%lu leastsearch=%lu findsearch=%lu\n", ncompare, nleastsrch, nfindsrch);
239 Printf(
"Turn on COLLECT_TQueue_STATISTICS_ in tqueue.h\n");
243 void RBTQueue::rotateLeft(
Node *x) {
253 if (y->left !=
NIL) y->left->parent = x;
256 if (y !=
NIL) y->parent = x->parent;
258 if (x == x->parent->left)
261 x->parent->right = y;
268 if (x !=
NIL) x->parent = y;
271 void RBTQueue::rotateRight(
Node *x) {
281 if (y->right !=
NIL) y->right->parent = x;
284 if (y !=
NIL) y->parent = x->parent;
286 if (x == x->parent->right)
287 x->parent->right = y;
296 if (x !=
NIL) x->parent = y;
299 void RBTQueue::insertFixup(
Node *x) {
307 while (x != root && x->parent->color ==
RED) {
309 if (x->parent == x->parent->parent->left) {
310 Node *y = x->parent->parent->right;
311 if (y->color ==
RED) {
314 x->parent->color =
BLACK;
316 x->parent->parent->color =
RED;
317 x = x->parent->parent;
321 if (x == x->parent->right) {
328 x->parent->color =
BLACK;
329 x->parent->parent->color =
RED;
330 rotateRight(x->parent->parent);
335 Node *y = x->parent->parent->left;
336 if (y->color ==
RED) {
339 x->parent->color =
BLACK;
341 x->parent->parent->color =
RED;
342 x = x->parent->parent;
346 if (x == x->parent->left) {
350 x->parent->color =
BLACK;
351 x->parent->parent->color =
RED;
352 rotateLeft(x->parent->parent);
360 TQItem* i = tpool_->alloc();
366 void RBTQueue::insertNode(
T data,
Node* x) {
372 if (
data < least_t()) {
385 while (current !=
NIL) {
387 if (
compEQ(
data, current->data))
return (current);
391 current->left : current->right;
396 if ((x = malloc (
sizeof(*x))) == 0) {
397 printf (
"insufficient memory (insertNode)\n");
420 void RBTQueue::deleteFixup(
Node *x) {
427 while (x != root && x->color ==
BLACK) {
428 if (x == x->parent->left) {
429 Node *w = x->parent->right;
430 if (w->color ==
RED) {
432 x->parent->color =
RED;
433 rotateLeft (x->parent);
434 w = x->parent->right;
436 if (w->left->color ==
BLACK && w->right->color ==
BLACK) {
440 if (w->right->color ==
BLACK) {
441 w->left->color =
BLACK;
444 w = x->parent->right;
446 w->color = x->parent->color;
447 x->parent->color =
BLACK;
448 w->right->color =
BLACK;
449 rotateLeft (x->parent);
453 Node *w = x->parent->left;
454 if (w->color ==
RED) {
456 x->parent->color =
RED;
457 rotateRight (x->parent);
460 if (w->right->color ==
BLACK && w->left->color ==
BLACK) {
464 if (w->left->color ==
BLACK) {
465 w->right->color =
BLACK;
470 w->color = x->parent->color;
471 x->parent->color =
BLACK;
472 w->left->color =
BLACK;
473 rotateRight (x->parent);
488 void RBTQueue::deleteNode(
Node *z) {
497 if (z ==
NIL)
return;
505 if (z->left ==
NIL || z->right ==
NIL) {
511 while (y->left !=
NIL) y = y->left;
521 x->parent = y->parent;
523 if (y == y->parent->left)
526 y->parent->right = x;
532 if (y != z) z->data = y->data;
535 if (y->color ==
BLACK)
544 y->parent_ = z->parent_;
546 y->right_ = z->right_;
547 if (z->left_ !=
NIL) { z->left_->parent_ = y; }
548 if (z->right_ !=
NIL) { z->right_->parent_ = y; }
549 if (z->parent && z->parent_ !=
NIL) {
550 if (z->parent_->left_ == z) {
551 z->parent_->left_ = y;
553 z->parent_->right_ = y;
568 while(current !=
NIL)
571 if(
compEQ(data, current->data))
574 current =
compLT (data, current->data) ?
575 current->left : current->right;
static void chk(TQItem *b, int level)
static void deleteitem(TQItem *i)
static void prnt(const TQItem *b, int level)
void forall_callback(void(*)(const TQItem *, int))
static double remove(void *v)
static double insert(void *v)
void hoc_execerror(const char *, const char *)
void move_least(double tnew)
static const char * errmess_
static double least(void *v)
int find(const int, const int, const int, const int, const int)
void check(const char *errmess)
void move(TQItem *, double tnew)
void t_iterate(void(*)(const TQItem *, int), int)