From a1c08f6fd8da1fe015aeb9035ae9035f3f29001f Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Mon, 18 Mar 2024 16:15:01 -0400 Subject: [PATCH] fixes --- examples/slm.c | 12 +++++++++++- flake.nix | 1 + include/sl.h | 14 +++++++------- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/examples/slm.c b/examples/slm.c index a192bd2..9afdcfa 100644 --- a/examples/slm.c +++ b/examples/slm.c @@ -58,6 +58,10 @@ SKIPLIST_DECL(slex, api_, entries, { return 0; }) +void sprintf_slex_node(slex_node_t *node, char *buf) { + sprintf(buf, "%d", node->key); +} + int main() { /* Allocate and initialize a Skiplist. */ slex_t _list = SKIP_HEAD_DEFAULT_INITIALIZER(__skip_key_compare_slex); @@ -77,7 +81,13 @@ int main() { n->value = -1; api_skip_insert_slex(list, n); - api_skip_dot_slex(NULL, list); + FILE* of = fopen("/tmp/slm.dot", "w"); + if (!of) { + perror("Failed to open file /tmp/slm.dot"); + return EXIT_FAILURE; + } + api_skip_dot_slex(of, list, sprintf_slex_node); + fclose(of); /* Insert 10 key/value pairs into the list. */ for (int i = 0; i < 10; i++) { diff --git a/flake.nix b/flake.nix index 0647dd4..e1f5aad 100644 --- a/flake.nix +++ b/flake.nix @@ -23,6 +23,7 @@ packages = with pkgs; [ autoconf bashInteractive + graphviz-nox ed gdb clang-tools diff --git a/include/sl.h b/include/sl.h index 43236aa..3089e2f 100644 --- a/include/sl.h +++ b/include/sl.h @@ -425,7 +425,7 @@ struct sl_trace { } \ \ /* -- skip_dot_start_ */ \ - static int __skip_dot_start_##decl(FILE *os, decl##_t *slist, size_t nsg) { \ + static int __skip_dot_start_##decl(FILE *os, decl##_t *slist, size_t nsg, skip_sprintf_node_##decl##_t fn) { \ if (nsg == 0) { \ fprintf(os, "digraph Skiplist {\n"); \ fprintf(os, "label = \"Skiplist.\"\n"); \ @@ -443,10 +443,10 @@ struct sl_trace { decl##_node_t *head = slist->slh_head; \ size_t level; \ if (SKIP_EMPTY(slist)) fprintf(os, "Empty HeadNode"); else { \ - level = ARRAY_LENGTH(head->field.sle_next); \ + level = ARRAY_LENGTH(head->field.sle_next) - 1; \ do { \ decl##_node_t *node = head->field.sle_next[level]; \ - fprintf(os, "{ %p }", level + 1, (void *)node); \ + fprintf(os, "{ %p }", level, (void *)node); \ if (level != 0) fprintf(os, " | "); \ } while(level--); \ } \ @@ -465,9 +465,9 @@ struct sl_trace { fprintf(os, "}\n\n"); \ \ /* Now all nodes via level 0, if non-empty */ \ - node = slist->slh_head; \ + node = slist->slh_head; \ if (ARRAY_LENGTH(node->field.sle_next)) \ - __skip_dot_node_##decl(os, slist, node, nsg, NULL); \ + __skip_dot_node_##decl(os, slist, node, nsg, fn); \ fprintf(os, "\n"); \ \ /* The tail, sentinal node */ \ @@ -501,7 +501,7 @@ struct sl_trace { * \ * https://en.wikipedia.org/wiki/DOT_(graph_description_language) \ */ \ - int prefix##skip_dot_##decl(FILE *os, decl##_t *slist) { \ + int prefix##skip_dot_##decl(FILE *os, decl##_t *slist, skip_sprintf_node_##decl##_t fn) { \ size_t nsg = 0; \ if (__skip_integrity_check_##decl(slist) != 0) { \ perror("Skiplist failed integrity checks, impossible to diagram.");\ @@ -513,7 +513,7 @@ struct sl_trace { perror("Failed to open output file, unable to write DOT file.");\ return -1; \ } \ - __skip_dot_start_##decl(os, slist, nsg); \ + __skip_dot_start_##decl(os, slist, nsg, fn); \ __skip_dot_finish_##decl(os, nsg); \ return 0; \ } \