2 ** Copyright (C) 2002 by Kevin L. Mitchell <klmitch@mit.edu>
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.
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.
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,
22 #include "dbprim_int.h"
26 /** \ingroup dbprim_hash
27 * \brief Move an entry in the hash table.
29 * This function moves an existing entry in the hash table to
30 * correspond to the new key.
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
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
44 * \retval DB_ERR_READDFAILED Unable to re-add entry to table.
47 ht_move(hash_table_t *table, hash_entry_t *entry, db_key_t *key)
51 initialize_dbpr_error_table(); /* initialize error table */
53 if (!ht_verify(table) || !he_verify(entry) || !key) /* verify arguments */
54 return DB_ERR_BADARGS;
56 if (!entry->he_table) /* it's not in a table */
58 if (entry->he_table != table) /* it's in the wrong table */
59 return DB_ERR_WRONGTABLE;
61 if (table->ht_flags & HASH_FLAG_FREEZE) /* don't mess with frozen tables */
64 if (!ht_find(table, 0, key)) /* don't permit duplicates */
65 return DB_ERR_DUPLICATE;
67 /* remove the entry from the table */
68 if ((retval = ll_remove(&table->ht_table[entry->he_hash], &entry->he_elem)))
72 entry->he_key = *key; /* thank goodness for structure copy! */
74 /* get the new hash value for the entry */
76 (*table->ht_func)(table, &entry->he_key) % table->ht_modulus;
78 /* Now re-add it to the table */
79 if ((retval = ll_add(&table->ht_table[entry->he_hash], &entry->he_elem,
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;