ca50e5707b7d2c7593560d37159d24171069283c
[ircu2.10.12-pk.git] / libs / dbprim / ht_remove.c
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$
20 */
21 #include "dbprim.h"
22 #include "dbprim_int.h"
23
24 RCSTAG("@(#)$Id$");
25
26 /** \ingroup dbprim_hash
27  * \brief Remove an element from a hash table.
28  *
29  * This function removes the given element from the specified hash
30  * table.
31  *
32  * \param table A pointer to a #hash_table_t.
33  * \param entry A pointer to a #hash_entry_t to be removed from the
34  *              table.
35  *
36  * \retval DB_ERR_BADARGS       An invalid argument was given.
37  * \retval DB_ERR_UNUSED        Entry is not in a hash table.
38  * \retval DB_ERR_WRONGTABLE    Entry is not in this hash table.
39  * \retval DB_ERR_FROZEN        Hash table is frozen.
40  * \retval DB_ERR_UNRECOVERABLE An unrecoverable error occurred while
41  *                              resizing the table.
42  */
43 unsigned long
44 ht_remove(hash_table_t *table, hash_entry_t *entry)
45 {
46   unsigned long retval = 0;
47
48   initialize_dbpr_error_table(); /* initialize error table */
49
50   if (!ht_verify(table) || !he_verify(entry)) /* verify arguments */
51     return DB_ERR_BADARGS;
52
53   if (!entry->he_table) /* it's not in a table */
54     return DB_ERR_UNUSED;
55   if (entry->he_table != table) /* it's in the wrong table */
56     return DB_ERR_WRONGTABLE;
57
58   if (table->ht_flags & HASH_FLAG_FREEZE) /* don't remove from frozen tables */
59     return DB_ERR_FROZEN;
60
61   /* remove the entry from the table */
62   if ((retval = ll_remove(&table->ht_table[entry->he_hash], &entry->he_elem)))
63     return retval;
64
65   entry->he_table = 0; /* reset the table */
66
67   /* decrement element count and shrink the table if necessary and allowed */
68   if (--table->ht_count < table->ht_rollunder &&
69       (table->ht_flags & HASH_FLAG_AUTOSHRINK))
70     retval = ht_resize(table, 0);
71
72   return retval;
73 }