コード例 #1
0
ファイル: hashmap.c プロジェクト: bane138/lcthw
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;
}
コード例 #2
0
ファイル: hashmap.c プロジェクト: gergalerg/Hashmap_in_C
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;
}
コード例 #3
0
ファイル: Hashmap_a.c プロジェクト: fill1890/lcthwlib
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;
}