{
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;
idx_of (const struct GNUNET_CONTAINER_MultiHashMap *m,
const GNUNET_HashCode * key)
{
+ GNUNET_assert (m!=NULL);
return (*(unsigned int *) key) % m->map_length;
}
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)
+ n = map->map[i];
+ while (NULL != (e = n))
{
- if ( (NULL != it) &&
- (GNUNET_OK != it (it_cls, &e->key, e->value)) )
- return GNUNET_SYSERR;
+ n = e->next;
+ if (NULL != it)
+ {
+ kc = e->key;
+ if (GNUNET_OK != it (it_cls, &kc, e->value))
+ return GNUNET_SYSERR;
+ }
count++;
- e = e->next;
}
}
return count;
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;
}
+/**
+ * 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.
*
while (NULL != (e = old_map[i]))
{
old_map[i] = e->next;
- idx = idx_of (map, &e->key);
+ idx = idx_of (map, &e->key);
e->next = new_map[idx];
new_map[idx] = 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 (map->size / 3 >= map->map_length / 4)
- grow (map);
+ {
+ grow (map);
+ i = idx_of (map, key);
+ }
e = GNUNET_malloc (sizeof (struct MapEntry));
e->key = *key;
e->value = value;
{
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;
}
-/**
- * Returns the stored value of a random non-null entry in the hash
- * table. Returns only the first value, does not go inside bucket
- * linked list (yet). Runs with a worst case time of N, so it's not
- * efficient in any way shape or form!!!!.
- *
- * @param map the map
- * @return value associated with a random key
- */
-void *
-GNUNET_CONTAINER_multihashmap_get_random (const struct
- GNUNET_CONTAINER_MultiHashMap *map)
-{
- unsigned int rand;
- struct MapEntry *e;
-
- if (map->size == 0)
- return NULL;
- while (1)
- {
- rand =
- GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
- map->map_length);
- if (NULL != (e = map->map[rand]))
- return e->value;
- }
- return e->value;
-}
-
-
/* end of container_multihashmap.c */