working on it...

This commit is contained in:
Gregory Burd 2024-03-19 21:33:43 -04:00
parent 6e97787aa3
commit 03790de272
2 changed files with 219 additions and 190 deletions

View file

@ -143,7 +143,7 @@ main()
perror("Failed to open file /tmp/slm.dot"); perror("Failed to open file /tmp/slm.dot");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
api_skip_dot_slex(of, list, sprintf_slex_node); api_skip_dot_slex(of, list, 0, sprintf_slex_node);
fclose(of); fclose(of);
api_skip_destroy_slex(list); api_skip_destroy_slex(list);

View file

@ -277,8 +277,9 @@
return NULL; \ return NULL; \
} \ } \
\ \
/* -- skip_insert_ */ \ /* -- __skip_insert_ */ \
int prefix##skip_insert_##decl(decl##_t *slist, decl##_node_t *n) \ static int __skip_insert_##decl(decl##_t *slist, decl##_node_t *n, \
int flags) \
{ \ { \
unsigned int i; \ unsigned int i; \
decl##_node_t *prev, *elm = slist->slh_head; \ decl##_node_t *prev, *elm = slist->slh_head; \
@ -298,26 +299,28 @@
__skip_key_compare_##decl(slist, elm->field.sle_next[i], n, \ __skip_key_compare_##decl(slist, elm->field.sle_next[i], n, \
slist->aux) < 0) \ slist->aux) < 0) \
elm = elm->field.sle_next[i]; \ elm = elm->field.sle_next[i]; \
path[i] = elm; \ path[i] = elm; \
ARRAY_SET_LENGTH(path, ARRAY_LENGTH(path) + 1); \ ARRAY_SET_LENGTH(path, ARRAY_LENGTH(path) + 1); \
} while (i--); \ } while (i--); \
i = 0; \ i = 0; \
prev = elm; \ prev = elm; \
elm = elm->field.sle_next[0]; \ elm = elm->field.sle_next[0]; \
if (__skip_key_compare_##decl(slist, elm, n, slist->aux) == 0) { \ if (__skip_key_compare_##decl(slist, elm, n, slist->aux) == 0) { \
/* Don't overwrite, to do that use _REPLACE not _INSERT */ \ if (flags == 0) { \
ARRAY_FREE(path); \ /* Don't overwrite, to do that use _REPLACE not _INSERT */ \
return -1; \ ARRAY_FREE(path); \
return -1; \
} \
} \ } \
size_t level = __skip_toss_##decl(slist->max, slist->fanout); \ size_t level = __skip_toss_##decl(slist->max, slist->fanout); \
ARRAY_SET_LENGTH(n->field.sle_next, level); \ ARRAY_SET_LENGTH(n->field.sle_next, level); \
if (level > slist->level) { \ if (level > slist->level) { \
for (i = slist->level + 1; i <= level; i++) { \ for (i = slist->level + 1; i <= level; i++) { \
path[i] = slist->slh_tail; \ path[i] = slist->slh_tail; \
} \ } \
slist->level = level; \ slist->level = level; \
} \ } \
for (i = 0; i <= level; i++) { \ for (i = 0; i <= level; i++) { \
n->field.sle_next[i] = path[i]->field.sle_next[i]; \ n->field.sle_next[i] = path[i]->field.sle_next[i]; \
path[i]->field.sle_next[i] = n; \ path[i]->field.sle_next[i] = n; \
} \ } \
@ -330,14 +333,17 @@
return 0; \ return 0; \
} \ } \
\ \
/* -- skip_insert_dup_ TODO \ /* -- skip_insert_ */ \
int prefix##skip_insert_##decl(decl##_t *slist, decl##_node_t *n) \
{ \
return __skip_insert_##decl(slist, n, 0); \
} \
\
/* -- skip_insert_dup_ */ \
int prefix##skip_insert_dup_##decl(decl##_t *slist, decl##_node_t *n) \ int prefix##skip_insert_dup_##decl(decl##_t *slist, decl##_node_t *n) \
{ \ { \
((void)slist); \ return __skip_insert_##decl(slist, n, 1); \
((void)n); \
return 0; \
} \ } \
*/ \
\ \
/* -- skip_find_ \ /* -- skip_find_ \
* Find a node that matches another node. This differs from the locate() \ * Find a node that matches another node. This differs from the locate() \
@ -477,7 +483,7 @@
ARRAY_FREE(path); \ ARRAY_FREE(path); \
free_node_blk; \ free_node_blk; \
/* Find all levels in the first element in the list that point \ /* Find all levels in the first element in the list that point \
at the tail and shrink the level*/ \ at the tail and shrink the level. */ \
while (slist->level > 0 && \ while (slist->level > 0 && \
slist->slh_head->field.sle_next[slist->level] == slist->slh_tail) { \ slist->slh_head->field.sle_next[slist->level] == slist->slh_tail) { \
slist->level--; \ slist->level--; \
@ -531,6 +537,31 @@
return 0; \ return 0; \
} \ } \
\ \
/* -- skip_snapshot_ \
* A snapshot is a read-only view of a Skiplist at a point in \
* time. Once taken, a snapshot must be restored or disposed. \
* Any number of snapshots can be created. \
*/ \
int prefix##skip_snapshot_##decl(decl##_t *slist) \
{ \
((void)slist); /* TODO */ \
return 0; \
} \
\
/* -- skip_restore_snapshot_ */ \
int prefix##skip_restore_snapshot_##decl(decl##_t *slist) \
{ \
((void)slist); /* TODO */ \
return 0; \
} \
\
/* -- skip_dispose_snapshot_ */ \
int prefix##skip_dispose_snapshot_##decl(decl##_t *slist) \
{ \
((void)slist); /* TODO */ \
return 0; \
} \
\
/* -- __skip_integrity_check_ */ \ /* -- __skip_integrity_check_ */ \
static int __skip_integrity_check_##decl(decl##_t *slist) \ static int __skip_integrity_check_##decl(decl##_t *slist) \
{ \ { \
@ -573,181 +604,179 @@
return (vtype)0; \ return (vtype)0; \
} }
#define SKIPLIST_DECL_DOT(decl, prefix, field) \ #define SKIPLIST_DECL_DOT(decl, prefix, field) \
\ \
/* A type for a function that writes into a char[2048] buffer \ /* A type for a function that writes into a char[2048] buffer \
* a description of the value within the node. */ \ * a description of the value within the node. */ \
typedef void (*skip_sprintf_node_##decl##_t)(decl##_node_t *, char *); \ typedef void (*skip_sprintf_node_##decl##_t)(decl##_node_t *, char *); \
\ \
/* -- __skip_dot_node_ \ /* -- __skip_dot_node_ \
* Writes out a fragment of a DOT file representing a node. \ * Writes out a fragment of a DOT file representing a node. \
*/ \ */ \
static void __skip_dot_node_##decl(FILE *os, decl##_t *slist, \ static void __skip_dot_node_##decl(FILE *os, decl##_t *slist, \
decl##_node_t *node, size_t nsg, skip_sprintf_node_##decl##_t fn) \ decl##_node_t *node, size_t nsg, skip_sprintf_node_##decl##_t fn) \
{ \ { \
char buf[2048]; \ char buf[2048]; \
size_t level, height = ARRAY_LENGTH(node->field.sle_next); \ size_t level, height = ARRAY_LENGTH(node->field.sle_next) - 1; \
fprintf(os, "\"node%zu%p\"", nsg, (void *)node); \ decl##_node_t *next; \
fprintf(os, " [label = \""); \ \
level = height; \ fprintf(os, "\"node%zu %p\"", nsg, (void *)node); \
do { \ fprintf(os, " [label = \""); \
fprintf(os, " { <w%zu> | <f%zu> %p }", level + 1, level + 1, \ level = height; \
(void *)node->field.sle_next[level]); \ do { \
if (level != 0) \ fprintf(os, " { <w%zu> | <f%zu> %p } |", level, level, \
fprintf(os, " | "); \ (void *)node->field.sle_next[level]); \
} while (level--); \ } while (level--); \
if (fn) { \ if (fn) { \
fn(node, buf); \ fn(node, buf); \
fprintf(os, " <f0> %s\"\n", buf); \ fprintf(os, " <f0> %s\"\n", buf); \
} else { \ } else { \
fprintf(os, " <f0> ?\"\n"); \ fprintf(os, " <f0> ?\"\n"); \
} \ } \
fprintf(os, "shape = \"record\"\n"); \ fprintf(os, "shape = \"record\"\n"); \
fprintf(os, "];\n"); \ fprintf(os, "];\n"); \
\ \
/* Now edges */ \ /* Now edges */ \
level = 0; \ level = 0; \
for (level = 0; level < height; level++) { \ for (level = 0; level < height; level++) { \
fprintf(os, "\"node%zu%p\"", nsg, (void *)node); \ fprintf(os, "\"node%zu %p\"", nsg, (void *)node); \
fprintf(os, ":f%zu -> ", level + 1); \ fprintf(os, ":f%zu -> ", level); \
fprintf(os, "\"node%zu%p\"", nsg, (void *)node->field.sle_next[level]); \ fprintf(os, "\"node%zu %p\"", nsg, (void *)node->field.sle_next[level]); \
fprintf(os, ":w%zu [];\n", level + 1); \ fprintf(os, ":w%zu [];\n", level); \
} \ } \
\ next = prefix##skip_next_node_##decl(slist, node); \
if (node->field.sle_next[0] != SKIP_LAST(slist)) \ if (next) \
__skip_dot_node_##decl(os, slist, node->field.sle_next[0], nsg, fn); \ __skip_dot_node_##decl(os, slist, next, nsg, fn); \
} \ } \
\ \
/* -- __skip_dot_finish_ \ /* -- __skip_dot_finish_ \
* Finalise the DOT file of the internal representation. \ * Finalize the DOT file of the internal representation. \
*/ \ */ \
static void __skip_dot_finish_##decl(FILE *os, size_t nsg) \ static void __skip_dot_finish_##decl(FILE *os, size_t nsg) \
{ \ { \
size_t i; \ size_t i; \
if (nsg > 0) { \ if (nsg > 0) { \
/* Link the nodes together with an invisible node. \ /* Link the nodes together with an invisible node. \
* node0 [shape=record, label = "<f0> | <f1> | <f2> | <f3> | \ * node0 [shape=record, label = "<f0> | <f1> | <f2> | <f3> | \
* <f4> | <f5> | <f6> | <f7> | <f8> | ", style=invis, width=0.01]; \ * <f4> | <f5> | <f6> | <f7> | <f8> | ", style=invis, width=0.01]; \
*/ \ */ \
fprintf(os, "node0 [shape=record, label = \""); \ fprintf(os, "node0 [shape=record, label = \""); \
for (i = 0; i < nsg; ++i) { \ for (i = 0; i < nsg; ++i) { \
fprintf(os, "<f%zu> | ", i); \ fprintf(os, "<f%zu> | ", i); \
} \ } \
fprintf(os, "\", style=invis, width=0.01];\n"); \ fprintf(os, "\", style=invis, width=0.01];\n"); \
\ \
/* Now connect nodes with invisible edges \ /* Now connect nodes with invisible edges \
* \ * \
* node0:f0 -> HeadNode [style=invis]; \ * node0:f0 -> HeadNode [style=invis]; \
* node0:f1 -> HeadNode1 [style=invis]; \ * node0:f1 -> HeadNode1 [style=invis]; \
*/ \ */ \
for (i = 0; i < nsg; ++i) { \ for (i = 0; i < nsg; ++i) { \
fprintf(os, "node0:f%zu -> HeadNode%zu [style=invis];\n", i, i); \ fprintf(os, "node0:f%zu -> HeadNode%zu [style=invis];\n", i, i); \
} \ } \
nsg = 0; \ nsg = 0; \
} \ } \
fprintf(os, "}\n"); \ } \
} \ \
\ /* -- skip_dot_start_ */ \
/* -- 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) \
skip_sprintf_node_##decl##_t fn) \ { \
{ \ size_t level; \
size_t level; \ decl##_node_t *head; \
decl##_node_t *head; \ if (nsg == 0) { \
if (nsg == 0) { \ fprintf(os, "digraph Skiplist {\n"); \
fprintf(os, "digraph Skiplist {\n"); \ fprintf(os, "label = \"Skiplist.\"\n"); \
fprintf(os, "label = \"Skiplist.\"\n"); \ fprintf(os, "graph [rankdir = \"LR\"];\n"); \
fprintf(os, "graph [rankdir = \"LR\"];\n"); \ fprintf(os, "node [fontsize = \"12\" shape = \"ellipse\"];\n"); \
fprintf(os, "node [fontsize = \"12\" shape = \"ellipse\"];\n"); \ fprintf(os, "edge [];\n\n"); \
fprintf(os, "edge [];\n\n"); \ } \
} \ fprintf(os, "subgraph cluster%zu {\n", nsg); \
fprintf(os, "subgraph cluster%zu {\n", nsg); \ fprintf(os, "style=dashed\n"); \
fprintf(os, "style=dashed\n"); \ fprintf(os, "label=\"Skip list iteration %zu\"\n\n", nsg); \
fprintf(os, "label=\"Skip list iteration %zu\"\n\n", nsg); \ fprintf(os, "\"HeadNode%zu\" [\n", nsg); \
fprintf(os, "\"HeadNode%zu\" [\n", nsg); \ fprintf(os, "label = \""); \
fprintf(os, "label = \""); \ \
\ /* Write out the fields */ \
/* Write out the fields */ \ head = slist->slh_head; \
head = slist->slh_head; \ if (SKIP_EMPTY(slist)) \
if (SKIP_EMPTY(slist)) \ fprintf(os, "Empty HeadNode"); \
fprintf(os, "Empty HeadNode"); \ else { \
else { \ level = ARRAY_LENGTH(head->field.sle_next) - 1; \
level = ARRAY_LENGTH(head->field.sle_next); \ do { \
while (level--) { \ decl##_node_t *node = head->field.sle_next[level]; \
decl##_node_t *node = head->field.sle_next[level]; \ fprintf(os, "{ <f%zu> %p }", level, (void *)node); \
fprintf(os, "{ <f%zu> %p }", level + 1, (void *)node); \ if (level) \
if (level + 1 != 0) \ fprintf(os, " | "); \
fprintf(os, " | "); \ } while (level--); \
} \ } \
} \ fprintf(os, "\"\n"); \
fprintf(os, "\"\n"); \ fprintf(os, "shape = \"record\"\n"); \
fprintf(os, "shape = \"record\"\n"); \ fprintf(os, "];\n"); \
fprintf(os, "];\n"); \ \
\ /* Edges for head node */ \
/* Edges for head node */ \ decl##_node_t *node = slist->slh_head; \
decl##_node_t *node = slist->slh_head; \ level = 0; \
level = 0; \ do { \
do { \ fprintf(os, "\"HeadNode%zu\":f%zu -> ", nsg, level); \
fprintf(os, "\"HeadNode%zu\":f%zu -> ", nsg, level + 1); \ fprintf(os, "\"node%zu %p\"", nsg, (void *)node->field.sle_next[level]); \
fprintf(os, "\"node%zu%p\"", nsg, (void *)node->field.sle_next[level]); \ fprintf(os, ":w%zu [];\n", level); \
fprintf(os, ":w%zu [];\n", level + 1); \ } while (++level < slist->level); \
} while (level++ < slist->level); \ fprintf(os, "}\n\n"); \
fprintf(os, "}\n\n"); \ \
\ /* Now all nodes via level 0, if non-empty */ \
/* Now all nodes via level 0, if non-empty */ \ node = prefix##skip_head_##decl(slist); \
node = slist->slh_head; \ if (node) \
if (ARRAY_LENGTH(node->field.sle_next)) \ __skip_dot_node_##decl(os, slist, node->field.sle_next[0], nsg, fn); \
__skip_dot_node_##decl(os, slist, node->field.sle_next[0], nsg, fn); \ fprintf(os, "\n"); \
fprintf(os, "\n"); \ \
\ /* The tail, sentinal node */ \
/* The tail, sentinal node */ \ if (!SKIP_EMPTY(slist)) { \
if (!SKIP_EMPTY(slist)) { \ fprintf(os, "\"node%zu 0x0\" [label = \"", nsg); \
fprintf(os, "\"node%zu0x0\" [label = \"", nsg); \ level = slist->level; \
level = slist->level; \ do { \
do { \ fprintf(os, "<w%zu> NULL", level); \
fprintf(os, "<w%zu> NULL", level + 1); \ if (level) \
if (level != 0) \ fprintf(os, " | "); \
fprintf(os, " | "); \ } while (--level); \
} while (level-- > 0); \ fprintf(os, "\" shape = \"record\"];\n"); \
fprintf(os, "\" shape = \"record\"];\n"); \ } \
} \ \
\ /* End: "subgraph cluster0 {" */ \
/* End: "subgraph cluster1 {" */ \ fprintf(os, "}\n\n"); \
fprintf(os, "}\n\n"); \ nsg += 1; \
nsg += 1; \ \
\ return nsg; \
return 0; \ } \
} \ \
\ /* -- skip_dot_ \
/* -- skip_dot_ \ * Create a DOT file of the internal representation of the \
* Create a DOT file of the internal representation of the \ * Skiplist on the provided file descriptor (default: STDOUT). \
* Skiplist on the provided file descriptor (default: STDOUT). \ * \
* \ * To view the output: \
* To view the output: \ * $ dot -Tps filename.dot -o outfile.ps \
* $ dot -Tps filename.dot -o outfile.ps \ * You can change the output format by varying the value after -T and \
* You can change the output format by varying the value after -T and \ * choosing an appropriate filename extension after -o. \
* choosing an appropriate filename extension after -o. \ * See: https://graphviz.org/docs/outputs/ for the format options. \
* See: https://graphviz.org/docs/outputs/ for the format options. \ * \
* \ * https://en.wikipedia.org/wiki/DOT_(graph_description_language) \
* https://en.wikipedia.org/wiki/DOT_(graph_description_language) \ */ \
*/ \ int prefix##skip_dot_##decl(FILE *os, decl##_t *slist, size_t nsg, \
int prefix##skip_dot_##decl(FILE *os, decl##_t *slist, \ skip_sprintf_node_##decl##_t fn) \
skip_sprintf_node_##decl##_t fn) \ { \
{ \ if (__skip_integrity_check_##decl(slist) != 0) { \
size_t nsg = 0; \ perror("Skiplist failed integrity checks, impossible to diagram."); \
if (__skip_integrity_check_##decl(slist) != 0) { \ return -1; \
perror("Skiplist failed integrity checks, impossible to diagram."); \ } \
return -1; \ if (os == NULL) \
} \ os = stdout; \
if (os == NULL) \ if (!os) { \
os = stdout; \ perror("Failed to open output file, unable to write DOT file."); \
if (!os) { \ return -1; \
perror("Failed to open output file, unable to write DOT file."); \ } \
return -1; \ __skip_dot_start_##decl(os, slist, nsg, fn); \
} \ __skip_dot_finish_##decl(os, nsg); \
__skip_dot_start_##decl(os, slist, nsg, fn); \ return 0; \
__skip_dot_finish_##decl(os, nsg); \
return 0; \
} }
#endif /* _SKIPLIST_H_ */ #endif /* _SKIPLIST_H_ */