#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 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;
*/
void
GNUNET_CONTAINER_multihashmap_destroy (struct GNUNET_CONTAINER_MultiHashMap
- *map)
+ *map)
{
unsigned int i;
struct MapEntry *e;
for (i = 0; i < map->map_length; 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);
*/
static unsigned int
idx_of (const struct GNUNET_CONTAINER_MultiHashMap *m,
- const GNUNET_HashCode * key)
+ const GNUNET_HashCode * key)
{
+ GNUNET_assert (m != NULL);
return (*(unsigned int *) key) % m->map_length;
}
*/
unsigned int
GNUNET_CONTAINER_multihashmap_size (const struct GNUNET_CONTAINER_MultiHashMap
- *map)
+ *map)
{
return map->size;
}
*/
void *
GNUNET_CONTAINER_multihashmap_get (const struct GNUNET_CONTAINER_MultiHashMap
- *map, const GNUNET_HashCode * key)
+ *map, const GNUNET_HashCode * key)
{
struct MapEntry *e;
while (e != NULL)
{
if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
- return e->value;
+ return e->value;
e = e->next;
}
return NULL;
*/
int
GNUNET_CONTAINER_multihashmap_iterate (const struct
- GNUNET_CONTAINER_MultiHashMap *map,
- GNUNET_CONTAINER_HashMapIterator it,
- void *it_cls)
+ GNUNET_CONTAINER_MultiHashMap *map,
+ GNUNET_CONTAINER_HashMapIterator it,
+ void *it_cls)
{
int count;
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++)
{
- 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 = 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++;
+ }
}
return count;
}
*/
int
GNUNET_CONTAINER_multihashmap_remove (struct GNUNET_CONTAINER_MultiHashMap
- *map, const GNUNET_HashCode * key,
- void *value)
+ *map, const GNUNET_HashCode * key,
+ void *value)
{
struct MapEntry *e;
struct MapEntry *p;
e = map->map[i];
while (e != NULL)
{
- 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;
- }
+ 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;
}
*/
int
GNUNET_CONTAINER_multihashmap_remove_all (struct GNUNET_CONTAINER_MultiHashMap
- *map, const GNUNET_HashCode * key)
+ *map, const GNUNET_HashCode * key)
{
struct MapEntry *e;
struct MapEntry *p;
while (e != NULL)
{
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
+ e = p->next;
+ ret++;
+ }
else
- {
- p = e;
- e = e->next;
- }
+ {
+ p = e;
+ e = e->next;
+ }
}
return ret;
}
*/
int
GNUNET_CONTAINER_multihashmap_contains (const struct
- GNUNET_CONTAINER_MultiHashMap *map,
- const GNUNET_HashCode * key)
+ GNUNET_CONTAINER_MultiHashMap *map,
+ const GNUNET_HashCode * key)
{
struct MapEntry *e;
while (e != NULL)
{
if (0 == memcmp (key, &e->key, sizeof (GNUNET_HashCode)))
- return GNUNET_YES;
+ 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;
for (i = 0; i < old_len; i++)
{
while (NULL != (e = old_map[i]))
- {
- old_map[i] = e->next;
+ {
+ old_map[i] = e->next;
idx = idx_of (map, &e->key);
- e->next = new_map[idx];
- new_map[idx] = e;
- }
+ 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) )
+ if ((opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE) &&
+ (opt != GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
{
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);
+ i = idx_of (map, key);
}
e = GNUNET_malloc (sizeof (struct MapEntry));
e->key = *key;
*/
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;
+ struct MapEntry *n;
count = 0;
- e = map->map[idx_of (map, key)];
- while (e != NULL)
+ n = map->map[idx_of (map, key)];
+ while (NULL != (e = n))
{
- 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 = 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;
}