WiP
[oweals/gnunet.git] / src / util / container_multihashmap.c
index 7ca6676cbc0b308e32e0e80d6f393d979a9a18ed..579573bdb89b7b7a27c6fcfcb9673a3911b085dc 100644 (file)
@@ -130,6 +130,7 @@ static unsigned int
 idx_of (const struct GNUNET_CONTAINER_MultiHashMap *m,
         const GNUNET_HashCode * key)
 {
+  GNUNET_assert (m!=NULL);
   return (*(unsigned int *) key) % m->map_length;
 }
 
@@ -194,16 +195,22 @@ GNUNET_CONTAINER_multihashmap_iterate (const struct
   unsigned int i;
   struct MapEntry *e;
   struct MapEntry *n;
+  GNUNET_HashCode kc;
 
   count = 0;
+  GNUNET_assert(map != NULL);
   for (i = 0; i < map->map_length; i++)
     {
       n = map->map[i];
       while (NULL != (e = n))
         {
          n = e->next;
-          if ((NULL != it) && (GNUNET_OK != it (it_cls, &e->key, e->value)))
-            return GNUNET_SYSERR;
+          if (NULL != it)
+           {
+             kc = e->key;
+             if (GNUNET_OK != it (it_cls, &kc, e->value))
+               return GNUNET_SYSERR;
+           }
           count++;
         }
     }
@@ -328,6 +335,36 @@ GNUNET_CONTAINER_multihashmap_contains (const struct
 }
 
 
+/**
+ * Check if the map contains the given value under the given
+ * key.
+ *
+ * @param map the map
+ * @param key the key to test if a value exists for it
+ * @param value value to test for
+ * @return GNUNET_YES if such a value exists,
+ *         GNUNET_NO if not
+ */
+int GNUNET_CONTAINER_multihashmap_contains_value (const struct
+                                                 GNUNET_CONTAINER_MultiHashMap
+                                                 *map,
+                                                 const GNUNET_HashCode * key,
+                                                 const void *value)
+{
+  struct MapEntry *e;
+
+  e = map->map[idx_of (map, key)];
+  while (e != NULL)
+    {
+      if ( (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) &&
+          (e->value == value) )
+        return GNUNET_YES;
+      e = e->next;
+    }
+  return GNUNET_NO;
+}
+
+
 /**
  * Grow the given map to a more appropriate size.
  *
@@ -446,7 +483,7 @@ GNUNET_CONTAINER_multihashmap_get_multiple (const struct
       n = e->next;
       if (0 != memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
        continue;
-      if ((it != NULL) && (GNUNET_OK != it (it_cls, &e->key, e->value)))
+      if ((it != NULL) && (GNUNET_OK != it (it_cls, key, e->value)))
        return GNUNET_SYSERR;
       count++;
     }