05732c645abc1e81f06de03a191251b3831b3982
[ircu2.10.12-pk.git] / libs / dbprim / st_iter.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 _st_iter_s {
27   smat_table_t *si_table;       /* pointer to the smat table */
28   smat_iter_t   si_iter;        /* iter function */
29   void         *si_extra;       /* extra data */
30 };
31
32 static unsigned long
33 _st_iter_iter(hash_table_t *table, hash_entry_t *ent, void *extra)
34 {
35   struct _st_iter_s *si;
36
37   si = extra;
38
39   /* call the user iteration function--with appropriate translation */
40   return (*si->si_iter)(si->si_table, he_value(ent), si->si_extra);
41 }
42
43 /** \ingroup dbprim_smat
44  * \brief Iterate over each entry in a sparse matrix.
45  *
46  * This function iterates over every entry in a sparse matrix (in an
47  * unspecified order), executing the given \p iter_func on each entry.
48  *
49  * \param table A pointer to a #smat_table_t.
50  * \param iter_func
51  *              A pointer to a callback function used to perform
52  *              user-specified actions on an entry in a sparse
53  *              matrix.  \c NULL is an invalid value.  See the
54  *              documentation for #smat_iter_t for more information.
55  * \param extra A \c void pointer that will be passed to \p
56  *              iter_func.
57  *
58  * \retval DB_ERR_BADARGS       An argument was invalid.
59  * \retval DB_ERR_FROZEN        The sparse matrix is frozen.
60  */
61 unsigned long
62 st_iter(smat_table_t *table, smat_iter_t iter_func, void *extra)
63 {
64   struct _st_iter_s si;
65
66   initialize_dbpr_error_table(); /* initialize error table */
67
68   if (!st_verify(table) || !iter_func) /* verify arguments */
69     return DB_ERR_BADARGS;
70
71   /* initialize extra data... */
72   si.si_table = table;
73   si.si_iter = iter_func;
74   si.si_extra = extra;
75
76   /* call into linked list library to iterate over the list */
77   return ht_iter(&table->st_table, _st_iter_iter, &si);
78 }