コード例 #1
0
void *hash_table_get(hash_table *ht, void *key) {
    hash_table_entry *hte;
    if((hte = hash_table_get_entry(ht, key)) != NULL) {
        return hte->value;
    }
    return NULL;
}
コード例 #2
0
bool hash_table_delete(hash_table *ht, void *key) {
    hash_table_entry *hte;
    hte = hash_table_get_entry(ht, key);
    if(hte == NULL) {
        // key doesn't even exist!
        return false;
    }

    unsigned int hash = ht->hash(key);
    int bucket_index = hash % ht->buckets_count;
    hash_table_entry **bucket = &ht->buckets[bucket_index];
    // Find the element in this bucket
    if(*bucket == hte) {
        // This IS the first node.
        *bucket = (*bucket)->next;
    } else {
        // TODO: This could go into an infinite loop if something is wrong
        // with the table.
        // Fix it.
        while((*bucket)->next != hte) {
            *bucket = (*bucket)->next;
        }
        // bucket->next == hte, so set bucket's next to hte's next!
        (*bucket)->next = hte->next;
    }

    // Successfully deleted.
    ht->entries_count--;
    return true;
}
コード例 #3
0
bool hash_table_put(hash_table *ht, void *key, void *value) {
    hash_table_entry *hte;

    // Already exists.
    if((hte = hash_table_get_entry(ht, key)) != NULL) {
        hte->value = value;
    } else {
        // Make sure load is < HASH_TABLE_MAX_LOAD
        hash_table_ensure_load(ht);

        unsigned int hash = ht->hash(key);
        int bucket = hash % ht->buckets_count;

        hte = malloc(sizeof(hash_table_entry));
        hte->hash = hash;
        hte->key = key;
        hte->value = value;
        hte->next = NULL;

        if(ht->buckets[bucket] == NULL) {
            ht->buckets[bucket] = hte;
        } else {
            hash_table_entry *ptr = ht->buckets[bucket];
            while(ptr->next != NULL) {
                ptr = ptr->next;
            }
            ptr->next = hte;
        }
        // This was a new entry, increment entries_count.
        ht->entries_count++;
    }

    return true;
}
コード例 #4
0
int *hash_table_get(hash_table *t, const char *key) {
	vector *b = hash_table_get_bucket(t, key);
	hash_table_entry *e = hash_table_get_entry(b, key);
	if (e) {
		return &e->value;
	} else {
		return NULL;
	}
}
コード例 #5
0
void hash_table_set(hash_table *t, const char *key, int value) {
	if (hash_table_get_load(t) >= HASH_TABLE_MAX_LOAD_FACTOR) {
		hash_table replacement = hash_table_new_size(vector_size(&t->buckets) * 2);
		for (size_t i = 0; i < vector_size(&t->buckets); i++) {
			vector *bucket = vector_get(&t->buckets, i);
			for (size_t j = 0; j < vector_size(bucket); j++) {
				hash_table_entry *e = vector_get(bucket, j);
				replacement.entries++;
				vector_append(hash_table_get_bucket(&replacement, e->key), e);
			}
		}
		hash_table_adopt(t, &replacement);
	}
	vector *b = hash_table_get_bucket(t, key);
	hash_table_entry *e = hash_table_get_entry(b, key);
	if (e) {
		e->value = value;
	} else {
		t->entries++;
		vector_append(b, hash_table_entry_new(key, value));
	}
}