Ejemplo n.º 1
0
void hash_table_insert(HashTable* table, pointer key, pointer val)
{
	int i = table->hashFunc(key) % table->capacity;
	//printf("Insert index: %i\n", i);
	Node* p = (Node*)malloc(sizeof(Node));
	p->key = key;
	p->val = val;
	p->next = table->table[i];
	/*if (p->next)
		printf("Insert collision!\n");*/
	table->table[i] = p;
	table->size++;
	check_rehash(table);
}
Ejemplo n.º 2
0
static int _chash_add_internel(struct chashtable *cht, void *key, void *value, int force) {
    if( ! cht ) return 0;

    uint64_t ind;
    struct bucket *curr;

    check_rehash(cht);

    ind = cht->hash_func(key) & (cht->size - 1);
    for(curr = cht->buckets[ind]; curr; curr = curr->next) {
        if(!cht->comp_func(curr->key, key)) {
            if(!force) return 0;

            if(cht->fv_func) {
                cht->fv_func(curr->value);
            }
            if(cht->dv_func) {
                curr->value = cht->dv_func(value);
            } else {
                curr->value = value;
            }

            return 1;
        }
    }
    
    struct bucket *new_bucket = malloc(sizeof(*new_bucket));
    if(cht->dk_func) {
        new_bucket->key = cht->dk_func(key);
    } else {
        new_bucket->key = key;
    }
    if(cht->dv_func) {
        new_bucket->value = cht->dv_func(value);
    } else {
        new_bucket->value = value;
    }
    
    // insert into hashtable
    new_bucket->next = cht->buckets[ind];
    cht->buckets[ind] = new_bucket;
    cht->used++;

    return 1;
}