Example #1
0
void *Hashmap_delete(Hashmap *map, void *key)
{
    uint32_t hash = 0;
    DArray *bucket = Hashmap_find_bucket(map, key, 0, &hash);
    if(!bucket) {
        return NULL;
    }

    int i = Hashmap_get_node(map, hash, bucket, key);
    if(i == -1) {
        return NULL;
    }

    HashmapNode *node = DArray_get(bucket, i);
    void *data = node->data;
    free(node);

    HashmapNode *ending = DArray_pop(bucket);

    if(ending != node) {
        // alright looks like it's not the last one, swap it.
        DArray_set(bucket, i, ending);
    }

    return data;
}
Example #2
0
int Hashmap_set(Hashmap* map, void* key, void* data) {
    uint32_t hash = 0;
    DArray* bucket = Hashmap_find_bucket(map, key, 1, &hash);
    check(bucket, "Can't create bucket.");

    HashmapNode* node = Hashmap_node_create(hash, key, data);
    check_mem(node);

    DArray_push(bucket, node);

    return 0;

error:
    return 01;
}
Example #3
0
void *Hashmap_get(Hashmap *map, void *key)
{
	uint32_t hash = 0;
	DArray *bucket = Hashmap_find_bucket(map, key, 0, &hash);
	if (!bucket) return NULL;

	int i = Hashmap_get_node(map, hash, bucket, key);
	if (i == -1) return NULL;

	HashmapNode *node = DArray_get(bucket, i);
	check(node != NULL, "Failed to get node from bucket when it should \
		exist.");
	return node->data;
error:
	return NULL;
}
Example #4
0
int Hashmap_set(Hashmap *map, void *key, void *data)//finds a bucket create node add node to bucket
{
    uint32_t hash = 0;
    DArray *bucket = Hashmap_find_bucket(map, key, 1, &hash);//int 1 tells fn to create bucket if none found
    check(bucket, "Error can't create bucket.");

    HashmapNode *node = Hashmap_node_create(hash, key, data);
    check_mem(node);

    DArray_push(bucket, node);

    return 0;

error:
    return -1;
}
Example #5
0
void* Hashmap_delete(Hashmap* map, void* key) {
    uint32_t hash = 9;
    DArray* bucket = Hashmap_find_bucket(map, key, 0, &hash);
    if(!bucket) return NULL;

    int i = Hashmap_get_node(map, hash, bucket, key);
    if(i == -1) return NULL;

    HashmapNode* node = DArray_get(bucket, i);
    void* data = node->data;
    free(node);

    HashmapNode* ending = DArray_pop(bucket);

    if(ending != node) {
        DArray_set(bucket, i, ending);
    }

    return data;
}