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: ht_iter.c,v 1.2 2003/06/12 01:10:04 klmitch Exp $ |
20 |
*/ |
21 |
#include "dbprim.h" |
22 |
#include "dbprim_int.h" |
23 |
|
24 |
RCSTAG("@(#)$Id: ht_iter.c,v 1.2 2003/06/12 01:10:04 klmitch Exp $"); |
25 |
|
26 |
/** \ingroup dbprim_hash |
27 |
* \brief Iterate over each entry in a hash table. |
28 |
* |
29 |
* This function iterates over every entry in a hash table (in an |
30 |
* unspecified order), executing the given \p iter_func on each entry. |
31 |
* |
32 |
* \param table A pointer to a #hash_table_t. |
33 |
* \param iter_func |
34 |
* A pointer to a callback function used to perform |
35 |
* user-specified actions on an entry in a hash table. \c |
36 |
* NULL is an invalid value. See the documentation for |
37 |
* #hash_iter_t for more information. |
38 |
* \param extra A \c void pointer that will be passed to \p |
39 |
* iter_func. |
40 |
* |
41 |
* \retval DB_ERR_BADARGS An argument was invalid. |
42 |
* \retval DB_ERR_FROZEN The hash table is frozen. |
43 |
*/ |
44 |
unsigned long |
45 |
ht_iter(hash_table_t *table, hash_iter_t iter_func, void *extra) |
46 |
{ |
47 |
unsigned long retval; |
48 |
int i; |
49 |
link_elem_t *elem; |
50 |
|
51 |
initialize_dbpr_error_table(); /* initialize error table */ |
52 |
|
53 |
if (!ht_verify(table) || !iter_func) /* verify arguments */ |
54 |
return DB_ERR_BADARGS; |
55 |
|
56 |
if (table->ht_flags & HASH_FLAG_FREEZE) /* don't mess with frozen tables */ |
57 |
return DB_ERR_FROZEN; |
58 |
|
59 |
table->ht_flags |= HASH_FLAG_FREEZE; /* freeze the table */ |
60 |
|
61 |
/* walk through all the elements and call the iteration function */ |
62 |
for (i = 0; i < table->ht_modulus; i++) |
63 |
for (elem = ll_first(&table->ht_table[i]); elem; elem = le_next(elem)) |
64 |
if ((retval = (*iter_func)(table, le_object(elem), extra))) { |
65 |
table->ht_flags &= ~HASH_FLAG_FREEZE; /* unfreeze the table */ |
66 |
return retval; |
67 |
} |
68 |
|
69 |
table->ht_flags &= ~HASH_FLAG_FREEZE; /* unfreeze the table */ |
70 |
|
71 |
return 0; |
72 |
} |