gtkrbtree.c 45.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* gtkrbtree.c
 * Copyright (C) 2000  Red Hat, Inc.,  Jonathan Blandford <jrb@redhat.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "gtkrbtree.h"
21
#include "gtkdebug.h"
22

Jonathan Blandford's avatar
Jonathan Blandford committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
static void        _gtk_rbnode_validate_allocator (GAllocator *allocator);
static GtkRBNode * _gtk_rbnode_new                (GtkRBTree  *tree,
						   gint        height);
static void        _gtk_rbnode_free               (GtkRBNode  *node);
static void        _gtk_rbnode_rotate_left        (GtkRBTree  *tree,
						   GtkRBNode  *node);
static void        _gtk_rbnode_rotate_right       (GtkRBTree  *tree,
						   GtkRBNode  *node);
static void        _gtk_rbtree_insert_fixup       (GtkRBTree  *tree,
						   GtkRBNode  *node);
static void        _gtk_rbtree_remove_node_fixup  (GtkRBTree  *tree,
						   GtkRBNode  *node);
static gint        _count_nodes                   (GtkRBTree  *tree,
						   GtkRBNode  *node);
static inline void _fixup_validation              (GtkRBTree  *tree,
						   GtkRBNode  *node);
39 40
static inline void _fixup_parity                  (GtkRBTree  *tree,
						   GtkRBNode  *node);
Jonathan Blandford's avatar
Jonathan Blandford committed
41

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60


/* node allocation
 */
struct _GAllocator /* from gmem.c */
{
  gchar      *name;
  guint16     n_preallocs;
  guint       is_unused : 1;
  guint       type : 4;
  GAllocator *last;
  GMemChunk  *mem_chunk;
  GtkRBNode  *free_nodes; /* implementation specific */
};


G_LOCK_DEFINE_STATIC (current_allocator);
static GAllocator *current_allocator = NULL;

Jonathan Blandford's avatar
Jonathan Blandford committed
61

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
/* HOLDS: current_allocator_lock */
static void
_gtk_rbnode_validate_allocator (GAllocator *allocator)
{
  g_return_if_fail (allocator != NULL);
  g_return_if_fail (allocator->is_unused == TRUE);

  if (allocator->type != G_ALLOCATOR_NODE)
    {
      allocator->type = G_ALLOCATOR_NODE;
      if (allocator->mem_chunk)
	{
	  g_mem_chunk_destroy (allocator->mem_chunk);
	  allocator->mem_chunk = NULL;
	}
    }

  if (!allocator->mem_chunk)
    {
      allocator->mem_chunk = g_mem_chunk_new (allocator->name,
					      sizeof (GtkRBNode),
					      sizeof (GtkRBNode) * allocator->n_preallocs,
					      G_ALLOC_ONLY);
      allocator->free_nodes = NULL;
    }

  allocator->is_unused = FALSE;
}

static GtkRBNode *
_gtk_rbnode_new (GtkRBTree *tree,
		 gint       height)
{
  GtkRBNode *node;

  G_LOCK (current_allocator);
  if (!current_allocator)
    {
      GAllocator *allocator = g_allocator_new ("GTK+ default GtkRBNode allocator",
					       128);
      _gtk_rbnode_validate_allocator (allocator);
      allocator->last = NULL;
      current_allocator = allocator;
    }
  if (!current_allocator->free_nodes)
    node = g_chunk_new (GtkRBNode, current_allocator->mem_chunk);
  else
    {
      node = current_allocator->free_nodes;
      current_allocator->free_nodes = node->left;
    }
  G_UNLOCK (current_allocator);

  node->left = tree->nil;
  node->right = tree->nil;
  node->parent = tree->nil;
  node->flags = GTK_RBNODE_RED;
Havoc Pennington's avatar
Havoc Pennington committed
119
  node->parity = 1;
120 121 122 123 124 125 126 127 128 129 130 131
  node->count = 1;
  node->children = NULL;
  node->offset = height;
  return node;
}

static void
_gtk_rbnode_free (GtkRBNode *node)
{
  G_LOCK (current_allocator);
  node->left = current_allocator->free_nodes;
  current_allocator->free_nodes = node;
Havoc Pennington's avatar
Havoc Pennington committed
132 133 134 135 136 137 138 139 140 141 142
  if (gtk_debug_flags & GTK_DEBUG_TREE)
    {
      /* unfortunately node->left has to continue to point to
       * a node...
       */
      node->right = (gpointer) 0xdeadbeef;
      node->parent = (gpointer) 0xdeadbeef;
      node->offset = 56789;
      node->count = 56789;
      node->flags = 0;
    }
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
  G_UNLOCK (current_allocator);
}

static void
_gtk_rbnode_rotate_left (GtkRBTree *tree,
			 GtkRBNode *node)
{
  gint node_height, right_height;
  GtkRBNode *right = node->right;

  g_return_if_fail (node != tree->nil);

  node_height = node->offset -
    (node->left?node->left->offset:0) -
    (node->right?node->right->offset:0) -
    (node->children?node->children->root->offset:0);
  right_height = right->offset -
    (right->left?right->left->offset:0) -
    (right->right?right->right->offset:0) -
    (right->children?right->children->root->offset:0);
  node->right = right->left;
  if (right->left != tree->nil)
    right->left->parent = node;

  if (right != tree->nil)
    right->parent = node->parent;
  if (node->parent != tree->nil)
    {
      if (node == node->parent->left)
	node->parent->left = right;
      else
	node->parent->right = right;
    } else {
      tree->root = right;
    }

  right->left = node;
  if (node != tree->nil)
    node->parent = right;

  node->count = 1 + (node->left?node->left->count:0) +
    (node->right?node->right->count:0);
  right->count = 1 + (right->left?right->left->count:0) +
    (right->right?right->right->count:0);
Havoc Pennington's avatar
Havoc Pennington committed
187

188 189 190 191 192 193 194 195
  node->offset = node_height +
    (node->left?node->left->offset:0) +
    (node->right?node->right->offset:0) +
    (node->children?node->children->root->offset:0);
  right->offset = right_height +
    (right->left?right->left->offset:0) +
    (right->right?right->right->offset:0) +
    (right->children?right->children->root->offset:0);
Havoc Pennington's avatar
Havoc Pennington committed
196

Jonathan Blandford's avatar
Jonathan Blandford committed
197 198
  _fixup_validation (tree, node);
  _fixup_validation (tree, right);
199 200
  _fixup_parity (tree, node);
  _fixup_parity (tree, right);
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
}

static void
_gtk_rbnode_rotate_right (GtkRBTree *tree,
			  GtkRBNode *node)
{
  gint node_height, left_height;
  GtkRBNode *left = node->left;

  g_return_if_fail (node != tree->nil);

  node_height = node->offset -
    (node->left?node->left->offset:0) -
    (node->right?node->right->offset:0) -
    (node->children?node->children->root->offset:0);
  left_height = left->offset -
    (left->left?left->left->offset:0) -
    (left->right?left->right->offset:0) -
    (left->children?left->children->root->offset:0);
Havoc Pennington's avatar
Havoc Pennington committed
220
  
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
  node->left = left->right;
  if (left->right != tree->nil)
    left->right->parent = node;

  if (left != tree->nil)
    left->parent = node->parent;
  if (node->parent != tree->nil)
    {
      if (node == node->parent->right)
	node->parent->right = left;
      else
	node->parent->left = left;
    }
  else
    {
      tree->root = left;
    }

  /* link node and left */
  left->right = node;
  if (node != tree->nil)
    node->parent = left;

  node->count = 1 + (node->left?node->left->count:0) +
    (node->right?node->right->count:0);
  left->count = 1 + (left->left?left->left->count:0) +
    (left->right?left->right->count:0);
Havoc Pennington's avatar
Havoc Pennington committed
248

249 250 251 252 253 254 255 256
  node->offset = node_height +
    (node->left?node->left->offset:0) +
    (node->right?node->right->offset:0) +
    (node->children?node->children->root->offset:0);
  left->offset = left_height +
    (left->left?left->left->offset:0) +
    (left->right?left->right->offset:0) +
    (left->children?left->children->root->offset:0);
257

Jonathan Blandford's avatar
Jonathan Blandford committed
258 259
  _fixup_validation (tree, node);
  _fixup_validation (tree, left);
260 261
  _fixup_parity (tree, node);
  _fixup_parity (tree, left);
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
}

static void
_gtk_rbtree_insert_fixup (GtkRBTree *tree,
			  GtkRBNode *node)
{

  /* check Red-Black properties */
  while (node != tree->root && GTK_RBNODE_GET_COLOR (node->parent) == GTK_RBNODE_RED)
    {
      /* we have a violation */
      if (node->parent == node->parent->parent->left)
	{
	  GtkRBNode *y = node->parent->parent->right;
	  if (GTK_RBNODE_GET_COLOR (y) == GTK_RBNODE_RED)
	    {
				/* uncle is GTK_RBNODE_RED */
	      GTK_RBNODE_SET_COLOR (node->parent, GTK_RBNODE_BLACK);
	      GTK_RBNODE_SET_COLOR (y, GTK_RBNODE_BLACK);
	      GTK_RBNODE_SET_COLOR (node->parent->parent, GTK_RBNODE_RED);
	      node = node->parent->parent;
	    }
	  else
	    {
				/* uncle is GTK_RBNODE_BLACK */
	      if (node == node->parent->right)
		{
		  /* make node a left child */
		  node = node->parent;
		  _gtk_rbnode_rotate_left (tree, node);
		}

				/* recolor and rotate */
	      GTK_RBNODE_SET_COLOR (node->parent, GTK_RBNODE_BLACK);
	      GTK_RBNODE_SET_COLOR (node->parent->parent, GTK_RBNODE_RED);
	      _gtk_rbnode_rotate_right(tree, node->parent->parent);
	    }
	}
      else
	{
	  /* mirror image of above code */
	  GtkRBNode *y = node->parent->parent->left;
	  if (GTK_RBNODE_GET_COLOR (y) == GTK_RBNODE_RED)
	    {
				/* uncle is GTK_RBNODE_RED */
	      GTK_RBNODE_SET_COLOR (node->parent, GTK_RBNODE_BLACK);
	      GTK_RBNODE_SET_COLOR (y, GTK_RBNODE_BLACK);
	      GTK_RBNODE_SET_COLOR (node->parent->parent, GTK_RBNODE_RED);
	      node = node->parent->parent;
	    }
	  else
	    {
				/* uncle is GTK_RBNODE_BLACK */
	      if (node == node->parent->left)
		{
		  node = node->parent;
		  _gtk_rbnode_rotate_right (tree, node);
		}
	      GTK_RBNODE_SET_COLOR (node->parent, GTK_RBNODE_BLACK);
	      GTK_RBNODE_SET_COLOR (node->parent->parent, GTK_RBNODE_RED);
	      _gtk_rbnode_rotate_left (tree, node->parent->parent);
	    }
	}
    }
  GTK_RBNODE_SET_COLOR (tree->root, GTK_RBNODE_BLACK);
}

static void
_gtk_rbtree_remove_node_fixup (GtkRBTree *tree,
			       GtkRBNode *node)
{
  while (node != tree->root && GTK_RBNODE_GET_COLOR (node) == GTK_RBNODE_BLACK)
    {
      if (node == node->parent->left)
	{
	  GtkRBNode *w = node->parent->right;
	  if (GTK_RBNODE_GET_COLOR (w) == GTK_RBNODE_RED)
	    {
	      GTK_RBNODE_SET_COLOR (w, GTK_RBNODE_BLACK);
	      GTK_RBNODE_SET_COLOR (node->parent, GTK_RBNODE_RED);
	      _gtk_rbnode_rotate_left (tree, node->parent);
	      w = node->parent->right;
	    }
	  if (GTK_RBNODE_GET_COLOR (w->left) == GTK_RBNODE_BLACK && GTK_RBNODE_GET_COLOR (w->right) == GTK_RBNODE_BLACK)
	    {
	      GTK_RBNODE_SET_COLOR (w, GTK_RBNODE_RED);
	      node = node->parent;
	    }
	  else
	    {
	      if (GTK_RBNODE_GET_COLOR (w->right) == GTK_RBNODE_BLACK)
		{
		  GTK_RBNODE_SET_COLOR (w->left, GTK_RBNODE_BLACK);
		  GTK_RBNODE_SET_COLOR (w, GTK_RBNODE_RED);
		  _gtk_rbnode_rotate_right (tree, w);
		  w = node->parent->right;
		}
	      GTK_RBNODE_SET_COLOR (w, GTK_RBNODE_GET_COLOR (node->parent));
	      GTK_RBNODE_SET_COLOR (node->parent, GTK_RBNODE_BLACK);
	      GTK_RBNODE_SET_COLOR (w->right, GTK_RBNODE_BLACK);
	      _gtk_rbnode_rotate_left (tree, node->parent);
	      node = tree->root;
	    }
	}
      else
	{
	  GtkRBNode *w = node->parent->left;
	  if (GTK_RBNODE_GET_COLOR (w) == GTK_RBNODE_RED)
	    {
	      GTK_RBNODE_SET_COLOR (w, GTK_RBNODE_BLACK);
	      GTK_RBNODE_SET_COLOR (node->parent, GTK_RBNODE_RED);
	      _gtk_rbnode_rotate_right (tree, node->parent);
	      w = node->parent->left;
	    }
	  if (GTK_RBNODE_GET_COLOR (w->right) == GTK_RBNODE_BLACK && GTK_RBNODE_GET_COLOR (w->left) == GTK_RBNODE_BLACK)
	    {
	      GTK_RBNODE_SET_COLOR (w, GTK_RBNODE_RED);
	      node = node->parent;
	    }
	  else
	    {
	      if (GTK_RBNODE_GET_COLOR (w->left) == GTK_RBNODE_BLACK)
		{
		  GTK_RBNODE_SET_COLOR (w->right, GTK_RBNODE_BLACK);
		  GTK_RBNODE_SET_COLOR (w, GTK_RBNODE_RED);
		  _gtk_rbnode_rotate_left (tree, w);
		  w = node->parent->left;
		}
	      GTK_RBNODE_SET_COLOR (w, GTK_RBNODE_GET_COLOR (node->parent));
	      GTK_RBNODE_SET_COLOR (node->parent, GTK_RBNODE_BLACK);
	      GTK_RBNODE_SET_COLOR (w->left, GTK_RBNODE_BLACK);
	      _gtk_rbnode_rotate_right (tree, node->parent);
	      node = tree->root;
	    }
	}
    }
  GTK_RBNODE_SET_COLOR (node, GTK_RBNODE_BLACK);
}

/* Public functions */
void
_gtk_rbnode_push_allocator (GAllocator *allocator)
{
  G_LOCK (current_allocator);
  _gtk_rbnode_validate_allocator ( allocator );
  allocator->last = current_allocator;
  current_allocator = allocator;
  G_UNLOCK (current_allocator);
}

void
_gtk_rbnode_pop_allocator (void)
{
  G_LOCK (current_allocator);
  if (current_allocator)
    {
      GAllocator *allocator;

      allocator = current_allocator;
      current_allocator = allocator->last;
      allocator->last = NULL;
      allocator->is_unused = TRUE;
    }
  G_UNLOCK (current_allocator);
}

GtkRBTree *
_gtk_rbtree_new (void)
{
  GtkRBTree *retval;

  retval = (GtkRBTree *) g_new (GtkRBTree, 1);
  retval->parent_tree = NULL;
  retval->parent_node = NULL;

  retval->nil = g_new0 (GtkRBNode, 1);
  retval->nil->left = NULL;
  retval->nil->right = NULL;
  retval->nil->parent = NULL;
  retval->nil->flags = GTK_RBNODE_BLACK;
  retval->nil->count = 0;
  retval->nil->offset = 0;
Havoc Pennington's avatar
Havoc Pennington committed
444
  retval->nil->parity = 0;
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483

  retval->root = retval->nil;
  return retval;
}

static void
_gtk_rbtree_free_helper (GtkRBTree  *tree,
			 GtkRBNode  *node,
			 gpointer    data)
{
  if (node->children)
    _gtk_rbtree_free (node->children);

  _gtk_rbnode_free (node);
}

void
_gtk_rbtree_free (GtkRBTree *tree)
{
  _gtk_rbtree_traverse (tree,
			tree->root,
			G_POST_ORDER,
			_gtk_rbtree_free_helper,
			NULL);

  if (tree->parent_node &&
      tree->parent_node->children == tree)
    tree->parent_node->children = NULL;
  _gtk_rbnode_free (tree->nil);
  g_free (tree);
}

void
_gtk_rbtree_remove (GtkRBTree *tree)
{
  GtkRBTree *tmp_tree;
  GtkRBNode *tmp_node;

  gint height = tree->root->offset;
Havoc Pennington's avatar
Havoc Pennington committed
484

485
#ifdef G_ENABLE_DEBUG  
Havoc Pennington's avatar
Havoc Pennington committed
486 487
  if (gtk_debug_flags & GTK_DEBUG_TREE)
    _gtk_rbtree_test (G_STRLOC, tree);
488
#endif
Havoc Pennington's avatar
Havoc Pennington committed
489
  
490 491 492
  tmp_tree = tree->parent_tree;
  tmp_node = tree->parent_node;

Jonathan Blandford's avatar
Jonathan Blandford committed
493 494 495 496
  /* ugly hack to make _fixup_validation work in the first iteration of the
   * loop below */
  GTK_RBNODE_UNSET_FLAG (tree->root, GTK_RBNODE_DESCENDANTS_INVALID);
  
497 498
  while (tmp_tree && tmp_node && tmp_node != tmp_tree->nil)
    {
Jonathan Blandford's avatar
Jonathan Blandford committed
499
      _fixup_validation (tmp_tree, tmp_node);
500
      tmp_node->offset -= height;
Havoc Pennington's avatar
Havoc Pennington committed
501 502 503 504 505

      /* If the removed tree was odd, flip all parents */
      if (tree->root->parity)
        tmp_node->parity = !tmp_node->parity;
      
506 507 508 509 510 511 512
      tmp_node = tmp_node->parent;
      if (tmp_node == tmp_tree->nil)
	{
	  tmp_node = tmp_tree->parent_node;
	  tmp_tree = tmp_tree->parent_tree;
	}
    }
Havoc Pennington's avatar
Havoc Pennington committed
513 514

  tmp_tree = tree->parent_tree;
515
  tmp_node = tree->parent_node;
516
  _gtk_rbtree_free (tree);
Havoc Pennington's avatar
Havoc Pennington committed
517

518
#ifdef G_ENABLE_DEBUG  
Havoc Pennington's avatar
Havoc Pennington committed
519 520
  if (gtk_debug_flags & GTK_DEBUG_TREE)
    _gtk_rbtree_test (G_STRLOC, tmp_tree);
521
#endif
522 523 524 525
}


GtkRBNode *
Jonathan Blandford's avatar
Jonathan Blandford committed
526 527 528 529
_gtk_rbtree_insert_after (GtkRBTree *tree,
			  GtkRBNode *current,
			  gint       height,
			  gboolean   valid)
530 531 532 533
{
  GtkRBNode *node;
  gboolean right = TRUE;
  GtkRBNode *tmp_node;
Havoc Pennington's avatar
Havoc Pennington committed
534
  GtkRBTree *tmp_tree;  
535

Owen Taylor's avatar
Owen Taylor committed
536
#ifdef G_ENABLE_DEBUG  
537 538
  if (gtk_debug_flags & GTK_DEBUG_TREE)
    {
Owen Taylor's avatar
Owen Taylor committed
539
      g_print ("\n\n_gtk_rbtree_insert_after: %p\n", current);
540 541 542
      _gtk_rbtree_debug_spew (tree);
      _gtk_rbtree_test (G_STRLOC, tree);
    }
Owen Taylor's avatar
Owen Taylor committed
543
#endif /* G_ENABLE_DEBUG */  
544

545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
  if (current != NULL && current->right != tree->nil)
    {
      current = current->right;
      while (current->left != tree->nil)
	current = current->left;
      right = FALSE;
    }
  /* setup new node */
  node = _gtk_rbnode_new (tree, height);
  node->parent = (current?current:tree->nil);

  /* insert node in tree */
  if (current)
    {
      if (right)
	current->right = node;
      else
	current->left = node;
      tmp_node = node->parent;
      tmp_tree = tree;
    }
  else
    {
      tree->root = node;
      tmp_node = tree->parent_node;
      tmp_tree = tree->parent_tree;
    }

  while (tmp_tree && tmp_node && tmp_node != tmp_tree->nil)
    {
      /* We only want to propagate the count if we are in the tree we
       * started in. */
      if (tmp_tree == tree)
	tmp_node->count++;
Havoc Pennington's avatar
Havoc Pennington committed
579 580

      tmp_node->parity += 1;
581 582 583 584 585 586 587 588
      tmp_node->offset += height;
      tmp_node = tmp_node->parent;
      if (tmp_node == tmp_tree->nil)
	{
	  tmp_node = tmp_tree->parent_node;
	  tmp_tree = tmp_tree->parent_tree;
	}
    }
Jonathan Blandford's avatar
Jonathan Blandford committed
589 590 591 592 593 594

  if (valid)
    _gtk_rbtree_node_mark_valid (tree, node);
  else
    _gtk_rbtree_node_mark_invalid (tree, node);

Jonathan Blandford's avatar
Jonathan Blandford committed
595 596
  _gtk_rbtree_insert_fixup (tree, node);

Owen Taylor's avatar
Owen Taylor committed
597
#ifdef G_ENABLE_DEBUG  
Jonathan Blandford's avatar
Jonathan Blandford committed
598
  if (gtk_debug_flags & GTK_DEBUG_TREE)
599
    {
600
      g_print ("_gtk_rbtree_insert_after finished...\n");
601
      _gtk_rbtree_debug_spew (tree);
602
      g_print ("\n\n");
603 604
      _gtk_rbtree_test (G_STRLOC, tree);
    }
Owen Taylor's avatar
Owen Taylor committed
605
#endif /* G_ENABLE_DEBUG */  
Jonathan Blandford's avatar
Jonathan Blandford committed
606

607 608 609 610
  return node;
}

GtkRBNode *
Jonathan Blandford's avatar
Jonathan Blandford committed
611 612 613 614
_gtk_rbtree_insert_before (GtkRBTree *tree,
			   GtkRBNode *current,
			   gint       height,
			   gboolean   valid)
615 616 617 618 619
{
  GtkRBNode *node;
  gboolean left = TRUE;
  GtkRBNode *tmp_node;
  GtkRBTree *tmp_tree;
Owen Taylor's avatar
Owen Taylor committed
620 621

#ifdef G_ENABLE_DEBUG  
622 623
  if (gtk_debug_flags & GTK_DEBUG_TREE)
    {
Owen Taylor's avatar
Owen Taylor committed
624
      g_print ("\n\n_gtk_rbtree_insert_before: %p\n", current);
625 626 627
      _gtk_rbtree_debug_spew (tree);
      _gtk_rbtree_test (G_STRLOC, tree);
    }
Owen Taylor's avatar
Owen Taylor committed
628 629
#endif /* G_ENABLE_DEBUG */
  
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
  if (current != NULL && current->left != tree->nil)
    {
      current = current->left;
      while (current->right != tree->nil)
	current = current->right;
      left = FALSE;
    }

  /* setup new node */
  node = _gtk_rbnode_new (tree, height);
  node->parent = (current?current:tree->nil);

  /* insert node in tree */
  if (current)
    {
      if (left)
	current->left = node;
      else
	current->right = node;
      tmp_node = node->parent;
      tmp_tree = tree;
    }
  else
    {
      tree->root = node;
      tmp_node = tree->parent_node;
      tmp_tree = tree->parent_tree;
    }

  while (tmp_tree && tmp_node && tmp_node != tmp_tree->nil)
    {
      /* We only want to propagate the count if we are in the tree we
       * started in. */
      if (tmp_tree == tree)
	tmp_node->count++;
Havoc Pennington's avatar
Havoc Pennington committed
665 666

      tmp_node->parity += 1;
667 668 669 670 671 672 673 674
      tmp_node->offset += height;
      tmp_node = tmp_node->parent;
      if (tmp_node == tmp_tree->nil)
	{
	  tmp_node = tmp_tree->parent_node;
	  tmp_tree = tmp_tree->parent_tree;
	}
    }
Jonathan Blandford's avatar
Jonathan Blandford committed
675 676 677 678 679 680

  if (valid)
    _gtk_rbtree_node_mark_valid (tree, node);
  else
    _gtk_rbtree_node_mark_invalid (tree, node);

Jonathan Blandford's avatar
Jonathan Blandford committed
681 682
  _gtk_rbtree_insert_fixup (tree, node);

Owen Taylor's avatar
Owen Taylor committed
683
#ifdef G_ENABLE_DEBUG  
Jonathan Blandford's avatar
Jonathan Blandford committed
684
  if (gtk_debug_flags & GTK_DEBUG_TREE)
685
    {
686
      g_print ("_gtk_rbtree_insert_before finished...\n");
687
      _gtk_rbtree_debug_spew (tree);
688
      g_print ("\n\n");
689 690
      _gtk_rbtree_test (G_STRLOC, tree);
    }
Owen Taylor's avatar
Owen Taylor committed
691 692
#endif /* G_ENABLE_DEBUG */
  
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
  return node;
}

GtkRBNode *
_gtk_rbtree_find_count (GtkRBTree *tree,
			gint       count)
{
  GtkRBNode *node;

  node = tree->root;
  while (node != tree->nil && (node->left->count + 1 != count))
    {
      if (node->left->count >= count)
	node = node->left;
      else
	{
	  count -= (node->left->count + 1);
	  node = node->right;
	}
    }
  if (node == tree->nil)
    return NULL;
  return node;
}

void
_gtk_rbtree_node_set_height (GtkRBTree *tree,
			     GtkRBNode *node,
			     gint       height)
{
  gint diff = height - GTK_RBNODE_GET_HEIGHT (node);
  GtkRBNode *tmp_node = node;
  GtkRBTree *tmp_tree = tree;

  if (diff == 0)
    return;

  while (tmp_tree && tmp_node && tmp_node != tmp_tree->nil)
    {
      tmp_node->offset += diff;
      tmp_node = tmp_node->parent;
      if (tmp_node == tmp_tree->nil)
	{
	  tmp_node = tmp_tree->parent_node;
	  tmp_tree = tmp_tree->parent_tree;
	}
    }
740
#ifdef G_ENABLE_DEBUG  
Jonathan Blandford's avatar
Jonathan Blandford committed
741 742
  if (gtk_debug_flags & GTK_DEBUG_TREE)
    _gtk_rbtree_test (G_STRLOC, tree);
743
#endif
744 745
}

746 747 748 749 750 751 752 753 754 755 756 757 758 759
void
_gtk_rbtree_node_mark_invalid (GtkRBTree *tree,
			       GtkRBNode *node)
{
  if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_INVALID))
    return;

  GTK_RBNODE_SET_FLAG (node, GTK_RBNODE_INVALID);
  do
    {
      if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_DESCENDANTS_INVALID))
	return;
      GTK_RBNODE_SET_FLAG (node, GTK_RBNODE_DESCENDANTS_INVALID);
      node = node->parent;
Jonathan Blandford's avatar
Jonathan Blandford committed
760
      if (node == tree->nil)
761 762 763 764 765 766 767 768
	{
	  node = tree->parent_node;
	  tree = tree->parent_tree;
	}
    }
  while (node);
}

Jonathan Blandford's avatar
Jonathan Blandford committed
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
#if 0
/* Draconian version. */
void
_gtk_rbtree_node_mark_invalid (GtkRBTree *tree,
			       GtkRBNode *node)
{
  GTK_RBNODE_SET_FLAG (node, GTK_RBNODE_INVALID);
  do
    {
      _fixup_validation (tree, node);
      node = node->parent;
      if (node == tree->nil)
	{
	  node = tree->parent_node;
	  tree = tree->parent_tree;
	}
    }
  while (node);
}
#endif

790 791 792 793
void
_gtk_rbtree_node_mark_valid (GtkRBTree *tree,
			     GtkRBNode *node)
{
Jonathan Blandford's avatar
Jonathan Blandford committed
794 795
  if ((!GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_INVALID)) &&
      (!GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_COLUMN_INVALID)))
796 797 798
    return;

  GTK_RBNODE_UNSET_FLAG (node, GTK_RBNODE_INVALID);
Jonathan Blandford's avatar
Jonathan Blandford committed
799 800
  GTK_RBNODE_UNSET_FLAG (node, GTK_RBNODE_COLUMN_INVALID);

801 802
  do
    {
Jonathan Blandford's avatar
Jonathan Blandford committed
803 804
      if ((GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_INVALID)) ||
	  (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_COLUMN_INVALID)) ||
805
	  (node->children && GTK_RBNODE_FLAG_SET (node->children->root, GTK_RBNODE_DESCENDANTS_INVALID)) ||
Jonathan Blandford's avatar
Jonathan Blandford committed
806 807
	  (node->left != tree->nil && GTK_RBNODE_FLAG_SET (node->left, GTK_RBNODE_DESCENDANTS_INVALID)) ||
	  (node->right != tree->nil && GTK_RBNODE_FLAG_SET (node->right, GTK_RBNODE_DESCENDANTS_INVALID)))
808
	return;
Jonathan Blandford's avatar
Jonathan Blandford committed
809

810 811
      GTK_RBNODE_UNSET_FLAG (node, GTK_RBNODE_DESCENDANTS_INVALID);
      node = node->parent;
Jonathan Blandford's avatar
Jonathan Blandford committed
812
      if (node == tree->nil)
813 814 815 816 817 818 819 820
	{
	  node = tree->parent_node;
	  tree = tree->parent_tree;
	}
    }
  while (node);
}

Jonathan Blandford's avatar
Jonathan Blandford committed
821 822 823 824 825 826 827 828
#if 0
/* Draconian version */
void
_gtk_rbtree_node_mark_valid (GtkRBTree *tree,
			     GtkRBNode *node)
{
  GTK_RBNODE_UNSET_FLAG (node, GTK_RBNODE_INVALID);
  GTK_RBNODE_UNSET_FLAG (node, GTK_RBNODE_COLUMN_INVALID);
Jonathan Blandford's avatar
Jonathan Blandford committed
829

Jonathan Blandford's avatar
Jonathan Blandford committed
830 831 832 833 834 835 836 837 838 839 840 841 842
  do
    {
      _fixup_validation (tree, node);
      node = node->parent;
      if (node == tree->nil)
	{
	  node = tree->parent_node;
	  tree = tree->parent_tree;
	}
    }
  while (node);
}
#endif
Jonathan Blandford's avatar
Jonathan Blandford committed
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
/* Assume tree is the root node as it doesn't set DESCENDANTS_INVALID above.
 */
void
_gtk_rbtree_column_invalid (GtkRBTree *tree)
{
  GtkRBNode *node;

  if (tree == NULL)
    return;
  node = tree->root;
  g_assert (node);

  while (node->left != tree->nil)
    node = node->left;

  do
    {
      if (! (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_INVALID)))
	GTK_RBNODE_SET_FLAG (node, GTK_RBNODE_COLUMN_INVALID);
      GTK_RBNODE_SET_FLAG (node, GTK_RBNODE_DESCENDANTS_INVALID);

      if (node->children)
	_gtk_rbtree_column_invalid (node->children);
    }
  while ((node = _gtk_rbtree_next (tree, node)) != NULL);
}

870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
void
_gtk_rbtree_mark_invalid (GtkRBTree *tree)
{
  GtkRBNode *node;

  if (tree == NULL)
    return;
  node = tree->root;
  g_assert (node);

  while (node->left != tree->nil)
    node = node->left;

  do
    {
      GTK_RBNODE_SET_FLAG (node, GTK_RBNODE_INVALID);
      GTK_RBNODE_SET_FLAG (node, GTK_RBNODE_DESCENDANTS_INVALID);

      if (node->children)
	_gtk_rbtree_mark_invalid (node->children);
    }
  while ((node = _gtk_rbtree_next (tree, node)) != NULL);
}

894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
void
_gtk_rbtree_set_fixed_height (GtkRBTree *tree,
			      gint       height)
{
  GtkRBNode *node;

  if (tree == NULL)
    return;

  node = tree->root;
  g_assert (node);

  while (node->left != tree->nil)
    node = node->left;

  do
    {
      if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_INVALID))
	_gtk_rbtree_node_set_height (tree, node, height);

      if (node->children)
	_gtk_rbtree_set_fixed_height (node->children, height);
    }
  while ((node = _gtk_rbtree_next (tree, node)) != NULL);
}

920 921 922 923 924 925
typedef struct _GtkRBReorder
{
  GtkRBTree *children;
  gint height;
  gint flags;
  gint order;
926
  gint invert_order;
Jonathan Blandford's avatar
Jonathan Blandford committed
927
  gint parity;
928 929 930 931 932 933 934 935 936
} GtkRBReorder;

static int
gtk_rbtree_reorder_sort_func (gconstpointer a,
			      gconstpointer b)
{
  return ((GtkRBReorder *) a)->order > ((GtkRBReorder *) b)->order;
}

937 938 939 940 941 942 943
static int
gtk_rbtree_reorder_invert_func (gconstpointer a,
				gconstpointer b)
{
  return ((GtkRBReorder *) a)->invert_order > ((GtkRBReorder *) b)->invert_order;
}

944 945 946 947 948 949 950
static void
gtk_rbtree_reorder_fixup (GtkRBTree *tree,
			  GtkRBNode *node)
{
  if (node == tree->nil)
    return;

Jonathan Blandford's avatar
Jonathan Blandford committed
951 952
  node->parity = 1;

953 954 955 956
  if (node->left != tree->nil)
    {
      gtk_rbtree_reorder_fixup (tree, node->left);
      node->offset += node->left->offset;
Jonathan Blandford's avatar
Jonathan Blandford committed
957
      node->parity += node->left->parity;
958 959 960 961 962
    }
  if (node->right != tree->nil)
    {
      gtk_rbtree_reorder_fixup (tree, node->right);
      node->offset += node->right->offset;
Jonathan Blandford's avatar
Jonathan Blandford committed
963
      node->parity += node->right->parity;
964 965 966
    }
      
  if (node->children)
Jonathan Blandford's avatar
Jonathan Blandford committed
967 968 969 970
    {
      node->offset += node->children->root->offset;
      node->parity += node->children->root->parity;
    }
Jonathan Blandford's avatar
Jonathan Blandford committed
971 972 973 974 975 976 977 978
  
  if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_INVALID) ||
      (node->right != tree->nil && GTK_RBNODE_FLAG_SET (node->right, GTK_RBNODE_DESCENDANTS_INVALID)) ||
      (node->left != tree->nil && GTK_RBNODE_FLAG_SET (node->left, GTK_RBNODE_DESCENDANTS_INVALID)) ||
      (node->children && GTK_RBNODE_FLAG_SET (node->children->root, GTK_RBNODE_DESCENDANTS_INVALID)))
    GTK_RBNODE_SET_FLAG (node, GTK_RBNODE_DESCENDANTS_INVALID);
  else
    GTK_RBNODE_UNSET_FLAG (node, GTK_RBNODE_DESCENDANTS_INVALID);
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
}

/* It basically pulls everything out of the tree, rearranges it, and puts it
 * back together.  Our strategy is to keep the old RBTree intact, and just
 * rearrange the contents.  When that is done, we go through and update the
 * heights.  There is probably a more elegant way to write this function.  If
 * anyone wants to spend the time writing it, patches will be accepted.
 */
void
_gtk_rbtree_reorder (GtkRBTree *tree,
		     gint      *new_order,
		     gint       length)
{
  GtkRBReorder reorder;
  GArray *array;
  GtkRBNode *node;
  gint i;
996 997 998 999 1000
  
  g_return_if_fail (tree != NULL);
  g_return_if_fail (length > 0);
  g_return_if_fail (tree->root->count == length);
  
1001 1002 1003 1004 1005
  /* Sort the trees values in the new tree. */
  array = g_array_sized_new (FALSE, FALSE, sizeof (GtkRBReorder), length);
  for (i = 0; i < length; i++)
    {
      reorder.order = new_order[i];
1006
      reorder.invert_order = i;
1007
      g_array_append_val (array, reorder);
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
    }

  g_array_sort(array, gtk_rbtree_reorder_sort_func);

  /* rewind node*/
  node = tree->root;
  while (node && node->left != tree->nil)
    node = node->left;

  for (i = 0; i < length; i++)
    {
      g_assert (node != tree->nil);
      g_array_index (array, GtkRBReorder, i).children = node->children;
      g_array_index (array, GtkRBReorder, i).flags = GTK_RBNODE_NON_COLORS & node->flags;
      g_array_index (array, GtkRBReorder, i).height = GTK_RBNODE_GET_HEIGHT (node);
1023 1024 1025 1026

      node = _gtk_rbtree_next (tree, node);
    }

1027 1028
  g_array_sort (array, gtk_rbtree_reorder_invert_func);
 
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
  /* rewind node*/
  node = tree->root;
  while (node && node->left != tree->nil)
    node = node->left;

  /* Go through the tree and change the values to the new ones. */
  for (i = 0; i < length; i++)
    {
      reorder = g_array_index (array, GtkRBReorder, i);
      node->children = reorder.children;
1039 1040
      if (node->children)
	node->children->parent_node = node;
1041 1042 1043 1044 1045 1046 1047 1048 1049
      node->flags = GTK_RBNODE_GET_COLOR (node) | reorder.flags;
      /* We temporarily set the height to this. */
      node->offset = reorder.height;
      node = _gtk_rbtree_next (tree, node);
    }
  gtk_rbtree_reorder_fixup (tree, tree->root);
}


1050 1051 1052 1053 1054
gint
_gtk_rbtree_node_find_offset (GtkRBTree *tree,
			      GtkRBNode *node)
{
  GtkRBNode *last;
Havoc Pennington's avatar
Havoc Pennington committed
1055 1056 1057 1058 1059 1060
  gint retval;

  g_assert (node);
  g_assert (node->left);
  
  retval = node->left->offset;
1061 1062 1063 1064 1065

  while (tree && node && node != tree->nil)
    {
      last = node;
      node = node->parent;
Havoc Pennington's avatar
Havoc Pennington committed
1066 1067

      /* Add left branch, plus children, iff we came from the right */
1068
      if (node->right == last)
Havoc Pennington's avatar
Havoc Pennington committed
1069 1070
	retval += node->offset - node->right->offset;
      
1071 1072 1073 1074
      if (node == tree->nil)
	{
	  node = tree->parent_node;
	  tree = tree->parent_tree;
Havoc Pennington's avatar
Havoc Pennington committed
1075 1076

          /* Add the parent node, plus the left branch. */
1077
	  if (node)
Havoc Pennington's avatar
Havoc Pennington committed
1078
	    retval += node->left->offset + GTK_RBNODE_GET_HEIGHT (node);
1079 1080 1081 1082 1083
	}
    }
  return retval;
}

Havoc Pennington's avatar
Havoc Pennington committed
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
gint
_gtk_rbtree_node_find_parity (GtkRBTree *tree,
                              GtkRBNode *node)
{
  GtkRBNode *last;
  gint retval;  
  
  g_assert (node);
  g_assert (node->left);
  
  retval = node->left->parity;

  while (tree && node && node != tree->nil)
    {
      last = node;
      node = node->parent;

      /* Add left branch, plus children, iff we came from the right */
      if (node->right == last)
	retval += node->parity - node->right->parity;
      
      if (node == tree->nil)
	{
	  node = tree->parent_node;
	  tree = tree->parent_tree;

          /* Add the parent node, plus the left branch. */
	  if (node)
	    retval += node->left->parity + 1; /* 1 == GTK_RBNODE_GET_PARITY() */
	}
    }
  
  return retval % 2;
}

1119
gint
Jonathan Blandford's avatar
Jonathan Blandford committed
1120 1121 1122 1123
_gtk_rbtree_real_find_offset (GtkRBTree  *tree,
			      gint        height,
			      GtkRBTree **new_tree,
			      GtkRBNode **new_node)
1124 1125 1126
{
  GtkRBNode *tmp_node;

Jonathan Blandford's avatar
Jonathan Blandford committed
1127 1128
  g_assert (tree);

1129 1130 1131 1132
  if (height < 0)
    {
      *new_tree = NULL;
      *new_node = NULL;
Jonathan Blandford's avatar
Jonathan Blandford committed
1133

1134 1135
      return 0;
    }
Jonathan Blandford's avatar
Jonathan Blandford committed
1136
  
1137
    
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
  tmp_node = tree->root;
  while (tmp_node != tree->nil &&
	 (tmp_node->left->offset > height ||
	  (tmp_node->offset - tmp_node->right->offset) < height))
    {
      if (tmp_node->left->offset > height)
	tmp_node = tmp_node->left;
      else
	{
	  height -= (tmp_node->offset - tmp_node->right->offset);
	  tmp_node = tmp_node->right;
	}
    }
  if (tmp_node == tree->nil)
    {
      *new_tree = NULL;
      *new_node = NULL;
      return 0;
    }
  if (tmp_node->children)
    {
      if ((tmp_node->offset -
	   tmp_node->right->offset -
	   tmp_node->children->root->offset) > height)
	{
	  *new_tree = tree;
	  *new_node = tmp_node;
	  return (height - tmp_node->left->offset);
	}
Jonathan Blandford's avatar
Jonathan Blandford committed
1167 1168 1169 1170 1171 1172 1173 1174
      return _gtk_rbtree_real_find_offset (tmp_node->children,
					   height - tmp_node->left->offset -
					   (tmp_node->offset -
					    tmp_node->left->offset -
					    tmp_node->right->offset -
					    tmp_node->children->root->offset),
					   new_tree,
					   new_node);
1175 1176 1177 1178 1179 1180
    }
  *new_tree = tree;
  *new_node = tmp_node;
  return (height - tmp_node->left->offset);
}

Jonathan Blandford's avatar
Jonathan Blandford committed
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
gint
_gtk_rbtree_find_offset (GtkRBTree  *tree,
			      gint        height,
			      GtkRBTree **new_tree,
			      GtkRBNode **new_node)
{
  g_assert (tree);

  if ((height < 0) ||
      (height >= tree->root->offset))
    {
      *new_tree = NULL;
      *new_node = NULL;

      return 0;
    }
Jonathan Blandford's avatar
Jonathan Blandford committed
1197
  return _gtk_rbtree_real_find_offset (tree, height, new_tree, new_node);
Jonathan Blandford's avatar
Jonathan Blandford committed
1198
}
1199 1200 1201 1202 1203 1204

void
_gtk_rbtree_remove_node (GtkRBTree *tree,
			 GtkRBNode *node)
{
  GtkRBNode *x, *y;
Havoc Pennington's avatar
Havoc Pennington committed
1205 1206
  GtkRBTree *tmp_tree;
  GtkRBNode *tmp_node;
1207 1208
  gint node_height;
  gint y_height;
Havoc Pennington's avatar
Havoc Pennington committed
1209
  
1210 1211
  g_return_if_fail (tree != NULL);
  g_return_if_fail (node != NULL);
Jonathan Blandford's avatar
Jonathan Blandford committed
1212

Owen Taylor's avatar
Owen Taylor committed
1213 1214
  
#ifdef G_ENABLE_DEBUG
Jonathan Blandford's avatar
Jonathan Blandford committed
1215 1216
  if (gtk_debug_flags & GTK_DEBUG_TREE)
    {
Owen Taylor's avatar
Owen Taylor committed
1217
      g_print ("\n\n_gtk_rbtree_remove_node: %p\n", node);
Jonathan Blandford's avatar
Jonathan Blandford committed
1218 1219 1220
      _gtk_rbtree_debug_spew (tree);
      _gtk_rbtree_test (G_STRLOC, tree);
    }
Owen Taylor's avatar
Owen Taylor committed
1221 1222
#endif /* G_ENABLE_DEBUG */
  
1223 1224 1225 1226 1227
  /* make sure we're deleting a node that's actually in the tree */
  for (x = node; x->parent != tree->nil; x = x->parent)
    ;
  g_return_if_fail (x == tree->root);

1228
#ifdef G_ENABLE_DEBUG  
Havoc Pennington's avatar
Havoc Pennington committed
1229 1230
  if (gtk_debug_flags & GTK_DEBUG_TREE)
    _gtk_rbtree_test (G_STRLOC, tree);
1231 1232
#endif
  
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
  if (node->left == tree->nil || node->right == tree->nil)
    {
      y = node;
    }
  else
    {
      y = node->right;

      while (y->left != tree->nil)
	y = y->left;
    }
Havoc Pennington's avatar
Havoc Pennington committed
1244 1245

  /* adjust count only beneath tree */
1246
  for (x = y; x != tree->nil; x = x->parent)
Jonathan Blandford's avatar
Jonathan Blandford committed
1247 1248 1249
    {
      x->count--;
    }
Havoc Pennington's avatar
Havoc Pennington committed
1250 1251

  /* offsets and parity adjust all the way up through parent trees */
1252 1253 1254
  y_height = GTK_RBNODE_GET_HEIGHT (y);
  node_height = GTK_RBNODE_GET_HEIGHT (node) + (node->children?node->children->root->offset:0);

Havoc Pennington's avatar
Havoc Pennington committed
1255 1256 1257 1258
  tmp_tree = tree;
  tmp_node = y;
  while (tmp_tree && tmp_node && tmp_node != tmp_tree->nil)
    {
1259
      tmp_node->offset -= (y_height + (y->children?y->children->root->offset:0));
Jonathan Blandford's avatar
Jonathan Blandford committed
1260
      _fixup_validation (tmp_tree, tmp_node);
1261
      _fixup_parity (tmp_tree, tmp_node);
Havoc Pennington's avatar
Havoc Pennington committed
1262 1263 1264 1265 1266 1267 1268
      tmp_node = tmp_node->parent;
      if (tmp_node == tmp_tree->nil)
	{
	  tmp_node = tmp_tree->parent_node;
	  tmp_tree = tmp_tree->parent_tree;
	}
    }
1269

Jonathan Blandford's avatar
Jonathan Blandford committed
1270
  /* x is y's only child, or nil */
1271 1272 1273 1274 1275 1276 1277 1278
  if (y->left != tree->nil)
    x = y->left;
  else
    x = y->right;

  /* remove y from the parent chain */
  x->parent = y->parent;
  if (y->parent != tree->nil)
Jonathan Blandford's avatar
Jonathan Blandford committed
1279 1280 1281 1282 1283 1284
    {
      if (y == y->parent->left)
	y->parent->left = x;
      else
	y->parent->right = x;
    }
1285
  else
Jonathan Blandford's avatar
Jonathan Blandford committed
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
    {
      tree->root = x;
    }

  /* We need to clean up the validity of the tree.
   */

  tmp_tree = tree;
  tmp_node = x;
  do
    {
1297
      /* We skip the first time, iff x is nil */
Jonathan Blandford's avatar
Jonathan Blandford committed
1298 1299 1300
      if (tmp_node != tmp_tree->nil)
	{
	  _fixup_validation (tmp_tree, tmp_node);
1301
	  _fixup_parity (tmp_tree, tmp_node);
Jonathan Blandford's avatar
Jonathan Blandford committed
1302 1303 1304 1305 1306 1307 1308 1309 1310
	}
      tmp_node = tmp_node->parent;
      if (tmp_node == tmp_tree->nil)
	{
	  tmp_node = tmp_tree->parent_node;
	  tmp_tree = tmp_tree->parent_tree;
	}
    }
  while (tmp_tree != NULL);
1311 1312

  if (y != node)
1313
    {
1314 1315
      gint diff;

1316 1317 1318 1319 1320 1321
      /* Copy the node over */
      if (GTK_RBNODE_GET_COLOR (node) == GTK_RBNODE_BLACK)
	node->flags = ((y->flags & (GTK_RBNODE_NON_COLORS)) | GTK_RBNODE_BLACK);
      else
	node->flags = ((y->flags & (GTK_RBNODE_NON_COLORS)) | GTK_RBNODE_RED);
      node->children = y->children;
Jonathan Blandford's avatar
Jonathan Blandford committed
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
      if (y->children)
	{
	  node->children = y->children;
	  node->children->parent_node = node;
	}
      else
	{
	  node->children = NULL;
	}
      _fixup_validation (tree, node);
1332
      _fixup_parity (tree, node);
1333 1334 1335 1336
      /* We want to see how different our height is from the previous node.
       * To do this, we compare our current height with our supposed height.
       */
      diff = y_height - GTK_RBNODE_GET_HEIGHT (node);
Jonathan Blandford's avatar
Jonathan Blandford committed
1337 1338
      tmp_tree = tree;
      tmp_node = node;
1339

Jonathan Blandford's avatar
Jonathan Blandford committed
1340 1341 1342 1343
      while (tmp_tree && tmp_node && tmp_node != tmp_tree->nil)
	{
	  tmp_node->offset += diff;
	  _fixup_validation (tmp_tree, tmp_node);
1344
	  _fixup_parity (tmp_tree, tmp_node);
Jonathan Blandford's avatar
Jonathan Blandford committed
1345 1346
	  tmp_node = tmp_node->parent;
	  if (tmp_node == tmp_tree->nil)
1347
	    {
Jonathan Blandford's avatar
Jonathan Blandford committed
1348 1349
	      tmp_node = tmp_tree->parent_node;
	      tmp_tree = tmp_tree->parent_tree;
1350 1351
	    }
	}
1352
    }
1353 1354 1355

  if (GTK_RBNODE_GET_COLOR (y) == GTK_RBNODE_BLACK)
    _gtk_rbtree_remove_node_fixup (tree, x);
Havoc Pennington's avatar
Havoc Pennington committed
1356
  _gtk_rbnode_free (y);
1357

Owen Taylor's avatar
Owen Taylor committed
1358
#ifdef G_ENABLE_DEBUG  
1359
  if (gtk_debug_flags & GTK_DEBUG_TREE)
Jonathan Blandford's avatar
Jonathan Blandford committed
1360
    {
1361
      g_print ("_gtk_rbtree_remove_node finished...\n");
Jonathan Blandford's avatar
Jonathan Blandford committed
1362
      _gtk_rbtree_debug_spew (tree);
1363
      g_print ("\n\n");
Jonathan Blandford's avatar
Jonathan Blandford committed
1364 1365
      _gtk_rbtree_test (G_STRLOC, tree);
    }
Owen Taylor's avatar
Owen Taylor committed
1366
#endif /* G_ENABLE_DEBUG */  
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490
}

GtkRBNode *
_gtk_rbtree_next (GtkRBTree *tree,
		  GtkRBNode *node)
{
  g_return_val_if_fail (tree != NULL, NULL);
  g_return_val_if_fail (node != NULL, NULL);

  /* Case 1: the node's below us. */
  if (node->right != tree->nil)
    {
      node = node->right;
      while (node->left != tree->nil)
	node = node->left;
      return node;
    }

  /* Case 2: it's an ancestor */
  while (node->parent != tree->nil)
    {
      if (node->parent->right == node)
	node = node->parent;
      else
	return (node->parent);
    }

  /* Case 3: There is no next node */
  return NULL;
}

GtkRBNode *
_gtk_rbtree_prev (GtkRBTree *tree,
		  GtkRBNode *node)
{
  g_return_val_if_fail (tree != NULL, NULL);
  g_return_val_if_fail (node != NULL, NULL);

  /* Case 1: the node's below us. */
  if (node->left != tree->nil)
    {
      node = node->left;
      while (node->right != tree->nil)
	node = node->right;
      return node;
    }

  /* Case 2: it's an ancestor */
  while (node->parent != tree->nil)
    {
      if (node->parent->left == node)
	node = node->parent;
      else
	return (node->parent);
    }

  /* Case 3: There is no next node */
  return NULL;
}

void
_gtk_rbtree_next_full (GtkRBTree  *tree,
		       GtkRBNode  *node,
		       GtkRBTree **new_tree,
		       GtkRBNode **new_node)
{
  g_return_if_fail (tree != NULL);
  g_return_if_fail (node != NULL);
  g_return_if_fail (new_tree != NULL);
  g_return_if_fail (new_node != NULL);

  if (node->children)
    {
      *new_tree = node->children;
      *new_node = (*new_tree)->root;
      while ((*new_node)->left != (*new_tree)->nil)
	*new_node = (*new_node)->left;
      return;
    }

  *new_tree = tree;
  *new_node = _gtk_rbtree_next (tree, node);

  while ((*new_node == NULL) &&
	 (*new_tree != NULL))
    {
      *new_node = (*new_tree)->parent_node;
      *new_tree = (*new_tree)->parent_tree;
      if (*new_tree)
	*new_node = _gtk_rbtree_next (*new_tree, *new_node);
    }
}

void
_gtk_rbtree_prev_full (GtkRBTree  *tree,
		       GtkRBNode  *node,
		       GtkRBTree **new_tree,
		       GtkRBNode **new_node)
{
  g_return_if_fail (tree != NULL);
  g_return_if_fail (node != NULL);
  g_return_if_fail (new_tree != NULL);
  g_return_if_fail (new_node != NULL);

  *new_tree = tree;
  *new_node = _gtk_rbtree_prev (tree, node);

  if (*new_node == NULL)
    {
      *new_node = (*new_tree)->parent_node;
      *new_tree = (*new_tree)->parent_tree;
    }
  else
    {
      while ((*new_node)->children)
	{
	  *new_tree = (*new_node)->children;
	  *new_node = (*new_tree)->root;
	  while ((*new_node)->right != (*new_tree)->nil)
	    *new_node = (*new_node)->right;
	}
    }
}

Havoc Pennington's avatar
Havoc Pennington committed
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
gint
_gtk_rbtree_get_depth (GtkRBTree *tree)
{
  GtkRBTree *tmp_tree;
  gint depth = 0;

  tmp_tree = tree->parent_tree;
  while (tmp_tree)
    {
      ++depth;
      tmp_tree = tmp_tree->parent_tree;
    }

  return depth;
}

1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570
static void
_gtk_rbtree_traverse_pre_order (GtkRBTree             *tree,
				GtkRBNode             *node,
				GtkRBTreeTraverseFunc  func,
				gpointer               data)
{
  if (node == tree->nil)
    return;

  (* func) (tree, node, data);
  _gtk_rbtree_traverse_pre_order (tree, node->left, func, data);
  _gtk_rbtree_traverse_pre_order (tree, node->right, func, data);
}

static void
_gtk_rbtree_traverse_post_order (GtkRBTree             *tree,
				 GtkRBNode             *node,
				 GtkRBTreeTraverseFunc  func,
				 gpointer               data)
{
  if (node == tree->nil)
    return;

  _gtk_rbtree_traverse_post_order (tree, node->left, func, data);
  _gtk_rbtree_traverse_post_order (tree, node->right, func, data);
  (* func) (tree, node, data);
}

void
_gtk_rbtree_traverse (GtkRBTree             *tree,
		      GtkRBNode             *node,
		      GTraverseType          order,
		      GtkRBTreeTraverseFunc  func,
		      gpointer               data)
{
  g_return_if_fail (tree != NULL);
  g_return_if_fail (node != NULL);
  g_return_if_fail (func != NULL);
  g_return_if_fail (order <= G_LEVEL_ORDER);

  switch (order)
    {
    case G_PRE_ORDER:
      _gtk_rbtree_traverse_pre_order (tree, node, func, data);
      break;
    case G_POST_ORDER:
      _gtk_rbtree_traverse_post_order (tree, node, func, data);
      break;
    case G_IN_ORDER:
    case G_LEVEL_ORDER:
    default:
      g_warning ("unsupported traversal order.");
      break;
    }
}

static gint
_count_nodes (GtkRBTree *tree,
	      GtkRBNode *node)
{
  gint res;
  if (node == tree->nil)
    return 0;

Havoc Pennington's avatar
Havoc Pennington committed
1571 1572 1573
  g_assert (node->left);
  g_assert (node->right);
  
1574 1575 1576 1577 1578 1579 1580 1581
  res = (_count_nodes (tree, node->left) +
	 _count_nodes (tree, node->right) + 1);

  if (res != node->count)
    g_print ("Tree failed\n");
  return res;
}

Jonathan Blandford's avatar
Jonathan Blandford committed
1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599
static inline
void _fixup_validation (GtkRBTree *tree,
			GtkRBNode *node)
{
  if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_INVALID) ||
      GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_COLUMN_INVALID) ||
      (node->left != tree->nil && GTK_RBNODE_FLAG_SET (node->left, GTK_RBNODE_DESCENDANTS_INVALID)) ||
      (node->right != tree->nil && GTK_RBNODE_FLAG_SET (node->right, GTK_RBNODE_DESCENDANTS_INVALID)) ||
      (node->children != NULL && GTK_RBNODE_FLAG_SET (node->children->root, GTK_RBNODE_DESCENDANTS_INVALID)))
    {
      GTK_RBNODE_SET_FLAG (node, GTK_RBNODE_DESCENDANTS_INVALID);
    }
  else
    {
      GTK_RBNODE_UNSET_FLAG (node, GTK_RBNODE_DESCENDANTS_INVALID);
    }
}

1600 1601 1602 1603 1604 1605 1606 1607 1608 1609
static inline
void _fixup_parity (GtkRBTree *tree,
		    GtkRBNode *node)
{
  node->parity = 1 +
    ((node->children != NULL && node->children->root != node->children->nil) ? node->children->root->parity : 0) + 
    ((node->left != tree->nil) ? node->left->parity : 0) + 
    ((node->right != tree->nil) ? node->right->parity : 0);
}

Owen Taylor's avatar
Owen Taylor committed
1610
#ifdef G_ENABLE_DEBUG
Havoc Pennington's avatar
Havoc Pennington committed
1611 1612
static guint
get_parity (GtkRBNode *node)
1613
{
Havoc Pennington's avatar
Havoc Pennington committed
1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
  guint child_total = 0;
  guint rem;

  /* The parity of a node is node->parity minus
   * the parity of left, right, and children.
   *
   * This is equivalent to saying that if left, right, children
   * sum to 0 parity, then node->parity is the parity of node,
   * and if left, right, children are odd parity, then
   * node->parity is the reverse of the node's parity.
   */
  
  child_total += (guint) node->left->parity;
  child_total += (guint) node->right->parity;

  if (node->children)
    child_total += (guint) node->children->root->parity;

  rem = child_total % 2;
  
  if (rem == 0)
    return node->parity;
1636
  else
Havoc Pennington's avatar
Havoc Pennington committed
1637 1638
    return !node->parity;
}
1639

Havoc Pennington's avatar
Havoc Pennington committed
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
static guint
count_parity (GtkRBTree *tree,
              GtkRBNode *node)
{
  guint res;
  
  if (node == tree->nil)
    return 0;
  
  res =
    count_parity (tree, node->left) +
    count_parity (tree, node->right) +
    (guint)1 +
    (node->children ? count_parity (node->children, node->children->root) : 0);

  res = res % (guint)2;
  
  if (res != node->parity)
    g_print ("parity incorrect for node\n");

  if (get_parity (node) != 1)
    g_error ("Node has incorrect parity %d", get_parity (node));
  
  return res;
1664 1665 1666
}

static void
Havoc Pennington's avatar
Havoc Pennington committed
1667 1668
_gtk_rbtree_test_height (GtkRBTree *tree,
                         GtkRBNode *node)
1669
{
Havoc Pennington's avatar
Havoc Pennington committed
1670
  gint computed_offset = 0;
1671

Havoc Pennington's avatar
Havoc Pennington committed
1672 1673 1674 1675
  /* This whole test is sort of a useless truism. */
  
  if (node->left != tree->nil)
    computed_offset += node->left->offset;
1676

Havoc Pennington's avatar
Havoc Pennington committed
1677 1678 1679 1680 1681 1682 1683 1684
  if (node->right != tree->nil)
    computed_offset += node->right->offset;

  if (node->children && node->children->root != node->children->nil)
    computed_offset += node->children->root->offset;

  if (GTK_RBNODE_GET_HEIGHT (node) + computed_offset != node->offset)
    g_error ("node has broken offset\n");
1685

Havoc Pennington's avatar
Havoc Pennington committed
1686 1687 1688 1689 1690 1691 1692 1693
  if (node->left != tree->nil)
    _gtk_rbtree_test_height (tree, node->left);

  if (node->right != tree->nil)
    _gtk_rbtree_test_height (tree, node->right);

  if (node->children && node->children->root != node->children->nil)
    _gtk_rbtree_test_height (node->children, node->children->root);
1694 1695
}

Jonathan Blandford's avatar
Jonathan Blandford committed
1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725
static void
_gtk_rbtree_test_dirty (GtkRBTree *tree,
			GtkRBNode *node,
			 gint      expected_dirtyness)
{

  if (expected_dirtyness)
    {
      g_assert (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_COLUMN_INVALID) ||
		GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_INVALID) ||
		(node->left != tree->nil && GTK_RBNODE_FLAG_SET (node->left, GTK_RBNODE_DESCENDANTS_INVALID)) ||
		(node->right != tree->nil && GTK_RBNODE_FLAG_SET (node->right, GTK_RBNODE_DESCENDANTS_INVALID)) ||
		(node->children && GTK_RBNODE_FLAG_SET (node->children->root, GTK_RBNODE_DESCENDANTS_INVALID)));
    }
  else
    {
      g_assert (! GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_COLUMN_INVALID) &&
		! GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_INVALID));
      if (node->left != tree->nil)
	g_assert (! GTK_RBNODE_FLAG_SET (node->left, GTK_RBNODE_DESCENDANTS_INVALID));
      if (node->right != tree->nil)
	g_assert (! GTK_RBNODE_FLAG_SET (node->right, GTK_RBNODE_DESCENDANTS_INVALID));
      if (node->children != NULL)
	g_assert (! GTK_RBNODE_FLAG_SET (node->children->root, GTK_RBNODE_DESCENDANTS_INVALID));
    }

  if (node->left != tree->nil)
    _gtk_rbtree_test_dirty (tree, node->left, GTK_RBNODE_FLAG_SET (node->left, GTK_RBNODE_DESCENDANTS_INVALID));
  if (node->right != tree->nil)
    _gtk_rbtree_test_dirty (tree, node->right, GTK_RBNODE_FLAG_SET (node->right, GTK_RBNODE_DESCENDANTS_INVALID));
1726
  if (node->children != NULL && node->children->root != node->children->nil)
Jonathan Blandford's avatar
Jonathan Blandford committed
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
    _gtk_rbtree_test_dirty (node->children, node->children->root, GTK_RBNODE_FLAG_SET (node->children->root, GTK_RBNODE_DESCENDANTS_INVALID));
}

static void _gtk_rbtree_test_structure (GtkRBTree *tree);

static void
_gtk_rbtree_test_structure_helper (GtkRBTree *tree,
				   GtkRBNode *node)
{
  g_assert (node != tree->nil);

  g_assert (node->left != NULL);
  g_assert (node->right != NULL);
  g_assert (node->parent != NULL);

  if (node->left != tree->nil)
    {
      g_assert (node->left->parent == node);
      _gtk_rbtree_test_structure_helper (tree, node->left);
    }
  if (node->right != tree->nil)
    {
      g_assert (node->right->parent == node);
      _gtk_rbtree_test_structure_helper (tree, node->right);
    }

  if (node->children != NULL)
    {
      g_assert (node->children->parent_tree == tree);
      g_assert (node->children->parent_node == node);

      _gtk_rbtree_test_structure (node->children);
    }
}
static void
_gtk_rbtree_test_structure (GtkRBTree *tree)
{
  g_assert (tree->root);
1765 1766
  if (tree->root == tree->nil)
    return;
Jonathan Blandford's avatar
Jonathan Blandford committed
1767

1768
  g_assert (tree->root->parent == tree->nil);
Jonathan Blandford's avatar
Jonathan Blandford committed
1769 1770 1771
  _gtk_rbtree_test_structure_helper (tree, tree->root);
}
			    
1772
void
Havoc Pennington's avatar
Havoc Pennington committed
1773 1774
_gtk_rbtree_test (const gchar *where,
                  GtkRBTree   *tree)
1775
{
Havoc Pennington's avatar
Havoc Pennington committed
1776 1777
  GtkRBTree *tmp_tree;

Jonathan Blandford's avatar
Jonathan Blandford committed
1778 1779 1780
  if (tree == NULL)
    return;

Havoc Pennington's avatar
Havoc Pennington committed
1781 1782 1783 1784 1785
  /* Test the entire tree */
  tmp_tree = tree;
  while (tmp_tree->parent_tree)
    tmp_tree = tmp_tree->parent_tree;
  
1786
  g_assert (tmp_tree->nil != NULL);
Havoc Pennington's avatar
Havoc Pennington committed
1787

1788 1789
  if (tmp_tree->root == tmp_tree->nil)
    return;
Jonathan Blandford's avatar
Jonathan Blandford committed
1790 1791 1792 1793 1794

  _gtk_rbtree_test_structure (tmp_tree);

  g_assert ((_count_nodes (tmp_tree, tmp_tree->root->left) +
	     _count_nodes (tmp_tree, tmp_tree->root->right) + 1) == tmp_tree->root->count);
Havoc Pennington's avatar
Havoc Pennington committed
1795 1796
      
      
Jonathan Blandford's avatar
Jonathan Blandford committed
1797 1798 1799
  _gtk_rbtree_test_height (tmp_tree, tmp_tree->root);
  _gtk_rbtree_test_dirty (tmp_tree, tmp_tree->root, GTK_RBNODE_FLAG_SET (tmp_tree->root, GTK_RBNODE_DESCENDANTS_INVALID));
  g_assert (count_parity (tmp_tree, tmp_tree->root) == tmp_tree->root->parity);
1800
}
Havoc Pennington's avatar
Havoc Pennington committed
1801

Jonathan Blandford's avatar
Jonathan Blandford committed
1802 1803 1804 1805 1806 1807 1808 1809 1810
static void
_gtk_rbtree_debug_spew_helper (GtkRBTree *tree,
			       GtkRBNode *node,
			       gint       depth)
{
  gint i;
  for (i = 0; i < depth; i++)
    g_print ("\t");

Owen Taylor's avatar
Owen Taylor committed
1811 1812
  g_print ("(%p - %s) (Offset %d) (Parity %d) (Validity %d%d%d)\n",
	   node,
Jonathan Blandford's avatar
Jonathan Blandford committed
1813
	   (GTK_RBNODE_GET_COLOR (node) == GTK_RBNODE_BLACK)?"BLACK":" RED ",
1814 1815
	   node->offset,
	   node->parity?1:0,
Jonathan Blandford's avatar
Jonathan Blandford committed
1816 1817 1818
	   (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_DESCENDANTS_INVALID))?1:0,
	   (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_INVALID))?1:0,
	   (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_COLUMN_INVALID))?1:0);
1819 1820 1821 1822 1823 1824
  if (node->children != NULL)
    {
      g_print ("Looking at child.\n");
      _gtk_rbtree_debug_spew (node->children);
      g_print ("Done looking at child.\n");
    }
Jonathan Blandford's avatar
Jonathan Blandford committed
1825
  if (node->left != tree->nil)
1826 1827 1828
    {
      _gtk_rbtree_debug_spew_helper (tree, node->left, depth+1);
    }
Jonathan Blandford's avatar
Jonathan Blandford committed
1829
  if (node->right != tree->nil)
1830 1831 1832
    {
      _gtk_rbtree_debug_spew_helper (tree, node->right, depth+1);
    }
Jonathan Blandford's avatar
Jonathan Blandford committed
1833 1834 1835 1836 1837 1838 1839
}

void
_gtk_rbtree_debug_spew (GtkRBTree *tree)
{
  g_return_if_fail (tree != NULL);

1840 1841 1842 1843
  if (tree->root == tree->nil)
    g_print ("Empty tree...\n");
  else
    _gtk_rbtree_debug_spew_helper (tree, tree->root, 0);