chiark / gitweb /
hashmap: drop assert(h) from hashmap_next()
[elogind.git] / src / shared / hashmap.c
index 715484ce7cf18912adffdd937f21b6b9b2aa1693..8225b8ebccd5e742eea1f90b42091c1c48238254 100644 (file)
@@ -488,19 +488,10 @@ static bool resize_buckets(Hashmap *h) {
         return true;
 }
 
-int hashmap_put(Hashmap *h, const void *key, void *value) {
-        struct hashmap_entry *e;
-        unsigned hash;
+static int __hashmap_put(Hashmap *h, const void *key, void *value, unsigned hash) {
+        /* For when we know no such entry exists yet */
 
-        assert(h);
-
-        hash = bucket_hash(h, key);
-        e = hash_scan(h, hash, key);
-        if (e) {
-                if (e->value == value)
-                        return 0;
-                return -EEXIST;
-        }
+        struct hashmap_entry *e;
 
         if (resize_buckets(h))
                 hash = bucket_hash(h, key);
@@ -521,6 +512,23 @@ int hashmap_put(Hashmap *h, const void *key, void *value) {
         return 1;
 }
 
+int hashmap_put(Hashmap *h, const void *key, void *value) {
+        struct hashmap_entry *e;
+        unsigned hash;
+
+        assert(h);
+
+        hash = bucket_hash(h, key);
+        e = hash_scan(h, hash, key);
+        if (e) {
+                if (e->value == value)
+                        return 0;
+                return -EEXIST;
+        }
+
+        return __hashmap_put(h, key, value, hash);
+}
+
 int hashmap_replace(Hashmap *h, const void *key, void *value) {
         struct hashmap_entry *e;
         unsigned hash;
@@ -535,7 +543,7 @@ int hashmap_replace(Hashmap *h, const void *key, void *value) {
                 return 0;
         }
 
-        return hashmap_put(h, key, value);
+        return __hashmap_put(h, key, value, hash);
 }
 
 int hashmap_update(Hashmap *h, const void *key, void *value) {
@@ -753,59 +761,6 @@ at_end:
         return NULL;
 }
 
-void *hashmap_iterate_backwards(Hashmap *h, Iterator *i, const void **key) {
-        struct hashmap_entry *e;
-
-        assert(i);
-
-        if (!h)
-                goto at_beginning;
-
-        if (*i == ITERATOR_FIRST)
-                goto at_beginning;
-
-        if (*i == ITERATOR_LAST && !h->iterate_list_tail)
-                goto at_beginning;
-
-        e = *i == ITERATOR_LAST ? h->iterate_list_tail : (struct hashmap_entry*) *i;
-
-        if (e->iterate_previous)
-                *i = (Iterator) e->iterate_previous;
-        else
-                *i = ITERATOR_FIRST;
-
-        if (key)
-                *key = e->key;
-
-        return e->value;
-
-at_beginning:
-        *i = ITERATOR_FIRST;
-
-        if (key)
-                *key = NULL;
-
-        return NULL;
-}
-
-void *hashmap_iterate_skip(Hashmap *h, const void *key, Iterator *i) {
-        unsigned hash;
-        struct hashmap_entry *e;
-
-        if (!h)
-                return NULL;
-
-        hash = bucket_hash(h, key);
-
-        e = hash_scan(h, hash, key);
-        if (!e)
-                return NULL;
-
-        *i = (Iterator) e;
-
-        return e->value;
-}
-
 void* hashmap_first(Hashmap *h) {
 
         if (!h)
@@ -828,17 +783,6 @@ void* hashmap_first_key(Hashmap *h) {
         return (void*) h->iterate_list_head->key;
 }
 
-void* hashmap_last(Hashmap *h) {
-
-        if (!h)
-                return NULL;
-
-        if (!h->iterate_list_tail)
-                return NULL;
-
-        return h->iterate_list_tail->value;
-}
-
 void* hashmap_steal_first(Hashmap *h) {
         void *data;
 
@@ -942,15 +886,15 @@ int hashmap_move_one(Hashmap *h, Hashmap *other, const void *key) {
         unsigned h_hash, other_hash;
         struct hashmap_entry *e;
 
-        if (!other)
-                return 0;
-
         assert(h);
 
         h_hash = bucket_hash(h, key);
         if (hash_scan(h, h_hash, key))
                 return -EEXIST;
 
+        if (!other)
+                return -ENOENT;
+
         other_hash = bucket_hash(other, key);
         e = hash_scan(other, other_hash, key);
         if (!e)
@@ -1001,7 +945,6 @@ void *hashmap_next(Hashmap *h, const void *key) {
         unsigned hash;
         struct hashmap_entry *e;
 
-        assert(h);
         assert(key);
 
         if (!h)