#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.
*/
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);
}
idx_of (const struct GNUNET_CONTAINER_MultiHashMap *m,
const GNUNET_HashCode * key)
{
+ GNUNET_assert (m != NULL);
return (*(unsigned int *) key) % m->map_length;
}
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 (GNUNET_HashCode)))
+ return e->value;
+ e = e->next;
+ }
return NULL;
}
GNUNET_HashCode kc;
count = 0;
- GNUNET_assert(map!=NULL);
-
+ GNUNET_assert (map != NULL);
for (i = 0; i < map->map_length; i++)
+ {
+ n = map->map[i];
+ while (NULL != (e = n))
{
- n = map->map[i];
- while (NULL != (e = n))
- {
- n = e->next;
- if (NULL != it)
- {
- kc = e->key;
- if (GNUNET_OK != it (it_cls, &kc, e->value))
- return GNUNET_SYSERR;
- }
- count++;
- }
+ n = e->next;
+ if (NULL != it)
+ {
+ kc = e->key;
+ if (GNUNET_OK != it (it_cls, &kc, e->value))
+ return GNUNET_SYSERR;
+ }
+ count++;
}
+ }
return count;
}
* 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 GNUNET_HashCode * key, void *value)
{
struct MapEntry *e;
struct MapEntry *p;
p = NULL;
e = map->map[i];
while (e != NULL)
+ {
+ if ((0 == memcmp (key, &e->key, sizeof (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;
}
p = NULL;
e = map->map[i];
while (e != NULL)
+ {
+ if (0 == memcmp (key, &e->key, sizeof (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 = e;
- e = e->next;
- }
+ p->next = e->next;
+ GNUNET_free (e);
+ map->size--;
+ if (p == NULL)
+ e = map->map[i];
+ else
+ e = p->next;
+ ret++;
+ }
+ else
+ {
+ p = e;
+ e = e->next;
}
+ }
return ret;
}
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 (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 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;
}
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);
}
*/
int
GNUNET_CONTAINER_multihashmap_put (struct GNUNET_CONTAINER_MultiHashMap *map,
- const GNUNET_HashCode * key,
- void *value,
- enum GNUNET_CONTAINER_MultiHashMapOption
- opt)
+ const 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))
+ {
+ 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 (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;
*/
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 GNUNET_HashCode * key,
+ GNUNET_CONTAINER_HashMapIterator it,
+ void *it_cls)
{
int count;
struct MapEntry *e;
count = 0;
n = map->map[idx_of (map, key)];
while (NULL != (e = n))
- {
- n = e->next;
- if (0 != memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
- continue;
- if ((it != NULL) && (GNUNET_OK != it (it_cls, key, e->value)))
- return GNUNET_SYSERR;
- count++;
- }
+ {
+ n = e->next;
+ if (0 != memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
+ continue;
+ if ((it != NULL) && (GNUNET_OK != it (it_cls, key, e->value)))
+ return GNUNET_SYSERR;
+ count++;
+ }
return count;
}