From 48c2554c49929c31168ec4448e9d20154422dd84 Mon Sep 17 00:00:00 2001 From: mo khan Date: Mon, 3 Aug 2020 15:56:50 -0600 Subject: Pretty print tuple in console --- src/02/04/hash.c | 11 ++++++++++- src/02/04/list.c | 5 +++-- src/02/04/list.h | 4 +++- 3 files changed, 16 insertions(+), 4 deletions(-) (limited to 'src/02') diff --git a/src/02/04/hash.c b/src/02/04/hash.c index 1d872a4..06fa63d 100644 --- a/src/02/04/hash.c +++ b/src/02/04/hash.c @@ -43,9 +43,18 @@ void hash_set(Hash *hash, int key, void *value) { hash->buckets[bucket] = *list_initialize(tuple); } +void print_tuple(void *data) { + Tuple *t = data; + + if (t) + printf("(%d:%d)", t->key, t->value); + else + printf("(nil)"); +} + void hash_inspect(Hash *hash) { for (int i = 0; i < hash->size; i++) { printf("%2d: ", i); - list_inspect(hash->buckets + i); + list_inspect(hash->buckets + i, print_tuple); } } diff --git a/src/02/04/list.c b/src/02/04/list.c index 3551a6e..99f8c83 100644 --- a/src/02/04/list.c +++ b/src/02/04/list.c @@ -41,13 +41,14 @@ int list_size(Node *head) { return i; } -void list_inspect(Node *self) { +void list_inspect(Node *self, Printer printer) { if (!self) return; printf("["); while (self) { - printf(" %p ", self->data); + printer(self->data); + /*printf(" %p ", self->data);*/ self = self->next; } printf("]\n"); diff --git a/src/02/04/list.h b/src/02/04/list.h index 2c9be9f..b04a282 100644 --- a/src/02/04/list.h +++ b/src/02/04/list.h @@ -3,7 +3,9 @@ typedef struct node { void *data; } Node; +typedef void (*Printer)(void *); + Node *list_initialize(void *data); Node *list_get(Node *from, int index); Node *list_add(Node *head, void *data); -void list_inspect(Node *node); +void list_inspect(Node *self, Printer printer); -- cgit v1.2.3