rb_create.c

Go to the documentation of this file.
00001 /*                     R B _ C R E A T E . 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 
00025 /** @file rb_create.c
00026  *              Routines to create a red-black tree
00027  *
00028  *
00029  *  @author     Paul J. Tanenbaum
00030  *
00031  * @par Source -
00032  *      The U. S. Army Research Laboratory
00033  *@n    Aberdeen Proving Ground, Maryland  21005-5068  USA
00034  *
00035  */
00036 
00037 
00038 #ifndef lint
00039 static const char libbu_rb_create_RCSid[] = "@(#) $Header: /cvsroot/brlcad/brlcad/src/libbu/rb_create.c,v 14.13 2006/08/31 23:16:38 lbutler Exp $";
00040 #endif
00041 
00042 #include "common.h"
00043 
00044 
00045 
00046 #include <stdio.h>
00047 #include <math.h>
00048 #include "machine.h"
00049 #include "bu.h"
00050 #include "./rb_internals.h"
00051 
00052 /**                 B U _ R B _ C R E A T E ( )
00053  *
00054  *                  Create a red-black tree
00055  *
00056  *      This function has three parameters: a comment describing the
00057  *      tree to create, the number of linear orders to maintain
00058  *      simultaneously, and the comparison functions (one per order).
00059  *      bu_rb_create() returns a pointer to the red-black tree header
00060  *      record.
00061  */
00062 bu_rb_tree *bu_rb_create (char *description, int nm_orders, int (**order_funcs)() )
00063 {
00064     int         order;
00065     bu_rb_tree  *tree;
00066 
00067     /*
00068      *  Allocate memory for the tree
00069      */
00070     tree = (bu_rb_tree *) bu_malloc(sizeof(bu_rb_tree), "red-black tree");
00071     tree -> rbt_root = (struct bu_rb_node **)
00072                     bu_malloc(nm_orders * sizeof(struct bu_rb_node),
00073                         "red-black roots");
00074     tree -> rbt_unique = (char *)
00075                 bu_malloc((size_t) ceil((double) (nm_orders / 8.0)),
00076                             "red-black uniqueness flags");
00077     bu_rb_null(tree) = (struct bu_rb_node *)
00078                     bu_malloc(sizeof(struct bu_rb_node),
00079                                 "red-black empty node");
00080     bu_rb_null(tree) -> rbn_parent = (struct bu_rb_node **)
00081                 bu_malloc(nm_orders * sizeof(struct bu_rb_node *),
00082                             "red-black parents");
00083     bu_rb_null(tree) -> rbn_left = (struct bu_rb_node **)
00084                 bu_malloc(nm_orders * sizeof(struct bu_rb_node *),
00085                             "red-black left children");
00086     bu_rb_null(tree) -> rbn_right = (struct bu_rb_node **)
00087                 bu_malloc(nm_orders * sizeof(struct bu_rb_node *),
00088                             "red-black right children");
00089     bu_rb_null(tree) -> rbn_color = (char *)
00090                 bu_malloc((size_t) ceil((double) (nm_orders / 8.0)),
00091                             "red-black colors");
00092     bu_rb_null(tree) -> rbn_size = (int *)
00093                 bu_malloc(nm_orders * sizeof(int),
00094                             "red-black subtree sizes");
00095     bu_rb_null(tree) -> rbn_package = (struct bu_rb_package **)
00096                 bu_malloc(nm_orders * sizeof(struct bu_rb_package *),
00097                             "red-black packages");
00098     /*
00099      *  Fill in the tree
00100      */
00101     tree -> rbt_magic = BU_RB_TREE_MAGIC;
00102     tree -> rbt_description = description;
00103     tree -> rbt_nm_orders = nm_orders;
00104     tree -> rbt_order = order_funcs;
00105     tree -> rbt_print = 0;
00106     bu_rb_uniq_all_off(tree);
00107     tree -> rbt_debug = 0x0;
00108     tree -> rbt_current = bu_rb_null(tree);
00109     for (order = 0; order < nm_orders; ++order)
00110         bu_rb_root(tree, order) = bu_rb_null(tree);
00111     BU_LIST_INIT(&(tree -> rbt_nodes.l));
00112     BU_LIST_INIT(&(tree -> rbt_packages.l));
00113 
00114     /*
00115      *  Initialize the nil sentinel
00116      */
00117     bu_rb_null(tree) -> rbn_magic = BU_RB_NODE_MAGIC;
00118     bu_rb_null(tree) -> rbn_tree = tree;
00119     for (order = 0; order < nm_orders; ++order)
00120     {
00121         bu_rb_parent(bu_rb_null(tree), order) = BU_RB_NODE_NULL;
00122         bu_rb_set_color(bu_rb_null(tree), order, BU_RB_BLACK);
00123         bu_rb_left_child(bu_rb_null(tree), order) = BU_RB_NODE_NULL;
00124         bu_rb_right_child(bu_rb_null(tree), order) = BU_RB_NODE_NULL;
00125         bu_rb_size(bu_rb_null(tree), order) = 0;
00126         (bu_rb_null(tree) -> rbn_package)[order] = BU_RB_PKG_NULL;
00127     }
00128 
00129     return (tree);
00130 }
00131 
00132 /**                 B U _ R B _ C R E A T E 1 ( )
00133  *
00134  *              Create a single-order red-black tree
00135  *
00136  *      This function has two parameters: a comment describing the
00137  *      tree to create and a comparison function.  bu_rb_create1() builds
00138  *      an array of one function pointer and passes it to bu_rb_create().
00139  *      bu_rb_create1() returns a pointer to the red-black tree header
00140  *      record.
00141  *
00142  *      N.B. - Since this function allocates storage for the array of
00143  *      function pointers, in order to avoid memory leaks on freeing
00144  *      the tree, applications should call bu_rb_free1(), NOT bu_rb_free().
00145  */
00146 bu_rb_tree *bu_rb_create1 (char *description, int (*order_func) (/* ??? */))
00147 {
00148     int         (**ofp)();
00149 
00150     ofp = (int (**)())
00151                 bu_malloc(sizeof(int (*)()), "red-black function table");
00152     *ofp = order_func;
00153     return (bu_rb_create(description, 1, ofp));
00154 }
00155 
00156 /*@}*/
00157 /*
00158  * Local Variables:
00159  * mode: C
00160  * tab-width: 8
00161  * c-basic-offset: 4
00162  * indent-tabs-mode: t
00163  * End:
00164  * ex: shiftwidth=4 tabstop=8
00165  */

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