Finalize _set.

This commit is contained in:
Glenn Y. Rolland 2016-05-20 00:10:05 +02:00
parent 70022ae1f6
commit c9c662dd00

View file

@ -59,7 +59,7 @@ int hashtable_set(HASHTABLE* hashtable, char* key, void * value) {
idx = hash_of_key(hashtable, key); idx = hash_of_key(hashtable, key);
hashtable->size += 1; hashtable->size += 1;
printf("hashtable_set -- search value [%d/%d]\n", idx, hashtable->capacity); printf("hashtable_set -- index [%d/%d]\n", idx, hashtable->capacity);
// search value // search value
cell = hashtable->data[idx]; cell = hashtable->data[idx];
while(cell) { while(cell) {
@ -70,6 +70,7 @@ int hashtable_set(HASHTABLE* hashtable, char* key, void * value) {
if (cell) { if (cell) {
// if cell exists, then change inner value // if cell exists, then change inner value
printf("hashtable_set -- update value\n"); printf("hashtable_set -- update value\n");
cell->value = value;
} else { } else {
printf("hashtable_set -- create value\n"); printf("hashtable_set -- create value\n");
// create cell & prepend it to the chain // create cell & prepend it to the chain
@ -128,10 +129,11 @@ int main(int argc, char** argv) {
hashtable_set(hashtable, "alice", &x); hashtable_set(hashtable, "alice", &x);
printf("h['alice'] <- %d\n", x); printf("h['alice'] <- %d\n", x);
hashtable_set(hashtable, "bob", &y); hashtable_set(hashtable, "bob", &y);
printf("h['bob'] <- %d\n", x); printf("h['bob'] <- %d\n", y);
hashtable_set(hashtable, "charlie", &z); hashtable_set(hashtable, "charlie", &z);
printf("h['charlie'] <- %d\n", x); printf("h['charlie'] <- %d\n", z);
hashtable_set(hashtable, "alice", &y); hashtable_set(hashtable, "alice", &y);
printf("h['alice'] <- %d\n", y);
value = hashtable_get(hashtable, "alice"); value = hashtable_get(hashtable, "alice");
printf("h['alice'] ? %d\n", *value); printf("h['alice'] ? %d\n", *value);
value = hashtable_get(hashtable, "bob"); value = hashtable_get(hashtable, "bob");