rb_search.c

Go to the documentation of this file.
00001 /*                     R B _ S E A R C H . 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_search.c
00025  *      Routines to search for a node in a red-black tree
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 
00036 #ifndef lint
00037 static const char libbu_rb_search_RCSid[] = "@(#) $Header: /cvsroot/brlcad/brlcad/src/libbu/rb_search.c,v 14.13 2006/08/31 23:16:39 lbutler Exp $";
00038 #endif
00039 
00040 #include "common.h"
00041 
00042 #include <stdlib.h>
00043 #include <stdio.h>
00044 #include <math.h>
00045 
00046 #include "machine.h"
00047 #include "rtlist.h"
00048 #include "bu.h"
00049 #include "compat4.h"
00050 #include "./rb_internals.h"
00051 
00052 
00053 /**                     _ R B _ S E A R C H ( )
00054  *
00055  *              Search for a node in a red-black tree
00056  *
00057  *      This function has four parameters: the root and order of the tree
00058  *      in which to search, the comparison function, and a data block
00059  *      containing the desired value of the key.  On success, _rb_search()
00060  *      returns a pointer to the discovered node.  Otherwise, it returns
00061  *      (tree -> rbt_empty_node).
00062  */
00063 static struct bu_rb_node *_rb_search (struct bu_rb_node *root, int order_nm, int (*order) (/* ??? */), void *data)
00064 {
00065     int         result;
00066     bu_rb_tree  *tree;
00067 
00068     BU_CKMAG(root, BU_RB_NODE_MAGIC, "red-black node");
00069     tree = root -> rbn_tree;
00070     BU_RB_CKORDER(tree, order_nm);
00071 
00072     while (1)
00073     {
00074         if (root == bu_rb_null(root -> rbn_tree))
00075             break;
00076         if ((result = (*order)(data, bu_rb_data(root, order_nm))) == 0)
00077             break;
00078         else if (result < 0)
00079             root = bu_rb_left_child(root, order_nm);
00080         else    /* result > 0 */
00081             root = bu_rb_right_child(root, order_nm);
00082         BU_CKMAG(root, BU_RB_NODE_MAGIC, "red-black node");
00083     }
00084     bu_rb_current(tree) = root;
00085     return (root);
00086 }
00087 
00088 /**                     B U _ R B _ S E A R C H ( )
00089  *
00090  *              Applications interface to _rb_search()
00091  *
00092  *      This function has three parameters: the tree in which to search,
00093  *      the order on which to do the searching, and a data block containing
00094  *      the desired value of the key.  On success, bu_rb_search() returns a
00095  *      pointer to the data block in the discovered node.  Otherwise,
00096  *      it returns NULL.
00097  */
00098 void *bu_rb_search (bu_rb_tree *tree, int order, void *data)
00099 {
00100 
00101     int                 (*compare)();
00102     struct bu_rb_node   *node;
00103 
00104     BU_CKMAG(tree, BU_RB_TREE_MAGIC, "red-black tree");
00105     BU_RB_CKORDER(tree, order);
00106 
00107     compare = bu_rb_order_func(tree, order);
00108     node = _rb_search(bu_rb_root(tree, order), order, compare, data);
00109     if (node == bu_rb_null(tree))
00110         return (NULL);
00111     else
00112         return (bu_rb_data(node, order));
00113 }
00114 /*@}*/
00115 /*
00116  * Local Variables:
00117  * mode: C
00118  * tab-width: 8
00119  * c-basic-offset: 4
00120  * indent-tabs-mode: t
00121  * End:
00122  * ex: shiftwidth=4 tabstop=8
00123  */

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