chiark / gitweb /
keymap: Tolerate invalid entries in keymaps
[elogind.git] / src / shared / hashmap.c
index ab00957306e9fb2fdb2d8e1b0e63263368973e07..26a4eff07fb3b7336d95e585e3e6bd338bdbd4e2 100644 (file)
@@ -265,6 +265,8 @@ static void remove_entry(Hashmap *h, struct hashmap_entry *e) {
 
 void hashmap_free(Hashmap*h) {
 
+        /* Free the hashmap, but nothing in it */
+
         if (!h)
                 return;
 
@@ -277,6 +279,13 @@ void hashmap_free(Hashmap*h) {
 }
 
 void hashmap_free_free(Hashmap *h) {
+
+        /* Free the hashmap and all data objects in it, but not the
+         * keys */
+
+        if (!h)
+                return;
+
         hashmap_clear_free(h);
         hashmap_free(h);
 }
@@ -292,7 +301,8 @@ void hashmap_clear(Hashmap *h) {
 void hashmap_clear_free(Hashmap *h) {
         void *p;
 
-        assert(h);
+        if (!h)
+                return;
 
         while ((p = hashmap_steal_first(h)))
                 free(p);
@@ -367,13 +377,27 @@ void* hashmap_get(Hashmap *h, const void *key) {
                 return NULL;
 
         hash = h->hash_func(key) % NBUCKETS;
-
-        if (!(e = hash_scan(h, hash, key)))
+        e = hash_scan(h, hash, key);
+        if (!e)
                 return NULL;
 
         return e->value;
 }
 
+bool hashmap_contains(Hashmap *h, const void *key) {
+        unsigned hash;
+
+        if (!h)
+                return false;
+
+        hash = h->hash_func(key) % NBUCKETS;
+
+        if (!hash_scan(h, hash, key))
+                return false;
+
+        return true;
+}
+
 void* hashmap_remove(Hashmap *h, const void *key) {
         struct hashmap_entry *e;
         unsigned hash;
@@ -734,3 +758,25 @@ char **hashmap_get_strv(Hashmap *h) {
 
         return sv;
 }
+
+void *hashmap_next(Hashmap *h, const void *key) {
+        unsigned hash;
+        struct hashmap_entry *e;
+
+        assert(h);
+        assert(key);
+
+        if (!h)
+                return NULL;
+
+        hash = h->hash_func(key) % NBUCKETS;
+        e = hash_scan(h, hash, key);
+        if (!e)
+                return NULL;
+
+        e = e->iterate_next;
+        if (!e)
+                return NULL;
+
+        return e->value;
+}