1 |
/* |
2 |
** Copyright (C) 2003 by Kevin L. Mitchell <klmitch@mit.edu> |
3 |
** |
4 |
** This library is free software; you can redistribute it and/or |
5 |
** modify it under the terms of the GNU Library General Public |
6 |
** License as published by the Free Software Foundation; either |
7 |
** version 2 of the License, or (at your option) any later version. |
8 |
** |
9 |
** This library is distributed in the hope that it will be useful, |
10 |
** but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 |
** Library General Public License for more details. |
13 |
** |
14 |
** You should have received a copy of the GNU Library General Public |
15 |
** License along with this library; if not, write to the Free |
16 |
** Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
17 |
** MA 02111-1307, USA |
18 |
** |
19 |
** @(#)$Id: rt_move.c,v 1.1 2003/06/28 18:48:21 klmitch Exp $ |
20 |
*/ |
21 |
#include "dbprim.h" |
22 |
#include "dbprim_int.h" |
23 |
|
24 |
RCSTAG("@(#)$Id: rt_move.c,v 1.1 2003/06/28 18:48:21 klmitch Exp $"); |
25 |
|
26 |
/** \ingroup dbprim_rbtree |
27 |
* \brief Move a node in a red-black tree. |
28 |
* |
29 |
* This function moves an existing node in the red-black tree to |
30 |
* correspond to the new key. |
31 |
* |
32 |
* \param tree A pointer to a #rb_tree_t. |
33 |
* \param node A pointer to a #rb_node_t to be moved. It must |
34 |
* already be in the red-black tree. |
35 |
* \param key A pointer to a #db_key_t describing the new key for |
36 |
* the node. |
37 |
* |
38 |
* \retval DB_ERR_BADARGS An invalid argument was given. |
39 |
* \retval DB_ERR_UNUSED Node is not in a red-black tree. |
40 |
* \retval DB_ERR_WRONGTABLE Node is not in this tree. |
41 |
* \retval DB_ERR_FROZEN Red-black tree is frozen. |
42 |
* \retval DB_ERR_DUPLICATE New key is a duplicate of an existing |
43 |
* key. |
44 |
* \retval DB_ERR_READDFAILED Unable to re-add node to tree. |
45 |
*/ |
46 |
unsigned long |
47 |
rt_move(rb_tree_t *tree, rb_node_t *node, db_key_t *key) |
48 |
{ |
49 |
unsigned long retval; |
50 |
|
51 |
initialize_dbpr_error_table(); /* initialize error table */ |
52 |
|
53 |
if (!rt_verify(tree) || !rn_verify(node) || !key) /* verify arguments */ |
54 |
return DB_ERR_BADARGS; |
55 |
|
56 |
if (!node->rn_tree) /* it's not in a tree */ |
57 |
return DB_ERR_UNUSED; |
58 |
if (node->rn_tree != tree) /* it's in the wrong tree */ |
59 |
return DB_ERR_WRONGTABLE; |
60 |
|
61 |
if (tree->rt_flags & RBT_FLAG_FREEZE) /* don't mess with frozen trees */ |
62 |
return DB_ERR_FROZEN; |
63 |
|
64 |
if (_rb_locate(tree, 0, key)) /* don't permit duplicates */ |
65 |
return DB_ERR_DUPLICATE; |
66 |
|
67 |
/* OK, remove node from the tree... */ |
68 |
if ((retval = rt_remove(tree, node))) |
69 |
return retval; |
70 |
|
71 |
/* Now re-add it to the tree... */ |
72 |
return rt_add(tree, node, key) ? DB_ERR_READDFAILED : 0; |
73 |
} |