ce41c64a313b8c02553b4f6bc39a3b10043db2c7
[ircu2.10.12-pk.git] / libs / dbprim / st_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 unsigned long
27 _st_remove(smat_table_t *table, smat_entry_t *entry, unsigned int remflag)
28 {
29   unsigned long retval;
30
31   if (remflag & ST_REM_HASH) { /* remove from hash table */
32     if ((retval = ht_remove(&table->st_table, &entry->se_hash)))
33       return retval;
34   }
35
36   if (remflag & ST_REM_FIRST) { /* remove from first linked list */
37     if ((retval = ll_remove(entry->se_link[SMAT_LOC_FIRST].le_head,
38                             &entry->se_link[SMAT_LOC_FIRST])))
39       return retval;
40   }
41
42   if (remflag & ST_REM_SECOND) { /* remove from second linked list */
43     if ((retval = ll_remove(entry->se_link[SMAT_LOC_SECOND].le_head,
44                             &entry->se_link[SMAT_LOC_SECOND])))
45       return retval;
46   }
47
48   if (remflag & ST_REM_FREE) /* free entry */
49     _smat_free(entry);
50
51   return 0;
52 }
53
54 /** \ingroup dbprim_smat
55  * \brief Remove an entry from a sparse matrix.
56  *
57  * This function removes the given entry from the specified sparse
58  * matrix.
59  *
60  * \param table A pointer to a #smat_table_t.
61  * \param entry A pointer to a #smat_entry_t to be removed from the
62  *              table.
63  *
64  * \retval DB_ERR_BADARGS       An invalid argument was given.
65  * \retval DB_ERR_WRONGTABLE    Entry is not in this sparse matrix.
66  * \retval DB_ERR_UNRECOVERABLE An unrecoverable error occurred while
67  *                              removing the entry from the table.
68  */
69 unsigned long
70 st_remove(smat_table_t *table, smat_entry_t *entry)
71 {
72   initialize_dbpr_error_table(); /* initialize error table */
73
74   /* verify arguments */
75   if (!st_verify(table) || !se_verify(entry))
76     return DB_ERR_BADARGS;
77
78   /* verify entry is in this table */
79   if (entry->se_table != table)
80     return DB_ERR_WRONGTABLE;
81
82   /* remove the entry from the linked lists and from the hash table */
83   if (_st_remove(table, entry, (ST_REM_HASH | ST_REM_FIRST | ST_REM_SECOND |
84                                 ST_REM_FREE)))
85     return DB_ERR_UNRECOVERABLE;
86
87   return 0;
88 }