diff options
| author | Quentin Young <qlyoung@cumulusnetworks.com> | 2018-05-24 15:44:54 +0000 |
|---|---|---|
| committer | Quentin Young <qlyoung@cumulusnetworks.com> | 2018-05-25 16:16:22 +0000 |
| commit | 91f1037064d503f2e5f1756f66c10f3926960836 (patch) | |
| tree | 0f2a75ac569e7f9127c856b7e34287db65f1188e | |
| parent | 3a5c3bcb17451e3275a29d1f72d91274391a2cd5 (diff) | |
lib: add hash_to_list()
Convenience function to convert hash table to an unsorted linked list.
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
| -rw-r--r-- | lib/hash.c | 15 | ||||
| -rw-r--r-- | lib/hash.h | 9 |
2 files changed, 24 insertions, 0 deletions
diff --git a/lib/hash.c b/lib/hash.c index 4894b65aff..01f160f94a 100644 --- a/lib/hash.c +++ b/lib/hash.c @@ -312,6 +312,21 @@ void hash_clean(struct hash *hash, void (*free_func)(void *)) hash->stats.empty = hash->size; } +static void hash_to_list_iter(struct hash_backet *hb, void *arg) +{ + struct list *list = arg; + + listnode_add(list, hb->data); +} + +struct list *hash_to_list(struct hash *hash) +{ + struct list *list = list_new(); + + hash_iterate(hash, hash_to_list_iter, list); + return list; +} + /* Free hash memory. You may call hash_clean before call this function. */ void hash_free(struct hash *hash) diff --git a/lib/hash.h b/lib/hash.h index b6fe27e257..2510422e21 100644 --- a/lib/hash.h +++ b/lib/hash.h @@ -106,6 +106,15 @@ extern void hash_walk(struct hash *, int (*)(struct hash_backet *, void *), extern void hash_clean(struct hash *, void (*)(void *)); extern void hash_free(struct hash *); +/* + * Converts a hash table to an unsorted linked list. + * Does not modify the hash table in any way. + * + * hash + * the hash to convert + */ +extern struct list *hash_to_list(struct hash *hash); + extern unsigned int string_hash_make(const char *); extern void hash_cmd_init(void); |
