bf07c9cd3ac3ae04d9ef557dfd191143d39dd152
[ircu2.10.12-pk.git] / libs / dbprim / sh_flush.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 struct _sh_flush_s {
27   smat_table_t *sf_table;       /* pointer to the smat table */
28   smat_loc_t    sf_elem;        /* which list we're traversing */
29   smat_iter_t   sf_flush;       /* flush function */
30   void         *sf_extra;       /* extra data */
31 };
32
33 static unsigned long
34 _sh_flush_iter(link_head_t *head, link_elem_t *elem, void *extra)
35 {
36   unsigned long retval = 0;
37   struct _sh_flush_s *sf;
38
39   sf = extra;
40
41   /* Remove the object from all lists first */
42   if (_st_remove(sf->sf_table, le_object(elem), ST_REM_HASH |
43                  (sf->sf_elem == SMAT_LOC_FIRST ? ST_REM_SECOND :
44                   ST_REM_FIRST)))
45     return DB_ERR_UNRECOVERABLE;
46
47   /* call the user flush function if so desired */
48   if (sf->sf_flush)
49     retval = (*sf->sf_flush)(sf->sf_table, le_object(elem), sf->sf_extra);
50
51   _smat_free(le_object(elem)); /* destroy the entry */
52
53   return retval;
54 }
55
56 /** ingroup dbprim_smat
57  * \brief Flush a row or column of a sparse matrix.
58  *
59  * This function flushes a sparse matrix row or column--that is, it
60  * removes each element from that row or column.  If a \p flush_func
61  * is specified, it will be called on the entry after it has been
62  * removed from the row or column, and may safely call
63  * <CODE>free()</CODE>.
64  *
65  * \param list  A pointer to a #smat_head_t.
66  * \param flush_func
67  *              A pointer to a callback function used to perform
68  *              user-specifed actions on an entry after removing it
69  *              from the row or column.  May be \c NULL.  See the
70  *              documentation for #smat_iter_t for more information.
71  * \param extra A \c void pointer that will be passed to \p
72  *              flush_func.
73  *
74  * \retval DB_ERR_BADARGS       An argument was invalid.
75  */
76 unsigned long
77 sh_flush(smat_head_t *head, smat_iter_t flush_func, void *extra)
78 {
79   struct _sh_flush_s sf;
80
81   initialize_dbpr_error_table(); /* initialize error table */
82
83   if (!sh_verify(head)) /* verify arguments */
84     return DB_ERR_BADARGS;
85
86   /* initialize extra data... */
87   sf.sf_table = head->sh_table;
88   sf.sf_elem = head->sh_elem;
89   sf.sf_flush = flush_func;
90   sf.sf_extra = extra;
91
92   /* call into linked list library to flush the list */
93   return ll_flush(&head->sh_head, _sh_flush_iter, &sf);
94 }