#include #include #include "Counter.h" #include "HashTable.h" struct counter { HashTable ht; }; Counter CounterNew(void) { Counter c = malloc(sizeof(*c)); if (c == NULL) { fprintf(stderr, "error: out of memory\n"); exit(EXIT_FAILURE); } c->ht = HashTableNew(); return c; } void CounterFree(Counter c) { HashTableFree(c->ht); free(c); } void CounterAdd(Counter c, int item) { // How many times has this item already been counted? int num = HashTableGetOrDefault(c->ht, item, 0); num = num + 1; HashTableInsert(c->ht, item, num); } // HashTableGetOrDefault // give it a ht, key, and default value // if the key isn't in the hashtable -> return the defaultvalue // otherwise (key is in ht) -> return value associated with that key int CounterGet(Counter c, int item) { return HashTableGetOrDefault(c->ht, item, 0); }