Esempio n. 1
0
int map_unset(map_t map, void *key, void **olddata)
{
	bucket_t *b, *prev;
	int hash;

	if (!map || !key)
		return RETERROR(EINVAL, -1);

	hash = map->hashf(map->size, key);
	b = map->buckets[hash];
	prev = 0;
	while (b)
	{
		if (!map->compf(key, b->key))
		{
			if (prev)
				prev->next = b->next;
			else
				map->buckets[hash] = b->next;
			mem_init(olddata, b->data);
			map_bucket_free(map, b);
			return -- map->count;;
		}
		prev = b;
		b = b->next;
	}
	return RETERROR(ERANGE, -1);
}
Esempio n. 2
0
int map_set(map_t map, void *key, void *data, void **olddata)
{
	bucket_t *b;

	if (!map || !key)
		return RETERROR(EINVAL, -1);

	b = map_bucket_find(map, key);
	if (b)
	{
		mem_init(olddata, b->data);
		b->data = data;
	}
	else
	{
		int hash;

		if (!map_resize(map, map->count + 1, 0))
			return -1;

		if (!(b = map_bucket_alloc(map, key, data)))
			return -1;

		hash = map->hashf(map->size, key);
		b->next = map->buckets[hash];
		map->buckets[hash] = b;
		++ map->count;
	}

	return map->count;
}