1 |
/* |
2 |
** Copyright (C) 2002 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: ht_move.c,v 1.2 2003/06/12 01:10:04 klmitch Exp $ |
20 |
*/ |
21 |
#include "dbprim.h" |
22 |
#include "dbprim_int.h" |
23 |
|
24 |
RCSTAG("@(#)$Id: ht_move.c,v 1.2 2003/06/12 01:10:04 klmitch Exp $"); |
25 |
|
26 |
/** \ingroup dbprim_hash |
27 |
* \brief Move an entry in the hash table. |
28 |
* |
29 |
* This function moves an existing entry in the hash table to |
30 |
* correspond to the new key. |
31 |
* |
32 |
* \param table A pointer to a #hash_table_t. |
33 |
* \param entry A pointer to a #hash_entry_t to be moved. It must |
34 |
* already be in the hash table. |
35 |
* \param key A pointer to a #db_key_t describing the new key for |
36 |
* the entry. |
37 |
* |
38 |
* \retval DB_ERR_BADARGS An invalid argument was given. |
39 |
* \retval DB_ERR_UNUSED Entry is not in a hash table. |
40 |
* \retval DB_ERR_WRONGTABLE Entry is not in this hash table. |
41 |
* \retval DB_ERR_FROZEN Hash table 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 entry to table. |
45 |
*/ |
46 |
unsigned long |
47 |
ht_move(hash_table_t *table, hash_entry_t *entry, db_key_t *key) |
48 |
{ |
49 |
unsigned long retval; |
50 |
|
51 |
initialize_dbpr_error_table(); /* initialize error table */ |
52 |
|
53 |
if (!ht_verify(table) || !he_verify(entry) || !key) /* verify arguments */ |
54 |
return DB_ERR_BADARGS; |
55 |
|
56 |
if (!entry->he_table) /* it's not in a table */ |
57 |
return DB_ERR_UNUSED; |
58 |
if (entry->he_table != table) /* it's in the wrong table */ |
59 |
return DB_ERR_WRONGTABLE; |
60 |
|
61 |
if (table->ht_flags & HASH_FLAG_FREEZE) /* don't mess with frozen tables */ |
62 |
return DB_ERR_FROZEN; |
63 |
|
64 |
if (!ht_find(table, 0, key)) /* don't permit duplicates */ |
65 |
return DB_ERR_DUPLICATE; |
66 |
|
67 |
/* remove the entry from the table */ |
68 |
if ((retval = ll_remove(&table->ht_table[entry->he_hash], &entry->he_elem))) |
69 |
return retval; |
70 |
|
71 |
/* rekey the entry */ |
72 |
entry->he_key = *key; /* thank goodness for structure copy! */ |
73 |
|
74 |
/* get the new hash value for the entry */ |
75 |
entry->he_hash = |
76 |
(*table->ht_func)(table, &entry->he_key) % table->ht_modulus; |
77 |
|
78 |
/* Now re-add it to the table */ |
79 |
if ((retval = ll_add(&table->ht_table[entry->he_hash], &entry->he_elem, |
80 |
LINK_LOC_HEAD, 0))) { |
81 |
table->ht_count--; /* decrement the count--don't worry about shrinking */ |
82 |
entry->he_table = 0; /* zero the table pointer */ |
83 |
return DB_ERR_READDFAILED; |
84 |
} |
85 |
|
86 |
return 0; |
87 |
} |