00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
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
00054
00055
00056
00057
00058
00059
00060
00061
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
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
00089
00090
00091
00092
00093
00094
00095
00096
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
00117
00118
00119
00120
00121
00122
00123