#include #include #include #include #include "usdot.h" #include "string_util.h" #include "smap.h" void free_value(const char *key, int *value, void *); void print_entry(const char *key, int *value, void *); void update_count(smap *c, const char *s); #define CODE_LENGTH 3 int main(int argc, char **argv) { smap *count = smap_create(really_bad_string_hash); int id, seq, tot; char origin[CODE_LENGTH + 1]; char destination[CODE_LENGTH + 1]; while (read_segment(stdin, &id, &seq, &tot, origin, destination)) { update_count(count, origin); update_count(count, destination); } smap_for_each(count, print_entry, NULL); // free allocated memory smap_for_each(count, free_value, NULL); smap_destroy(count); } /** * Prints the given map entry to standard output. */ void print_entry(const char *key, int *value, void *arg) { printf("%s=%d\n", key, *value); } /** * Frees the value in the given map entry. */ void free_value(const char *key, int *value, void *arg) { free(value); } /** * A very bad hash function for integers. */ int int_hash_identity(int n) { return n; } /** * Updates the count in the given map for the given string. * * @param m a map, non-NULL * @param s a key, non-NULL */ void update_count(smap *m, const char *s) { if (m == NULL || s == NULL) { return; } if (!smap_contains_key(m, s)) { // key is not present; add it with new value of 1 int *c = malloc(sizeof(int)); *c = 1; smap_put(m, s, c); } else { int *c = smap_get(m, s); (*c)++; } }