-dead
[oweals/gnunet.git] / src / util / container_multihashmap.c
index 2c88b1a52aa95dbfe85c6b99920325a7502f0b6e..ada98251cc5b5967c69a5ab3cd9a7dbaf63d1844 100644 (file)
@@ -28,6 +28,8 @@
 #include "gnunet_container_lib.h"
 #include "gnunet_crypto_lib.h"
 
+#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
+
 /**
  * An entry in the hash map.
  */
@@ -37,7 +39,7 @@ struct MapEntry
   /**
    * Key for the entry.
    */
-  GNUNET_HashCode key;
+  struct GNUNET_HashCode key;
 
   /**
    * Value of the entry.
@@ -85,6 +87,7 @@ GNUNET_CONTAINER_multihashmap_create (unsigned int len)
 {
   struct GNUNET_CONTAINER_MultiHashMap *ret;
 
+  GNUNET_assert (len > 0);
   ret = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_MultiHashMap));
   ret->map = GNUNET_malloc (len * sizeof (struct MapEntry *));
   ret->map_length = len;
@@ -106,13 +109,13 @@ GNUNET_CONTAINER_multihashmap_destroy (struct GNUNET_CONTAINER_MultiHashMap
   struct MapEntry *e;
 
   for (i = 0; i < map->map_length; i++)
+  {
+    while (NULL != (e = map->map[i]))
     {
-      while (NULL != (e = map->map[i]))
-        {
-          map->map[i] = e->next;
-          GNUNET_free (e);
-        }
+      map->map[i] = e->next;
+      GNUNET_free (e);
     }
+  }
   GNUNET_free (map->map);
   GNUNET_free (map);
 }
@@ -127,8 +130,9 @@ GNUNET_CONTAINER_multihashmap_destroy (struct GNUNET_CONTAINER_MultiHashMap
  */
 static unsigned int
 idx_of (const struct GNUNET_CONTAINER_MultiHashMap *m,
-        const GNUNET_HashCode * key)
+        const struct GNUNET_HashCode * key)
 {
+  GNUNET_assert (m != NULL);
   return (*(unsigned int *) key) % m->map_length;
 }
 
@@ -159,17 +163,17 @@ GNUNET_CONTAINER_multihashmap_size (const struct GNUNET_CONTAINER_MultiHashMap
  */
 void *
 GNUNET_CONTAINER_multihashmap_get (const struct GNUNET_CONTAINER_MultiHashMap
-                                   *map, const GNUNET_HashCode * key)
+                                   *map, const struct GNUNET_HashCode * key)
 {
   struct MapEntry *e;
 
   e = map->map[idx_of (map, key)];
   while (e != NULL)
-    {
-      if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
-        return e->value;
-      e = e->next;
-    }
+  {
+    if (0 == memcmp (key, &e->key, sizeof (struct GNUNET_HashCode)))
+      return e->value;
+    e = e->next;
+  }
   return NULL;
 }
 
@@ -192,20 +196,26 @@ GNUNET_CONTAINER_multihashmap_iterate (const struct
   int count;
   unsigned int i;
   struct MapEntry *e;
+  struct MapEntry *n;
+  struct 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))
     {
-      e = map->map[i];
-      while (e != NULL)
-        {
-          if ( (NULL != it) && 
-              (GNUNET_OK != it (it_cls, &e->key, e->value)) )
-            return GNUNET_SYSERR;
-          count++;
-          e = e->next;
-        }
+      n = e->next;
+      if (NULL != it)
+      {
+        kc = e->key;
+        if (GNUNET_OK != it (it_cls, &kc, e->value))
+          return GNUNET_SYSERR;
+      }
+      count++;
     }
+  }
   return count;
 }
 
@@ -222,9 +232,8 @@ GNUNET_CONTAINER_multihashmap_iterate (const struct
  *  is not in the map
  */
 int
-GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap
-                                      *map, const GNUNET_HashCode * key,
-                                      void *value)
+GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap *map,
+                                      const struct GNUNET_HashCode * key, void *value)
 {
   struct MapEntry *e;
   struct MapEntry *p;
@@ -234,21 +243,21 @@ GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap
   p = NULL;
   e = map->map[i];
   while (e != NULL)
+  {
+    if ((0 == memcmp (key, &e->key, sizeof (struct GNUNET_HashCode))) &&
+        (value == e->value))
     {
-      if ( (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode))) &&
-          (value == e->value))
-        {
-          if (p == NULL)
-            map->map[i] = e->next;
-          else
-            p->next = e->next;
-          GNUNET_free (e);
-          map->size--;
-          return GNUNET_YES;
-        }
-      p = e;
-      e = e->next;
+      if (p == NULL)
+        map->map[i] = e->next;
+      else
+        p->next = e->next;
+      GNUNET_free (e);
+      map->size--;
+      return GNUNET_YES;
     }
+    p = e;
+    e = e->next;
+  }
   return GNUNET_NO;
 }
 
@@ -263,7 +272,7 @@ GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap
  */
 int
 GNUNET_CONTAINER_multihashmap_remove_all (struct GNUNET_CONTAINER_MultiHashMap
-                                          *map, const GNUNET_HashCode * key)
+                                          *map, const struct GNUNET_HashCode * key)
 {
   struct MapEntry *e;
   struct MapEntry *p;
@@ -275,27 +284,27 @@ GNUNET_CONTAINER_multihashmap_remove_all (struct GNUNET_CONTAINER_MultiHashMap
   p = NULL;
   e = map->map[i];
   while (e != NULL)
+  {
+    if (0 == memcmp (key, &e->key, sizeof (struct GNUNET_HashCode)))
     {
-      if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
-        {
-          if (p == NULL)
-            map->map[i] = e->next;
-          else
-            p->next = e->next;
-          GNUNET_free (e);
-          map->size--;
-          if (p == NULL)
-            e = map->map[i];
-          else
-            e = p->next;
-          ret++;
-        }
+      if (p == NULL)
+        map->map[i] = e->next;
+      else
+        p->next = e->next;
+      GNUNET_free (e);
+      map->size--;
+      if (p == NULL)
+        e = map->map[i];
       else
-        {
-          p = e;
-          e = e->next;
-        }
+        e = p->next;
+      ret++;
+    }
+    else
+    {
+      p = e;
+      e = e->next;
     }
+  }
   return ret;
 }
 
@@ -312,17 +321,47 @@ GNUNET_CONTAINER_multihashmap_remove_all (struct GNUNET_CONTAINER_MultiHashMap
 int
 GNUNET_CONTAINER_multihashmap_contains (const struct
                                         GNUNET_CONTAINER_MultiHashMap *map,
-                                        const GNUNET_HashCode * key)
+                                        const struct GNUNET_HashCode * key)
 {
   struct MapEntry *e;
 
   e = map->map[idx_of (map, key)];
   while (e != NULL)
-    {
-      if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
-        return GNUNET_YES;
-      e = e->next;
-    }
+  {
+    if (0 == memcmp (key, &e->key, sizeof (struct GNUNET_HashCode)))
+      return GNUNET_YES;
+    e = e->next;
+  }
+  return GNUNET_NO;
+}
+
+
+/**
+ * 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 struct 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 (struct GNUNET_HashCode))) &&
+        (e->value == value))
+      return GNUNET_YES;
+    e = e->next;
+  }
   return GNUNET_NO;
 }
 
@@ -350,15 +389,15 @@ grow (struct GNUNET_CONTAINER_MultiHashMap *map)
   map->map_length = new_len;
   map->map = new_map;
   for (i = 0; i < old_len; i++)
+  {
+    while (NULL != (e = old_map[i]))
     {
-      while (NULL != (e = old_map[i]))
-        {
-          old_map[i] = e->next;
-         idx = idx_of (map, &e->key);
-          e->next = new_map[idx];
-          new_map[idx] = e;
-        }
+      old_map[i] = e->next;
+      idx = idx_of (map, &e->key);
+      e->next = new_map[idx];
+      new_map[idx] = e;
     }
+  }
   GNUNET_free (old_map);
 }
 
@@ -377,36 +416,34 @@ grow (struct GNUNET_CONTAINER_MultiHashMap *map)
  */
 int
 GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map,
-                                   const GNUNET_HashCode * key,
-                                   void *value,
-                                   enum GNUNET_CONTAINER_MultiHashMapOption
-                                   opt)
+                                   const struct GNUNET_HashCode * key, void *value,
+                                   enum GNUNET_CONTAINER_MultiHashMapOption opt)
 {
   struct MapEntry *e;
   unsigned int i;
 
   i = idx_of (map, key);
-  if ( (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
-       (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST) )
+  if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
+      (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
+  {
+    e = map->map[i];
+    while (e != NULL)
     {
-      e = map->map[i];
-      while (e != NULL)
-        {
-          if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
-            {
-              if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
-                return GNUNET_SYSERR;
-              e->value = value;
-              return GNUNET_NO;
-            }
-          e = e->next;
-        }
+      if (0 == memcmp (key, &e->key, sizeof (struct GNUNET_HashCode)))
+      {
+        if (opt == GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)
+          return GNUNET_SYSERR;
+        e->value = value;
+        return GNUNET_NO;
+      }
+      e = e->next;
     }
+  }
   if (map->size / 3 >= map->map_length / 4)
-    {
-      grow (map);
-      i = idx_of (map, key);  
-    }
+  {
+    grow (map);
+    i = idx_of (map, key);
+  }
   e = GNUNET_malloc (sizeof (struct MapEntry));
   e->key = *key;
   e->value = value;
@@ -429,27 +466,26 @@ GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map,
  */
 int
 GNUNET_CONTAINER_multihashmap_get_multiple (const struct
-                                            GNUNET_CONTAINER_MultiHashMap
-                                            *map, const GNUNET_HashCode * key,
-                                            GNUNET_CONTAINER_HashMapIterator
-                                            it, void *it_cls)
+                                            GNUNET_CONTAINER_MultiHashMap *map,
+                                            const struct GNUNET_HashCode * key,
+                                            GNUNET_CONTAINER_HashMapIterator it,
+                                            void *it_cls)
 {
   int count;
   struct MapEntry *e;
+  struct MapEntry *n;
 
   count = 0;
-  e = map->map[idx_of (map, key)];
-  while (e != NULL)
-    {
-      if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
-        {
-          if ( (it != NULL) && 
-              (GNUNET_OK != it (it_cls, &e->key, e->value)) )
-            return GNUNET_SYSERR;
-          count++;
-        }
-      e = e->next;
-    }
+  n = map->map[idx_of (map, key)];
+  while (NULL != (e = n))
+  {
+    n = e->next;
+    if (0 != memcmp (key, &e->key, sizeof (struct GNUNET_HashCode)))
+      continue;
+    if ((it != NULL) && (GNUNET_OK != it (it_cls, key, e->value)))
+      return GNUNET_SYSERR;
+    count++;
+  }
   return count;
 }