X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=lib%2Fshash.c;h=a5bfecf04a0d53b4b8fdd28a6e718aef4495a040;hb=fb0d597fb64308c60001e3afc9b31eb295dedb6b;hp=c35164bd92e277b98e5599c46f41f92485f909f5;hpb=07423999f1759af064316aa4c1a1f499322c5ddc;p=openvswitch diff --git a/lib/shash.c b/lib/shash.c index c35164bd..a5bfecf0 100644 --- a/lib/shash.c +++ b/lib/shash.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009 Nicira Networks. + * Copyright (c) 2009, 2010 Nicira Networks. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -40,6 +40,18 @@ shash_destroy(struct shash *sh) } } +void +shash_swap(struct shash *a, struct shash *b) +{ + hmap_swap(&a->map, &b->map); +} + +void +shash_moved(struct shash *sh) +{ + hmap_moved(&sh->map); +} + void shash_clear(struct shash *sh) { @@ -67,15 +79,26 @@ shash_count(const struct shash *shash) /* It is the caller's responsibility to avoid duplicate names, if that is * desirable. */ struct shash_node * -shash_add(struct shash *sh, const char *name, void *data) +shash_add(struct shash *sh, const char *name, const void *data) { struct shash_node *node = xmalloc(sizeof *node); node->name = xstrdup(name); - node->data = data; + node->data = (void *) data; hmap_insert(&sh->map, &node->node, hash_name(name)); return node; } +bool +shash_add_once(struct shash *sh, const char *name, const void *data) +{ + if (!shash_find(sh, name)) { + shash_add(sh, name, data); + return true; + } else { + return false; + } +} + void shash_delete(struct shash *sh, struct shash_node *node) { @@ -157,3 +180,21 @@ shash_sort(const struct shash *sh) return nodes; } } + +/* Returns true if 'a' and 'b' contain the same keys (regardless of their + * values), false otherwise. */ +bool +shash_equal_keys(const struct shash *a, const struct shash *b) +{ + struct shash_node *node; + + if (hmap_count(&a->map) != hmap_count(&b->map)) { + return false; + } + SHASH_FOR_EACH (node, a) { + if (!shash_find(b, node->name)) { + return false; + } + } + return true; +}