[dovecot-cvs] dovecot/src/lib hash.c,1.18,1.19 hash.h,1.10,1.11
cras at procontrol.fi
cras at procontrol.fi
Wed Dec 3 02:40:23 EET 2003
Update of /home/cvs/dovecot/src/lib
In directory danu:/tmp/cvs-serv14047/lib
Modified Files:
hash.c hash.h
Log Message:
Changed hash_foreach() to iterator.
Index: hash.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/hash.c,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -d -r1.18 -r1.19
--- hash.c 26 Aug 2003 21:18:16 -0000 1.18
+++ hash.c 3 Dec 2003 00:40:21 -0000 1.19
@@ -32,8 +32,6 @@
static int hash_resize(struct hash_table *table, int grow);
-static int foreach_stop;
-
static int direct_cmp(const void *p1, const void *p2)
{
return p1 == p2 ? 0 : 1;
@@ -321,37 +319,65 @@
return table->nodes_count;
}
-void hash_foreach(struct hash_table *table, hash_foreach_callback_t *callback,
- void *context)
+struct hash_iterate_context {
+ struct hash_table *table;
+ struct hash_node *next;
+ size_t pos;
+};
+
+struct hash_iterate_context *hash_iterate_init(struct hash_table *table)
{
- struct hash_node *node;
- size_t i;
+ struct hash_iterate_context *ctx;
hash_freeze(table);
- foreach_stop = FALSE;
-
- for (i = 0; i < table->size; i++) {
- node = &table->nodes[i];
+ ctx = i_new(struct hash_iterate_context, 1);
+ ctx->table = table;
+ ctx->next = &table->nodes[0];
+ return ctx;
+}
- do {
- if (node->key != NULL) {
- callback(node->key, node->value, context);
- if (foreach_stop) {
- table->frozen--;
- return;
- }
+static struct hash_node *hash_iterate_next(struct hash_iterate_context *ctx,
+ struct hash_node *node)
+{
+ do {
+ if (node == NULL) {
+ if (++ctx->pos == ctx->table->size) {
+ ctx->pos--;
+ return NULL;
}
+ node = &ctx->table->nodes[ctx->pos];
+ } else {
node = node->next;
- } while (node != NULL);
+ }
+ } while (node->key == NULL);
+
+ return node;
+}
+
+int hash_iterate(struct hash_iterate_context *ctx,
+ void **key_r, void **value_r)
+{
+ struct hash_node *node;
+
+ node = ctx->next;
+ if (node != NULL && node->key == NULL)
+ node = hash_iterate_next(ctx, node);
+ if (node == NULL) {
+ *key_r = *value_r = NULL;
+ return FALSE;
}
+ *key_r = node->key;
+ *value_r = node->value;
- hash_thaw(table);
+ ctx->next = hash_iterate_next(ctx, node);
+ return TRUE;
}
-void hash_foreach_stop(void)
+void hash_iterate_deinit(struct hash_iterate_context *ctx)
{
- foreach_stop = TRUE;
+ hash_thaw(ctx->table);
+ i_free(ctx);
}
void hash_freeze(struct hash_table *table)
Index: hash.h
===================================================================
RCS file: /home/cvs/dovecot/src/lib/hash.h,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- hash.h 27 Jan 2003 01:59:59 -0000 1.10
+++ hash.h 3 Dec 2003 00:40:21 -0000 1.11
@@ -5,7 +5,6 @@
typedef unsigned int hash_callback_t(const void *p);
/* Returns 0 if the pointers are equal. */
typedef int hash_cmp_callback_t(const void *p1, const void *p2);
-typedef void hash_foreach_callback_t(void *key, void *value, void *context);
/* Create a new hash table. If initial_size is 0, the default value is used.
If hash_cb or key_compare_cb is NULL, direct hashing/comparing is used.
@@ -35,13 +34,13 @@
void hash_remove(struct hash_table *table, const void *key);
size_t hash_size(struct hash_table *table);
-/* Calls the given function for each node in hash table. You may safely
- call hash_*() functions inside your function, but if you add any
- new nodes, they may or may not be called for in this foreach loop. */
-void hash_foreach(struct hash_table *table,
- hash_foreach_callback_t *callback, void *context);
-/* Stop the active hash_foreach() loop */
-void hash_foreach_stop(void);
+/* Iterates through all nodes in hash table. You may safely call hash_*()
+ functions while iterating, but if you add any new nodes, they may or may
+ not be called for in this iteration. */
+struct hash_iterate_context *hash_iterate_init(struct hash_table *table);
+int hash_iterate(struct hash_iterate_context *ctx,
+ void **key_r, void **value_r);
+void hash_iterate_deinit(struct hash_iterate_context *ctx);
/* Hash table isn't resized, and removed nodes aren't removed from
the list while hash table is freezed. Supports nesting. */
More information about the dovecot-cvs
mailing list