rb_rotate.c

Go to the documentation of this file.
00001 /*                     R B _ R O T 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 /** @file rb_rotate.c
00025  *          Routines to perform rotations on 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_rotate_RCSid[] = "@(#) $Header: /cvsroot/brlcad/brlcad/src/libbu/rb_rotate.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 _ R O T _ L E F T ( )
00054  *
00055  *              Perfrom left rotation on a red-black tree
00056  *
00057  *      This function has two parameters: the node about which to rotate
00058  *      and the order to be rotated.  _rb_rot_left() is an implementation
00059  *      of the routine called LEFT-ROTATE on p. 266 of Cormen et al,
00060  *      with modification on p. 285.
00061  */
00062 void _rb_rot_left (struct bu_rb_node *x, int order)
00063 {
00064     struct bu_rb_node   *y;             /* x's child to pivot up */
00065     struct bu_rb_node   *beta;          /* y's child in direction of rot. */
00066     struct bu_rb_node   *x_parent;      /* x's parent */
00067     bu_rb_tree          *tree = x -> rbn_tree;  /* Tree where it all happens */
00068 
00069     /*
00070      *  Set y and check data types of both x and y
00071      */
00072     BU_CKMAG(x, BU_RB_NODE_MAGIC, "red-black node");
00073     BU_RB_CKORDER(x -> rbn_tree, order);
00074 
00075     y = bu_rb_right_child(x, order);
00076 
00077     if (tree -> rbt_debug & BU_RB_DEBUG_ROTATE)
00078         bu_log("_rb_rot_left(<%x>, %d)...\n", x, order);
00079 
00080     bu_rb_right_child(x, order) = beta = bu_rb_left_child(y, order);
00081     if (beta != bu_rb_null(tree))
00082         bu_rb_parent(beta, order) = x;
00083     bu_rb_parent(y, order) = x_parent = bu_rb_parent(x, order);
00084     if (x_parent == bu_rb_null(tree))
00085         bu_rb_root(tree, order) = y;
00086     else if (x == bu_rb_left_child(x_parent, order))
00087         bu_rb_left_child(x_parent, order) = y;
00088     else
00089         bu_rb_right_child(x_parent, order) = y;
00090     bu_rb_left_child(y, order) = x;
00091     bu_rb_parent(x, order) = y;
00092 
00093     bu_rb_size(y, order) = bu_rb_size(x, order);
00094     bu_rb_size(x, order) =
00095         bu_rb_size(bu_rb_left_child(x, order), order) +
00096         bu_rb_size(bu_rb_right_child(x, order), order) + 1;
00097     if (tree -> rbt_debug & BU_RB_DEBUG_OS)
00098         bu_log("After rotation, size(%x, %d)=%d, size(%x, %d)=%d\n",
00099             x, order, bu_rb_size(x, order), y, order, bu_rb_size(y, order));
00100 }
00101 
00102 /**                 _ R B _ R O T _ R I G H T ( )
00103  *
00104  *              Perfrom right rotation on a red-black tree
00105  *
00106  *      This function has two parameters: the node about which to rotate
00107  *      and the order to be rotated.  _rb_rot_right() is hacked from
00108  *      _rb_rot_left() above.
00109  */
00110 void _rb_rot_right (struct bu_rb_node *y, int order)
00111 {
00112     struct bu_rb_node   *x;             /* y's child to pivot up */
00113     struct bu_rb_node   *beta;          /* x's child in direction of rot. */
00114     struct bu_rb_node   *y_parent;      /* y's parent */
00115     bu_rb_tree          *tree = y -> rbn_tree;  /* Tree where it all happens */
00116 
00117     /*
00118      *  Set x and check data types of both x and y
00119      */
00120     BU_CKMAG(y, BU_RB_NODE_MAGIC, "red-black node");
00121     BU_RB_CKORDER(y -> rbn_tree, order);
00122 
00123     x = bu_rb_left_child(y, order);
00124 
00125     if (tree -> rbt_debug & BU_RB_DEBUG_ROTATE)
00126         bu_log("_rb_rot_right(<%x>, %d)...\n", y, order);
00127 
00128     bu_rb_left_child(y, order) = beta = bu_rb_right_child(x, order);
00129     if (beta != bu_rb_null(tree))
00130         bu_rb_parent(beta, order) = y;
00131     bu_rb_parent(x, order) = y_parent = bu_rb_parent(y, order);
00132     if (y_parent == bu_rb_null(tree))
00133         bu_rb_root(tree, order) = x;
00134     else if (y == bu_rb_left_child(y_parent, order))
00135         bu_rb_left_child(y_parent, order) = x;
00136     else
00137         bu_rb_right_child(y_parent, order) = x;
00138     bu_rb_right_child(x, order) = y;
00139     bu_rb_parent(y, order) = x;
00140 
00141     bu_rb_size(x, order) = bu_rb_size(y, order);
00142     bu_rb_size(y, order) =
00143         bu_rb_size(bu_rb_left_child(y, order), order) +
00144         bu_rb_size(bu_rb_right_child(y, order), order) + 1;
00145     if (tree -> rbt_debug & BU_RB_DEBUG_OS)
00146         bu_log("After rotation, size(%x, %d)=%d, size(%x, %d)=%d\n",
00147             x, order, bu_rb_size(x, order), y, order, bu_rb_size(y, order));
00148 }
00149 /*@}*/
00150 /*
00151  * Local Variables:
00152  * mode: C
00153  * tab-width: 8
00154  * c-basic-offset: 4
00155  * indent-tabs-mode: t
00156  * End:
00157  * ex: shiftwidth=4 tabstop=8
00158  */

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