rb_diag.c

Go to the documentation of this file.
00001 /*                       R B _ D I A G . C
00002  * BRL-CAD
00003  *
00004  * Copyright (c) 1998-2006 United States Government as represented by
00005  * the U.S. Army Research Laboratory.
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License
00009  * as published by the Free Software Foundation; either version 2 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this file; see the file named COPYING for more
00019  * information.
00020  */
00021 
00022 /** \addtogroup rb */
00023 /*@{*/
00024 /** @file rb_diag.c
00025  *      Diagnostic routines for red-black tree maintenance
00026  *
00027  *  @author
00028  *      Paul J. Tanenbaum
00029  *
00030  *  @par Source
00031  *      The U. S. Army Research Laboratory
00032  *@n    Aberdeen Proving Ground, Maryland  21005-5068  USA
00033  */
00034 
00035 #ifndef lint
00036 static const char libbu_rb_diag_RCSid[] = "@(#) $Header: /cvsroot/brlcad/brlcad/src/libbu/rb_diag.c,v 14.13 2006/08/31 23:16:38 lbutler Exp $";
00037 #endif
00038 
00039 #include "common.h"
00040 
00041 #include <stdlib.h>
00042 #include <stdio.h>
00043 #include <math.h>
00044 
00045 #include "machine.h"
00046 #include "rtlist.h"
00047 #include "bu.h"
00048 #include "compat4.h"
00049 #include "./rb_internals.h"
00050 
00051 
00052 static int d_order;     /* Used by describe_node() */
00053 
00054 /*@                 D E S C R I B E _ N O D E ( )
00055  *
00056  *              Print out the contents of a red-black node
00057  *
00058  *      This function has two parameters:  the node to describe and
00059  *      its depth in the tree.  Describe_node() is intended to be
00060  *      called by bu_rb_diagnose_tree().
00061  */
00062 static void describe_node (struct bu_rb_node *node, int depth)
00063 {
00064     bu_rb_tree                  *tree;
00065     struct bu_rb_package        *package;
00066     void                        (*pp)();        /* Pretty print function */
00067 
00068     BU_CKMAG(node, BU_RB_NODE_MAGIC, "red-black node");
00069     tree = node -> rbn_tree;
00070     BU_RB_CKORDER(tree, d_order);
00071 
00072     package = (node -> rbn_package)[d_order];
00073     pp = tree -> rbt_print;
00074 
00075     bu_log("%*snode <%x>...\n", depth * 2, "", node);
00076     bu_log("%*s  tree:   <%x>\n", depth * 2, "", node -> rbn_tree);
00077     bu_log("%*s  parent: <%x>\n", depth * 2, "",
00078         bu_rb_parent(node, d_order));
00079     bu_log("%*s  left:   <%x>\n", depth * 2, "",
00080         bu_rb_left_child(node, d_order));
00081     bu_log("%*s  right:  <%x>\n", depth * 2, "",
00082         bu_rb_right_child(node, d_order));
00083     bu_log("%*s  color:  %s\n", depth * 2, "",
00084             (bu_rb_get_color(node, d_order) == BU_RB_RED) ? "RED" :
00085             (bu_rb_get_color(node, d_order) == BU_RB_BLACK) ? "BLACK" :
00086                                                                 "Huhh?");
00087     bu_log("%*s  package: <%x> ", depth * 2, "", package);
00088 
00089     if ((pp != 0) && (package != BU_RB_PKG_NULL))
00090         (*pp)(package -> rbp_data);
00091     else
00092         bu_log("\n");
00093 }
00094 
00095 /**                 B U _ R B _ D I A G N O S E _ T R E E ( )
00096  *
00097  *          Produce a diagnostic printout of a red-black tree
00098  *
00099  *      This function has three parameters: the root and order of the tree
00100  *      to print out and the type of traversal (preorder, inorder, or
00101  *      postorder).
00102  */
00103 void bu_rb_diagnose_tree (bu_rb_tree *tree, int order, int trav_type)
00104 {
00105     BU_CKMAG(tree, BU_RB_TREE_MAGIC, "red-black tree");
00106     BU_RB_CKORDER(tree, order);
00107 
00108     bu_log("-------- Red-black tree <%x> contents --------\n", tree);
00109     bu_log("Description: '%s'\n", tree -> rbt_description);
00110     bu_log("Order:       %d of %d\n", order, tree -> rbt_nm_orders);
00111     bu_log("Current:     <%x>\n", tree -> rbt_current);
00112     bu_log("Empty node:  <%x>\n", tree -> rbt_empty_node);
00113     bu_log("Uniqueness:  %d\n", bu_rb_get_uniqueness(tree, order));
00114     d_order = order;
00115     _rb_walk(tree, order, describe_node, WALK_NODES, trav_type);
00116     bu_log("--------------------------------------------------\n");
00117 }
00118 
00119 /**             B U _ R B _ S U M M A R I Z E _ T R E E ( )
00120  *
00121  *                  Describe a red-black tree
00122  *
00123  *      This function has one parameter: a pointer to a red-black
00124  *      tree.  bu_rb_summarize_tree() prints out the header information
00125  *      for the tree.  It is intended for diagnostic purposes.
00126  */
00127 void bu_rb_summarize_tree (bu_rb_tree *tree)
00128 {
00129     int         i;
00130 
00131     BU_CKMAG(tree, BU_RB_TREE_MAGIC, "red-black tree");
00132 
00133     bu_log("-------- Red-black tree <%x> summary --------\n", tree);
00134     bu_log("Description:      '%s'\n", tree -> rbt_description);
00135     bu_log("Current:          <%x>\n", tree -> rbt_current);
00136     bu_log("Empty node:       <%x>\n", tree -> rbt_empty_node);
00137     bu_log("Size (in nodes):  %d\n", tree -> rbt_nm_nodes);
00138     bu_log("Number of orders: %d\n", tree -> rbt_nm_orders);
00139     bu_log("Debug bits:       <%x>\n", tree -> rbt_debug);
00140     if ((tree -> rbt_nm_orders > 0) && (tree -> rbt_nm_nodes > 0))
00141     {
00142         bu_log("i    Order[i]   Uniq[i]  Root[i]      Package[i]     Data[i]\n");
00143         for (i = 0; i < tree -> rbt_nm_orders; ++i)
00144         {
00145             bu_log("%-3d  <%x>    %c      <%x>    <%x>    <%x>\n",
00146                     i,
00147                     bu_rb_order_func(tree, i),
00148                     bu_rb_get_uniqueness(tree, i) ? 'Y' : 'N',
00149                     bu_rb_root(tree, i),
00150                     (bu_rb_root(tree, i) == BU_RB_NODE_NULL) ? 0 :
00151                         (bu_rb_root(tree, i) -> rbn_package)[i],
00152                     (bu_rb_root(tree, i) == BU_RB_NODE_NULL) ? 0 :
00153                         bu_rb_data(bu_rb_root(tree, i), i));
00154         }
00155     }
00156     bu_log("-------------------------------------------------\n");
00157 }
00158 /*@}*/
00159 /*
00160  * Local Variables:
00161  * mode: C
00162  * tab-width: 8
00163  * c-basic-offset: 4
00164  * indent-tabs-mode: t
00165  * End:
00166  * ex: shiftwidth=4 tabstop=8
00167  */

Generated on Mon Sep 18 01:24:48 2006 for BRL-CAD by  doxygen 1.4.6