summaryrefslogtreecommitdiff
path: root/src/02
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2020-08-03 15:56:50 -0600
committermo khan <mo.khan@gmail.com>2020-08-03 15:56:50 -0600
commit48c2554c49929c31168ec4448e9d20154422dd84 (patch)
treead3a1de73f76f9d037331b42c985d4ce536cc7aa /src/02
parent8b6d6e4756008d412bd5daf3db44f3537deacd3f (diff)
Pretty print tuple in console
Diffstat (limited to 'src/02')
-rw-r--r--src/02/04/hash.c11
-rw-r--r--src/02/04/list.c5
-rw-r--r--src/02/04/list.h4
3 files changed, 16 insertions, 4 deletions
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);