Commit 48c2554

mo khan <mo.khan@gmail.com>
2020-08-03 21:56:50
Pretty print tuple in console
1 parent 8b6d6e4
Changed files (3)
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);
   }
 }
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");
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);