#include #include #include #include "counter.h" #include "hash_table.h" struct counter { HashTable ht; }; Counter CounterNew(void) { Counter c = malloc(sizeof(*c)); c->ht = HashTableNew(); return c; } void CounterFree(Counter c) { HashTableFree(c->ht); free(c); return; } void CounterAdd(Counter c, int item) { bool is_in = HashTableContains(c->ht, item); if (!is_in) { HashTableInsert(c->ht, item, 1); } else { int val = HashTableGet(c->ht, item); val++; HashTableInsert(c->ht, item, val); } } int CounterGet(Counter c, int item) { if (HashTableContains(c->ht, item)) { return HashTableGet(c->ht, item); } return 0; }