From 1c6d8802c23c64a9439be43fa2064aca8083bfb2 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Wed, 30 Jun 2010 13:22:28 -0700 Subject: [PATCH] shash: Refactor shash_add_nocopy(), shash_find(). By breaking out the hash calculation we can enable operations that need to do both to avoid duplicating the hash calculations. A following commit will add such an operation. --- lib/shash.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/lib/shash.c b/lib/shash.c index c4d3ccb8..e2b1fe2c 100644 --- a/lib/shash.c +++ b/lib/shash.c @@ -19,6 +19,9 @@ #include #include "hash.h" +static struct shash_node *shash_find__(const struct shash *, + const char *name, size_t hash); + static size_t hash_name(const char *name) { @@ -100,18 +103,24 @@ shash_count(const struct shash *shash) return hmap_count(&shash->map); } -/* It is the caller's responsibility to avoid duplicate names, if that is - * desirable. */ -struct shash_node * -shash_add_nocopy(struct shash *sh, char *name, const void *data) +static struct shash_node * +shash_add_nocopy__(struct shash *sh, char *name, const void *data, size_t hash) { struct shash_node *node = xmalloc(sizeof *node); node->name = name; node->data = (void *) data; - hmap_insert(&sh->map, &node->node, hash_name(name)); + hmap_insert(&sh->map, &node->node, hash); return node; } +/* It is the caller's responsibility to avoid duplicate names, if that is + * desirable. */ +struct shash_node * +shash_add_nocopy(struct shash *sh, char *name, const void *data) +{ + return shash_add_nocopy__(sh, name, data, hash_name(name)); +} + /* It is the caller's responsibility to avoid duplicate names, if that is * desirable. */ struct shash_node * @@ -146,14 +155,12 @@ shash_delete(struct shash *sh, struct shash_node *node) free(node); } -/* If there are duplicates, returns a random element. */ -struct shash_node * -shash_find(const struct shash *sh, const char *name) +static struct shash_node * +shash_find__(const struct shash *sh, const char *name, size_t hash) { struct shash_node *node; - HMAP_FOR_EACH_WITH_HASH (node, struct shash_node, node, - hash_name(name), &sh->map) { + HMAP_FOR_EACH_WITH_HASH (node, struct shash_node, node, hash, &sh->map) { if (!strcmp(node->name, name)) { return node; } @@ -161,6 +168,13 @@ shash_find(const struct shash *sh, const char *name) return NULL; } +/* If there are duplicates, returns a random element. */ +struct shash_node * +shash_find(const struct shash *sh, const char *name) +{ + return shash_find__(sh, name, hash_name(name)); +} + void * shash_find_data(const struct shash *sh, const char *name) { -- 2.30.2