rm cap, s/len/size/g in sle; comments; cmp fn type; contains api; dot fixes

This commit is contained in:
Gregory Burd 2024-03-22 10:18:38 -04:00
parent 7c4789e71d
commit 5481b99725

View file

@ -132,18 +132,26 @@
*/ */
/* /*
* Skiplist declarations. * Skip List declarations.
*/ */
#ifndef SKIPLIST_MAX_HEIGHT #ifndef SKIPLIST_MAX_HEIGHT
#define SKIPLIST_MAX_HEIGHT 1 #define SKIPLIST_MAX_HEIGHT 1
#endif #endif
/*
* A Skip List contains elements, a portion of which is used to manage those
* elements while the rest is defined by the use case for this declaration. The
* house keeping portion is the SKIPLIST_ENTRY below. It maintains the array of
* forward pointers to nodes and has a height (a zero-based count of levels, so
* a height of `0` means one (1) level and a height of `4` means five (5)
* levels).
*/
#define SKIPLIST_ENTRY(type) \ #define SKIPLIST_ENTRY(type) \
struct __skiplist_entry { \ struct __skiplist_entry { \
struct __skiplist_idx { \ struct __skiplist_idx { \
struct type *prev, **next; \ struct type *prev, **next; \
size_t cap, len; \ size_t height; \
} sle; \ } sle; \
} }
@ -152,15 +160,65 @@
*/ */
#define SKIP_FIRST(head) ((head)->slh_head) #define SKIP_FIRST(head) ((head)->slh_head)
#define SKIP_LAST(head) ((head)->slh_tail) #define SKIP_LAST(head) ((head)->slh_tail)
#define SKIP_NEXT(elm, field) ((elm)->field.sle.next[0])
#define SKIP_PREV(elm, field) ((elm)->field.sle.prev)
#define SKIP_EMPTY(head) ((head)->length == 0)
/* /*
* Skip List functions. * Skip List node comparison function. This macro builds a function used when
* comparing two nodes for equality. A portion of this function, `fn_blk`, is
* the code you supply to compare two nodes as a block (starts with `{`, your
* code, then `}`) that should compare the node referenced by `a` to the node
* `b` as follows:
*
* When:
* *a < *b : return -1
* *a == *b : return 0
* *a > *b : return 1
*
* As stated earlier, the ordered skiplist relies on a well-behaved comparison
* function. Specifically, given some ordering function f(a, b), it must satisfy
* the following properties:
*
* 1) Be well defined: f(a, b) should always return the same value
* 2) Be anti-symmetric: f(a, b) == Greater if and only if f(b, a) == Less, and
* f(a, b) == Equal == f(b, a).
* 3) Be transitive: If f(a, b) == Greater and f(b, c) == Greater then f(a, c)
* == Greater.
*
* Failure to satisfy these properties can result in unexpected behavior at
* best, and at worst will cause a segfault, null deref, or some other bad
* behavior.
*
* Example for nodes with keys that are signed integeters (`int`):
* {
* (void)list;
* (void)aux;
* if (a->key < b->key)
* return -1;
* if (a->key > b->key)
* return 1;
* return 0;
* }
*
* Note that the comparison function can also make use of the reference to the
* list as well as a reference to a variable `aux` that you can point to
* anything else required to perform your comparison. The auxiliary pointer
* is unused for other purposes. You could even use it a pointer to a function
* that chooses the proper comparison function for the two nodes in question.
*
* Example where the value of `decl` below is `ex`:
* {
* // Cast `aux` to a function that returns a function that properly compares
* // `a` and `b`, for example if they were objects or different structs.
* (skip_ex_cmp_t *(fn)(ex_node_t *, ex_node_t *)) =
* (skip_ex_cmp_t *()(ex_node_t *, ex_node_t *))aux;
*
* // Use the `fn` pointed to by `aux` to get the comparison function.
* skip_ex_cmp_t *cmp = fn(a, b);
*
* // Now call that function and return the proper result.
* return cmp(list, a, b, aux);
* }
*/ */
#define SKIP_COMPARATOR(list, type, fn_blk) \
#define SKIP_COMPARATOR(list, type, fn) \
int __skip_cmp_##type(struct list *head, struct type *a, struct type *b, void *aux) \ int __skip_cmp_##type(struct list *head, struct type *a, struct type *b, void *aux) \
{ \ { \
if (a == b) \ if (a == b) \
@ -169,15 +227,18 @@
return -1; \ return -1; \
if (a == (head)->slh_tail || b == (head)->slh_head) \ if (a == (head)->slh_tail || b == (head)->slh_head) \
return 1; \ return 1; \
fn \ fn_blk \
} }
/*
* Skip List declarations and access methods.
*/
#define SKIPLIST_DECL(decl, prefix, field, free_node_blk, update_node_blk, snap_node_blk, sizeof_entry_blk) \ #define SKIPLIST_DECL(decl, prefix, field, free_node_blk, update_node_blk, snap_node_blk, sizeof_entry_blk) \
\ \
/* Skiplist node type */ \ /* Skip List node type */ \
typedef struct decl##_node decl##_node_t; \ typedef struct decl##_node decl##_node_t; \
\ \
/* Skiplist type */ \ /* Skip List structure and type */ \
typedef struct decl { \ typedef struct decl { \
size_t level, length, max; \ size_t level, length, max; \
int (*cmp)(struct decl *, decl##_node_t *, decl##_node_t *, void *); \ int (*cmp)(struct decl *, decl##_node_t *, decl##_node_t *, void *); \
@ -186,27 +247,23 @@
decl##_node_t *slh_tail; \ decl##_node_t *slh_tail; \
} decl##_t; \ } decl##_t; \
\ \
/* Snapshot of a Skiplist */ \ /* Snapshot of a Skip List */ \
typedef struct decl##_snap { \ typedef struct decl##_snap { \
decl##_t list; \ decl##_t list; \
decl##_node_t *nodes; \ decl##_node_t *nodes; \
size_t bytes; \ size_t bytes; \
} decl##_snap_t; \ } decl##_snap_t; \
\ \
/* Path taken to find an element in the Skiplist, path[0] is the match if \ /* Skip List comparison function type */ \
one exists */ \ typedef int (*skip_##decl##_cmp_t)(decl##_t *, decl##_node_t *, decl##_node_t *, void *); \
struct __##decl##_path { \
size_t cap; \
size_t len; \
struct decl##_node **nodes; \
}; \
\ \
/* Used when positioning a cursor within a Skip List. */ \
typedef enum { SKIP_EQ = 0, SKIP_LTE = -1, SKIP_LT = -2, SKIP_GTE = 1, SKIP_GT = 2 } skip_pos_##decl_t; \ typedef enum { SKIP_EQ = 0, SKIP_LTE = -1, SKIP_LT = -2, SKIP_GTE = 1, SKIP_GT = 2 } skip_pos_##decl_t; \
\ \
/* -- __skip_key_compare_ \ /* -- __skip_key_compare_ \
* \ * \
* This function takes four arguments: \ * This function takes four arguments: \
* - a reference to the Skiplist \ * - a reference to the Skip List \
* - the two nodes to compare, `a` and `b` \ * - the two nodes to compare, `a` and `b` \
* - `aux` an additional auxiliary argument \ * - `aux` an additional auxiliary argument \
* and returns: \ * and returns: \
@ -227,7 +284,7 @@
\ \
/* -- __skip_toss_ \ /* -- __skip_toss_ \
* A "coin toss" function that is critical to the proper operation of the \ * A "coin toss" function that is critical to the proper operation of the \
* Skiplist. For example, when `max = 6` this function returns 0 with \ * Skip List. For example, when `max = 6` this function returns 0 with \
* probability 0.5, 1 with 0.25, 2 with 0.125, etc. until 6 with 0.5^7. \ * probability 0.5, 1 with 0.25, 2 with 0.125, etc. until 6 with 0.5^7. \
*/ \ */ \
static int __skip_toss_##decl(size_t max) \ static int __skip_toss_##decl(size_t max) \
@ -253,8 +310,7 @@
n = (decl##_node_t *)calloc(1, sizeof(decl##_node_t) + sle_arr_sz); \ n = (decl##_node_t *)calloc(1, sizeof(decl##_node_t) + sle_arr_sz); \
if (n == NULL) \ if (n == NULL) \
return ENOMEM; \ return ENOMEM; \
n->field.sle.cap = slist->max; \ n->field.sle.height = 0; \
n->field.sle.len = 0; \
n->field.sle.next = (decl##_node_t **)((uintptr_t)n + sizeof(decl##_node_t)); \ n->field.sle.next = (decl##_node_t **)((uintptr_t)n + sizeof(decl##_node_t)); \
*node = n; \ *node = n; \
return 0; \ return 0; \
@ -281,12 +337,12 @@
if (rc) \ if (rc) \
goto fail; \ goto fail; \
\ \
slist->slh_head->field.sle.len = 0; \ slist->slh_head->field.sle.height = 0; \
for (i = 0; i < slist->max; i++) \ for (i = 0; i < slist->max; i++) \
slist->slh_head->field.sle.next[i] = slist->slh_tail; \ slist->slh_head->field.sle.next[i] = slist->slh_tail; \
slist->slh_head->field.sle.prev = NULL; \ slist->slh_head->field.sle.prev = NULL; \
\ \
slist->slh_tail->field.sle.len = slist->max; \ slist->slh_tail->field.sle.height = slist->max; \
for (i = 0; i < slist->max; i++) \ for (i = 0; i < slist->max; i++) \
slist->slh_tail->field.sle.next[i] = NULL; \ slist->slh_tail->field.sle.next[i] = NULL; \
slist->slh_tail->field.sle.prev = slist->slh_head; \ slist->slh_tail->field.sle.prev = slist->slh_head; \
@ -349,7 +405,7 @@
return 0; \ return 0; \
\ \
/* Find the node that matches `node` or NULL. */ \ /* Find the node that matches `node` or NULL. */ \
i = slist->slh_head->field.sle.len; \ i = slist->slh_head->field.sle.height; \
do { \ do { \
while (elm && __skip_key_compare_##decl(slist, elm->field.sle.next[i], n, slist->aux) < 0) \ while (elm && __skip_key_compare_##decl(slist, elm->field.sle.next[i], n, slist->aux) < 0) \
elm = elm->field.sle.next[i]; \ elm = elm->field.sle.next[i]; \
@ -388,9 +444,9 @@
return -1; \ return -1; \
} \ } \
level = __skip_toss_##decl(slist->max - 1); \ level = __skip_toss_##decl(slist->max - 1); \
n->field.sle.len = level + 1; \ n->field.sle.height = level + 1; \
for (i = 0; i <= level; i++) { \ for (i = 0; i <= level; i++) { \
if (i <= slist->slh_head->field.sle.len) { \ if (i <= slist->slh_head->field.sle.height) { \
n->field.sle.next[i] = path[i + 1]->field.sle.next[i]; \ n->field.sle.next[i] = path[i + 1]->field.sle.next[i]; \
path[i + 1]->field.sle.next[i] = n; \ path[i + 1]->field.sle.next[i] = n; \
} else { \ } else { \
@ -402,9 +458,9 @@
if (n->field.sle.next[0] == slist->slh_tail) { \ if (n->field.sle.next[0] == slist->slh_tail) { \
slist->slh_tail->field.sle.prev = n; \ slist->slh_tail->field.sle.prev = n; \
} \ } \
if (level > slist->slh_head->field.sle.len) { \ if (level > slist->slh_head->field.sle.height) { \
slist->slh_head->field.sle.len = level; \ slist->slh_head->field.sle.height = level; \
slist->slh_tail->field.sle.len = level; \ slist->slh_tail->field.sle.height = level; \
} \ } \
slist->length++; \ slist->length++; \
\ \
@ -441,7 +497,7 @@
if (slist == NULL || n == NULL) \ if (slist == NULL || n == NULL) \
return NULL; \ return NULL; \
\ \
i = slist->slh_head->field.sle.len; \ i = slist->slh_head->field.sle.height; \
\ \
do { \ do { \
while (elm && __skip_key_compare_##decl(slist, elm->field.sle.next[i], n, slist->aux) < 0) \ while (elm && __skip_key_compare_##decl(slist, elm->field.sle.next[i], n, slist->aux) < 0) \
@ -471,7 +527,7 @@
if (slist == NULL || n == NULL) \ if (slist == NULL || n == NULL) \
return NULL; \ return NULL; \
\ \
i = slist->slh_head->field.sle.len; \ i = slist->slh_head->field.sle.height; \
\ \
do { \ do { \
while (elm && __skip_key_compare_##decl(slist, elm->field.sle.next[i], n, slist->aux) < 0) \ while (elm && __skip_key_compare_##decl(slist, elm->field.sle.next[i], n, slist->aux) < 0) \
@ -501,7 +557,7 @@
if (slist == NULL || n == NULL) \ if (slist == NULL || n == NULL) \
return NULL; \ return NULL; \
\ \
i = slist->slh_head->field.sle.len; \ i = slist->slh_head->field.sle.height; \
\ \
do { \ do { \
while (elm && __skip_key_compare_##decl(slist, elm->field.sle.next[i], n, slist->aux) < 0) \ while (elm && __skip_key_compare_##decl(slist, elm->field.sle.next[i], n, slist->aux) < 0) \
@ -531,7 +587,7 @@
if (slist == NULL || n == NULL) \ if (slist == NULL || n == NULL) \
return NULL; \ return NULL; \
\ \
i = slist->slh_head->field.sle.len; \ i = slist->slh_head->field.sle.height; \
\ \
do { \ do { \
while (elm && __skip_key_compare_##decl(slist, elm->field.sle.next[i], n, slist->aux) < 0) \ while (elm && __skip_key_compare_##decl(slist, elm->field.sle.next[i], n, slist->aux) < 0) \
@ -565,7 +621,7 @@
if (slist == NULL || n == NULL) \ if (slist == NULL || n == NULL) \
return NULL; \ return NULL; \
\ \
i = slist->slh_head->field.sle.len; \ i = slist->slh_head->field.sle.height; \
\ \
do { \ do { \
while (elm && __skip_key_compare_##decl(slist, elm->field.sle.next[i], n, slist->aux) < 0) \ while (elm && __skip_key_compare_##decl(slist, elm->field.sle.next[i], n, slist->aux) < 0) \
@ -631,7 +687,7 @@
return -1; \ return -1; \
} \ } \
\ \
/* -- skip_remove_ BUG: prev pointer wrong!*/ \ /* -- skip_remove_ */ \
int prefix##skip_remove_##decl(decl##_t *slist, decl##_node_t *n) \ int prefix##skip_remove_##decl(decl##_t *slist, decl##_node_t *n) \
{ \ { \
static decl##_node_t apath[SKIPLIST_MAX_HEIGHT + 1]; \ static decl##_node_t apath[SKIPLIST_MAX_HEIGHT + 1]; \
@ -659,8 +715,8 @@
break; \ break; \
path[i]->field.sle.next[i - 1] = node->field.sle.next[i - 1]; \ path[i]->field.sle.next[i - 1] = node->field.sle.next[i - 1]; \
if (path[i]->field.sle.next[i - 1] == slist->slh_tail) { \ if (path[i]->field.sle.next[i - 1] == slist->slh_tail) { \
level = path[i]->field.sle.len; \ level = path[i]->field.sle.height; \
path[i]->field.sle.len = level - 1; \ path[i]->field.sle.height = level - 1; \
} \ } \
} \ } \
if (node->field.sle.next[0] == slist->slh_tail) { \ if (node->field.sle.next[0] == slist->slh_tail) { \
@ -674,10 +730,10 @@
at the tail and shrink the level. */ \ at the tail and shrink the level. */ \
i = 0; \ i = 0; \
node = slist->slh_head; \ node = slist->slh_head; \
while (node->field.sle.next[i] != slist->slh_tail && i++ < slist->slh_head->field.sle.len) \ while (node->field.sle.next[i] != slist->slh_tail && i++ < slist->slh_head->field.sle.height) \
; \ ; \
slist->slh_head->field.sle.len = i; \ slist->slh_head->field.sle.height = i; \
slist->slh_tail->field.sle.len = i; \ slist->slh_tail->field.sle.height = i; \
slist->length--; \ slist->length--; \
} \ } \
return 0; \ return 0; \
@ -725,7 +781,7 @@
} \ } \
\ \
/* -- skip_snapshot_ TODO/WIP \ /* -- skip_snapshot_ TODO/WIP \
* A snapshot is a read-only view of a Skiplist at a point in \ * A snapshot is a read-only view of a Skip List at a point in \
* time. Once taken, a snapshot must be restored or disposed. \ * time. Once taken, a snapshot must be restored or disposed. \
* Any number of snapshots can be created. \ * Any number of snapshots can be created. \
* \ * \
@ -786,12 +842,12 @@
if (rc) \ if (rc) \
goto fail; \ goto fail; \
\ \
slist->slh_head->field.sle.len = 0; \ slist->slh_head->field.sle.height = 0; \
for (i = 0; i < slist->max; i++) \ for (i = 0; i < slist->max; i++) \
slist->slh_head->field.sle.next[i] = slist->slh_tail; \ slist->slh_head->field.sle.next[i] = slist->slh_tail; \
slist->slh_head->field.sle.prev = NULL; \ slist->slh_head->field.sle.prev = NULL; \
\ \
slist->slh_tail->field.sle.len = slist->max; \ slist->slh_tail->field.sle.height = slist->max; \
for (i = 0; i < slist->max; i++) \ for (i = 0; i < slist->max; i++) \
slist->slh_tail->field.sle.next[i] = NULL; \ slist->slh_tail->field.sle.next[i] = NULL; \
slist->slh_tail->field.sle.prev = slist->slh_head; \ slist->slh_tail->field.sle.prev = slist->slh_head; \
@ -893,12 +949,12 @@
if (rc) \ if (rc) \
goto fail; \ goto fail; \
\ \
slist->slh_head->field.sle.len = 0; \ slist->slh_head->field.sle.height = 0; \
for (i = 0; i < slist->max; i++) \ for (i = 0; i < slist->max; i++) \
slist->slh_head->field.sle.next[i] = slist->slh_tail; \ slist->slh_head->field.sle.next[i] = slist->slh_tail; \
slist->slh_head->field.sle.prev = NULL; \ slist->slh_head->field.sle.prev = NULL; \
\ \
slist->slh_tail->field.sle.len = slist->max; \ slist->slh_tail->field.sle.height = slist->max; \
for (i = 0; i < slist->max; i++) \ for (i = 0; i < slist->max; i++) \
slist->slh_tail->field.sle.next[i] = NULL; \ slist->slh_tail->field.sle.next[i] = NULL; \
slist->slh_tail->field.sle.prev = slist->slh_head; \ slist->slh_tail->field.sle.prev = slist->slh_head; \
@ -941,6 +997,17 @@
return (vtype)0; \ return (vtype)0; \
} \ } \
\ \
int prefix##skip_contains_##decl(decl##_t *slist, ktype key) \
{ \
decl##_node_t *node, query; \
\
qblk; \
node = prefix##skip_position_eq_##decl(slist, &query); \
if (node) \
return 1; \
return 0; \
} \
\
decl##_node_t *prefix##skip_pos_##decl(decl##_t *slist, skip_pos_##decl_t op, ktype key) \ decl##_node_t *prefix##skip_pos_##decl(decl##_t *slist, skip_pos_##decl_t op, ktype key) \
{ \ { \
decl##_node_t *node, query; \ decl##_node_t *node, query; \
@ -1029,7 +1096,7 @@
static void __skip_dot_node_##decl(FILE *os, decl##_t *slist, decl##_node_t *node, size_t nsg, skip_sprintf_node_##decl##_t fn) \ static void __skip_dot_node_##decl(FILE *os, decl##_t *slist, decl##_node_t *node, size_t nsg, skip_sprintf_node_##decl##_t fn) \
{ \ { \
char buf[2048]; \ char buf[2048]; \
size_t level, width, height = node->field.sle.len; \ size_t level, width, height = node->field.sle.height; \
decl##_node_t *next; \ decl##_node_t *next; \
\ \
fprintf(os, "\"node%zu %p\"", nsg, (void *)node); \ fprintf(os, "\"node%zu %p\"", nsg, (void *)node); \
@ -1093,7 +1160,7 @@
\ \
/* -- 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). \ * Skip List 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 \
@ -1132,8 +1199,8 @@
fprintf(os, "Empty HeadNode"); \ fprintf(os, "Empty HeadNode"); \
else { \ else { \
node = slist->slh_head->field.sle.next[0]; \ node = slist->slh_head->field.sle.next[0]; \
level = slist->slh_head->field.sle.len; \ level = slist->slh_head->field.sle.height; \
next = node->field.sle.next[level] == NULL ? slist->slh_tail : node->field.sle.next[level]; \ next = node->field.sle.next[level] == NULL ? slist->slh_tail : node->field.sle.next[level]; /*TODO*/ \
do { \ do { \
width = __skip_dot_width_##decl(slist, node, level); \ width = __skip_dot_width_##decl(slist, node, level); \
fprintf(os, "{ %zu | <f%zu> %p }", width, level, (void *)next); \ fprintf(os, "{ %zu | <f%zu> %p }", width, level, (void *)next); \
@ -1147,7 +1214,7 @@
\ \
/* Edges for head node */ \ /* Edges for head node */ \
node = slist->slh_head; \ node = slist->slh_head; \
for (level = 0; level < slist->slh_head->field.sle.len; level++) { \ for (level = 0; level < slist->slh_head->field.sle.height; level++) { \
if (node->field.sle.next[level] == slist->slh_tail) \ if (node->field.sle.next[level] == slist->slh_tail) \
break; \ break; \
fprintf(os, "\"HeadNode%zu\":f%zu -> ", nsg, level); \ fprintf(os, "\"HeadNode%zu\":f%zu -> ", nsg, level); \
@ -1166,7 +1233,7 @@
node = slist->slh_tail; \ node = slist->slh_tail; \
if (prefix##skip_size_##decl(slist) > 0) { \ if (prefix##skip_size_##decl(slist) > 0) { \
fprintf(os, "\"node%zu %p\" [label = \"", nsg, (void *)slist->slh_tail); \ fprintf(os, "\"node%zu %p\" [label = \"", nsg, (void *)slist->slh_tail); \
level = node->field.sle.len; \ level = node->field.sle.height; \
do { \ do { \
fprintf(os, "<w%zu> %p", level, (void *)node); \ fprintf(os, "<w%zu> %p", level, (void *)node); \
if (level && node->field.sle.prev != slist->slh_head) \ if (level && node->field.sle.prev != slist->slh_head) \