From 8b71b98bd71cb474bd6d03dcffc8a8c7419c66e8 Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Fri, 24 May 2024 22:11:03 -0400 Subject: [PATCH 01/11] splay pt1 --- examples/ex1.c | 2 +- include/sl.h | 126 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+), 1 deletion(-) diff --git a/examples/ex1.c b/examples/ex1.c index d611bc5..773d469 100644 --- a/examples/ex1.c +++ b/examples/ex1.c @@ -18,7 +18,7 @@ // Local demo application OPTIONS: // --------------------------------------------------------------------------- -#define TEST_ARRAY_SIZE 10 +#define TEST_ARRAY_SIZE 100 #define VALIDATE //define SNAPSHOTS //define TODO_RESTORE_SNAPSHOTS diff --git a/include/sl.h b/include/sl.h index 6e24806..91ee9a1 100644 --- a/include/sl.h +++ b/include/sl.h @@ -217,6 +217,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li struct decl##_node *sle_prev; \ struct __skiplist_##decl##_level { \ struct decl##_node *next; \ + size_t hits; \ } *sle_levels; \ } @@ -238,6 +239,12 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li #define __SKIP_ENTRIES_B2T_FROM(field, elm, off) for (size_t lvl = off; lvl <= elm->field.sle_height; lvl++) #define __SKIP_IS_LAST_ENTRY_B2T() if (lvl + 1 == elm->field.sle_height) +/* Iterate over the subtree to the left (v, or 'lt') and right (u) or "CHu" and "CHv". */ +#define __SKIP_SUBTREE_CHv(decl, field, list, path, nth) \ + for (decl##_node_t *elm = path[nth].node; elm->field.sle_levels[path[nth].in].next == path[nth].node; elm = elm->field.sle_prev) +#define __SKIP_SUBTREE_CHu(decl, field, list, path, nth) \ + for (decl##_node_t *elm = path[nth].node; elm != path[nth].node->field.sle_levels[0].next; elm = elm->field.sle_levels[0].next) + /* * Skiplist declarations and access methods. */ @@ -279,6 +286,8 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li \ typedef struct __skiplist_path_##decl { \ decl##_node_t *node; /* node traversed in the act of location */ \ + size_t in; /* level at which the node was intersected */ \ + size_t pu; /* sum of hits from intersection to level[1] */ \ } __skiplist_path_##decl##_t; \ \ /* Xorshift algorithm for PRNG */ \ @@ -604,6 +613,110 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li return nodes; \ } \ \ + /** \ + * -- __skip_adjust_hit_counts_ TODO \ + * \ + * On delete we check the hit counts across all nodes and next[] pointers \ + * and find the smallest counter then subtract that + 1 from all hit \ + * counters. \ + * \ + */ \ + static void __skip_adjust_hit_counts_##decl(decl##_t *slist) \ + { \ + ((void)slist); \ + } \ + \ + /** \ + * -- __skip_rebalance_ \ + * \ + * Restore balance to our list by adjusting heights and forward pointers \ + * according to the algorithm put forth in "The Splay-List: A \ + * Distribution-Adaptive Concurrent Skip-List". \ + * \ + */ \ + static void __skip_rebalance_##decl(decl##_t *slist, size_t len, __skiplist_path_##decl##_t path[]) \ + { \ + size_t i, j, u_hits, hits_CHu = 0, hits_CHv = 0, delta_height, new_height, cur_hits, prev_hits; \ + double k_threshold, m_total_hits, asc_cond, dsc_cond; \ + \ + /* return; TODO/WIP */ \ + /* Total hits, `k`, accross all nodes. */ \ + m_total_hits = slist->slh_head->field.sle_levels[slist->slh_head->field.sle_height].hits; \ + \ + /* Height of the head node, should be close to floor(log(max_height)). */ \ + k_threshold = slist->slh_head->field.sle_height + 1; \ + \ + /* Moving backwards along the path... \ + * - path[0] contains a match, if there was one \ + * - path[1..len] will be the nodes traversed along the way \ + * - path[len] is where the locate() terminated, just before path[0] \ + * if there was a match \ + */ \ + for (i = 1; i < len; i++) { \ + if (path[i].node == slist->slh_head || path[i].node == slist->slh_tail) \ + continue; \ + \ + __SKIP_SUBTREE_CHu(decl, field, slist, path, i) \ + { \ + hits_CHu += elm->field.sle_levels[i].hits; \ + } \ + __SKIP_SUBTREE_CHv(decl, field, slist, path, i) \ + { \ + hits_CHv += elm->field.sle_levels[i].hits; \ + } \ + u_hits = hits_CHu + hits_CHv; \ + \ + /* (a) Check the decent condition: \ + * u_hits <= m_total_hits / (2 ^ (k_threshold - height of node)) \ + * When met should induce: \ + * 1) traverse the path backward, and... \ + * 2) propagate path[i].level[i] hits backward along path, and... \ + * 3) adjust any forward pointers along the way, then... \ + * 4) lower the path[i]'s node height by 1 \ + */ \ + delta_height = k_threshold - path[i].node->field.sle_height + 1; \ + dsc_cond = m_total_hits / pow(2.0, delta_height); \ + if (u_hits <= dsc_cond && path[i].node->field.sle_height > 0) { \ + if (path[i - 1].node->field.sle_prev != slist->slh_head) { \ + /* 1) go backwards along path from where we are until head */ \ + j = i; \ + cur_hits = path[j].node->field.sle_levels[path[j].in].hits; \ + do { \ + /* 2) propagate hits */ \ + prev_hits = path[j - 1].node->field.sle_levels[path[j - 1].in].hits; \ + path[j - 1].node->field.sle_levels[path[j - 1].in].hits += cur_hits; \ + cur_hits = prev_hits; \ + /* 3) adjust forward pointers */ \ + if (path[j - 1].node->field.sle_levels[j].next == path[i].node) \ + path[j - 1].node->field.sle_levels[j].next = path[i].node->field.sle_levels[j].next; \ + } while (j-- > 1); \ + /* 4) reduce height by one */ \ + path[i].node->field.sle_height--; \ + } \ + } \ + /* (b) Check the ascent condition: \ + * path[i].pu + node_hits > hits total / (2 ^ (height of head - height of node - 1)) \ + * When met should induce: \ + * 1) check the ascent condition, then iff true ... \ + * 2) add a level, and ... \ + * 3) set its hits to the prev node at intersection height \ + * 4) set prev node hits to 0 and forward to this new level \ + */ \ + /* 1) check ascent condition */ \ + asc_cond = m_total_hits / pow(2.0, delta_height == 0 ? 0 : delta_height - 1); \ + if (path[i - 1].pu > asc_cond && path[i].node->field.sle_height < slist->slh_max_height - 1) { \ + /* 2) increase height by one */ \ + new_height = path[i].node->field.sle_height++; \ + /* 3) update hit counter */ \ + path[i].node->field.sle_levels[new_height].hits += path[i - 1].node->field.sle_levels[path[i - 1].in].hits; \ + /* 4) reset the prev node hits to 0 */ \ + path[i - 1].node->field.sle_levels[path[i - 1].in].hits = 0; \ + if (path[i - 1].in != 0) \ + path[i - 1].node->field.sle_levels[path[i - 1].in].next->field.sle_levels[path[i - 1].in].next = path[i].node; \ + } \ + } \ + } \ + \ /** \ * -- __skip_locate_ \ * \ @@ -624,16 +737,22 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li /* Find the node that matches `node` or NULL. */ \ i = slist->slh_head->field.sle_height; \ do { \ + path[i + 1].pu = 0; \ while (elm != slist->slh_tail && elm->field.sle_levels[i].next && \ __skip_compare_nodes_##decl(slist, elm->field.sle_levels[i].next, n, slist->slh_aux) < 0) { \ elm = elm->field.sle_levels[i].next; \ + path[i + 1].in = i; \ + path[i + 1].pu += elm->field.sle_levels[path[i + 1].in].hits; \ } \ path[i + 1].node = elm; \ + path[i + 1].node->field.sle_levels[path[i + 1].in].hits++; \ len++; \ } while (i--); \ elm = elm->field.sle_levels[0].next; \ if (__skip_compare_nodes_##decl(slist, elm, n, slist->slh_aux) == 0) { \ path[0].node = elm; \ + path[0].node->field.sle_levels[0].hits++; \ + __skip_rebalance_##decl(slist, len, path); \ } \ return len; \ } \ @@ -664,8 +783,12 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li } \ /* First element in path should be NULL, reset should start pointing at tail. */ \ path[0].node = NULL; \ + path[0].in = 0; \ + path[0].pu = 0; \ for (i = 1; i < slist->slh_max_height + 1; i++) { \ path[i].node = slist->slh_tail; \ + path[i].in = 0; \ + path[i].pu = 0; \ } \ \ /* Find a `path` to `new` in the list and a match (`path[0]`) if it exists. */ \ @@ -725,6 +848,8 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li /* Increase the the list's era/age and record it. */ \ new->field.sle_era = slist->slh_snap.cur_era++; \ } \ + /* Set hits for rebalencing to 1 when new born. */ \ + new->field.sle_levels[new_height].hits = 1; \ /* Increase our list length (aka. size, count, etc.) by one. */ \ slist->slh_length++; \ \ @@ -1088,6 +1213,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li slist->slh_tail->field.sle_height = i; \ \ slist->slh_length--; \ + __skip_adjust_hit_counts_##decl(slist); \ } \ return 0; \ } \ -- 2.43.4 From c5529b6677a357e8c3d599ee827796dfb6581f56 Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Fri, 24 May 2024 22:55:52 -0400 Subject: [PATCH 02/11] docs --- include/sl.h | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/include/sl.h b/include/sl.h index 91ee9a1..f6b3adf 100644 --- a/include/sl.h +++ b/include/sl.h @@ -53,20 +53,23 @@ #define _SKIPLIST_H_ /* - * This file defines a skiplist data structure written in C. Implemented as + * This file defines a skip-list data structure written in C. Implemented as * using macros this code provides a way to essentially "template" (as in C++) * and emit code with types and functions specific to your use case. You can - * apply these macros multiple times safely in your code, once for each - * application. + * apply these macros multiple times safely, once for each list type you need. * - * A skiplist is a sorted list with O(log(n)) on average for most operations. + * A skip-list is a sorted list with O(log(n)) on average for most operations. * It is a probabilistic datastructure, meaning that it does not guarantee - * O(log(n)) it approximates it over time. This implementation improves the - * probability by integrating the splay list algorithm for rebalancing trading - * off a bit of computational overhead and code complexity for a nearly always - * optimal, or "perfect" skiplist. + * O(log(n)), but it has been shown to approximate it over time. This + * implementation includes the rebalancing techniques that improve on that + * approximation using an adaptive technique called "splay-list" (named after + * the splay-tree due to the similarities between the two). It is similar to a + * standard skip-list, with the key distinction that the height of each element + * adapts dynamically to its access rate: popular elements increase in height, + * whereas rarely-accessed elements decrease in height. See below for the link + * to the research behind this adaptive technique. * - * Conceptually, a skiplist is arranged as follows: + * Conceptually, at a high level, a skip-list is arranged as follows: * * ----------> [2] --------------------------------------------------> [9] ----------> * ----------> [2] ------------------------------------[7] ----------> [9] ----------> @@ -79,22 +82,22 @@ * diagram). This allows for the algorithm to move down the list faster than * having to visit every element. * - * Conceptually, the skiplist can be thought of as a stack of linked lists. At - * the very bottom is the full linked list with every element, and each layer - * above corresponds to a linked list containing a random subset of the elements - * from the layer immediately below it. The probability distribution that - * determines this random subset can be customized, but typically a layer will - * contain half the nodes from the layer below. + * A skip-list can be thought of as a stack of linked lists. At the very bottom + * is a linked list with every element, and each layer above corresponds to a + * linked list containing a random subset of the elements from the layer + * immediately below it. The probability distribution that determines this + * random subset can be customized, but typically a layer will contain half the + * nodes from the layer below. * * This implementation maintains a doubly-linked list at the bottom layer to - * support efficient iteration in either direction. There is also a guard - * node at the tail rather than simply pointing to NULL. + * support efficient iteration in either direction. There is also a guard node + * at the tail rather than simply pointing to NULL. * * <-> [1] <-> [2] <-> [3] <-> [4] <-> [5] <-> [6] <-> [7] <-> * * Safety: * - * The ordered skiplist relies on a well-behaved comparison + * The ordered skip-list relies on a well-behaved comparison * function. Specifically, given some ordering function f(a, b), it must satisfy * the following properties: * @@ -639,7 +642,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li size_t i, j, u_hits, hits_CHu = 0, hits_CHv = 0, delta_height, new_height, cur_hits, prev_hits; \ double k_threshold, m_total_hits, asc_cond, dsc_cond; \ \ - /* return; TODO/WIP */ \ + /* return; TODO/WIP */ \ /* Total hits, `k`, accross all nodes. */ \ m_total_hits = slist->slh_head->field.sle_levels[slist->slh_head->field.sle_height].hits; \ \ -- 2.43.4 From 1356b023035c9df264f093e8374a4387b5ddbc72 Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Sat, 25 May 2024 21:34:43 -0400 Subject: [PATCH 03/11] progress --- .gitignore | 3 + Makefile | 8 +- examples/ex1.c | 13 +- examples/ex2.c | 483 ++++++++++++++++++++++++++++++++++ include/sl.h | 700 ++++++++++++++++++++++--------------------------- 5 files changed, 819 insertions(+), 388 deletions(-) create mode 100644 examples/ex2.c diff --git a/.gitignore b/.gitignore index 7a98879..7725a60 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,9 @@ tests/test examples/mls.c examples/mls examples/mls.o +examples/ex2_sl.c +examples/ex2_sl +examples/ex2_sl.o /mxe .cache hints.txt diff --git a/Makefile b/Makefile index 5d8912e..575ad20 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ TEST_FLAGS = -Itests/ TESTS = tests/test TEST_OBJS = tests/test.o tests/munit.o -EXAMPLES = examples/ex1.c +EXAMPLES = examples/ex1 examples/ex2 .PHONY: all shared static clean test examples mls @@ -64,9 +64,15 @@ examples/%.o: examples/%.c examples/mls.c: examples/ex1.c $(CC) $(CFLAGS) -C -E examples/ex1.c | sed -e '1,7d' -e '/^# [0-9]* "/d' | clang-format > examples/mls.c +examples/ex2_sl.c: examples/ex2.c + $(CC) $(CFLAGS) -C -E examples/ex2.c | sed -e '1,7d' -e '/^# [0-9]* "/d' | clang-format > examples/ex2_sl.c + examples/mls: examples/mls.o $(STATIC_LIB) $(CC) $^ -o $@ $(CFLAGS) $(TEST_FLAGS) -lm -pthread +examples/ex2_sl: examples/ex2_sl.o $(STATIC_LIB) + $(CC) $^ -o $@ $(CFLAGS) $(TEST_FLAGS) -lm -pthread + #dot: # ./examples/mls # dot -Tpdf /tmp/ex1.dot -o /tmp/ex1.pdf >/dev/null 2>&1 diff --git a/examples/ex1.c b/examples/ex1.c index 773d469..b357b27 100644 --- a/examples/ex1.c +++ b/examples/ex1.c @@ -18,7 +18,7 @@ // Local demo application OPTIONS: // --------------------------------------------------------------------------- -#define TEST_ARRAY_SIZE 100 +#define TEST_ARRAY_SIZE 10 #define VALIDATE //define SNAPSHOTS //define TODO_RESTORE_SNAPSHOTS @@ -74,7 +74,10 @@ SKIPLIST_DECL( return 0; }, /* free entry: node */ - { free(node->value); }, + { + free(node->value); + node->value = NULL; + }, /* update entry: rc, node, value */ { char *numeral = (char *)value; @@ -310,10 +313,8 @@ main() if (list == NULL) return ENOMEM; - /* We set the max height here to 12, it's negative so that - the PRNG is seeded with this value as a testing trick for - predictable random sequences. */ - rc = api_skip_init_ex(list, -12); + rc = api_skip_init_ex(list); + list->slh_prng_state = 12; if (rc) return rc; api_skip_snapshots_init_ex(list); diff --git a/examples/ex2.c b/examples/ex2.c new file mode 100644 index 0000000..b357b27 --- /dev/null +++ b/examples/ex2.c @@ -0,0 +1,483 @@ +#pragma GCC push_options +#pragma GCC optimize("O0") + +// OPTIONS to set before including sl.h +// --------------------------------------------------------------------------- +#define DEBUG +#define SKIPLIST_DIAGNOSTIC +/* Setting SKIPLIST_MAX_HEIGHT will do two things: + * 1) limit our max height across all instances of this data structure. + * 2) remove a heap allocation on frequently used paths, insert/remove/etc. + * so, use it when you need it. + */ +#define SKIPLIST_MAX_HEIGHT 12 + +// Include our monolithic ADT, the Skiplist! +// --------------------------------------------------------------------------- +#include "../include/sl.h" + +// Local demo application OPTIONS: +// --------------------------------------------------------------------------- +#define TEST_ARRAY_SIZE 10 +#define VALIDATE +//define SNAPSHOTS +//define TODO_RESTORE_SNAPSHOTS +#define STABLE_SEED +#define DOT + +#ifdef DOT +size_t gen = 0; +FILE *of = 0; +#endif + +// --------------------------------------------------------------------------- +#ifdef VALIDATE +#define CHECK __skip_integrity_check_ex(list, 0) +#else +#define CHECK ((void)0) +#endif + +/* + * SKIPLIST EXAMPLE: + * + * This example creates an "ex" Skiplist where keys are integers, values are + * strings containing the roman numeral for the key allocated on the heap. + */ + +/* + * To start, you must create a type node that will contain the + * fields you'd like to maintain in your Skiplist. In this case + * we map int -> char [] on the heap, but what you put here is up + * to you. You don't even need a "key", just a way to compare one + * node against another, logic you'll provide in SKIP_DECL as a + * block below. + */ +struct ex_node { + int key; + char *value; + SKIPLIST_ENTRY(ex) entries; +}; + +/* + * Generate all the access functions for our type of Skiplist. + */ +SKIPLIST_DECL( + ex, api_, entries, + /* compare entries: list, a, b, aux */ + { + (void)list; + (void)aux; + if (a->key < b->key) + return -1; + if (a->key > b->key) + return 1; + return 0; + }, + /* free entry: node */ + { + free(node->value); + node->value = NULL; + }, + /* update entry: rc, node, value */ + { + char *numeral = (char *)value; + if (node->value) + free(node->value); + node->value = numeral; + }, + /* archive an entry: rc, src, dest */ + { + dest->key = src->key; + char *nv = calloc(strlen(src->value) + 1, sizeof(char)); + if (nv == NULL) + rc = ENOMEM; + else { + strncpy(nv, src->value, strlen(src->value)); + dest->value = nv; + } + }, + /* size in bytes of the content stored in an entry: bytes */ + { bytes = strlen(node->value) + 1; }) + +/* + * Skiplists are ordered, we need a way to compare entries. + * Let's create a function with four arguments: + * - a reference to the Skiplist, `slist` + * - the two nodes to compare, `a` and `b` + * - and `aux`, which you can use to pass into this function + * any additional information required to compare objects. + * `aux` is passed from the value in the Skiplist, you can + * modify that value at any time to suit your needs. + * + * Your function should result in a return statement: + * a < b : return -1 + * a == b : return 0 + * a > b : return 1 + * + * This result provides the ordering within the Skiplist. Sometimes + * your function will not be used when comparing nodes. This will + * happen when `a` or `b` are references to the head or tail of the + * list or when `a == b`. In those cases the comparison function + * returns before using the code in your block, don't panic. :) +int +__ex_key_compare(ex_t *list, ex_node_t *a, ex_node_t *b, void *aux) +{ + (void)list; + (void)aux; + if (a->key < b->key) + return -1; + if (a->key > b->key) + return 1; + return 0; +} +*/ + +/* + * Optional: Getters and Setters + * - get, put, dup(put), del, etc. functions + * + * It can be useful to have simple get/put-style API, but to + * do that you'll have to supply some blocks of code used to + * extract data from within your nodes. + */ +SKIPLIST_DECL_ACCESS( + ex, api_, key, int, value, char *, + /* query blk */ { query.key = key; }, + /* return blk */ { return node->value; }) + +/* + * Optional: Snapshots + * + * Enable functions that enable returning to an earlier point in + * time when a snapshot was created. + */ +SKIPLIST_DECL_SNAPSHOTS(ex, api_, entries) + +/* + * Optional: Archive to/from bytes + * + * Enable functions that can write/read the content of your Skiplist + * out/in to/from an array of bytes. + */ +// TODO SKIPLIST_DECL_ARCHIVE(ex, api_, entries) + +/* + * Optional: As Hashtable + * + * Turn your Skiplist into a hash table. + */ +// TODO SKIPLIST_DECL_HASHTABLE(ex, api_, entries, snaps) + +/* + * Optional: Check Skiplists at runtime + * + * Create a functions that validate the integrity of a Skiplist. + */ +SKIPLIST_DECL_VALIDATE(ex, api_, entries) + +/* Optional: Visualize your Skiplist using DOT/Graphviz in PDF + * + * Create the functions used to annotate a visualization of a Skiplist. + */ +SKIPLIST_DECL_DOT(ex, api_, entries) + +void +sprintf_ex_node(ex_node_t *node, char *buf) +{ + sprintf(buf, "%d:%s", node->key, node->value); +} + +// Function for this demo application. +// --------------------------------------------------------------------------- +int __xorshift32_state = 0; + +// Xorshift algorithm for PRNG +uint32_t +xorshift32() +{ + uint32_t x = __xorshift32_state; + if (x == 0) + x = 123456789; + x ^= x << 13; + x ^= x >> 17; + x ^= x << 5; + __xorshift32_state = x; + return x; +} + +void +xorshift32_seed() +{ + // Seed the PRNG +#ifdef STABLE_SEED + __xorshift32_state = 8675309; +#else + __xorshift32_state = (unsigned int)time(NULL) ^ getpid(); +#endif +} + +static char * +to_lower(char *str) +{ + char *p = str; + for (; *p; ++p) + *p = (char)(*p >= 'A' && *p <= 'Z' ? *p | 0x60 : *p); + return str; +} + +static char * +to_upper(char *str) +{ + char *p = str; + for (; *p; ++p) + *p = (char)(*p >= 'a' && *p <= 'z' ? *p & ~0x20 : *p); + return str; +} + +static char * +int_to_roman_numeral(int num) +{ + int del[] = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 }; // Key value in Roman counting + char *sym[] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" }; // Symbols for key values + // The maximum length of the Roman numeral representation for the maximum signed 64-bit integer would be approximately 19 * 3 = 57 characters, assuming + // every digit is represented by its Roman numeral equivalent up to 3 repetitions. Therefore, 64 should be more than enough. + char *res = (char *)calloc(4096, sizeof(char)); + int i = 0; + if (num < 0) { + res[0] = '-'; + i++; + num = -num; + } + if (num == 0) { + res[0] = '0'; + return res; + } + if (num > 10000) { + sprintf(res, "The person you were looking for is not here, their mailbox is full, good bye."); + return res; + } + while (num) { // while input number is not zero + while (num / del[i]) { // while a number contains the largest key value possible + strcat(res, sym[i]); // append the symbol for this key value to res string + num -= del[i]; // subtract the key value from number + } + i++; // proceed to the next key value + } + return res; +} + +void +shuffle(int *array, size_t n) +{ + if (n > 1) { + size_t i; + for (i = n - 1; i > 0; i--) { + size_t j = (unsigned int)(xorshift32() % (i + 1)); /* NOLINT(*-msc50-cpp) */ + int t = array[j]; + array[j] = array[i]; + array[i] = t; + } + } +} + +#ifdef TODO_RESTORE_SNAPSHOTS +typedef struct { + size_t length; + size_t key; + size_t snap_id; +} snap_info_t; +#endif + +// --------------------------------------------------------------------------- +int +main() +{ + int rc; +#ifdef TODO_RESTORE_SNAPSHOTS + size_t n_snaps = 0; + snap_info_t snaps[TEST_ARRAY_SIZE * 2 + 1]; +#endif + + xorshift32_seed(); + +#ifdef DOT + of = fopen("/tmp/ex1.dot", "w"); + if (!of) { + perror("Failed to open file /tmp/ex1.dot"); + return 1; + } +#endif + + /* Allocate and initialize a Skiplist. */ + ex_t *list = (ex_t *)malloc(sizeof(ex_t)); + if (list == NULL) + return ENOMEM; + + rc = api_skip_init_ex(list); + list->slh_prng_state = 12; + if (rc) + return rc; + api_skip_snapshots_init_ex(list); +#ifdef DOT + api_skip_dot_ex(of, list, gen++, "init", sprintf_ex_node); +#endif + if (api_skip_get_ex(list, 0) != NULL) + perror("found a non-existent item!"); + api_skip_del_ex(list, 0); + CHECK; + + /* Insert TEST_ARRAY_SIZE key/value pairs into the list. */ + int i, j; + char *numeral; +#ifdef DOT + char msg[1024]; + memset(msg, 0, 1024); +#endif + int amt = TEST_ARRAY_SIZE, asz = (amt * 2) + 1; + int array[(TEST_ARRAY_SIZE * 2) + 1]; + for (j = 0, i = -amt; i <= amt; i++, j++) + array[j] = i; + shuffle(array, asz); + + for (i = 0; i < asz; i++) { +#ifdef SNAPSHOTS + api_skip_snapshot_ex(list); +#elif defined(TODO_RESTORE_SNAPSHOTS) + /* Snapshot the first iteration, and then every 5th after that. */ + if (i % 5 == 0) { + snaps[i].length = api_skip_length_ex(list); + snaps[i].key = array[i]; + snaps[i].snap_id = api_skip_snapshot_ex(list); + n_snaps++; + CHECK; + } +#endif + numeral = to_lower(int_to_roman_numeral(array[i])); + rc = api_skip_put_ex(list, array[i], numeral); + CHECK; +#ifdef DOT + sprintf(msg, "put key: %d value: %s", i, numeral); + api_skip_dot_ex(of, list, gen++, msg, sprintf_ex_node); +#endif + char *v = api_skip_get_ex(list, array[i]); + CHECK; + char *upper_numeral = calloc(1, strlen(v) + 1); + strncpy(upper_numeral, v, strlen(v)); + assert(strncmp(v, upper_numeral, strlen(upper_numeral)) == 0); + to_upper(upper_numeral); + api_skip_set_ex(list, array[i], upper_numeral); + CHECK; + } + numeral = int_to_roman_numeral(-1); + api_skip_dup_ex(list, -1, numeral); + CHECK; +#ifdef DOT + sprintf(msg, "put dup key: %d value: %s", i, numeral); + api_skip_dot_ex(of, list, gen++, msg, sprintf_ex_node); +#endif + numeral = int_to_roman_numeral(1); + api_skip_dup_ex(list, 1, numeral); + CHECK; +#ifdef DOT + sprintf(msg, "put dup key: %d value: %s", i, numeral); + api_skip_dot_ex(of, list, gen++, msg, sprintf_ex_node); +#endif + + api_skip_del_ex(list, 0); + CHECK; + if (api_skip_get_ex(list, 0) != NULL) + perror("found a deleted item!"); + api_skip_del_ex(list, 0); + CHECK; + if (api_skip_get_ex(list, 0) != NULL) + perror("found a deleted item!"); + int key = TEST_ARRAY_SIZE + 1; + api_skip_del_ex(list, key); + CHECK; + key = -(TEST_ARRAY_SIZE)-1; + api_skip_del_ex(list, key); + CHECK; + +#ifdef DOT + sprintf(msg, "deleted key: %d, value: %s", 0, numeral); + api_skip_dot_ex(of, list, gen++, msg, sprintf_ex_node); +#endif + + numeral = int_to_roman_numeral(-(TEST_ARRAY_SIZE)); + assert(strcmp(api_skip_pos_ex(list, SKIP_GTE, -(TEST_ARRAY_SIZE)-1)->value, numeral) == 0); + free(numeral); + numeral = int_to_roman_numeral(-2); + assert(strcmp(api_skip_pos_ex(list, SKIP_GTE, -2)->value, numeral) == 0); + free(numeral); + numeral = int_to_roman_numeral(1); + assert(strcmp(api_skip_pos_ex(list, SKIP_GTE, 0)->value, numeral) == 0); + free(numeral); + numeral = int_to_roman_numeral(2); + assert(strcmp(api_skip_pos_ex(list, SKIP_GTE, 2)->value, numeral) == 0); + free(numeral); + assert(api_skip_pos_ex(list, SKIP_GTE, (TEST_ARRAY_SIZE + 1)) == NULL); + + numeral = int_to_roman_numeral(-(TEST_ARRAY_SIZE)); + assert(strcmp(api_skip_pos_ex(list, SKIP_GT, -(TEST_ARRAY_SIZE)-1)->value, numeral) == 0); + free(numeral); + numeral = int_to_roman_numeral(-1); + assert(strcmp(api_skip_pos_ex(list, SKIP_GT, -2)->value, numeral) == 0); + free(numeral); + numeral = int_to_roman_numeral(1); + assert(strcmp(api_skip_pos_ex(list, SKIP_GT, 0)->value, numeral) == 0); + free(numeral); + numeral = int_to_roman_numeral(2); + assert(strcmp(api_skip_pos_ex(list, SKIP_GT, 1)->value, numeral) == 0); + free(numeral); + assert(api_skip_pos_ex(list, SKIP_GT, TEST_ARRAY_SIZE) == NULL); + + assert(api_skip_pos_ex(list, SKIP_LT, -(TEST_ARRAY_SIZE)) == NULL); + numeral = int_to_roman_numeral(-2); + assert(strcmp(api_skip_pos_ex(list, SKIP_LT, -1)->value, numeral) == 0); + free(numeral); + numeral = int_to_roman_numeral(-1); + assert(strcmp(api_skip_pos_ex(list, SKIP_LT, 0)->value, numeral) == 0); + free(numeral); + numeral = int_to_roman_numeral(1); + assert(strcmp(api_skip_pos_ex(list, SKIP_LT, 2)->value, numeral) == 0); + free(numeral); + numeral = int_to_roman_numeral(TEST_ARRAY_SIZE); + assert(strcmp(api_skip_pos_ex(list, SKIP_LT, (TEST_ARRAY_SIZE + 1))->value, numeral) == 0); + free(numeral); + + assert(api_skip_pos_ex(list, SKIP_LTE, -(TEST_ARRAY_SIZE)-1) == NULL); + numeral = int_to_roman_numeral(-2); + assert(strcmp(api_skip_pos_ex(list, SKIP_LTE, -2)->value, numeral) == 0); + free(numeral); + numeral = int_to_roman_numeral(-1); + assert(strcmp(api_skip_pos_ex(list, SKIP_LTE, 0)->value, numeral) == 0); + free(numeral); + numeral = int_to_roman_numeral(2); + assert(strcmp(api_skip_pos_ex(list, SKIP_LTE, 2)->value, numeral) == 0); + free(numeral); + numeral = int_to_roman_numeral(TEST_ARRAY_SIZE); + assert(strcmp(api_skip_pos_ex(list, SKIP_LTE, (TEST_ARRAY_SIZE + 1))->value, numeral) == 0); + free(numeral); + +#ifdef TODO_RESTORE_SNAPSHOTS + // Walk backward by 2 and test snapshot restore. + for (i = n_snaps; i > 0; i -= 2) { + api_skip_restore_snapshot_ex(list, snaps[i].snap_id); + CHECK; + assert(api_skip_length_ex(list) == snaps[i].length); + numeral = int_to_roman_numeral(snaps[i].key); + assert(strncmp(api_skip_get_ex(list, snaps[i].key), numeral, strlen(numeral)) == 0); + free(numeral); + } + api_skip_release_snapshots_ex(list); +#endif + +#ifdef DOT + api_skip_dot_end_ex(of, gen); + fclose(of); +#endif + api_skip_free_ex(list); + free(list); + return rc; +} +#pragma GCC pop_options diff --git a/include/sl.h b/include/sl.h index f6b3adf..ada9558 100644 --- a/include/sl.h +++ b/include/sl.h @@ -62,12 +62,11 @@ * It is a probabilistic datastructure, meaning that it does not guarantee * O(log(n)), but it has been shown to approximate it over time. This * implementation includes the rebalancing techniques that improve on that - * approximation using an adaptive technique called "splay-list" (named after - * the splay-tree due to the similarities between the two). It is similar to a - * standard skip-list, with the key distinction that the height of each element - * adapts dynamically to its access rate: popular elements increase in height, - * whereas rarely-accessed elements decrease in height. See below for the link - * to the research behind this adaptive technique. + * approximation using an adaptive technique called "splay-list". It is similar + * to a standard skip-list, with the key distinction that the height of each + * element adapts dynamically to its access rate: popular elements increase in + * height, whereas rarely-accessed elements decrease in height. See below for + * the link to the research behind this adaptive technique. * * Conceptually, at a high level, a skip-list is arranged as follows: * @@ -202,7 +201,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li */ #ifndef SKIPLIST_MAX_HEIGHT -#define SKIPLIST_MAX_HEIGHT 1 +#define SKIPLIST_MAX_HEIGHT 64 #endif /* @@ -232,17 +231,17 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li for (iter = (list)->slh_length, (elm) = (list)->slh_tail; ((elm) = (elm)->field.sle_prev) != (list)->slh_head; iter--) /* Iterate over the next pointers in a node from bottom to top (B2T) or top to bottom (T2B). */ -#define __SKIP_ALL_ENTRIES_T2B(field, elm) for (size_t lvl = slist->slh_max_height; lvl != (size_t)-1; lvl--) +#define __SKIP_ALL_ENTRIES_T2B(field, elm) for (size_t lvl = slist->slh_head->entries.sle_height - 1; lvl != (size_t)-1; lvl--) #define __SKIP_ENTRIES_T2B(field, elm) for (size_t lvl = elm->field.sle_height; lvl != (size_t)-1; lvl--) #define __SKIP_ENTRIES_T2B_FROM(field, elm, off) for (size_t lvl = off; lvl != (size_t)-1; lvl--) #define __SKIP_IS_LAST_ENTRY_T2B() if (lvl == 0) -#define __SKIP_ALL_ENTRIES_B2T(field, elm) for (size_t lvl = 0; lvl < slist->slh_max_height; lvl++) +#define __SKIP_ALL_ENTRIES_B2T(field, elm) for (size_t lvl = 0; lvl < slist->slh_head->entries.sle_height - 1; lvl++) #define __SKIP_ENTRIES_B2T(field, elm) for (size_t lvl = 0; lvl <= elm->field.sle_height; lvl++) #define __SKIP_ENTRIES_B2T_FROM(field, elm, off) for (size_t lvl = off; lvl <= elm->field.sle_height; lvl++) #define __SKIP_IS_LAST_ENTRY_B2T() if (lvl + 1 == elm->field.sle_height) -/* Iterate over the subtree to the left (v, or 'lt') and right (u) or "CHu" and "CHv". */ +/* Iterate over the subtree to the left (v, or 'lt') and right (u, or 'gt') or "CHu" and "CHv". */ #define __SKIP_SUBTREE_CHv(decl, field, list, path, nth) \ for (decl##_node_t *elm = path[nth].node; elm->field.sle_levels[path[nth].in].next == path[nth].node; elm = elm->field.sle_prev) #define __SKIP_SUBTREE_CHu(decl, field, list, path, nth) \ @@ -264,7 +263,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li \ /* Skiplist structure */ \ struct decl { \ - size_t slh_length, slh_max_height; \ + size_t slh_length; \ void *slh_aux; \ int slh_prng_state; \ decl##_node_t *slh_head; \ @@ -414,12 +413,12 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li * \ * Allocates a new node on the heap and sets default values. \ */ \ - int prefix##skip_alloc_node_##decl(decl##_t *slist, decl##_node_t **node) \ + int prefix##skip_alloc_node_##decl(decl##_node_t **node) \ { \ decl##_node_t *n; \ /* Calculate the size of the struct sle within decl##_node_t, multiply \ by array size. (16/24 bytes on 32/64 bit systems) */ \ - size_t sle_arr_sz = sizeof(struct __skiplist_##decl##_level) * slist->slh_max_height; \ + size_t sle_arr_sz = sizeof(struct __skiplist_##decl##_level) * SKIPLIST_MAX_HEIGHT; \ n = (decl##_node_t *)calloc(1, sizeof(decl##_node_t) + sle_arr_sz); \ if (n == NULL) \ return ENOMEM; \ @@ -432,16 +431,15 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li /** \ * -- skip_init_ \ * \ - * Initializes a Skiplist to the deafault values, this must be called \ - * before using the list. \ + * Initializes a Skiplist to the default values, this must be \ + * called before using the list. \ */ \ - int prefix##skip_init_##decl(decl##_t *slist, int max) \ + int prefix##skip_init_##decl(decl##_t *slist) \ { \ int rc = 0; \ size_t i; \ \ slist->slh_length = 0; \ - slist->slh_max_height = SKIPLIST_MAX_HEIGHT == 1 ? (size_t)(max < 0 ? -max : max) : SKIPLIST_MAX_HEIGHT; \ slist->slh_snap.cur_era = 0; \ slist->slh_snap.pres_era = 0; \ slist->slh_snap.pres = 0; \ @@ -450,32 +448,25 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li slist->slh_fns.archive_entry = __skip_archive_entry_fn_##decl; \ slist->slh_fns.sizeof_entry = __skip_sizeof_entry_fn_##decl; \ slist->slh_fns.compare_entries = __skip_compare_entries_fn_##decl; \ - rc = prefix##skip_alloc_node_##decl(slist, &slist->slh_head); \ + rc = prefix##skip_alloc_node_##decl(&slist->slh_head); \ if (rc) \ goto fail; \ - rc = prefix##skip_alloc_node_##decl(slist, &slist->slh_tail); \ + rc = prefix##skip_alloc_node_##decl(&slist->slh_tail); \ if (rc) \ goto fail; \ \ - /* Initial height is 0 (meaning 1 level), all next point to tail */ \ - slist->slh_head->field.sle_height = 0; \ - for (i = 0; i < slist->slh_max_height; i++) \ + /* Initial height at head is 0 (meaning 0 sub-lists), all next point to tail */ \ + slist->slh_head->field.sle_height = 1; \ + for (i = 0; i < slist->slh_head->entries.sle_height + 1; i++) \ slist->slh_head->field.sle_levels[i].next = slist->slh_tail; \ slist->slh_head->field.sle_prev = NULL; \ \ - /* Initial height is 0 (meaning 1 level), all next point to tail */ \ + /* Initial height at tail is also 0 and all next point to tail */ \ slist->slh_tail->field.sle_height = slist->slh_head->field.sle_height; \ - for (i = 0; i < slist->slh_max_height; i++) \ + for (i = 0; i < slist->slh_head->entries.sle_height + 1; i++) \ slist->slh_tail->field.sle_levels[i].next = NULL; \ slist->slh_tail->field.sle_prev = slist->slh_head; \ - \ - /* NOTE: Here's a testing aid, simply set `max` to a negative number to \ - * seed the PRNG in a predictable way and have reproducible random numbers. \ - */ \ - if (max < 0) \ - slist->slh_prng_state = -max; \ - else \ - slist->slh_prng_state = ((unsigned int)time(NULL) ^ getpid()); \ + slist->slh_prng_state = ((unsigned int)time(NULL) ^ getpid()); \ fail:; \ return rc; \ } \ @@ -533,6 +524,8 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li */ \ decl##_node_t *prefix##skip_tail_##decl(decl##_t *slist) \ { \ + if (slist == NULL) \ + return NULL; \ return slist->slh_tail->field.sle_prev == slist->slh_head->field.sle_levels[0].next ? NULL : slist->slh_tail->field.sle_prev; \ } \ \ @@ -640,13 +633,13 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li static void __skip_rebalance_##decl(decl##_t *slist, size_t len, __skiplist_path_##decl##_t path[]) \ { \ size_t i, j, u_hits, hits_CHu = 0, hits_CHv = 0, delta_height, new_height, cur_hits, prev_hits; \ - double k_threshold, m_total_hits, asc_cond, dsc_cond; \ + size_t k_threshold, m_total_hits; \ + double asc_cond, dsc_cond; \ \ - /* return; TODO/WIP */ \ - /* Total hits, `k`, accross all nodes. */ \ + /* Total hits, `k`, across all nodes. */ \ m_total_hits = slist->slh_head->field.sle_levels[slist->slh_head->field.sle_height].hits; \ \ - /* Height of the head node, should be close to floor(log(max_height)). */ \ + /* Height of the head node, should be close to floor(log(m_total_hits)). */ \ k_threshold = slist->slh_head->field.sle_height + 1; \ \ /* Moving backwards along the path... \ @@ -655,7 +648,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li * - path[len] is where the locate() terminated, just before path[0] \ * if there was a match \ */ \ - for (i = 1; i < len; i++) { \ + for (i = 1; i <= len; i++) { \ if (path[i].node == slist->slh_head || path[i].node == slist->slh_tail) \ continue; \ \ @@ -707,7 +700,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li */ \ /* 1) check ascent condition */ \ asc_cond = m_total_hits / pow(2.0, delta_height == 0 ? 0 : delta_height - 1); \ - if (path[i - 1].pu > asc_cond && path[i].node->field.sle_height < slist->slh_max_height - 1) { \ + if (path[i - 1].pu > asc_cond && path[i].node->field.sle_height < SKIPLIST_MAX_HEIGHT) { \ /* 2) increase height by one */ \ new_height = path[i].node->field.sle_height++; \ /* 3) update hit counter */ \ @@ -726,19 +719,21 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li * Locates a node that matches another node updating `path` and then \ * returning the length of that path + 1 to the node and the matching \ * node in path[0], or NULL at path[0] where there wasn't a match. \ - * sizeof(path) should be `slist->slh_max_height + 1` \ + * sizeof(path) should be `slist->slh_head->entries.sle_height + 1` \ */ \ static size_t __skip_locate_##decl(decl##_t *slist, decl##_node_t *n, __skiplist_path_##decl##_t path[]) \ { \ unsigned int i; \ size_t len = 0; \ - decl##_node_t *elm = slist->slh_head; \ + decl##_node_t *elm; \ \ if (slist == NULL || n == NULL) \ return 0; \ \ + elm = slist->slh_head; \ + \ /* Find the node that matches `node` or NULL. */ \ - i = slist->slh_head->field.sle_height; \ + i = slist->slh_head->field.sle_height - 1; \ do { \ path[i + 1].pu = 0; \ while (elm != slist->slh_tail && elm->field.sle_levels[i].next && \ @@ -755,6 +750,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li if (__skip_compare_nodes_##decl(slist, elm, n, slist->slh_aux) == 0) { \ path[0].node = elm; \ path[0].node->field.sle_levels[0].hits++; \ + slist->slh_head->entries.sle_levels[slist->slh_head->entries.sle_height].hits++; \ __skip_rebalance_##decl(slist, len, path); \ } \ return len; \ @@ -778,17 +774,13 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li if (slist == NULL || new == NULL) \ return ENOENT; \ \ - /* Allocate a buffer, or use a static one. */ \ - if (SKIPLIST_MAX_HEIGHT == 1) { \ - path = malloc(sizeof(__skiplist_path_##decl##_t) * slist->slh_max_height + 1); \ - if (path == NULL) \ - return ENOMEM; \ - } \ - /* First element in path should be NULL, reset should start pointing at tail. */ \ + memset(path, 0, sizeof(__skiplist_path_##decl##_t) * SKIPLIST_MAX_HEIGHT + 1); \ + \ + /* First element in path should be NULL, rest should start pointing at tail. */ \ path[0].node = NULL; \ path[0].in = 0; \ path[0].pu = 0; \ - for (i = 1; i < slist->slh_max_height + 1; i++) { \ + for (i = 1; i < slist->slh_head->entries.sle_height + 1; i++) { \ path[i].node = slist->slh_tail; \ path[i].in = 0; \ path[i].pu = 0; \ @@ -802,9 +794,9 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li /* Don't insert, duplicate if flag not set. */ \ return -1; \ } \ - /* Coin toss to determine level of this new node [0, max) */ \ - cur_height = slist->slh_head->field.sle_height; \ - new_height = __skip_toss_##decl(slist, slist->slh_max_height - 1); \ + /* Coin toss to determine level of this new node [0, current max height) */ \ + cur_height = slist->slh_head->field.sle_height - 1; \ + new_height = __skip_toss_##decl(slist, cur_height); \ new->field.sle_height = new_height; \ /* Trim the path to at most the new height for the new node. */ \ for (i = cur_height + 1; i <= new_height; i++) { \ @@ -835,23 +827,18 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li slist->slh_head->field.sle_levels[lvl].next = slist->slh_tail; \ } \ } \ - /* Adujust the previous pointers in the nodes. */ \ + /* Adjust the previous pointers in the nodes. */ \ new->field.sle_prev = path[1].node; \ new->field.sle_levels[0].next->field.sle_prev = new; \ /* Account for insert at tail. */ \ if (new->field.sle_levels[0].next == slist->slh_tail) \ slist->slh_tail->field.sle_prev = new; \ - /* Adjust the head/tail boundary node heights if necessary. */ \ - if (new_height > cur_height) { \ - slist->slh_head->field.sle_height = new_height; \ - slist->slh_tail->field.sle_height = new_height; \ - } \ /* Record the era for this node to enable snapshots. */ \ if (slist->slh_snap.pres_era > 0) { \ - /* Increase the the list's era/age and record it. */ \ + /* Increase the list's era/age and record it. */ \ new->field.sle_era = slist->slh_snap.cur_era++; \ } \ - /* Set hits for rebalencing to 1 when new born. */ \ + /* Set hits for re-balancing to 1 when newborn. */ \ new->field.sle_levels[new_height].hits = 1; \ /* Increase our list length (aka. size, count, etc.) by one. */ \ slist->slh_length++; \ @@ -895,18 +882,11 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li decl##_node_t *node = NULL; \ __skiplist_path_##decl##_t *path = apath; \ \ - /* Allocate a buffer, or use a static one. */ \ - if (SKIPLIST_MAX_HEIGHT == 1) { \ - path = malloc(sizeof(__skiplist_path_##decl##_t) * slist->slh_max_height + 1); \ - if (path == NULL) \ - goto done; \ - } \ - memset(path, 0, sizeof(__skiplist_path_##decl##_t) * slist->slh_max_height + 1); \ + memset(path, 0, sizeof(__skiplist_path_##decl##_t) * SKIPLIST_MAX_HEIGHT + 1); \ \ /* Find a `path` to `new` in the list and a match (`path[0]`) if it exists. */ \ __skip_locate_##decl(slist, query, path); \ node = path[0].node; \ - done:; \ if (SKIPLIST_MAX_HEIGHT == 1) \ free(path); \ \ @@ -927,13 +907,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li decl##_node_t *node; \ __skiplist_path_##decl##_t *path = apath; \ \ - /* Allocate a buffer, or use a static one. */ \ - if (SKIPLIST_MAX_HEIGHT == 1) { \ - path = malloc(sizeof(__skiplist_path_##decl##_t) * slist->slh_max_height + 1); \ - if (path == NULL) \ - goto done; \ - } \ - memset(path, 0, sizeof(__skiplist_path_##decl##_t) * slist->slh_max_height + 1); \ + memset(path, 0, sizeof(__skiplist_path_##decl##_t) * SKIPLIST_MAX_HEIGHT + 1); \ \ /* Find a `path` to `new` in the list and a match (`path[0]`) if it exists. */ \ __skip_locate_##decl(slist, query, path); \ @@ -942,8 +916,6 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li node = node->field.sle_levels[0].next; \ cmp = __skip_compare_nodes_##decl(slist, node, query, slist->slh_aux); \ } while (cmp < 0); \ - \ - done:; \ if (SKIPLIST_MAX_HEIGHT == 1) \ free(path); \ \ @@ -964,13 +936,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li decl##_node_t *node; \ __skiplist_path_##decl##_t *path = apath; \ \ - /* Allocate a buffer, or use a static one. */ \ - if (SKIPLIST_MAX_HEIGHT == 1) { \ - path = malloc(sizeof(__skiplist_path_##decl##_t) * slist->slh_max_height + 1); \ - if (path == NULL) \ - goto done; \ - } \ - memset(path, 0, sizeof(__skiplist_path_##decl##_t) * slist->slh_max_height + 1); \ + memset(path, 0, sizeof(__skiplist_path_##decl##_t) * SKIPLIST_MAX_HEIGHT + 1); \ \ /* Find a `path` to `new` in the list and a match (`path[0]`) if it exists. */ \ __skip_locate_##decl(slist, query, path); \ @@ -981,7 +947,6 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li node = node->field.sle_levels[0].next; \ cmp = __skip_compare_nodes_##decl(slist, node, query, slist->slh_aux); \ } while (cmp <= 0 && node != slist->slh_tail); \ - \ done:; \ if (SKIPLIST_MAX_HEIGHT == 1) \ free(path); \ @@ -1002,13 +967,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li decl##_node_t *node; \ __skiplist_path_##decl##_t *path = apath; \ \ - /* Allocate a buffer, or use a static one. */ \ - if (SKIPLIST_MAX_HEIGHT == 1) { \ - path = malloc(sizeof(__skiplist_path_##decl##_t) * slist->slh_max_height + 1); \ - if (path == NULL) \ - goto done; \ - } \ - memset(path, 0, sizeof(__skiplist_path_##decl##_t) * slist->slh_max_height + 1); \ + memset(path, 0, sizeof(__skiplist_path_##decl##_t) * SKIPLIST_MAX_HEIGHT + 1); \ \ /* Find a `path` to `new` in the list and a match (`path[0]`) if it exists. */ \ __skip_locate_##decl(slist, query, path); \ @@ -1016,7 +975,6 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li if (node) \ goto done; \ node = path[1].node; \ - \ done:; \ if (SKIPLIST_MAX_HEIGHT == 1) \ free(path); \ @@ -1036,19 +994,11 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li decl##_node_t *node; \ __skiplist_path_##decl##_t *path = apath; \ \ - /* Allocate a buffer, or use a static one. */ \ - if (SKIPLIST_MAX_HEIGHT == 1) { \ - path = malloc(sizeof(__skiplist_path_##decl##_t) * slist->slh_max_height + 1); \ - if (path == NULL) \ - goto done; \ - } \ - memset(path, 0, sizeof(__skiplist_path_##decl##_t) * slist->slh_max_height + 1); \ + memset(path, 0, sizeof(__skiplist_path_##decl##_t) * SKIPLIST_MAX_HEIGHT + 1); \ \ /* Find a `path` to `new` in the list and a match (`path[0]`) if it exists. */ \ __skip_locate_##decl(slist, query, path); \ node = path[1].node; \ - \ - done:; \ if (SKIPLIST_MAX_HEIGHT == 1) \ free(path); \ \ @@ -1104,13 +1054,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li if (slist == NULL) \ return -1; \ \ - /* Allocate a buffer, or use a static one. */ \ - if (SKIPLIST_MAX_HEIGHT == 1) { \ - path = malloc(sizeof(__skiplist_path_##decl##_t) * slist->slh_max_height + 1); \ - if (path == NULL) \ - return ENOMEM; \ - } \ - memset(path, 0, sizeof(__skiplist_path_##decl##_t) * slist->slh_max_height + 1); \ + memset(path, 0, sizeof(__skiplist_path_##decl##_t) * SKIPLIST_MAX_HEIGHT + 1); \ \ __skip_locate_##decl(slist, query, path); \ node = path[0].node; \ @@ -1157,13 +1101,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li if (slist->slh_length == 0) \ return 0; \ \ - /* Allocate a buffer */ \ - if (SKIPLIST_MAX_HEIGHT == 1) { \ - path = malloc(sizeof(__skiplist_path_##decl##_t) * slist->slh_max_height + 1); \ - if (path == NULL) \ - return ENOMEM; \ - } \ - memset(path, 0, sizeof(__skiplist_path_##decl##_t) * slist->slh_max_height + 1); \ + memset(path, 0, sizeof(__skiplist_path_##decl##_t) * SKIPLIST_MAX_HEIGHT + 1); \ \ /* Attempt to locate the node in the list. */ \ len = __skip_locate_##decl(slist, query, path); \ @@ -1207,13 +1145,12 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li free(path); \ \ slist->slh_fns.free_entry(node); \ + free(node); \ \ - /* Reduce the height of the head node. */ \ - i = 0; \ - while (slist->slh_head->field.sle_levels[i].next != slist->slh_tail && i < slist->slh_head->field.sle_height) \ - i++; \ - slist->slh_head->field.sle_height = i; \ - slist->slh_tail->field.sle_height = i; \ + /* Reduce the height of the head/tail nodes. */ \ + for (i = 0; slist->slh_head->field.sle_levels[i].next != slist->slh_tail && i < SKIPLIST_MAX_HEIGHT; i++) \ + ; \ + slist->slh_head->field.sle_height = slist->slh_tail->field.sle_height = i; \ \ slist->slh_length--; \ __skip_adjust_hit_counts_##decl(slist); \ @@ -1319,8 +1256,8 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li return 0; \ \ /* (a) alloc, ... */ \ - size_t sle_arr_sz = sizeof(struct __skiplist_##decl##_level) * slist->slh_max_height; \ - rc = prefix##skip_alloc_node_##decl(slist, &dest); \ + size_t sle_arr_sz = sizeof(struct __skiplist_##decl##_level) * slist->slh_head->entries.sle_height - 1; \ + rc = prefix##skip_alloc_node_##decl(&dest); \ if (rc) \ return rc; \ \ @@ -1482,261 +1419,262 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li } \ } -#define SKIPLIST_DECL_VALIDATE(decl, prefix, field) \ - /** \ - * -- __skip_integrity_failure_ \ - */ \ - static void __attribute__((format(printf, 1, 2))) __skip_integrity_failure_##decl(const char *format, ...) \ - { \ - va_list args; \ - va_start(args, format); \ - vfprintf(stderr, format, args); \ - va_end(args); \ - } \ - \ - /** \ - * -- __skip_integrity_check_ \ - */ \ - static int __skip_integrity_check_##decl(decl##_t *slist, int flags) \ - { \ - size_t n = 0; \ - unsigned long nth, n_err = 0; \ - decl##_node_t *node, *prev, *next; \ - struct __skiplist_##decl##_entry *this; \ - \ - if (slist == NULL) { \ - __skip_integrity_failure_##decl("slist was NULL, nothing to check\n"); \ - n_err++; \ - return n_err; \ - } \ - \ - /* Check the Skiplist header (slh) */ \ - \ - if (slist->slh_head == NULL) { \ - __skip_integrity_failure_##decl("skiplist slh_head is NULL\n"); \ - n_err++; \ - return n_err; \ - } \ - \ - if (slist->slh_tail == NULL) { \ - __skip_integrity_failure_##decl("skiplist slh_tail is NULL\n"); \ - n_err++; \ - return n_err; \ - } \ - \ - if (slist->slh_fns.free_entry == NULL) { \ - __skip_integrity_failure_##decl("skiplist free_entry fn is NULL\n"); \ - n_err++; \ - return n_err; \ - } \ - \ - if (slist->slh_fns.update_entry == NULL) { \ - __skip_integrity_failure_##decl("skiplist update_entry fn is NULL\n"); \ - n_err++; \ - return n_err; \ - } \ - \ - if (slist->slh_fns.archive_entry == NULL) { \ - __skip_integrity_failure_##decl("skiplist archive_entry fn is NULL\n"); \ - n_err++; \ - return n_err; \ - } \ - \ - if (slist->slh_fns.sizeof_entry == NULL) { \ - __skip_integrity_failure_##decl("skiplist sizeof_entry fn is NULL\n"); \ - n_err++; \ - return n_err; \ - } \ - \ - if (slist->slh_fns.compare_entries == NULL) { \ - __skip_integrity_failure_##decl("skiplist compare_entries fn is NULL\n"); \ - n_err++; \ - return n_err; \ - } \ - \ - if (slist->slh_max_height < 1) { \ - __skip_integrity_failure_##decl("skiplist max level must be 1 at minimum\n"); \ - n_err++; \ - if (flags) \ - return n_err; \ - } \ - \ - if (slist->slh_head->field.sle_height >= slist->slh_max_height) { \ - /* level is 0-based, max of 12 means level cannot be > 11 */ \ - __skip_integrity_failure_##decl("skiplist level %lu in header was >= max %lu\n", slist->slh_head->field.sle_height, slist->slh_max_height); \ - n_err++; \ - if (flags) \ - return n_err; \ - } \ - \ - if (SKIPLIST_MAX_HEIGHT < 1) { \ - __skip_integrity_failure_##decl("SKIPLIST_MAX_HEIGHT cannot be less than 1\n"); \ - n_err++; \ - if (flags) \ - return n_err; \ - } \ - \ - if (SKIPLIST_MAX_HEIGHT > 1 && slist->slh_max_height > SKIPLIST_MAX_HEIGHT) { \ - __skip_integrity_failure_##decl("slist->slh_max_height %lu cannot be greater than SKIPLIST_MAX_HEIGHT %lu\n", slist->slh_max_height, \ - (size_t)SKIPLIST_MAX_HEIGHT); \ - n_err++; \ - if (flags) \ - return n_err; \ - } \ - \ - node = slist->slh_head; \ - __SKIP_ENTRIES_B2T(field, node) \ - { \ - if (node->field.sle_levels[lvl].next == NULL) { \ - __skip_integrity_failure_##decl("the head's %lu next node should not be NULL\n", lvl); \ - n_err++; \ - if (flags) \ - return n_err; \ - } \ - n = lvl; \ - if (node->field.sle_levels[lvl].next == slist->slh_tail) \ - break; \ - } \ - n++; \ - __SKIP_ENTRIES_B2T_FROM(field, node, n) \ - { \ - if (node->field.sle_levels[lvl].next == NULL) { \ - __skip_integrity_failure_##decl("the head's %lu next node should not be NULL\n", lvl); \ - n_err++; \ - if (flags) \ - return n_err; \ - } \ - /* TODO: really? \ - if (node->field.sle_levels[lvl].next != slist->slh_tail) { \ - __skip_integrity_failure_##decl("after internal nodes, the head's %lu next node should always be the tail\n", lvl); \ - n_err++; \ - if (flags) \ - return n_err; \ - } \ - */ \ - } \ - \ - if (slist->slh_length > 0 && slist->slh_tail->field.sle_prev == slist->slh_head) { \ - __skip_integrity_failure_##decl("slist->slh_length is 0, but tail->prev == head, not an internal node\n"); \ - n_err++; \ - if (flags) \ - return n_err; \ - } \ - \ - /* Validate the head node */ \ - \ - /* Validate the tail node */ \ - \ - /* Validate each node */ \ - SKIPLIST_FOREACH_H2T(decl, prefix, field, slist, node, nth) \ - { \ - this = &node->field; \ - \ - if (this->sle_height >= slist->slh_max_height) { \ - __skip_integrity_failure_##decl("the %lu node's [%p] height %lu is >= max %lu\n", nth, (void *)node, this->sle_height, slist->slh_max_height); \ - n_err++; \ - if (flags) \ - return n_err; \ - } \ - \ - if (this->sle_levels == NULL) { \ - __skip_integrity_failure_##decl("the %lu node's [%p] next field should never be NULL\n", nth, (void *)node); \ - n_err++; \ - if (flags) \ - return n_err; \ - } \ - \ - if (this->sle_prev == NULL) { \ - __skip_integrity_failure_##decl("the %lu node [%p] prev field should never be NULL\n", nth, (void *)node); \ - n_err++; \ - if (flags) \ - return n_err; \ - } \ - \ - __SKIP_ENTRIES_B2T(field, node) \ - { \ - if (this->sle_levels[lvl].next == NULL) { \ - __skip_integrity_failure_##decl("the %lu node's next[%lu] should not be NULL\n", nth, lvl); \ - n_err++; \ - if (flags) \ - return n_err; \ - } \ - n = lvl; \ - if (this->sle_levels[lvl].next == slist->slh_tail) \ - break; \ - } \ - n++; \ - __SKIP_ENTRIES_B2T_FROM(field, node, n) \ - { \ - if (this->sle_levels[lvl].next == NULL) { \ - __skip_integrity_failure_##decl("after the %lunth the %lu node's next[%lu] should not be NULL\n", n, nth, lvl); \ - n_err++; \ - if (flags) \ - return n_err; \ - } else if (this->sle_levels[lvl].next != slist->slh_tail) { \ - __skip_integrity_failure_##decl("after the %lunth the %lu node's next[%lu] should point to the tail\n", n, nth, lvl); \ - n_err++; \ - if (flags) \ - return n_err; \ - } \ - } \ - \ - decl##_node_t *a = (decl##_node_t *)(uintptr_t)this->sle_levels; \ - decl##_node_t *b = (decl##_node_t *)(intptr_t)((uintptr_t)node + sizeof(decl##_node_t)); \ - if (a != b) { \ - __skip_integrity_failure_##decl("the %lu node's [%p] next field isn't at the proper offset relative to the node\n", nth, (void *)node); \ - n_err++; \ - if (flags) \ - return n_err; \ - } \ - \ - next = this->sle_levels[0].next; \ - prev = this->sle_prev; \ - if (__skip_compare_nodes_##decl(slist, node, node, slist->slh_aux) != 0) { \ - __skip_integrity_failure_##decl("the %lu node [%p] is not equal to itself\n", nth, (void *)node); \ - n_err++; \ - if (flags) \ - return n_err; \ - } \ - \ - if (__skip_compare_nodes_##decl(slist, node, prev, slist->slh_aux) < 0) { \ - __skip_integrity_failure_##decl("the %lu node [%p] is not greater than the prev node [%p]\n", nth, (void *)node, (void *)prev); \ - n_err++; \ - if (flags) \ - return n_err; \ - } \ - \ - if (__skip_compare_nodes_##decl(slist, node, next, slist->slh_aux) > 0) { \ - __skip_integrity_failure_##decl("the %lu node [%p] is not less than the next node [%p]\n", nth, (void *)node, (void *)next); \ - n_err++; \ - if (flags) \ - return n_err; \ - } \ - \ - if (__skip_compare_nodes_##decl(slist, prev, node, slist->slh_aux) > 0) { \ - __skip_integrity_failure_##decl("the prev node [%p] is not less than the %lu node [%p]\n", (void *)prev, nth, (void *)node); \ - n_err++; \ - if (flags) \ - return n_err; \ - } \ - \ - if (__skip_compare_nodes_##decl(slist, next, node, slist->slh_aux) < 0) { \ - __skip_integrity_failure_##decl("the next node [%p] is not greater than the %lu node [%p]\n", (void *)next, nth, (void *)node); \ - n_err++; \ - if (flags) \ - return n_err; \ - } \ - } \ - \ - if (slist->slh_length != nth) { \ - __skip_integrity_failure_##decl("slist->slh_length (%lu) doesn't match the count (%lu) of nodes between the head and tail\n", slist->slh_length, \ - nth); \ - n_err++; \ - if (flags) \ - return n_err; \ - } \ - \ - return 0; \ +#define SKIPLIST_DECL_VALIDATE(decl, prefix, field) \ + /** \ + * -- __skip_integrity_failure_ \ + */ \ + static void __attribute__((format(printf, 1, 2))) __skip_integrity_failure_##decl(const char *format, ...) \ + { \ + va_list args; \ + va_start(args, format); \ + vfprintf(stderr, format, args); \ + va_end(args); \ + } \ + \ + /** \ + * -- __skip_integrity_check_ \ + */ \ + static int __skip_integrity_check_##decl(decl##_t *slist, int flags) \ + { \ + size_t n = 0; \ + unsigned long nth, n_err = 0; \ + decl##_node_t *node, *prev, *next; \ + struct __skiplist_##decl##_entry *this; \ + \ + if (slist == NULL) { \ + __skip_integrity_failure_##decl("slist was NULL, nothing to check\n"); \ + n_err++; \ + return n_err; \ + } \ + \ + /* Check the Skiplist header (slh) */ \ + \ + if (slist->slh_head == NULL) { \ + __skip_integrity_failure_##decl("skiplist slh_head is NULL\n"); \ + n_err++; \ + return n_err; \ + } \ + \ + if (slist->slh_tail == NULL) { \ + __skip_integrity_failure_##decl("skiplist slh_tail is NULL\n"); \ + n_err++; \ + return n_err; \ + } \ + \ + if (slist->slh_fns.free_entry == NULL) { \ + __skip_integrity_failure_##decl("skiplist free_entry fn is NULL\n"); \ + n_err++; \ + return n_err; \ + } \ + \ + if (slist->slh_fns.update_entry == NULL) { \ + __skip_integrity_failure_##decl("skiplist update_entry fn is NULL\n"); \ + n_err++; \ + return n_err; \ + } \ + \ + if (slist->slh_fns.archive_entry == NULL) { \ + __skip_integrity_failure_##decl("skiplist archive_entry fn is NULL\n"); \ + n_err++; \ + return n_err; \ + } \ + \ + if (slist->slh_fns.sizeof_entry == NULL) { \ + __skip_integrity_failure_##decl("skiplist sizeof_entry fn is NULL\n"); \ + n_err++; \ + return n_err; \ + } \ + \ + if (slist->slh_fns.compare_entries == NULL) { \ + __skip_integrity_failure_##decl("skiplist compare_entries fn is NULL\n"); \ + n_err++; \ + return n_err; \ + } \ + \ + if (slist->slh_head->entries.sle_height > SKIPLIST_MAX_HEIGHT) { \ + __skip_integrity_failure_##decl("skiplist head height > SKIPLIST_MAX_HEIGHT\n"); \ + n_err++; \ + if (flags) \ + return n_err; \ + } \ + \ + if (slist->slh_tail->entries.sle_height > SKIPLIST_MAX_HEIGHT) { \ + __skip_integrity_failure_##decl("skiplist tail height > SKIPLIST_MAX_HEIGHT\n"); \ + n_err++; \ + if (flags) \ + return n_err; \ + } \ + \ + if (slist->slh_head->entries.sle_height != slist->slh_tail->entries.sle_height) { \ + __skip_integrity_failure_##decl("skiplist head & tail height are not equal\n"); \ + n_err++; \ + if (flags) \ + return n_err; \ + } \ + \ + /* TODO: slh_head->entries.sle_height should == log(m) where m is the sum of all hits on all nodes */ \ + \ + if (SKIPLIST_MAX_HEIGHT < 1) { \ + __skip_integrity_failure_##decl("SKIPLIST_MAX_HEIGHT cannot be less than 1\n"); \ + n_err++; \ + if (flags) \ + return n_err; \ + } \ + \ + node = slist->slh_head; \ + __SKIP_ENTRIES_B2T(field, node) \ + { \ + if (node->field.sle_levels[lvl].next == NULL) { \ + __skip_integrity_failure_##decl("the head's %lu next node should not be NULL\n", lvl); \ + n_err++; \ + if (flags) \ + return n_err; \ + } \ + n = lvl; \ + if (node->field.sle_levels[lvl].next == slist->slh_tail) \ + break; \ + } \ + n++; \ + __SKIP_ENTRIES_B2T_FROM(field, node, n) \ + { \ + if (node->field.sle_levels[lvl].next == NULL) { \ + __skip_integrity_failure_##decl("the head's %lu next node should not be NULL\n", lvl); \ + n_err++; \ + if (flags) \ + return n_err; \ + } \ + /* TODO: really? \ + if (node->field.sle_levels[lvl].next != slist->slh_tail) { \ + __skip_integrity_failure_##decl("after internal nodes, the head's %lu next node should always be the tail\n", lvl); \ + n_err++; \ + if (flags) \ + return n_err; \ + } \ + */ \ + } \ + \ + if (slist->slh_length > 0 && slist->slh_tail->field.sle_prev == slist->slh_head) { \ + __skip_integrity_failure_##decl("slist->slh_length is 0, but tail->prev == head, not an internal node\n"); \ + n_err++; \ + if (flags) \ + return n_err; \ + } \ + \ + /* Validate the head node */ \ + \ + /* Validate the tail node */ \ + \ + /* Validate each node */ \ + SKIPLIST_FOREACH_H2T(decl, prefix, field, slist, node, nth) \ + { \ + this = &node->field; \ + \ + if (this->sle_height > slist->slh_head->entries.sle_height) { \ + __skip_integrity_failure_##decl("the %lu node's [%p] height %lu is > head %lu\n", nth, (void *)node, this->sle_height, \ + slist->slh_head->entries.sle_height); \ + n_err++; \ + if (flags) \ + return n_err; \ + } \ + \ + if (this->sle_levels == NULL) { \ + __skip_integrity_failure_##decl("the %lu node's [%p] next field should never be NULL\n", nth, (void *)node); \ + n_err++; \ + if (flags) \ + return n_err; \ + } \ + \ + if (this->sle_prev == NULL) { \ + __skip_integrity_failure_##decl("the %lu node [%p] prev field should never be NULL\n", nth, (void *)node); \ + n_err++; \ + if (flags) \ + return n_err; \ + } \ + \ + __SKIP_ENTRIES_B2T(field, node) \ + { \ + if (this->sle_levels[lvl].next == NULL) { \ + __skip_integrity_failure_##decl("the %lu node's next[%lu] should not be NULL\n", nth, lvl); \ + n_err++; \ + if (flags) \ + return n_err; \ + } \ + n = lvl; \ + if (this->sle_levels[lvl].next == slist->slh_tail) \ + break; \ + } \ + n++; \ + __SKIP_ENTRIES_B2T_FROM(field, node, n) \ + { \ + if (this->sle_levels[lvl].next == NULL) { \ + __skip_integrity_failure_##decl("after the %lunth the %lu node's next[%lu] should not be NULL\n", n, nth, lvl); \ + n_err++; \ + if (flags) \ + return n_err; \ + } else if (this->sle_levels[lvl].next != slist->slh_tail) { \ + __skip_integrity_failure_##decl("after the %lunth the %lu node's next[%lu] should point to the tail\n", n, nth, lvl); \ + n_err++; \ + if (flags) \ + return n_err; \ + } \ + } \ + \ + decl##_node_t *a = (decl##_node_t *)(uintptr_t)this->sle_levels; \ + decl##_node_t *b = (decl##_node_t *)(intptr_t)((uintptr_t)node + sizeof(decl##_node_t)); \ + if (a != b) { \ + __skip_integrity_failure_##decl("the %lu node's [%p] next field isn't at the proper offset relative to the node\n", nth, (void *)node); \ + n_err++; \ + if (flags) \ + return n_err; \ + } \ + \ + next = this->sle_levels[0].next; \ + prev = this->sle_prev; \ + if (__skip_compare_nodes_##decl(slist, node, node, slist->slh_aux) != 0) { \ + __skip_integrity_failure_##decl("the %lu node [%p] is not equal to itself\n", nth, (void *)node); \ + n_err++; \ + if (flags) \ + return n_err; \ + } \ + \ + if (__skip_compare_nodes_##decl(slist, node, prev, slist->slh_aux) < 0) { \ + __skip_integrity_failure_##decl("the %lu node [%p] is not greater than the prev node [%p]\n", nth, (void *)node, (void *)prev); \ + n_err++; \ + if (flags) \ + return n_err; \ + } \ + \ + if (__skip_compare_nodes_##decl(slist, node, next, slist->slh_aux) > 0) { \ + __skip_integrity_failure_##decl("the %lu node [%p] is not less than the next node [%p]\n", nth, (void *)node, (void *)next); \ + n_err++; \ + if (flags) \ + return n_err; \ + } \ + \ + if (__skip_compare_nodes_##decl(slist, prev, node, slist->slh_aux) > 0) { \ + __skip_integrity_failure_##decl("the prev node [%p] is not less than the %lu node [%p]\n", (void *)prev, nth, (void *)node); \ + n_err++; \ + if (flags) \ + return n_err; \ + } \ + \ + if (__skip_compare_nodes_##decl(slist, next, node, slist->slh_aux) < 0) { \ + __skip_integrity_failure_##decl("the next node [%p] is not greater than the %lu node [%p]\n", (void *)next, nth, (void *)node); \ + n_err++; \ + if (flags) \ + return n_err; \ + } \ + } \ + \ + if (slist->slh_length != nth) { \ + __skip_integrity_failure_##decl("slist->slh_length (%lu) doesn't match the count (%lu) of nodes between the head and tail\n", slist->slh_length, \ + nth); \ + n_err++; \ + if (flags) \ + return n_err; \ + } \ + \ + return 0; \ } #define SKIPLIST_DECL_ACCESS(decl, prefix, key, ktype, value, vtype, qblk, rblk) \ @@ -1808,7 +1746,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li { \ int rc; \ decl##_node_t *node; \ - rc = prefix##skip_alloc_node_##decl(slist, &node); \ + rc = prefix##skip_alloc_node_##decl(&node); \ if (rc) \ return rc; \ node->key = key; \ @@ -1829,7 +1767,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li { \ int rc; \ decl##_node_t *node; \ - rc = prefix##skip_alloc_node_##decl(slist, &node); \ + rc = prefix##skip_alloc_node_##decl(&node); \ if (rc) \ return rc; \ node->key = key; \ -- 2.43.4 From ded941b5eed5a33beed184e895a388d054fefa7a Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Sun, 26 May 2024 21:37:16 -0400 Subject: [PATCH 04/11] WIP --- examples/ex1.c | 304 +++++++++++++++++-------------------------------- examples/ex2.c | 28 ++--- include/sl.h | 62 +++++----- 3 files changed, 148 insertions(+), 246 deletions(-) diff --git a/examples/ex1.c b/examples/ex1.c index b357b27..ce8435d 100644 --- a/examples/ex1.c +++ b/examples/ex1.c @@ -5,12 +5,6 @@ // --------------------------------------------------------------------------- #define DEBUG #define SKIPLIST_DIAGNOSTIC -/* Setting SKIPLIST_MAX_HEIGHT will do two things: - * 1) limit our max height across all instances of this data structure. - * 2) remove a heap allocation on frequently used paths, insert/remove/etc. - * so, use it when you need it. - */ -#define SKIPLIST_MAX_HEIGHT 12 // Include our monolithic ADT, the Skiplist! // --------------------------------------------------------------------------- @@ -20,8 +14,8 @@ // --------------------------------------------------------------------------- #define TEST_ARRAY_SIZE 10 #define VALIDATE -//define SNAPSHOTS -//define TODO_RESTORE_SNAPSHOTS +// define SNAPSHOTS +// define TODO_RESTORE_SNAPSHOTS #define STABLE_SEED #define DOT @@ -181,7 +175,7 @@ SKIPLIST_DECL_VALIDATE(ex, api_, entries) */ SKIPLIST_DECL_DOT(ex, api_, entries) -void +static void sprintf_ex_node(ex_node_t *node, char *buf) { sprintf(buf, "%d:%s", node->key, node->value); @@ -189,51 +183,8 @@ sprintf_ex_node(ex_node_t *node, char *buf) // Function for this demo application. // --------------------------------------------------------------------------- -int __xorshift32_state = 0; - -// Xorshift algorithm for PRNG -uint32_t -xorshift32() -{ - uint32_t x = __xorshift32_state; - if (x == 0) - x = 123456789; - x ^= x << 13; - x ^= x >> 17; - x ^= x << 5; - __xorshift32_state = x; - return x; -} - -void -xorshift32_seed() -{ - // Seed the PRNG -#ifdef STABLE_SEED - __xorshift32_state = 8675309; -#else - __xorshift32_state = (unsigned int)time(NULL) ^ getpid(); -#endif -} - -static char * -to_lower(char *str) -{ - char *p = str; - for (; *p; ++p) - *p = (char)(*p >= 'A' && *p <= 'Z' ? *p | 0x60 : *p); - return str; -} - -static char * -to_upper(char *str) -{ - char *p = str; - for (; *p; ++p) - *p = (char)(*p >= 'a' && *p <= 'z' ? *p & ~0x20 : *p); - return str; -} +/* convert a number into the Roman numeral equivalent, allocates a string caller must free */ static char * int_to_roman_numeral(int num) { @@ -266,18 +217,11 @@ int_to_roman_numeral(int num) return res; } -void -shuffle(int *array, size_t n) +/* calculate the floor of the log base 2 of a number m (⌊log2(m)⌋) */ +static int +floor_log2(unsigned int m) { - if (n > 1) { - size_t i; - for (i = n - 1; i > 0; i--) { - size_t j = (unsigned int)(xorshift32() % (i + 1)); /* NOLINT(*-msc50-cpp) */ - int t = array[j]; - array[j] = array[i]; - array[i] = t; - } - } + return (int)floor(log(m) / log(2)); } #ifdef TODO_RESTORE_SNAPSHOTS @@ -289,17 +233,25 @@ typedef struct { #endif // --------------------------------------------------------------------------- + +/* The head node's height is always 1 more than the tallest node, that location + is where we store the total hits, or "m". */ +#define splay_list_m(m) list->slh_head->entries.sle_levels[list->slh_head->entries.sle_height].hits + int main() { int rc; + char *numeral; +#ifdef DOT + char msg[1024]; + memset(msg, 0, 1024); +#endif #ifdef TODO_RESTORE_SNAPSHOTS size_t n_snaps = 0; snap_info_t snaps[TEST_ARRAY_SIZE * 2 + 1]; #endif - xorshift32_seed(); - #ifdef DOT of = fopen("/tmp/ex1.dot", "w"); if (!of) { @@ -314,162 +266,114 @@ main() return ENOMEM; rc = api_skip_init_ex(list); - list->slh_prng_state = 12; if (rc) return rc; + + /* Set the PRNG state to a known constant for reproducible generation, easing debugging. */ + list->slh_prng_state = 12; +#ifdef SNAPSHOTS api_skip_snapshots_init_ex(list); +#endif #ifdef DOT api_skip_dot_ex(of, list, gen++, "init", sprintf_ex_node); #endif - if (api_skip_get_ex(list, 0) != NULL) - perror("found a non-existent item!"); - api_skip_del_ex(list, 0); - CHECK; - /* Insert TEST_ARRAY_SIZE key/value pairs into the list. */ - int i, j; - char *numeral; -#ifdef DOT - char msg[1024]; - memset(msg, 0, 1024); -#endif - int amt = TEST_ARRAY_SIZE, asz = (amt * 2) + 1; - int array[(TEST_ARRAY_SIZE * 2) + 1]; - for (j = 0, i = -amt; i <= amt; i++, j++) - array[j] = i; - shuffle(array, asz); + /* This example mirrors the example given in the paper about splay-lists + to test implementation against research. */ - for (i = 0; i < asz; i++) { -#ifdef SNAPSHOTS - api_skip_snapshot_ex(list); -#elif defined(TODO_RESTORE_SNAPSHOTS) - /* Snapshot the first iteration, and then every 5th after that. */ - if (i % 5 == 0) { - snaps[i].length = api_skip_length_ex(list); - snaps[i].key = array[i]; - snaps[i].snap_id = api_skip_snapshot_ex(list); - n_snaps++; - CHECK; - } -#endif - numeral = to_lower(int_to_roman_numeral(array[i])); - rc = api_skip_put_ex(list, array[i], numeral); - CHECK; + for (int i = 1; i < 8; i++) { + numeral = int_to_roman_numeral(i); + if ((rc = api_skip_put_ex(list, i, numeral))) + perror("put failed"); #ifdef DOT sprintf(msg, "put key: %d value: %s", i, numeral); api_skip_dot_ex(of, list, gen++, msg, sprintf_ex_node); #endif - char *v = api_skip_get_ex(list, array[i]); - CHECK; - char *upper_numeral = calloc(1, strlen(v) + 1); - strncpy(upper_numeral, v, strlen(v)); - assert(strncmp(v, upper_numeral, strlen(upper_numeral)) == 0); - to_upper(upper_numeral); - api_skip_set_ex(list, array[i], upper_numeral); - CHECK; } - numeral = int_to_roman_numeral(-1); - api_skip_dup_ex(list, -1, numeral); CHECK; -#ifdef DOT - sprintf(msg, "put dup key: %d value: %s", i, numeral); - api_skip_dot_ex(of, list, gen++, msg, sprintf_ex_node); -#endif - numeral = int_to_roman_numeral(1); - api_skip_dup_ex(list, 1, numeral); - CHECK; -#ifdef DOT - sprintf(msg, "put dup key: %d value: %s", i, numeral); - api_skip_dot_ex(of, list, gen++, msg, sprintf_ex_node); -#endif - api_skip_del_ex(list, 0); - CHECK; - if (api_skip_get_ex(list, 0) != NULL) - perror("found a deleted item!"); - api_skip_del_ex(list, 0); - CHECK; - if (api_skip_get_ex(list, 0) != NULL) - perror("found a deleted item!"); - int key = TEST_ARRAY_SIZE + 1; - api_skip_del_ex(list, key); - CHECK; - key = -(TEST_ARRAY_SIZE)-1; - api_skip_del_ex(list, key); + /* Now we're going to poke around the internals a bit to set things up. + This first time around we're going to build the list by hand, later + we'll ensure that we can build this shape using only API calls. */ + + ex_node_t *head = list->slh_head; + ex_node_t *tail = list->slh_tail; + ex_node_t *node_1 = head->entries.sle_levels[0].next; + ex_node_t *node_2 = node_1->entries.sle_levels[0].next; + ex_node_t *node_3 = node_2->entries.sle_levels[0].next; + ex_node_t *node_4 = node_3->entries.sle_levels[0].next; + ex_node_t *node_5 = node_4->entries.sle_levels[0].next; + ex_node_t *node_6 = node_5->entries.sle_levels[0].next; + + // Head/Tail-nodes are height 3, ... + head->entries.sle_height = tail->entries.sle_height = 3; + + // Head-node + head->entries.sle_levels[3].hits = 10; + head->entries.sle_levels[2].hits = 5; + head->entries.sle_levels[1].hits = 1; + head->entries.sle_levels[0].hits = 1; + head->entries.sle_levels[1].next = node_2; + head->entries.sle_levels[2].next = node_6; + head->entries.sle_levels[3].next = tail; + + // Tail-node + tail->entries.sle_levels[3].hits = 0; + tail->entries.sle_levels[2].hits = 0; + tail->entries.sle_levels[1].hits = 0; + tail->entries.sle_levels[0].hits = 1; + tail->entries.sle_levels[1].next = tail; + tail->entries.sle_levels[2].next = tail; + tail->entries.sle_levels[3].next = tail; + + // First node has key "1", height "0", hits(0) = 1 + node_1->entries.sle_height = 0; + node_1->entries.sle_levels[0].hits = 1; + + // Second node has key "2", height "1", hits(0) = 1, hits(1) = 0 + node_2->entries.sle_height = 1; + node_2->entries.sle_levels[0].hits = 1; + node_2->entries.sle_levels[1].hits = 0; + node_2->entries.sle_levels[1].next = node_3; + + // Third node has key "3", height "1", hits(0) = 1, hits(1) = 2 + node_3->entries.sle_height = 1; + node_3->entries.sle_levels[0].hits = 1; + node_3->entries.sle_levels[1].hits = 2; + node_3->entries.sle_levels[1].next = node_6; + + // Fourth node has key "4", height "0", hits(0) = 1 + node_4->entries.sle_height = 0; + node_4->entries.sle_levels[0].hits = 1; + + // Fifth node has key "5", height "0", hits(0) = 1 + node_5->entries.sle_height = 0; + node_5->entries.sle_levels[0].hits = 1; + + // Sixth node has key "6", height "2", hits(0) = 5, hits(1) = 0, hits(2) = 0 + node_6->entries.sle_height = 2; + node_6->entries.sle_levels[0].hits = 5; + node_6->entries.sle_levels[1].hits = 0; + node_6->entries.sle_levels[2].hits = 0; + node_6->entries.sle_levels[1].next = tail; + node_6->entries.sle_levels[2].next = tail; + CHECK; #ifdef DOT - sprintf(msg, "deleted key: %d, value: %s", 0, numeral); + sprintf(msg, "manually adjusted"); api_skip_dot_ex(of, list, gen++, msg, sprintf_ex_node); #endif - numeral = int_to_roman_numeral(-(TEST_ARRAY_SIZE)); - assert(strcmp(api_skip_pos_ex(list, SKIP_GTE, -(TEST_ARRAY_SIZE)-1)->value, numeral) == 0); - free(numeral); - numeral = int_to_roman_numeral(-2); - assert(strcmp(api_skip_pos_ex(list, SKIP_GTE, -2)->value, numeral) == 0); - free(numeral); - numeral = int_to_roman_numeral(1); - assert(strcmp(api_skip_pos_ex(list, SKIP_GTE, 0)->value, numeral) == 0); - free(numeral); - numeral = int_to_roman_numeral(2); - assert(strcmp(api_skip_pos_ex(list, SKIP_GTE, 2)->value, numeral) == 0); - free(numeral); - assert(api_skip_pos_ex(list, SKIP_GTE, (TEST_ARRAY_SIZE + 1)) == NULL); + printf("m = %ld\n", splay_list_m(list)); + printf("(⌊log2(m)⌋) = %d\n", floor_log2(splay_list_m(list))); - numeral = int_to_roman_numeral(-(TEST_ARRAY_SIZE)); - assert(strcmp(api_skip_pos_ex(list, SKIP_GT, -(TEST_ARRAY_SIZE)-1)->value, numeral) == 0); - free(numeral); - numeral = int_to_roman_numeral(-1); - assert(strcmp(api_skip_pos_ex(list, SKIP_GT, -2)->value, numeral) == 0); - free(numeral); - numeral = int_to_roman_numeral(1); - assert(strcmp(api_skip_pos_ex(list, SKIP_GT, 0)->value, numeral) == 0); - free(numeral); - numeral = int_to_roman_numeral(2); - assert(strcmp(api_skip_pos_ex(list, SKIP_GT, 1)->value, numeral) == 0); - free(numeral); - assert(api_skip_pos_ex(list, SKIP_GT, TEST_ARRAY_SIZE) == NULL); - - assert(api_skip_pos_ex(list, SKIP_LT, -(TEST_ARRAY_SIZE)) == NULL); - numeral = int_to_roman_numeral(-2); - assert(strcmp(api_skip_pos_ex(list, SKIP_LT, -1)->value, numeral) == 0); - free(numeral); - numeral = int_to_roman_numeral(-1); - assert(strcmp(api_skip_pos_ex(list, SKIP_LT, 0)->value, numeral) == 0); - free(numeral); - numeral = int_to_roman_numeral(1); - assert(strcmp(api_skip_pos_ex(list, SKIP_LT, 2)->value, numeral) == 0); - free(numeral); - numeral = int_to_roman_numeral(TEST_ARRAY_SIZE); - assert(strcmp(api_skip_pos_ex(list, SKIP_LT, (TEST_ARRAY_SIZE + 1))->value, numeral) == 0); - free(numeral); - - assert(api_skip_pos_ex(list, SKIP_LTE, -(TEST_ARRAY_SIZE)-1) == NULL); - numeral = int_to_roman_numeral(-2); - assert(strcmp(api_skip_pos_ex(list, SKIP_LTE, -2)->value, numeral) == 0); - free(numeral); - numeral = int_to_roman_numeral(-1); - assert(strcmp(api_skip_pos_ex(list, SKIP_LTE, 0)->value, numeral) == 0); - free(numeral); - numeral = int_to_roman_numeral(2); - assert(strcmp(api_skip_pos_ex(list, SKIP_LTE, 2)->value, numeral) == 0); - free(numeral); - numeral = int_to_roman_numeral(TEST_ARRAY_SIZE); - assert(strcmp(api_skip_pos_ex(list, SKIP_LTE, (TEST_ARRAY_SIZE + 1))->value, numeral) == 0); - free(numeral); - -#ifdef TODO_RESTORE_SNAPSHOTS - // Walk backward by 2 and test snapshot restore. - for (i = n_snaps; i > 0; i -= 2) { - api_skip_restore_snapshot_ex(list, snaps[i].snap_id); - CHECK; - assert(api_skip_length_ex(list) == snaps[i].length); - numeral = int_to_roman_numeral(snaps[i].key); - assert(strncmp(api_skip_get_ex(list, snaps[i].key), numeral, strlen(numeral)) == 0); - free(numeral); - } - api_skip_release_snapshots_ex(list); + if (!(rc = api_skip_contains_ex(list, 5))) + perror("missing element 5"); + CHECK; +#ifdef DOT + sprintf(msg, "contains(5)"); + api_skip_dot_ex(of, list, gen++, msg, sprintf_ex_node); #endif #ifdef DOT diff --git a/examples/ex2.c b/examples/ex2.c index b357b27..fb24c4b 100644 --- a/examples/ex2.c +++ b/examples/ex2.c @@ -5,12 +5,6 @@ // --------------------------------------------------------------------------- #define DEBUG #define SKIPLIST_DIAGNOSTIC -/* Setting SKIPLIST_MAX_HEIGHT will do two things: - * 1) limit our max height across all instances of this data structure. - * 2) remove a heap allocation on frequently used paths, insert/remove/etc. - * so, use it when you need it. - */ -#define SKIPLIST_MAX_HEIGHT 12 // Include our monolithic ADT, the Skiplist! // --------------------------------------------------------------------------- @@ -181,7 +175,7 @@ SKIPLIST_DECL_VALIDATE(ex, api_, entries) */ SKIPLIST_DECL_DOT(ex, api_, entries) -void +static void sprintf_ex_node(ex_node_t *node, char *buf) { sprintf(buf, "%d:%s", node->key, node->value); @@ -192,7 +186,7 @@ sprintf_ex_node(ex_node_t *node, char *buf) int __xorshift32_state = 0; // Xorshift algorithm for PRNG -uint32_t +static uint32_t xorshift32() { uint32_t x = __xorshift32_state; @@ -205,7 +199,7 @@ xorshift32() return x; } -void +static void xorshift32_seed() { // Seed the PRNG @@ -216,6 +210,7 @@ xorshift32_seed() #endif } +/* convert upper case characters to lower case */ static char * to_lower(char *str) { @@ -225,6 +220,7 @@ to_lower(char *str) return str; } +/* convert lower case characters to upper case */ static char * to_upper(char *str) { @@ -234,6 +230,7 @@ to_upper(char *str) return str; } +/* convert a number into the Roman numeral equivalent, allocates a string caller must free */ static char * int_to_roman_numeral(int num) { @@ -266,7 +263,8 @@ int_to_roman_numeral(int num) return res; } -void +/* shuffle an array of length n */ +static void shuffle(int *array, size_t n) { if (n > 1) { @@ -301,9 +299,9 @@ main() xorshift32_seed(); #ifdef DOT - of = fopen("/tmp/ex1.dot", "w"); + of = fopen("/tmp/ex2.dot", "w"); if (!of) { - perror("Failed to open file /tmp/ex1.dot"); + perror("Failed to open file /tmp/ex2.dot"); return 1; } #endif @@ -314,10 +312,14 @@ main() return ENOMEM; rc = api_skip_init_ex(list); - list->slh_prng_state = 12; if (rc) return rc; + + /* Set the PRNG state to a known constant for reproducible generation, easing debugging. */ + list->slh_prng_state = 12; +#ifdef SNAPSHOTS api_skip_snapshots_init_ex(list); +#endif #ifdef DOT api_skip_dot_ex(of, list, gen++, "init", sprintf_ex_node); #endif diff --git a/include/sl.h b/include/sl.h index ada9558..8fdd752 100644 --- a/include/sl.h +++ b/include/sl.h @@ -205,10 +205,10 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li #endif /* - * Every Skiplist node has to hvae an additional section of data used to manage + * Every Skiplist node has to have an additional section of data used to manage * nodes in the list. The rest of the datastructure is defined by the use case. * This housekeeping portion is the SKIPLIST_ENTRY, see below. It maintains the - * array of forward pointers to nodes and has a height, this height is a a + * array of forward pointers to nodes and has a height, this height is a * zero-based count of levels, so a height of `0` means one (1) level and a * height of `4` means five (5) forward pointers (levels) in the node, [0-4). */ @@ -241,11 +241,14 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li #define __SKIP_ENTRIES_B2T_FROM(field, elm, off) for (size_t lvl = off; lvl <= elm->field.sle_height; lvl++) #define __SKIP_IS_LAST_ENTRY_B2T() if (lvl + 1 == elm->field.sle_height) -/* Iterate over the subtree to the left (v, or 'lt') and right (u, or 'gt') or "CHu" and "CHv". */ +/* Iterate over the left (v) subtree or right (u) subtree or "CHu" and "CHv". */ #define __SKIP_SUBTREE_CHv(decl, field, list, path, nth) \ for (decl##_node_t *elm = path[nth].node; elm->field.sle_levels[path[nth].in].next == path[nth].node; elm = elm->field.sle_prev) #define __SKIP_SUBTREE_CHu(decl, field, list, path, nth) \ for (decl##_node_t *elm = path[nth].node; elm != path[nth].node->field.sle_levels[0].next; elm = elm->field.sle_levels[0].next) +#define __SKIP_SUBTREE_CHhx(decl, field, list, path, nth) \ + for (decl##_node_t *elm = path[nth].node->field.sle_levels[path[nth].in].next->field.sle_prev; elm != path[nth].node->field.sle_prev; \ + elm = elm->field.sle_prev) /* * Skiplist declarations and access methods. @@ -639,8 +642,8 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li /* Total hits, `k`, across all nodes. */ \ m_total_hits = slist->slh_head->field.sle_levels[slist->slh_head->field.sle_height].hits; \ \ - /* Height of the head node, should be close to floor(log(m_total_hits)). */ \ - k_threshold = slist->slh_head->field.sle_height + 1; \ + /* Height of the head node, should be equal to floor(log(m_total_hits)). */ \ + k_threshold = slist->slh_head->field.sle_height; \ \ /* Moving backwards along the path... \ * - path[0] contains a match, if there was one \ @@ -648,17 +651,19 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li * - path[len] is where the locate() terminated, just before path[0] \ * if there was a match \ */ \ - for (i = 1; i <= len; i++) { \ + i = 0; \ + do { \ + hits_CHu = hits_CHv = 0; \ if (path[i].node == slist->slh_head || path[i].node == slist->slh_tail) \ continue; \ \ - __SKIP_SUBTREE_CHu(decl, field, slist, path, i) \ + __SKIP_SUBTREE_CHhx(decl, field, slist, path, i) \ { \ - hits_CHu += elm->field.sle_levels[i].hits; \ + hits_CHu += elm->field.sle_levels[0].hits; \ } \ - __SKIP_SUBTREE_CHv(decl, field, slist, path, i) \ + __SKIP_SUBTREE_CHhx(decl, field, slist, path, i + 1) \ { \ - hits_CHv += elm->field.sle_levels[i].hits; \ + hits_CHv += elm->field.sle_levels[0].hits; \ } \ u_hits = hits_CHu + hits_CHv; \ \ @@ -710,7 +715,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li if (path[i - 1].in != 0) \ path[i - 1].node->field.sle_levels[path[i - 1].in].next->field.sle_levels[path[i - 1].in].next = path[i].node; \ } \ - } \ + } while (i++ < len); \ } \ \ /** \ @@ -743,7 +748,8 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li path[i + 1].pu += elm->field.sle_levels[path[i + 1].in].hits; \ } \ path[i + 1].node = elm; \ - path[i + 1].node->field.sle_levels[path[i + 1].in].hits++; \ + if (path[i + 1].in > 0) \ + path[i + 1].node->field.sle_levels[path[i + 1].in].hits++; \ len++; \ } while (i--); \ elm = elm->field.sle_levels[0].next; \ @@ -776,16 +782,6 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li \ memset(path, 0, sizeof(__skiplist_path_##decl##_t) * SKIPLIST_MAX_HEIGHT + 1); \ \ - /* First element in path should be NULL, rest should start pointing at tail. */ \ - path[0].node = NULL; \ - path[0].in = 0; \ - path[0].pu = 0; \ - for (i = 1; i < slist->slh_head->entries.sle_height + 1; i++) { \ - path[i].node = slist->slh_tail; \ - path[i].in = 0; \ - path[i].pu = 0; \ - } \ - \ /* Find a `path` to `new` in the list and a match (`path[0]`) if it exists. */ \ len = __skip_locate_##decl(slist, new, path); \ node = path[0].node; \ @@ -1074,7 +1070,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li if (np > 0) \ return np; \ \ - /* Increase the the list's era/age. */ \ + /* Increase the list's era/age. */ \ slist->slh_snap.cur_era++; \ } \ \ @@ -1116,7 +1112,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li if (np > 0) \ return np; \ \ - /* Increase the the list's era/age. */ \ + /* Increase the list's era/age. */ \ slist->slh_snap.cur_era++; \ } \ /* We found it, set the next->prev to the node->prev keeping in mind \ @@ -1129,8 +1125,8 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li break; \ /* ... adjust the next pointer at that level. */ \ path[i + 1].node->field.sle_levels[i].next = node->field.sle_levels[i].next; \ - /* Adjust the height so we're only pointing at the tail once at \ - the top so we don't waste steps later when searching. */ \ + /* Adjust the height such that we're only pointing at the tail once at \ + the top that way we don't waste steps later when searching. */ \ if (path[i + 1].node->field.sle_levels[i].next == slist->slh_tail) { \ height = path[i + 1].node->field.sle_height; \ path[i + 1].node->field.sle_height = height - 1; \ @@ -1843,7 +1839,6 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li 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]; \ - size_t width; \ decl##_node_t *next; \ \ __skip_dot_write_node_##decl(os, nsg, node); \ @@ -1852,8 +1847,9 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li __SKIP_ENTRIES_T2B(field, node) \ { \ next = (node->field.sle_levels[lvl].next == slist->slh_tail) ? NULL : node->field.sle_levels[lvl].next; \ - width = __skip_dot_width_##decl(slist, node, next ? next : slist->slh_tail); \ - fprintf(os, " { %lu | ", lvl, width, lvl); \ + (void)__skip_dot_width_##decl; \ + /* size_t width = __skip_dot_width_##decl(slist, node, next ? next : slist->slh_tail); */ \ + fprintf(os, " { %lu | ", lvl, node->field.sle_levels[lvl].hits, lvl); \ if (next) \ fprintf(os, "%p } |", (void *)next); \ else \ @@ -1862,7 +1858,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li } \ if (fn) { \ fn(node, buf); \ - fprintf(os, " \u219F %lu \u226B %s \"\n", node->field.sle_height + 1, buf); \ + fprintf(os, " \u219F %lu \u226B %s \"\n", node->field.sle_height, buf); \ } else { \ fprintf(os, " \u219F %lu \"\n", node->field.sle_height); \ } \ @@ -1927,7 +1923,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li int prefix##skip_dot_##decl(FILE *os, decl##_t *slist, size_t nsg, char *msg, skip_sprintf_node_##decl##_t fn) \ { \ int letitgo = 0; \ - size_t width, i; \ + size_t i; \ decl##_node_t *node, *next; \ \ if (slist == NULL || fn == NULL) \ @@ -1961,8 +1957,8 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li __SKIP_ENTRIES_T2B(field, node) \ { \ next = (node->field.sle_levels[lvl].next == slist->slh_tail) ? NULL : node->field.sle_levels[lvl].next; \ - width = __skip_dot_width_##decl(slist, node, next ? next : slist->slh_tail); \ - fprintf(os, "{ %lu | ", width, lvl); \ + /* size_t width = __skip_dot_width_##decl(slist, node, next ? next : slist->slh_tail); */ \ + fprintf(os, "{ %lu | ", node->field.sle_levels[lvl].hits, lvl); \ if (next) \ fprintf(os, "%p }", (void *)next); \ else \ -- 2.43.4 From d327c256aff092a9426ad11bf746669c7c2e8a85 Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Tue, 28 May 2024 11:05:39 -0400 Subject: [PATCH 05/11] WIP --- examples/ex1.c | 4 +- include/sl.h | 1849 ++++++++++++++++++++++++------------------------ 2 files changed, 929 insertions(+), 924 deletions(-) diff --git a/examples/ex1.c b/examples/ex1.c index ce8435d..efe5f46 100644 --- a/examples/ex1.c +++ b/examples/ex1.c @@ -365,12 +365,12 @@ main() api_skip_dot_ex(of, list, gen++, msg, sprintf_ex_node); #endif - printf("m = %ld\n", splay_list_m(list)); + printf("m = %ld; ", splay_list_m(list)); printf("(⌊log2(m)⌋) = %d\n", floor_log2(splay_list_m(list))); if (!(rc = api_skip_contains_ex(list, 5))) perror("missing element 5"); - CHECK; + #ifdef DOT sprintf(msg, "contains(5)"); api_skip_dot_ex(of, list, gen++, msg, sprintf_ex_node); diff --git a/include/sl.h b/include/sl.h index 8fdd752..634dd31 100644 --- a/include/sl.h +++ b/include/sl.h @@ -246,932 +246,937 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li for (decl##_node_t *elm = path[nth].node; elm->field.sle_levels[path[nth].in].next == path[nth].node; elm = elm->field.sle_prev) #define __SKIP_SUBTREE_CHu(decl, field, list, path, nth) \ for (decl##_node_t *elm = path[nth].node; elm != path[nth].node->field.sle_levels[0].next; elm = elm->field.sle_levels[0].next) -#define __SKIP_SUBTREE_CHhx(decl, field, list, path, nth) \ - for (decl##_node_t *elm = path[nth].node->field.sle_levels[path[nth].in].next->field.sle_prev; elm != path[nth].node->field.sle_prev; \ - elm = elm->field.sle_prev) +/* Iterate over a subtree starting at provided path element, u = path.in */ +#define __SKIP_SUBTREE_CHux(decl, field, list, path, level) \ + for (decl##_node_t *elm = path.node->field.sle_levels[level].next->field.sle_prev; elm != path.node->field.sle_prev; elm = elm->field.sle_prev) /* * Skiplist declarations and access methods. */ -#define SKIPLIST_DECL(decl, prefix, field, compare_entries_blk, free_entry_blk, update_entry_blk, archive_entry_blk, sizeof_entry_blk) \ - \ - /* Used when positioning a cursor within a Skiplist. */ \ - typedef enum { SKIP_EQ = 0, SKIP_LTE = -1, SKIP_LT = -2, SKIP_GTE = 1, SKIP_GT = 2 } skip_pos_##decl_t; \ - \ - /* Skiplist node type */ \ - typedef struct decl##_node decl##_node_t; \ - \ - /* Skiplist type. */ \ - typedef struct decl decl##_t; \ - \ - /* Skiplist structure */ \ - struct decl { \ - size_t slh_length; \ - void *slh_aux; \ - int slh_prng_state; \ - decl##_node_t *slh_head; \ - decl##_node_t *slh_tail; \ - struct { \ - void (*free_entry)(decl##_node_t *); \ - int (*update_entry)(decl##_node_t *, void *); \ - int (*archive_entry)(decl##_node_t *, const decl##_node_t *); \ - size_t (*sizeof_entry)(decl##_node_t *); \ - int (*compare_entries)(decl##_t *, decl##_node_t *, decl##_node_t *, void *); \ - \ - /* Optional: Snapshots */ \ - int (*snapshot_preserve_node)(decl##_t * slist, const decl##_node_t *src, decl##_node_t **preserved); \ - void (*snapshot_release)(decl##_t *); \ - } slh_fns; \ - struct { \ - size_t cur_era; \ - size_t pres_era; \ - decl##_node_t *pres; \ - } slh_snap; \ - }; \ - \ - typedef struct __skiplist_path_##decl { \ - decl##_node_t *node; /* node traversed in the act of location */ \ - size_t in; /* level at which the node was intersected */ \ - size_t pu; /* sum of hits from intersection to level[1] */ \ - } __skiplist_path_##decl##_t; \ - \ - /* Xorshift algorithm for PRNG */ \ - static uint32_t __##decl##_xorshift32(int *state) \ - { \ - uint32_t x = *state; \ - if (x == 0) \ - x = 123456789; \ - x ^= x << 13; \ - x ^= x >> 17; \ - x ^= x << 5; \ - *state = x; \ - return x; \ - } \ - \ - /** \ - * -- __skip_compare_entries_fn_ \ - * \ - * Wraps the `compare_entries_blk` code into `slh_fns.compare_entries`. \ - */ \ - static int __skip_compare_entries_fn_##decl(decl##_t *list, decl##_node_t *a, decl##_node_t *b, void *aux) \ - { \ - compare_entries_blk; \ - } \ - \ - /** \ - * -- __skip_free_entry_fn \ - * \ - * Wraps the `free_entry_blk` code into `slh_fns.free_entry`. \ - */ \ - static void __skip_free_entry_fn_##decl(decl##_node_t *node) \ - { \ - free_entry_blk; \ - } \ - \ - /** \ - * -- __skip_update_entry_fn_ \ - * \ - * Wraps the `update_entry_blk` code into `slh_fns.update_entry`. \ - */ \ - static int __skip_update_entry_fn_##decl(decl##_node_t *node, void *value) \ - { \ - int rc = 0; \ - update_entry_blk; \ - return rc; \ - } \ - \ - /** \ - * -- __skip_archive_entry_fn_ \ - * \ - * Wraps the `archive_entry_blk` code into `slh_fns.archive_entry`. \ - */ \ - static int __skip_archive_entry_fn_##decl(decl##_node_t *dest, const decl##_node_t *src) \ - { \ - int rc = 0; \ - archive_entry_blk; \ - return rc; \ - } \ - \ - /** \ - * -- __skip_sizeof_entry_fn_ \ - * \ - * Wraps the `sizeof_entry_blk` code into `slh_fns.sizeof_entry`. \ - */ \ - static size_t __skip_sizeof_entry_fn_##decl(decl##_node_t *node) \ - { \ - size_t bytes = 0; \ - sizeof_entry_blk; \ - return bytes; \ - } \ - \ - /** \ - * -- __skip_compare_nodes_ \ - * \ - * This function takes four arguments: \ - * - a reference to the Skiplist \ - * - the two nodes to compare, `a` and `b` \ - * - `aux` an additional auxiliary argument \ - * and returns: \ - * a < b : return -1 \ - * a == b : return 0 \ - * a > b : return 1 \ - */ \ - static int __skip_compare_nodes_##decl(decl##_t *slist, decl##_node_t *a, decl##_node_t *b, void *aux) \ - { \ - if (a == b) \ - return 0; \ - if (a == NULL) \ - return -1; \ - if (b == NULL) \ - return 1; \ - if (a == slist->slh_head || b == slist->slh_tail) \ - return -1; \ - if (a == slist->slh_tail || b == slist->slh_head) \ - return 1; \ - return slist->slh_fns.compare_entries(slist, a, b, aux); \ - } \ - \ - /** \ - * -- __skip_toss_ \ - * \ - * A "coin toss" function that is critical to the proper operation of the \ - * Skiplist. 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. \ - */ \ - static int __skip_toss_##decl(decl##_t *slist, size_t max) \ - { \ - size_t level = 0; \ - double probability = 0.5; \ - \ - double random_value = (double)__##decl##_xorshift32(&slist->slh_prng_state) / RAND_MAX; \ - while (random_value < probability && level < max) { \ - level++; \ - probability *= 0.5; \ - } \ - return level; \ - } \ - \ - /** \ - * -- skip_alloc_node_ \ - * \ - * Allocates a new node on the heap and sets default values. \ - */ \ - int prefix##skip_alloc_node_##decl(decl##_node_t **node) \ - { \ - decl##_node_t *n; \ - /* Calculate the size of the struct sle within decl##_node_t, multiply \ - by array size. (16/24 bytes on 32/64 bit systems) */ \ - size_t sle_arr_sz = sizeof(struct __skiplist_##decl##_level) * SKIPLIST_MAX_HEIGHT; \ - n = (decl##_node_t *)calloc(1, sizeof(decl##_node_t) + sle_arr_sz); \ - if (n == NULL) \ - return ENOMEM; \ - n->field.sle_height = 0; \ - n->field.sle_levels = (struct __skiplist_##decl##_level *)((uintptr_t)n + sizeof(decl##_node_t)); \ - *node = n; \ - return 0; \ - } \ - \ - /** \ - * -- skip_init_ \ - * \ - * Initializes a Skiplist to the default values, this must be \ - * called before using the list. \ - */ \ - int prefix##skip_init_##decl(decl##_t *slist) \ - { \ - int rc = 0; \ - size_t i; \ - \ - slist->slh_length = 0; \ - slist->slh_snap.cur_era = 0; \ - slist->slh_snap.pres_era = 0; \ - slist->slh_snap.pres = 0; \ - slist->slh_fns.free_entry = __skip_free_entry_fn_##decl; \ - slist->slh_fns.update_entry = __skip_update_entry_fn_##decl; \ - slist->slh_fns.archive_entry = __skip_archive_entry_fn_##decl; \ - slist->slh_fns.sizeof_entry = __skip_sizeof_entry_fn_##decl; \ - slist->slh_fns.compare_entries = __skip_compare_entries_fn_##decl; \ - rc = prefix##skip_alloc_node_##decl(&slist->slh_head); \ - if (rc) \ - goto fail; \ - rc = prefix##skip_alloc_node_##decl(&slist->slh_tail); \ - if (rc) \ - goto fail; \ - \ - /* Initial height at head is 0 (meaning 0 sub-lists), all next point to tail */ \ - slist->slh_head->field.sle_height = 1; \ - for (i = 0; i < slist->slh_head->entries.sle_height + 1; i++) \ - slist->slh_head->field.sle_levels[i].next = slist->slh_tail; \ - slist->slh_head->field.sle_prev = NULL; \ - \ - /* Initial height at tail is also 0 and all next point to tail */ \ - slist->slh_tail->field.sle_height = slist->slh_head->field.sle_height; \ - for (i = 0; i < slist->slh_head->entries.sle_height + 1; i++) \ - slist->slh_tail->field.sle_levels[i].next = NULL; \ - slist->slh_tail->field.sle_prev = slist->slh_head; \ - slist->slh_prng_state = ((unsigned int)time(NULL) ^ getpid()); \ - fail:; \ - return rc; \ - } \ - \ - /** \ - * -- skip_free_node_ \ - * \ - * Properly releases heap memory allocated for use as a node. \ - * This function invokes the `free_node_blk` within which you \ - * should release any heap objects or other resources held by \ - * this node in the list. \ - */ \ - void prefix##skip_free_node_##decl(decl##_t *slist, decl##_node_t *node) \ - { \ - slist->slh_fns.free_entry(node); \ - free(node); \ - } \ - \ - /** \ - * -- skip_length_ \ - * \ - * Returns the current length of the list. \ - */ \ - size_t prefix##skip_length_##decl(decl##_t *slist) \ - { \ - return slist->slh_length; \ - } \ - \ - /** \ - * -- skip_is_empty_ \ - * \ - * Returns non-zero when the list is empty. \ - */ \ - int prefix##skip_is_empty_##decl(decl##_t *slist) \ - { \ - return slist->slh_length == 0; \ - } \ - \ - /** \ - * -- skip_head_ \ - * \ - * Returns the node containing the first (smallest) element in the \ - * list which can be used to traverse the list. \ - */ \ - decl##_node_t *prefix##skip_head_##decl(decl##_t *slist) \ - { \ - return slist->slh_head->field.sle_levels[0].next == slist->slh_tail ? NULL : slist->slh_head->field.sle_levels[0].next; \ - } \ - \ - /** \ - * -- skip_tail_ \ - * \ - * Returns the node containing the last (largest) element in the \ - * list which can be used to traverse the list. \ - */ \ - decl##_node_t *prefix##skip_tail_##decl(decl##_t *slist) \ - { \ - if (slist == NULL) \ - return NULL; \ - return slist->slh_tail->field.sle_prev == slist->slh_head->field.sle_levels[0].next ? NULL : slist->slh_tail->field.sle_prev; \ - } \ - \ - /** \ - * -- skip_next_node_ \ - * \ - * A node reference can be thought of as a cursor. This moves the cursor \ - * to the next node in the list or returns NULL if the next is the tail. \ - */ \ - decl##_node_t *prefix##skip_next_node_##decl(decl##_t *slist, decl##_node_t *n) \ - { \ - if (slist == NULL || n == NULL) \ - return NULL; \ - if (n->field.sle_levels[0].next == slist->slh_tail) \ - return NULL; \ - return n->field.sle_levels[0].next; \ - } \ - \ - /** \ - * -- skip_prev_node_ \ - * \ - * A node reference can be thought of as a cursor. This moves the cursor \ - * to the previous node in the list or returns NULL if the previous node \ - * is the head. \ - */ \ - decl##_node_t *prefix##skip_prev_node_##decl(decl##_t *slist, decl##_node_t *n) \ - { \ - if (slist == NULL || n == NULL) \ - return NULL; \ - if (n->field.sle_prev == slist->slh_head) \ - return NULL; \ - return n->field.sle_prev; \ - } \ - \ - /** \ - * -- skip_release_ \ - * \ - * Release all nodes and their associated heap objects, but not the list \ - * itself. The list is still valid, only empty. \ - */ \ - void prefix##skip_release_##decl(decl##_t *slist) \ - { \ - decl##_node_t *node, *next; \ - \ - if (slist == NULL) \ - return; \ - if (prefix##skip_is_empty_##decl(slist)) \ - return; \ - node = slist->slh_head->field.sle_levels[0].next; \ - while (node != slist->slh_tail) { \ - next = node->field.sle_levels[0].next; \ - prefix##skip_free_node_##decl(slist, node); \ - node = next; \ - } \ - if (slist->slh_snap.pres_era > 0) \ - slist->slh_snap.cur_era++; \ - return; \ - } \ - \ - /** \ - * -- skip_to_array_ \ - * \ - * Returns a heap allocated array of nodes in the order they exist. \ - * This isn't maintained by the list, if you add/remove nodes it is \ - * no longer accurate. At [-1] is the length of the array. \ - * NOTE: Caller must deallocate. \ - */ \ - decl##_node_t **prefix##skip_to_array_##decl(decl##_t *slist) \ - { \ - size_t nth, len = prefix##skip_length_##decl(slist); \ - decl##_node_t *node, **nodes = NULL; \ - nodes = (decl##_node_t **)calloc(sizeof(decl##_node_t *), len + 1); \ - if (nodes != NULL) { \ - nodes[0] = (decl##_node_t *)(uintptr_t)len; \ - nodes++; \ - SKIPLIST_FOREACH_H2T(decl, prefix, field, slist, node, nth) \ - { \ - nodes[nth] = node; \ - } \ - } \ - return nodes; \ - } \ - \ - /** \ - * -- __skip_adjust_hit_counts_ TODO \ - * \ - * On delete we check the hit counts across all nodes and next[] pointers \ - * and find the smallest counter then subtract that + 1 from all hit \ - * counters. \ - * \ - */ \ - static void __skip_adjust_hit_counts_##decl(decl##_t *slist) \ - { \ - ((void)slist); \ - } \ - \ - /** \ - * -- __skip_rebalance_ \ - * \ - * Restore balance to our list by adjusting heights and forward pointers \ - * according to the algorithm put forth in "The Splay-List: A \ - * Distribution-Adaptive Concurrent Skip-List". \ - * \ - */ \ - static void __skip_rebalance_##decl(decl##_t *slist, size_t len, __skiplist_path_##decl##_t path[]) \ - { \ - size_t i, j, u_hits, hits_CHu = 0, hits_CHv = 0, delta_height, new_height, cur_hits, prev_hits; \ - size_t k_threshold, m_total_hits; \ - double asc_cond, dsc_cond; \ - \ - /* Total hits, `k`, across all nodes. */ \ - m_total_hits = slist->slh_head->field.sle_levels[slist->slh_head->field.sle_height].hits; \ - \ - /* Height of the head node, should be equal to floor(log(m_total_hits)). */ \ - k_threshold = slist->slh_head->field.sle_height; \ - \ - /* Moving backwards along the path... \ - * - path[0] contains a match, if there was one \ - * - path[1..len] will be the nodes traversed along the way \ - * - path[len] is where the locate() terminated, just before path[0] \ - * if there was a match \ - */ \ - i = 0; \ - do { \ - hits_CHu = hits_CHv = 0; \ - if (path[i].node == slist->slh_head || path[i].node == slist->slh_tail) \ - continue; \ - \ - __SKIP_SUBTREE_CHhx(decl, field, slist, path, i) \ - { \ - hits_CHu += elm->field.sle_levels[0].hits; \ - } \ - __SKIP_SUBTREE_CHhx(decl, field, slist, path, i + 1) \ - { \ - hits_CHv += elm->field.sle_levels[0].hits; \ - } \ - u_hits = hits_CHu + hits_CHv; \ - \ - /* (a) Check the decent condition: \ - * u_hits <= m_total_hits / (2 ^ (k_threshold - height of node)) \ - * When met should induce: \ - * 1) traverse the path backward, and... \ - * 2) propagate path[i].level[i] hits backward along path, and... \ - * 3) adjust any forward pointers along the way, then... \ - * 4) lower the path[i]'s node height by 1 \ - */ \ - delta_height = k_threshold - path[i].node->field.sle_height + 1; \ - dsc_cond = m_total_hits / pow(2.0, delta_height); \ - if (u_hits <= dsc_cond && path[i].node->field.sle_height > 0) { \ - if (path[i - 1].node->field.sle_prev != slist->slh_head) { \ - /* 1) go backwards along path from where we are until head */ \ - j = i; \ - cur_hits = path[j].node->field.sle_levels[path[j].in].hits; \ - do { \ - /* 2) propagate hits */ \ - prev_hits = path[j - 1].node->field.sle_levels[path[j - 1].in].hits; \ - path[j - 1].node->field.sle_levels[path[j - 1].in].hits += cur_hits; \ - cur_hits = prev_hits; \ - /* 3) adjust forward pointers */ \ - if (path[j - 1].node->field.sle_levels[j].next == path[i].node) \ - path[j - 1].node->field.sle_levels[j].next = path[i].node->field.sle_levels[j].next; \ - } while (j-- > 1); \ - /* 4) reduce height by one */ \ - path[i].node->field.sle_height--; \ - } \ - } \ - /* (b) Check the ascent condition: \ - * path[i].pu + node_hits > hits total / (2 ^ (height of head - height of node - 1)) \ - * When met should induce: \ - * 1) check the ascent condition, then iff true ... \ - * 2) add a level, and ... \ - * 3) set its hits to the prev node at intersection height \ - * 4) set prev node hits to 0 and forward to this new level \ - */ \ - /* 1) check ascent condition */ \ - asc_cond = m_total_hits / pow(2.0, delta_height == 0 ? 0 : delta_height - 1); \ - if (path[i - 1].pu > asc_cond && path[i].node->field.sle_height < SKIPLIST_MAX_HEIGHT) { \ - /* 2) increase height by one */ \ - new_height = path[i].node->field.sle_height++; \ - /* 3) update hit counter */ \ - path[i].node->field.sle_levels[new_height].hits += path[i - 1].node->field.sle_levels[path[i - 1].in].hits; \ - /* 4) reset the prev node hits to 0 */ \ - path[i - 1].node->field.sle_levels[path[i - 1].in].hits = 0; \ - if (path[i - 1].in != 0) \ - path[i - 1].node->field.sle_levels[path[i - 1].in].next->field.sle_levels[path[i - 1].in].next = path[i].node; \ - } \ - } while (i++ < len); \ - } \ - \ - /** \ - * -- __skip_locate_ \ - * \ - * Locates a node that matches another node updating `path` and then \ - * returning the length of that path + 1 to the node and the matching \ - * node in path[0], or NULL at path[0] where there wasn't a match. \ - * sizeof(path) should be `slist->slh_head->entries.sle_height + 1` \ - */ \ - static size_t __skip_locate_##decl(decl##_t *slist, decl##_node_t *n, __skiplist_path_##decl##_t path[]) \ - { \ - unsigned int i; \ - size_t len = 0; \ - decl##_node_t *elm; \ - \ - if (slist == NULL || n == NULL) \ - return 0; \ - \ - elm = slist->slh_head; \ - \ - /* Find the node that matches `node` or NULL. */ \ - i = slist->slh_head->field.sle_height - 1; \ - do { \ - path[i + 1].pu = 0; \ - while (elm != slist->slh_tail && elm->field.sle_levels[i].next && \ - __skip_compare_nodes_##decl(slist, elm->field.sle_levels[i].next, n, slist->slh_aux) < 0) { \ - elm = elm->field.sle_levels[i].next; \ - path[i + 1].in = i; \ - path[i + 1].pu += elm->field.sle_levels[path[i + 1].in].hits; \ - } \ - path[i + 1].node = elm; \ - if (path[i + 1].in > 0) \ - path[i + 1].node->field.sle_levels[path[i + 1].in].hits++; \ - len++; \ - } while (i--); \ - elm = elm->field.sle_levels[0].next; \ - if (__skip_compare_nodes_##decl(slist, elm, n, slist->slh_aux) == 0) { \ - path[0].node = elm; \ - path[0].node->field.sle_levels[0].hits++; \ - slist->slh_head->entries.sle_levels[slist->slh_head->entries.sle_height].hits++; \ - __skip_rebalance_##decl(slist, len, path); \ - } \ - return len; \ - } \ - \ - /** \ - * -- __skip_insert_ \ - * \ - * Inserts the node `new` into the list `slist`, when `flags` is non-zero \ - * duplicate keys are allowed. Duplicates are grouped together by key but \ - * are otherwise unordered. \ - */ \ - static int __skip_insert_##decl(decl##_t *slist, decl##_node_t *new, int flags) \ - { \ - static __skiplist_path_##decl##_t apath[SKIPLIST_MAX_HEIGHT + 1]; \ - int rc = 0; \ - size_t i, len, loc = 0, cur_height, new_height; \ - decl##_node_t *node; \ - __skiplist_path_##decl##_t *path = apath; \ - \ - if (slist == NULL || new == NULL) \ - return ENOENT; \ - \ - memset(path, 0, sizeof(__skiplist_path_##decl##_t) * SKIPLIST_MAX_HEIGHT + 1); \ - \ - /* Find a `path` to `new` in the list and a match (`path[0]`) if it exists. */ \ - len = __skip_locate_##decl(slist, new, path); \ - node = path[0].node; \ - if (len > 0) { \ - if ((node != NULL) && (flags == 0)) { \ - /* Don't insert, duplicate if flag not set. */ \ - return -1; \ - } \ - /* Coin toss to determine level of this new node [0, current max height) */ \ - cur_height = slist->slh_head->field.sle_height - 1; \ - new_height = __skip_toss_##decl(slist, cur_height); \ - new->field.sle_height = new_height; \ - /* Trim the path to at most the new height for the new node. */ \ - for (i = cur_height + 1; i <= new_height; i++) { \ - path[i + 1].node = slist->slh_tail; \ - } \ - /* Ensure all next[] point to tail. */ \ - __SKIP_ALL_ENTRIES_B2T(field, new) \ - { \ - new->field.sle_levels[lvl].next = slist->slh_tail; \ - } \ - /* Adjust all forward pointers for each element in the path. */ \ - for (i = 0; i <= new_height; i++) { \ - /* The tail's next[i] is always NULL, we don't want that in the \ - next[i] for our new node. Also, don't set the tail's next[i] \ - because it is always NULL. */ \ - if (path[i + 1].node != slist->slh_tail) { \ - new->field.sle_levels[i].next = path[i + 1].node->field.sle_levels[i].next; \ - path[i + 1].node->field.sle_levels[i].next = new; \ - loc = path[i + 1].node == slist->slh_head ? i : loc; \ - } else { \ - new->field.sle_levels[i].next = slist->slh_tail; \ - } \ - } \ - /* Ensure all slh_head->next[] above loc point to tail. */ \ - if (path[1].node == slist->slh_head) { \ - __SKIP_ENTRIES_B2T_FROM(field, slist->slh_head, loc + 1) \ - { \ - slist->slh_head->field.sle_levels[lvl].next = slist->slh_tail; \ - } \ - } \ - /* Adjust the previous pointers in the nodes. */ \ - new->field.sle_prev = path[1].node; \ - new->field.sle_levels[0].next->field.sle_prev = new; \ - /* Account for insert at tail. */ \ - if (new->field.sle_levels[0].next == slist->slh_tail) \ - slist->slh_tail->field.sle_prev = new; \ - /* Record the era for this node to enable snapshots. */ \ - if (slist->slh_snap.pres_era > 0) { \ - /* Increase the list's era/age and record it. */ \ - new->field.sle_era = slist->slh_snap.cur_era++; \ - } \ - /* Set hits for re-balancing to 1 when newborn. */ \ - new->field.sle_levels[new_height].hits = 1; \ - /* Increase our list length (aka. size, count, etc.) by one. */ \ - slist->slh_length++; \ - \ - if (SKIPLIST_MAX_HEIGHT == 1) \ - free(path); \ - } \ - return rc; \ - } \ - \ - /** \ - * -- skip_insert_ \ - * \ - * Insert into the list `slist` the node `n`. \ - */ \ - int prefix##skip_insert_##decl(decl##_t *slist, decl##_node_t *n) \ - { \ - return __skip_insert_##decl(slist, n, 0); \ - } \ - \ - /** \ - * -- skip_insert_dup_ \ - * \ - * Inserts into `slist` the node `n` even if that node's key already \ - * exists in the list. \ - */ \ - int prefix##skip_insert_dup_##decl(decl##_t *slist, decl##_node_t *n) \ - { \ - return __skip_insert_##decl(slist, n, 1); \ - } \ - \ - /** \ - * -- skip_position_eq_ \ - * \ - * Find a node that matches the node `n`. This differs from the locate() \ - * API in that it does not return the path to the node, only the match. \ - */ \ - decl##_node_t *prefix##skip_position_eq_##decl(decl##_t *slist, decl##_node_t *query) \ - { \ - static __skiplist_path_##decl##_t apath[SKIPLIST_MAX_HEIGHT + 1]; \ - decl##_node_t *node = NULL; \ - __skiplist_path_##decl##_t *path = apath; \ - \ - memset(path, 0, sizeof(__skiplist_path_##decl##_t) * SKIPLIST_MAX_HEIGHT + 1); \ - \ - /* Find a `path` to `new` in the list and a match (`path[0]`) if it exists. */ \ - __skip_locate_##decl(slist, query, path); \ - node = path[0].node; \ - if (SKIPLIST_MAX_HEIGHT == 1) \ - free(path); \ - \ - return node; \ - } \ - \ - /** \ - * -- skip_position_gte \ - * \ - * Position and return a cursor at the first node that is equal to \ - * or greater than the provided node `n`, otherwise if the largest \ - * key is less than the key in `n` return NULL. \ - */ \ - decl##_node_t *prefix##skip_position_gte_##decl(decl##_t *slist, decl##_node_t *query) \ - { \ - static __skiplist_path_##decl##_t apath[SKIPLIST_MAX_HEIGHT + 1]; \ - int cmp; \ - decl##_node_t *node; \ - __skiplist_path_##decl##_t *path = apath; \ - \ - memset(path, 0, sizeof(__skiplist_path_##decl##_t) * SKIPLIST_MAX_HEIGHT + 1); \ - \ - /* Find a `path` to `new` in the list and a match (`path[0]`) if it exists. */ \ - __skip_locate_##decl(slist, query, path); \ - node = path[1].node; \ - do { \ - node = node->field.sle_levels[0].next; \ - cmp = __skip_compare_nodes_##decl(slist, node, query, slist->slh_aux); \ - } while (cmp < 0); \ - if (SKIPLIST_MAX_HEIGHT == 1) \ - free(path); \ - \ - return node; \ - } \ - \ - /** \ - * -- skip_position_gt_ \ - * \ - * Position and return a cursor at the first node that is greater than \ - * the provided node `n`. If the largestkey is less than the key in `n` \ - * return NULL. \ - */ \ - decl##_node_t *prefix##skip_position_gt_##decl(decl##_t *slist, decl##_node_t *query) \ - { \ - static __skiplist_path_##decl##_t apath[SKIPLIST_MAX_HEIGHT + 1]; \ - int cmp; \ - decl##_node_t *node; \ - __skiplist_path_##decl##_t *path = apath; \ - \ - memset(path, 0, sizeof(__skiplist_path_##decl##_t) * SKIPLIST_MAX_HEIGHT + 1); \ - \ - /* Find a `path` to `new` in the list and a match (`path[0]`) if it exists. */ \ - __skip_locate_##decl(slist, query, path); \ - node = path[1].node; \ - if (node == slist->slh_tail) \ - goto done; \ - do { \ - node = node->field.sle_levels[0].next; \ - cmp = __skip_compare_nodes_##decl(slist, node, query, slist->slh_aux); \ - } while (cmp <= 0 && node != slist->slh_tail); \ - done:; \ - if (SKIPLIST_MAX_HEIGHT == 1) \ - free(path); \ - \ - return node; \ - } \ - \ - /** \ - * -- skip_position_lte \ - * \ - * Position and return a cursor at the last node that is less than \ - * or equal to node `n`. \ - * Return NULL if nothing is less than or equal. \ - */ \ - decl##_node_t *prefix##skip_position_lte_##decl(decl##_t *slist, decl##_node_t *query) \ - { \ - static __skiplist_path_##decl##_t apath[SKIPLIST_MAX_HEIGHT + 1]; \ - decl##_node_t *node; \ - __skiplist_path_##decl##_t *path = apath; \ - \ - memset(path, 0, sizeof(__skiplist_path_##decl##_t) * SKIPLIST_MAX_HEIGHT + 1); \ - \ - /* Find a `path` to `new` in the list and a match (`path[0]`) if it exists. */ \ - __skip_locate_##decl(slist, query, path); \ - node = path[0].node; \ - if (node) \ - goto done; \ - node = path[1].node; \ - done:; \ - if (SKIPLIST_MAX_HEIGHT == 1) \ - free(path); \ - \ - return node; \ - } \ - \ - /** \ - * -- skip_position_lt_ \ - * \ - * Position and return a cursor at the last node that is less than \ - * to the node `n`. Return NULL if nothing is less than or equal. \ - */ \ - decl##_node_t *prefix##skip_position_lt_##decl(decl##_t *slist, decl##_node_t *query) \ - { \ - static __skiplist_path_##decl##_t apath[SKIPLIST_MAX_HEIGHT + 1]; \ - decl##_node_t *node; \ - __skiplist_path_##decl##_t *path = apath; \ - \ - memset(path, 0, sizeof(__skiplist_path_##decl##_t) * SKIPLIST_MAX_HEIGHT + 1); \ - \ - /* Find a `path` to `new` in the list and a match (`path[0]`) if it exists. */ \ - __skip_locate_##decl(slist, query, path); \ - node = path[1].node; \ - if (SKIPLIST_MAX_HEIGHT == 1) \ - free(path); \ - \ - return node; \ - } \ - \ - /** \ - * -- skip_position_ \ - * \ - * Position a cursor relative to `n`. \ - */ \ - decl##_node_t *prefix##skip_position_##decl(decl##_t *slist, skip_pos_##decl_t op, decl##_node_t *query) \ - { \ - decl##_node_t *node; \ - \ - switch (op) { \ - case (SKIP_LT): \ - node = prefix##skip_position_lt_##decl(slist, query); \ - break; \ - case (SKIP_LTE): \ - node = prefix##skip_position_lte_##decl(slist, query); \ - break; \ - case (SKIP_GTE): \ - node = prefix##skip_position_gte_##decl(slist, query); \ - break; \ - case (SKIP_GT): \ - node = prefix##skip_position_gt_##decl(slist, query); \ - break; \ - default: \ - case (SKIP_EQ): \ - node = prefix##skip_position_eq_##decl(slist, query); \ - break; \ - } \ - return node; \ - } \ - \ - /** \ - * -- skip_update_ \ - * \ - * Locates a node in the list that equals the `new` node and then \ - * uses the `update_entry_blk` to update the contents. \ - * \ - * WARNING: Do not update the portion of the node used for ordering \ - * (e.g. `key`) unless you really know what you're doing. \ - */ \ - int prefix##skip_update_##decl(decl##_t *slist, decl##_node_t *query, void *value) \ - { \ - static __skiplist_path_##decl##_t apath[SKIPLIST_MAX_HEIGHT + 1]; \ - int rc = 0, np; \ - decl##_node_t *node; \ - __skiplist_path_##decl##_t *path = apath; \ - \ - if (slist == NULL) \ - return -1; \ - \ - memset(path, 0, sizeof(__skiplist_path_##decl##_t) * SKIPLIST_MAX_HEIGHT + 1); \ - \ - __skip_locate_##decl(slist, query, path); \ - node = path[0].node; \ - \ - if (SKIPLIST_MAX_HEIGHT == 1) \ - free(path); \ - \ - if (node == NULL) \ - return -1; \ - \ - /* If the optional snapshots feature is configured, use it now. \ - Snapshots preserve the node if it is older than our snapshot \ - and about to be mutated. */ \ - if (slist->slh_snap.pres_era > 0) { \ - /* Preserve the node. */ \ - np = slist->slh_fns.snapshot_preserve_node(slist, node, NULL); \ - if (np > 0) \ - return np; \ - \ - /* Increase the list's era/age. */ \ - slist->slh_snap.cur_era++; \ - } \ - \ - slist->slh_fns.update_entry(node, value); \ - \ - return rc; \ - } \ - \ - /** \ - * -- skip_remove_node_ \ - * \ - * Removes the node `n` from the `slist` if present. \ - */ \ - int prefix##skip_remove_node_##decl(decl##_t *slist, decl##_node_t *query) \ - { \ - static __skiplist_path_##decl##_t apath[SKIPLIST_MAX_HEIGHT + 1]; \ - int np = 0; \ - size_t i, len, height; \ - decl##_node_t *node; \ - __skiplist_path_##decl##_t *path = apath; \ - \ - if (slist == NULL || query == NULL) \ - return -1; \ - if (slist->slh_length == 0) \ - return 0; \ - \ - memset(path, 0, sizeof(__skiplist_path_##decl##_t) * SKIPLIST_MAX_HEIGHT + 1); \ - \ - /* Attempt to locate the node in the list. */ \ - len = __skip_locate_##decl(slist, query, path); \ - node = path[0].node; \ - if (node) { \ - /* If the optional snapshots feature is configured, use it now. \ - Snapshots preserve the node if it is older than our snapshot \ - and about to be mutated. */ \ - if (slist->slh_snap.pres_era > 0) { \ - /* Preserve the node. */ \ - np = slist->slh_fns.snapshot_preserve_node(slist, node, NULL); \ - if (np > 0) \ - return np; \ - \ - /* Increase the list's era/age. */ \ - slist->slh_snap.cur_era++; \ - } \ - /* We found it, set the next->prev to the node->prev keeping in mind \ - that the next node might be the tail). */ \ - node->field.sle_levels[0].next->field.sle_prev = node->field.sle_prev; \ - /* Walk the path, stop when the next node is not the one we're \ - removing. At each step along our walk... */ \ - for (i = 0; i < len; i++) { \ - if (path[i + 1].node->field.sle_levels[i].next != node) \ - break; \ - /* ... adjust the next pointer at that level. */ \ - path[i + 1].node->field.sle_levels[i].next = node->field.sle_levels[i].next; \ - /* Adjust the height such that we're only pointing at the tail once at \ - the top that way we don't waste steps later when searching. */ \ - if (path[i + 1].node->field.sle_levels[i].next == slist->slh_tail) { \ - height = path[i + 1].node->field.sle_height; \ - path[i + 1].node->field.sle_height = height - 1; \ - } \ - } \ - /* Account for delete at tail. */ \ - if (node->field.sle_levels[0].next == slist->slh_tail) { \ - slist->slh_tail->field.sle_prev = query->field.sle_prev; \ - } \ - \ - if (SKIPLIST_MAX_HEIGHT == 1) \ - free(path); \ - \ - slist->slh_fns.free_entry(node); \ - free(node); \ - \ - /* Reduce the height of the head/tail nodes. */ \ - for (i = 0; slist->slh_head->field.sle_levels[i].next != slist->slh_tail && i < SKIPLIST_MAX_HEIGHT; i++) \ - ; \ - slist->slh_head->field.sle_height = slist->slh_tail->field.sle_height = i; \ - \ - slist->slh_length--; \ - __skip_adjust_hit_counts_##decl(slist); \ - } \ - return 0; \ - } \ - \ - /** \ - * -- skip_free_ \ - * \ - * Release all nodes and their associated heap objects. The list reference \ - * is no longer valid after this call. To make it valid again call _init(). \ - */ \ - void prefix##skip_free_##decl(decl##_t *slist) \ - { \ - if (slist == NULL) \ - return; \ - \ - if (slist->slh_snap.pres_era > 0 && slist->slh_fns.snapshot_release) \ - slist->slh_fns.snapshot_release(slist); \ - \ - prefix##skip_release_##decl(slist); \ - \ - free(slist->slh_head); \ - free(slist->slh_tail); \ +#define SKIPLIST_DECL(decl, prefix, field, compare_entries_blk, free_entry_blk, update_entry_blk, archive_entry_blk, sizeof_entry_blk) \ + \ + /* Used when positioning a cursor within a Skiplist. */ \ + typedef enum { SKIP_EQ = 0, SKIP_LTE = -1, SKIP_LT = -2, SKIP_GTE = 1, SKIP_GT = 2 } skip_pos_##decl_t; \ + \ + /* Skiplist node type */ \ + typedef struct decl##_node decl##_node_t; \ + \ + /* Skiplist type. */ \ + typedef struct decl decl##_t; \ + \ + /* Skiplist structure */ \ + struct decl { \ + size_t slh_length; \ + void *slh_aux; \ + int slh_prng_state; \ + decl##_node_t *slh_head; \ + decl##_node_t *slh_tail; \ + struct { \ + void (*free_entry)(decl##_node_t *); \ + int (*update_entry)(decl##_node_t *, void *); \ + int (*archive_entry)(decl##_node_t *, const decl##_node_t *); \ + size_t (*sizeof_entry)(decl##_node_t *); \ + int (*compare_entries)(decl##_t *, decl##_node_t *, decl##_node_t *, void *); \ + \ + /* Optional: Snapshots */ \ + int (*snapshot_preserve_node)(decl##_t * slist, const decl##_node_t *src, decl##_node_t **preserved); \ + void (*snapshot_release)(decl##_t *); \ + } slh_fns; \ + struct { \ + size_t cur_era; \ + size_t pres_era; \ + decl##_node_t *pres; \ + } slh_snap; \ + }; \ + \ + typedef struct __skiplist_path_##decl { \ + decl##_node_t *node; /* node traversed in the act of location */ \ + size_t in; /* level at which the node was intersected */ \ + size_t pu; /* sum of hits from intersection to level[1] */ \ + } __skiplist_path_##decl##_t; \ + \ + /* Xorshift algorithm for PRNG */ \ + static uint32_t __##decl##_xorshift32(int *state) \ + { \ + uint32_t x = *state; \ + if (x == 0) \ + x = 123456789; \ + x ^= x << 13; \ + x ^= x >> 17; \ + x ^= x << 5; \ + *state = x; \ + return x; \ + } \ + \ + /** \ + * -- __skip_compare_entries_fn_ \ + * \ + * Wraps the `compare_entries_blk` code into `slh_fns.compare_entries`. \ + */ \ + static int __skip_compare_entries_fn_##decl(decl##_t *list, decl##_node_t *a, decl##_node_t *b, void *aux) \ + { \ + compare_entries_blk; \ + } \ + \ + /** \ + * -- __skip_free_entry_fn \ + * \ + * Wraps the `free_entry_blk` code into `slh_fns.free_entry`. \ + */ \ + static void __skip_free_entry_fn_##decl(decl##_node_t *node) \ + { \ + free_entry_blk; \ + } \ + \ + /** \ + * -- __skip_update_entry_fn_ \ + * \ + * Wraps the `update_entry_blk` code into `slh_fns.update_entry`. \ + */ \ + static int __skip_update_entry_fn_##decl(decl##_node_t *node, void *value) \ + { \ + int rc = 0; \ + update_entry_blk; \ + return rc; \ + } \ + \ + /** \ + * -- __skip_archive_entry_fn_ \ + * \ + * Wraps the `archive_entry_blk` code into `slh_fns.archive_entry`. \ + */ \ + static int __skip_archive_entry_fn_##decl(decl##_node_t *dest, const decl##_node_t *src) \ + { \ + int rc = 0; \ + archive_entry_blk; \ + return rc; \ + } \ + \ + /** \ + * -- __skip_sizeof_entry_fn_ \ + * \ + * Wraps the `sizeof_entry_blk` code into `slh_fns.sizeof_entry`. \ + */ \ + static size_t __skip_sizeof_entry_fn_##decl(decl##_node_t *node) \ + { \ + size_t bytes = 0; \ + sizeof_entry_blk; \ + return bytes; \ + } \ + \ + /** \ + * -- __skip_compare_nodes_ \ + * \ + * This function takes four arguments: \ + * - a reference to the Skiplist \ + * - the two nodes to compare, `a` and `b` \ + * - `aux` an additional auxiliary argument \ + * and returns: \ + * a < b : return -1 \ + * a == b : return 0 \ + * a > b : return 1 \ + */ \ + static int __skip_compare_nodes_##decl(decl##_t *slist, decl##_node_t *a, decl##_node_t *b, void *aux) \ + { \ + if (a == b) \ + return 0; \ + if (a == NULL) \ + return -1; \ + if (b == NULL) \ + return 1; \ + if (a == slist->slh_head || b == slist->slh_tail) \ + return -1; \ + if (a == slist->slh_tail || b == slist->slh_head) \ + return 1; \ + return slist->slh_fns.compare_entries(slist, a, b, aux); \ + } \ + \ + /** \ + * -- __skip_toss_ \ + * \ + * A "coin toss" function that is critical to the proper operation of the \ + * Skiplist. 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. \ + */ \ + static int __skip_toss_##decl(decl##_t *slist, size_t max) \ + { \ + size_t level = 0; \ + double probability = 0.5; \ + \ + double random_value = (double)__##decl##_xorshift32(&slist->slh_prng_state) / RAND_MAX; \ + while (random_value < probability && level < max) { \ + level++; \ + probability *= 0.5; \ + } \ + return level; \ + } \ + \ + /** \ + * -- skip_alloc_node_ \ + * \ + * Allocates a new node on the heap and sets default values. \ + */ \ + int prefix##skip_alloc_node_##decl(decl##_node_t **node) \ + { \ + decl##_node_t *n; \ + /* Calculate the size of the struct sle within decl##_node_t, multiply \ + by array size. (16/24 bytes on 32/64 bit systems) */ \ + size_t sle_arr_sz = sizeof(struct __skiplist_##decl##_level) * SKIPLIST_MAX_HEIGHT; \ + n = (decl##_node_t *)calloc(1, sizeof(decl##_node_t) + sle_arr_sz); \ + if (n == NULL) \ + return ENOMEM; \ + n->field.sle_height = 0; \ + n->field.sle_levels = (struct __skiplist_##decl##_level *)((uintptr_t)n + sizeof(decl##_node_t)); \ + *node = n; \ + return 0; \ + } \ + \ + /** \ + * -- skip_init_ \ + * \ + * Initializes a Skiplist to the default values, this must be \ + * called before using the list. \ + */ \ + int prefix##skip_init_##decl(decl##_t *slist) \ + { \ + int rc = 0; \ + size_t i; \ + \ + slist->slh_length = 0; \ + slist->slh_snap.cur_era = 0; \ + slist->slh_snap.pres_era = 0; \ + slist->slh_snap.pres = 0; \ + slist->slh_fns.free_entry = __skip_free_entry_fn_##decl; \ + slist->slh_fns.update_entry = __skip_update_entry_fn_##decl; \ + slist->slh_fns.archive_entry = __skip_archive_entry_fn_##decl; \ + slist->slh_fns.sizeof_entry = __skip_sizeof_entry_fn_##decl; \ + slist->slh_fns.compare_entries = __skip_compare_entries_fn_##decl; \ + rc = prefix##skip_alloc_node_##decl(&slist->slh_head); \ + if (rc) \ + goto fail; \ + rc = prefix##skip_alloc_node_##decl(&slist->slh_tail); \ + if (rc) \ + goto fail; \ + \ + /* Initial height at head is 0 (meaning 0 sub-lists), all next point to tail */ \ + slist->slh_head->field.sle_height = 1; \ + for (i = 0; i < slist->slh_head->entries.sle_height + 1; i++) \ + slist->slh_head->field.sle_levels[i].next = slist->slh_tail; \ + slist->slh_head->field.sle_prev = NULL; \ + \ + /* Initial height at tail is also 0 and all next point to tail */ \ + slist->slh_tail->field.sle_height = slist->slh_head->field.sle_height; \ + for (i = 0; i < slist->slh_head->entries.sle_height + 1; i++) \ + slist->slh_tail->field.sle_levels[i].next = NULL; \ + slist->slh_tail->field.sle_prev = slist->slh_head; \ + slist->slh_prng_state = ((unsigned int)time(NULL) ^ getpid()); \ + fail:; \ + return rc; \ + } \ + \ + /** \ + * -- skip_free_node_ \ + * \ + * Properly releases heap memory allocated for use as a node. \ + * This function invokes the `free_node_blk` within which you \ + * should release any heap objects or other resources held by \ + * this node in the list. \ + */ \ + void prefix##skip_free_node_##decl(decl##_t *slist, decl##_node_t *node) \ + { \ + slist->slh_fns.free_entry(node); \ + free(node); \ + } \ + \ + /** \ + * -- skip_length_ \ + * \ + * Returns the current length of the list. \ + */ \ + size_t prefix##skip_length_##decl(decl##_t *slist) \ + { \ + return slist->slh_length; \ + } \ + \ + /** \ + * -- skip_is_empty_ \ + * \ + * Returns non-zero when the list is empty. \ + */ \ + int prefix##skip_is_empty_##decl(decl##_t *slist) \ + { \ + return slist->slh_length == 0; \ + } \ + \ + /** \ + * -- skip_head_ \ + * \ + * Returns the node containing the first (smallest) element in the \ + * list which can be used to traverse the list. \ + */ \ + decl##_node_t *prefix##skip_head_##decl(decl##_t *slist) \ + { \ + return slist->slh_head->field.sle_levels[0].next == slist->slh_tail ? NULL : slist->slh_head->field.sle_levels[0].next; \ + } \ + \ + /** \ + * -- skip_tail_ \ + * \ + * Returns the node containing the last (largest) element in the \ + * list which can be used to traverse the list. \ + */ \ + decl##_node_t *prefix##skip_tail_##decl(decl##_t *slist) \ + { \ + if (slist == NULL) \ + return NULL; \ + return slist->slh_tail->field.sle_prev == slist->slh_head->field.sle_levels[0].next ? NULL : slist->slh_tail->field.sle_prev; \ + } \ + \ + /** \ + * -- skip_next_node_ \ + * \ + * A node reference can be thought of as a cursor. This moves the cursor \ + * to the next node in the list or returns NULL if the next is the tail. \ + */ \ + decl##_node_t *prefix##skip_next_node_##decl(decl##_t *slist, decl##_node_t *n) \ + { \ + if (slist == NULL || n == NULL) \ + return NULL; \ + if (n->field.sle_levels[0].next == slist->slh_tail) \ + return NULL; \ + return n->field.sle_levels[0].next; \ + } \ + \ + /** \ + * -- skip_prev_node_ \ + * \ + * A node reference can be thought of as a cursor. This moves the cursor \ + * to the previous node in the list or returns NULL if the previous node \ + * is the head. \ + */ \ + decl##_node_t *prefix##skip_prev_node_##decl(decl##_t *slist, decl##_node_t *n) \ + { \ + if (slist == NULL || n == NULL) \ + return NULL; \ + if (n->field.sle_prev == slist->slh_head) \ + return NULL; \ + return n->field.sle_prev; \ + } \ + \ + /** \ + * -- skip_release_ \ + * \ + * Release all nodes and their associated heap objects, but not the list \ + * itself. The list is still valid, only empty. \ + */ \ + void prefix##skip_release_##decl(decl##_t *slist) \ + { \ + decl##_node_t *node, *next; \ + \ + if (slist == NULL) \ + return; \ + if (prefix##skip_is_empty_##decl(slist)) \ + return; \ + node = slist->slh_head->field.sle_levels[0].next; \ + while (node != slist->slh_tail) { \ + next = node->field.sle_levels[0].next; \ + prefix##skip_free_node_##decl(slist, node); \ + node = next; \ + } \ + if (slist->slh_snap.pres_era > 0) \ + slist->slh_snap.cur_era++; \ + return; \ + } \ + \ + /** \ + * -- skip_to_array_ \ + * \ + * Returns a heap allocated array of nodes in the order they exist. \ + * This isn't maintained by the list, if you add/remove nodes it is \ + * no longer accurate. At [-1] is the length of the array. \ + * NOTE: Caller must deallocate. \ + */ \ + decl##_node_t **prefix##skip_to_array_##decl(decl##_t *slist) \ + { \ + size_t nth, len = prefix##skip_length_##decl(slist); \ + decl##_node_t *node, **nodes = NULL; \ + nodes = (decl##_node_t **)calloc(sizeof(decl##_node_t *), len + 1); \ + if (nodes != NULL) { \ + nodes[0] = (decl##_node_t *)(uintptr_t)len; \ + nodes++; \ + SKIPLIST_FOREACH_H2T(decl, prefix, field, slist, node, nth) \ + { \ + nodes[nth] = node; \ + } \ + } \ + return nodes; \ + } \ + \ + /** \ + * -- __skip_adjust_hit_counts_ TODO \ + * \ + * On delete we check the hit counts across all nodes and next[] pointers \ + * and find the smallest counter then subtract that + 1 from all hit \ + * counters. \ + * \ + */ \ + static void __skip_adjust_hit_counts_##decl(decl##_t *slist) \ + { \ + ((void)slist); \ + } \ + \ + /** \ + * -- __skip_rebalance_ \ + * \ + * Restore balance to our list by adjusting heights and forward pointers \ + * according to the algorithm put forth in "The Splay-List: A \ + * Distribution-Adaptive Concurrent Skip-List". \ + * \ + */ \ + static void __skip_rebalance_##decl(decl##_t *slist, size_t len, __skiplist_path_##decl##_t path[]) \ + { \ + size_t i, j, u_hits, hits_CHu = 0, hits_CHv = 0, delta_height, cur_hits, prev_hits; \ + size_t k_threshold, m_total_hits; \ + double asc_cond, dsc_cond; \ + __skiplist_path_##decl##_t cur, prv; \ + \ + /* Total hits, `k`, across all nodes. */ \ + m_total_hits = slist->slh_head->field.sle_levels[slist->slh_head->field.sle_height].hits; \ + \ + /* Height of the head node, should be equal to floor(log(m_total_hits)). */ \ + k_threshold = slist->slh_head->field.sle_height; \ + \ + /* Moving backwards along the path... \ + * - path[0] contains a match, if there was one \ + * - path[1..len] will be the nodes traversed along the way \ + * - path[len] is where the locate() terminated, just before path[0] \ + * if there was a match \ + */ \ + i = 0; \ + do { \ + hits_CHu = hits_CHv = 0; \ + if (path[i].node == slist->slh_head || path[i].node == slist->slh_tail) \ + continue; \ + \ + __SKIP_SUBTREE_CHux(decl, field, slist, path[i], path[i].in) \ + { \ + hits_CHu += elm->field.sle_levels[0].hits; \ + } \ + __SKIP_SUBTREE_CHux(decl, field, slist, path[i + 1], path[i].in) \ + { \ + hits_CHv += elm->field.sle_levels[0].hits; \ + } \ + u_hits = hits_CHu + hits_CHv; \ + \ + /* (a) Check the decent condition: \ + * u_hits <= m_total_hits / (2 ^ (k_threshold - height of node)) \ + * When met should induce: \ + * 1) traverse the path backward, and... \ + * 2) propagate path[i].level[i] hits backward along path, and... \ + * 3) adjust any forward pointers along the way, then... \ + * 4) lower the path[i]'s node height by 1 \ + */ \ + delta_height = k_threshold - path[i].node->field.sle_height; \ + dsc_cond = m_total_hits / pow(2.0, delta_height); \ + if (0 && u_hits <= dsc_cond && path[i].node->field.sle_height > 0) { \ + if (path[i].node->field.sle_prev != slist->slh_head) { \ + /* 1) go backwards along path from where we are until head */ \ + j = i; \ + cur = path[j]; \ + prv = path[j + 1]; \ + cur_hits = cur.node->field.sle_levels[cur.in].hits; \ + do { \ + /* 2) propagate hits */ \ + prev_hits = prv.node->field.sle_levels[prv.in].hits; \ + prv.node->field.sle_levels[prv.in].hits += cur_hits; \ + cur_hits = prev_hits; \ + /* 3) adjust forward pointers */ \ + if (prv.node->field.sle_levels[j].next == cur.node) \ + prv.node->field.sle_levels[j].next = cur.node->field.sle_levels[j].next; \ + } while (j-- > 1); \ + /* 4) reduce height by one */ \ + cur.node->field.sle_height--; \ + } \ + } \ + /* (b) Check the ascent condition: \ + * path[i].pu + node_hits > hits total / (2 ^ (height of head - height of node - 1)) \ + * When met should induce: \ + * 1) check the ascent condition, then iff true ... \ + * 2) add a level, and ... \ + * 3) set its hits to the prev node at intersection height \ + * 4) set prev node hits to 0 and forward to this new level \ + */ \ + /* 1) check ascent condition */ \ + asc_cond = m_total_hits / pow(2.0, delta_height - 1); \ + if (path[i + 1].pu + hits_CHv > asc_cond && path[i].node->field.sle_height < SKIPLIST_MAX_HEIGHT) { \ + cur = path[i]; \ + prv = path[i + 1]; \ + /* 2) increase height by one */ \ + cur.node->field.sle_height++; \ + /* 3) update hit counter */ \ + cur.node->field.sle_levels[prv.in].hits = prv.node->field.sle_levels[prv.in].hits; \ + /* 4) reset the prev node hits to 0 */ \ + prv.node->field.sle_levels[prv.in].hits = 0; \ + /* 5) update forward pointers in the level */ \ + cur.node->field.sle_levels[prv.in].next = prv.node->field.sle_levels[prv.in].next; \ + prv.node->field.sle_levels[prv.in].next = cur.node; \ + } \ + } while (i++ < len); \ + } \ + \ + /** \ + * -- __skip_locate_ \ + * \ + * Locates a node that matches another node updating `path` and then \ + * returning the length of that path + 1 to the node and the matching \ + * node in path[0], or NULL at path[0] where there wasn't a match. \ + * sizeof(path) should be `slist->slh_head->entries.sle_height + 1` \ + */ \ + static size_t __skip_locate_##decl(decl##_t *slist, decl##_node_t *n, __skiplist_path_##decl##_t path[]) \ + { \ + unsigned int i; \ + size_t len = 0; \ + decl##_node_t *elm; \ + \ + if (slist == NULL || n == NULL) \ + return 0; \ + \ + elm = slist->slh_head; \ + \ + /* Find the node that matches `node` or NULL. */ \ + i = slist->slh_head->field.sle_height - 1; \ + do { \ + __skiplist_path_##decl##_t *pe = &path[i + 1]; \ + pe->pu = 0; \ + while (elm != slist->slh_tail && __skip_compare_nodes_##decl(slist, elm->field.sle_levels[i].next, n, slist->slh_aux) < 0) { \ + pe->in = i; \ + pe->pu += elm->field.sle_levels[0].hits; \ + elm = elm->field.sle_levels[i].next; \ + } \ + pe->node = elm; \ + pe->node->field.sle_levels[pe->in].hits += i ? 1 : 0; \ + len++; \ + } while (i--); \ + elm = elm->field.sle_levels[0].next; \ + if (__skip_compare_nodes_##decl(slist, elm, n, slist->slh_aux) == 0) { \ + path[0].node = elm; \ + path[0].node->field.sle_levels[0].hits++; \ + slist->slh_head->entries.sle_levels[slist->slh_head->entries.sle_height].hits++; \ + __skip_rebalance_##decl(slist, len, path); \ + } \ + return len; \ + } \ + \ + /** \ + * -- __skip_insert_ \ + * \ + * Inserts the node `new` into the list `slist`, when `flags` is non-zero \ + * duplicate keys are allowed. Duplicates are grouped together by key but \ + * are otherwise unordered. \ + */ \ + static int __skip_insert_##decl(decl##_t *slist, decl##_node_t *new, int flags) \ + { \ + static __skiplist_path_##decl##_t apath[SKIPLIST_MAX_HEIGHT + 1]; \ + int rc = 0; \ + size_t i, len, loc = 0, cur_height, new_height; \ + decl##_node_t *node; \ + __skiplist_path_##decl##_t *path = apath; \ + \ + if (slist == NULL || new == NULL) \ + return ENOENT; \ + \ + memset(path, 0, sizeof(__skiplist_path_##decl##_t) * SKIPLIST_MAX_HEIGHT + 1); \ + \ + /* Find a `path` to `new` in the list and a match (`path[0]`) if it exists. */ \ + len = __skip_locate_##decl(slist, new, path); \ + node = path[0].node; \ + if (len > 0) { \ + if ((node != NULL) && (flags == 0)) { \ + /* Don't insert, duplicate if flag not set. */ \ + return -1; \ + } \ + /* Coin toss to determine level of this new node [0, current max height) */ \ + cur_height = slist->slh_head->field.sle_height - 1; \ + new_height = __skip_toss_##decl(slist, cur_height); \ + new->field.sle_height = new_height; \ + /* Trim the path to at most the new height for the new node. */ \ + for (i = cur_height + 1; i <= new_height; i++) { \ + path[i + 1].node = slist->slh_tail; \ + } \ + /* Ensure all next[] point to tail. */ \ + __SKIP_ALL_ENTRIES_B2T(field, new) \ + { \ + new->field.sle_levels[lvl].next = slist->slh_tail; \ + } \ + /* Adjust all forward pointers for each element in the path. */ \ + for (i = 0; i <= new_height; i++) { \ + /* The tail's next[i] is always NULL, we don't want that in the \ + next[i] for our new node. Also, don't set the tail's next[i] \ + because it is always NULL. */ \ + if (path[i + 1].node != slist->slh_tail) { \ + new->field.sle_levels[i].next = path[i + 1].node->field.sle_levels[i].next; \ + path[i + 1].node->field.sle_levels[i].next = new; \ + loc = path[i + 1].node == slist->slh_head ? i : loc; \ + } else { \ + new->field.sle_levels[i].next = slist->slh_tail; \ + } \ + } \ + /* Ensure all slh_head->next[] above loc point to tail. */ \ + if (path[1].node == slist->slh_head) { \ + __SKIP_ENTRIES_B2T_FROM(field, slist->slh_head, loc + 1) \ + { \ + slist->slh_head->field.sle_levels[lvl].next = slist->slh_tail; \ + } \ + } \ + /* Adjust the previous pointers in the nodes. */ \ + new->field.sle_prev = path[1].node; \ + new->field.sle_levels[0].next->field.sle_prev = new; \ + /* Account for insert at tail. */ \ + if (new->field.sle_levels[0].next == slist->slh_tail) \ + slist->slh_tail->field.sle_prev = new; \ + /* Record the era for this node to enable snapshots. */ \ + if (slist->slh_snap.pres_era > 0) { \ + /* Increase the list's era/age and record it. */ \ + new->field.sle_era = slist->slh_snap.cur_era++; \ + } \ + /* Set hits for re-balancing to 1 when newborn. */ \ + new->field.sle_levels[new_height].hits = 1; \ + /* Increase our list length (aka. size, count, etc.) by one. */ \ + slist->slh_length++; \ + \ + if (SKIPLIST_MAX_HEIGHT == 1) \ + free(path); \ + } \ + return rc; \ + } \ + \ + /** \ + * -- skip_insert_ \ + * \ + * Insert into the list `slist` the node `n`. \ + */ \ + int prefix##skip_insert_##decl(decl##_t *slist, decl##_node_t *n) \ + { \ + return __skip_insert_##decl(slist, n, 0); \ + } \ + \ + /** \ + * -- skip_insert_dup_ \ + * \ + * Inserts into `slist` the node `n` even if that node's key already \ + * exists in the list. \ + */ \ + int prefix##skip_insert_dup_##decl(decl##_t *slist, decl##_node_t *n) \ + { \ + return __skip_insert_##decl(slist, n, 1); \ + } \ + \ + /** \ + * -- skip_position_eq_ \ + * \ + * Find a node that matches the node `n`. This differs from the locate() \ + * API in that it does not return the path to the node, only the match. \ + */ \ + decl##_node_t *prefix##skip_position_eq_##decl(decl##_t *slist, decl##_node_t *query) \ + { \ + static __skiplist_path_##decl##_t apath[SKIPLIST_MAX_HEIGHT + 1]; \ + decl##_node_t *node = NULL; \ + __skiplist_path_##decl##_t *path = apath; \ + \ + memset(path, 0, sizeof(__skiplist_path_##decl##_t) * SKIPLIST_MAX_HEIGHT + 1); \ + \ + /* Find a `path` to `new` in the list and a match (`path[0]`) if it exists. */ \ + __skip_locate_##decl(slist, query, path); \ + node = path[0].node; \ + if (SKIPLIST_MAX_HEIGHT == 1) \ + free(path); \ + \ + return node; \ + } \ + \ + /** \ + * -- skip_position_gte \ + * \ + * Position and return a cursor at the first node that is equal to \ + * or greater than the provided node `n`, otherwise if the largest \ + * key is less than the key in `n` return NULL. \ + */ \ + decl##_node_t *prefix##skip_position_gte_##decl(decl##_t *slist, decl##_node_t *query) \ + { \ + static __skiplist_path_##decl##_t apath[SKIPLIST_MAX_HEIGHT + 1]; \ + int cmp; \ + decl##_node_t *node; \ + __skiplist_path_##decl##_t *path = apath; \ + \ + memset(path, 0, sizeof(__skiplist_path_##decl##_t) * SKIPLIST_MAX_HEIGHT + 1); \ + \ + /* Find a `path` to `new` in the list and a match (`path[0]`) if it exists. */ \ + __skip_locate_##decl(slist, query, path); \ + node = path[1].node; \ + do { \ + node = node->field.sle_levels[0].next; \ + cmp = __skip_compare_nodes_##decl(slist, node, query, slist->slh_aux); \ + } while (cmp < 0); \ + if (SKIPLIST_MAX_HEIGHT == 1) \ + free(path); \ + \ + return node; \ + } \ + \ + /** \ + * -- skip_position_gt_ \ + * \ + * Position and return a cursor at the first node that is greater than \ + * the provided node `n`. If the largestkey is less than the key in `n` \ + * return NULL. \ + */ \ + decl##_node_t *prefix##skip_position_gt_##decl(decl##_t *slist, decl##_node_t *query) \ + { \ + static __skiplist_path_##decl##_t apath[SKIPLIST_MAX_HEIGHT + 1]; \ + int cmp; \ + decl##_node_t *node; \ + __skiplist_path_##decl##_t *path = apath; \ + \ + memset(path, 0, sizeof(__skiplist_path_##decl##_t) * SKIPLIST_MAX_HEIGHT + 1); \ + \ + /* Find a `path` to `new` in the list and a match (`path[0]`) if it exists. */ \ + __skip_locate_##decl(slist, query, path); \ + node = path[1].node; \ + if (node == slist->slh_tail) \ + goto done; \ + do { \ + node = node->field.sle_levels[0].next; \ + cmp = __skip_compare_nodes_##decl(slist, node, query, slist->slh_aux); \ + } while (cmp <= 0 && node != slist->slh_tail); \ + done:; \ + if (SKIPLIST_MAX_HEIGHT == 1) \ + free(path); \ + \ + return node; \ + } \ + \ + /** \ + * -- skip_position_lte \ + * \ + * Position and return a cursor at the last node that is less than \ + * or equal to node `n`. \ + * Return NULL if nothing is less than or equal. \ + */ \ + decl##_node_t *prefix##skip_position_lte_##decl(decl##_t *slist, decl##_node_t *query) \ + { \ + static __skiplist_path_##decl##_t apath[SKIPLIST_MAX_HEIGHT + 1]; \ + decl##_node_t *node; \ + __skiplist_path_##decl##_t *path = apath; \ + \ + memset(path, 0, sizeof(__skiplist_path_##decl##_t) * SKIPLIST_MAX_HEIGHT + 1); \ + \ + /* Find a `path` to `new` in the list and a match (`path[0]`) if it exists. */ \ + __skip_locate_##decl(slist, query, path); \ + node = path[0].node; \ + if (node) \ + goto done; \ + node = path[1].node; \ + done:; \ + if (SKIPLIST_MAX_HEIGHT == 1) \ + free(path); \ + \ + return node; \ + } \ + \ + /** \ + * -- skip_position_lt_ \ + * \ + * Position and return a cursor at the last node that is less than \ + * to the node `n`. Return NULL if nothing is less than or equal. \ + */ \ + decl##_node_t *prefix##skip_position_lt_##decl(decl##_t *slist, decl##_node_t *query) \ + { \ + static __skiplist_path_##decl##_t apath[SKIPLIST_MAX_HEIGHT + 1]; \ + decl##_node_t *node; \ + __skiplist_path_##decl##_t *path = apath; \ + \ + memset(path, 0, sizeof(__skiplist_path_##decl##_t) * SKIPLIST_MAX_HEIGHT + 1); \ + \ + /* Find a `path` to `new` in the list and a match (`path[0]`) if it exists. */ \ + __skip_locate_##decl(slist, query, path); \ + node = path[1].node; \ + if (SKIPLIST_MAX_HEIGHT == 1) \ + free(path); \ + \ + return node; \ + } \ + \ + /** \ + * -- skip_position_ \ + * \ + * Position a cursor relative to `n`. \ + */ \ + decl##_node_t *prefix##skip_position_##decl(decl##_t *slist, skip_pos_##decl_t op, decl##_node_t *query) \ + { \ + decl##_node_t *node; \ + \ + switch (op) { \ + case (SKIP_LT): \ + node = prefix##skip_position_lt_##decl(slist, query); \ + break; \ + case (SKIP_LTE): \ + node = prefix##skip_position_lte_##decl(slist, query); \ + break; \ + case (SKIP_GTE): \ + node = prefix##skip_position_gte_##decl(slist, query); \ + break; \ + case (SKIP_GT): \ + node = prefix##skip_position_gt_##decl(slist, query); \ + break; \ + default: \ + case (SKIP_EQ): \ + node = prefix##skip_position_eq_##decl(slist, query); \ + break; \ + } \ + return node; \ + } \ + \ + /** \ + * -- skip_update_ \ + * \ + * Locates a node in the list that equals the `new` node and then \ + * uses the `update_entry_blk` to update the contents. \ + * \ + * WARNING: Do not update the portion of the node used for ordering \ + * (e.g. `key`) unless you really know what you're doing. \ + */ \ + int prefix##skip_update_##decl(decl##_t *slist, decl##_node_t *query, void *value) \ + { \ + static __skiplist_path_##decl##_t apath[SKIPLIST_MAX_HEIGHT + 1]; \ + int rc = 0, np; \ + decl##_node_t *node; \ + __skiplist_path_##decl##_t *path = apath; \ + \ + if (slist == NULL) \ + return -1; \ + \ + memset(path, 0, sizeof(__skiplist_path_##decl##_t) * SKIPLIST_MAX_HEIGHT + 1); \ + \ + __skip_locate_##decl(slist, query, path); \ + node = path[0].node; \ + \ + if (SKIPLIST_MAX_HEIGHT == 1) \ + free(path); \ + \ + if (node == NULL) \ + return -1; \ + \ + /* If the optional snapshots feature is configured, use it now. \ + Snapshots preserve the node if it is older than our snapshot \ + and about to be mutated. */ \ + if (slist->slh_snap.pres_era > 0) { \ + /* Preserve the node. */ \ + np = slist->slh_fns.snapshot_preserve_node(slist, node, NULL); \ + if (np > 0) \ + return np; \ + \ + /* Increase the list's era/age. */ \ + slist->slh_snap.cur_era++; \ + } \ + \ + slist->slh_fns.update_entry(node, value); \ + \ + return rc; \ + } \ + \ + /** \ + * -- skip_remove_node_ \ + * \ + * Removes the node `n` from the `slist` if present. \ + */ \ + int prefix##skip_remove_node_##decl(decl##_t *slist, decl##_node_t *query) \ + { \ + static __skiplist_path_##decl##_t apath[SKIPLIST_MAX_HEIGHT + 1]; \ + int np = 0; \ + size_t i, len, height; \ + decl##_node_t *node; \ + __skiplist_path_##decl##_t *path = apath; \ + \ + if (slist == NULL || query == NULL) \ + return -1; \ + if (slist->slh_length == 0) \ + return 0; \ + \ + memset(path, 0, sizeof(__skiplist_path_##decl##_t) * SKIPLIST_MAX_HEIGHT + 1); \ + \ + /* Attempt to locate the node in the list. */ \ + len = __skip_locate_##decl(slist, query, path); \ + node = path[0].node; \ + if (node) { \ + /* If the optional snapshots feature is configured, use it now. \ + Snapshots preserve the node if it is older than our snapshot \ + and about to be mutated. */ \ + if (slist->slh_snap.pres_era > 0) { \ + /* Preserve the node. */ \ + np = slist->slh_fns.snapshot_preserve_node(slist, node, NULL); \ + if (np > 0) \ + return np; \ + \ + /* Increase the list's era/age. */ \ + slist->slh_snap.cur_era++; \ + } \ + /* We found it, set the next->prev to the node->prev keeping in mind \ + that the next node might be the tail). */ \ + node->field.sle_levels[0].next->field.sle_prev = node->field.sle_prev; \ + /* Walk the path, stop when the next node is not the one we're \ + removing. At each step along our walk... */ \ + for (i = 0; i < len; i++) { \ + if (path[i + 1].node->field.sle_levels[i].next != node) \ + break; \ + /* ... adjust the next pointer at that level. */ \ + path[i + 1].node->field.sle_levels[i].next = node->field.sle_levels[i].next; \ + /* Adjust the height such that we're only pointing at the tail once at \ + the top that way we don't waste steps later when searching. */ \ + if (path[i + 1].node->field.sle_levels[i].next == slist->slh_tail) { \ + height = path[i + 1].node->field.sle_height; \ + path[i + 1].node->field.sle_height = height - 1; \ + } \ + } \ + /* Account for delete at tail. */ \ + if (node->field.sle_levels[0].next == slist->slh_tail) { \ + slist->slh_tail->field.sle_prev = query->field.sle_prev; \ + } \ + \ + if (SKIPLIST_MAX_HEIGHT == 1) \ + free(path); \ + \ + slist->slh_fns.free_entry(node); \ + free(node); \ + \ + /* Reduce the height of the head/tail nodes. */ \ + for (i = 0; slist->slh_head->field.sle_levels[i].next != slist->slh_tail && i < SKIPLIST_MAX_HEIGHT; i++) \ + ; \ + slist->slh_head->field.sle_height = slist->slh_tail->field.sle_height = i; \ + \ + slist->slh_length--; \ + __skip_adjust_hit_counts_##decl(slist); \ + } \ + return 0; \ + } \ + \ + /** \ + * -- skip_free_ \ + * \ + * Release all nodes and their associated heap objects. The list reference \ + * is no longer valid after this call. To make it valid again call _init(). \ + */ \ + void prefix##skip_free_##decl(decl##_t *slist) \ + { \ + if (slist == NULL) \ + return; \ + \ + if (slist->slh_snap.pres_era > 0 && slist->slh_fns.snapshot_release) \ + slist->slh_fns.snapshot_release(slist); \ + \ + prefix##skip_release_##decl(slist); \ + \ + free(slist->slh_head); \ + free(slist->slh_tail); \ } #define SKIPLIST_DECL_SNAPSHOTS(decl, prefix, field) \ -- 2.43.4 From a55f05c91b39b025fe17fec05715d996a7957b74 Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Tue, 28 May 2024 15:06:02 -0400 Subject: [PATCH 06/11] WIP --- include/sl.h | 90 +++++++++++++++++++++++++++++----------------------- 1 file changed, 50 insertions(+), 40 deletions(-) diff --git a/include/sl.h b/include/sl.h index 634dd31..7ddef8e 100644 --- a/include/sl.h +++ b/include/sl.h @@ -292,7 +292,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li typedef struct __skiplist_path_##decl { \ decl##_node_t *node; /* node traversed in the act of location */ \ size_t in; /* level at which the node was intersected */ \ - size_t pu; /* sum of hits from intersection to level[1] */ \ + size_t pu; /* see "partial sums trick" */ \ } __skiplist_path_##decl##_t; \ \ /* Xorshift algorithm for PRNG */ \ @@ -635,10 +635,11 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li */ \ static void __skip_rebalance_##decl(decl##_t *slist, size_t len, __skiplist_path_##decl##_t path[]) \ { \ - size_t i, j, u_hits, hits_CHu = 0, hits_CHv = 0, delta_height, cur_hits, prev_hits; \ + size_t i, u_hits, hits_CHu = 0, hits_CHv = 0, delta_height, cur_hits, prv_hits; \ size_t k_threshold, m_total_hits; \ double asc_cond, dsc_cond; \ - __skiplist_path_##decl##_t cur, prv; \ + __skiplist_path_##decl##_t *p, path_u, path_v, *cur; \ + decl##_node_t *prv_node; \ \ /* Total hits, `k`, across all nodes. */ \ m_total_hits = slist->slh_head->field.sle_levels[slist->slh_head->field.sle_height].hits; \ @@ -652,55 +653,64 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li * - path[len] is where the locate() terminated, just before path[0] \ * if there was a match \ */ \ - i = 0; \ - do { \ + p = &path[0]; \ + for (i = 0; i < len; i++, p++) { \ hits_CHu = hits_CHv = 0; \ - if (path[i].node == slist->slh_head || path[i].node == slist->slh_tail) \ + path_u = *p; \ + if (path_u.node == slist->slh_head || path_u.node == slist->slh_tail) \ continue; \ + path_v = *(p + 1); \ + if ((*(p + 1)).node->entries.sle_levels[path_u.in].next != path_u.node) { \ + path_v.node = (*(p + 1)).node->entries.sle_levels[path_u.in].next; \ + path_v.pu = 0; \ + } \ \ - __SKIP_SUBTREE_CHux(decl, field, slist, path[i], path[i].in) \ + /* Evaluate the subtree 'u'. */ \ + __SKIP_SUBTREE_CHux(decl, field, slist, path_u, path_u.in) \ { \ hits_CHu += elm->field.sle_levels[0].hits; \ } \ - __SKIP_SUBTREE_CHux(decl, field, slist, path[i + 1], path[i].in) \ + /* Evaluate the subtree 'v' at the same level. */ \ + __SKIP_SUBTREE_CHux(decl, field, slist, path_v, path_u.in) \ { \ hits_CHv += elm->field.sle_levels[0].hits; \ } \ - u_hits = hits_CHu + hits_CHv; \ \ /* (a) Check the decent condition: \ * u_hits <= m_total_hits / (2 ^ (k_threshold - height of node)) \ - * When met should induce: \ + * When met we: \ * 1) traverse the path backward, and... \ * 2) propagate path[i].level[i] hits backward along path, and... \ * 3) adjust any forward pointers along the way, then... \ * 4) lower the path[i]'s node height by 1 \ */ \ - delta_height = k_threshold - path[i].node->field.sle_height; \ + delta_height = k_threshold - path_u.node->field.sle_height; \ dsc_cond = m_total_hits / pow(2.0, delta_height); \ - if (0 && u_hits <= dsc_cond && path[i].node->field.sle_height > 0) { \ - if (path[i].node->field.sle_prev != slist->slh_head) { \ + u_hits = hits_CHu + hits_CHv; \ + if (u_hits <= dsc_cond && path_u.node->field.sle_height > 0) { \ + if (path_u.node->field.sle_prev != slist->slh_head) { \ /* 1) go backwards along path from where we are until head */ \ - j = i; \ - cur = path[j]; \ - prv = path[j + 1]; \ - cur_hits = cur.node->field.sle_levels[cur.in].hits; \ + cur = &path[i]; \ + cur_hits = cur->node->field.sle_levels[cur->in].hits; \ do { \ + prv_node = (*(p + 1)).node->entries.sle_levels[path_u.in].next; \ + \ /* 2) propagate hits */ \ - prev_hits = prv.node->field.sle_levels[prv.in].hits; \ - prv.node->field.sle_levels[prv.in].hits += cur_hits; \ - cur_hits = prev_hits; \ + prv_hits = prv_node->field.sle_levels[cur->in].hits; \ + prv_node->field.sle_levels[cur->in].hits += 1; \ + cur_hits = prv_hits; \ /* 3) adjust forward pointers */ \ - if (prv.node->field.sle_levels[j].next == cur.node) \ - prv.node->field.sle_levels[j].next = cur.node->field.sle_levels[j].next; \ - } while (j-- > 1); \ + if (prv_node->field.sle_levels[cur->in].next == cur->node) \ + prv_node->field.sle_levels[cur->in].next = cur->node->field.sle_levels[cur->in].next; \ + cur++; \ + } while (cur->node != slist->slh_head); \ /* 4) reduce height by one */ \ - cur.node->field.sle_height--; \ + cur->node->field.sle_height--; \ } \ } \ /* (b) Check the ascent condition: \ * path[i].pu + node_hits > hits total / (2 ^ (height of head - height of node - 1)) \ - * When met should induce: \ + * When met we: \ * 1) check the ascent condition, then iff true ... \ * 2) add a level, and ... \ * 3) set its hits to the prev node at intersection height \ @@ -708,20 +718,18 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li */ \ /* 1) check ascent condition */ \ asc_cond = m_total_hits / pow(2.0, delta_height - 1); \ - if (path[i + 1].pu + hits_CHv > asc_cond && path[i].node->field.sle_height < SKIPLIST_MAX_HEIGHT) { \ - cur = path[i]; \ - prv = path[i + 1]; \ + if (path_u.pu > asc_cond && path_u.node->field.sle_height < SKIPLIST_MAX_HEIGHT) { \ /* 2) increase height by one */ \ - cur.node->field.sle_height++; \ + path_u.node->field.sle_height++; \ /* 3) update hit counter */ \ - cur.node->field.sle_levels[prv.in].hits = prv.node->field.sle_levels[prv.in].hits; \ + path_u.node->field.sle_levels[path_v.in].hits = path_v.node->field.sle_levels[path_v.in].hits; \ /* 4) reset the prev node hits to 0 */ \ - prv.node->field.sle_levels[prv.in].hits = 0; \ + path_v.node->field.sle_levels[path_v.in].hits = 0; \ /* 5) update forward pointers in the level */ \ - cur.node->field.sle_levels[prv.in].next = prv.node->field.sle_levels[prv.in].next; \ - prv.node->field.sle_levels[prv.in].next = cur.node; \ + path_u.node->field.sle_levels[path_v.in].next = path_v.node->field.sle_levels[path_v.in].next; \ + path_v.node->field.sle_levels[path_v.in].next = path_u.node; \ } \ - } while (i++ < len); \ + } \ } \ \ /** \ @@ -737,6 +745,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li unsigned int i; \ size_t len = 0; \ decl##_node_t *elm; \ + __skiplist_path_##decl##_t *cur; \ \ if (slist == NULL || n == NULL) \ return 0; \ @@ -746,21 +755,22 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li /* Find the node that matches `node` or NULL. */ \ i = slist->slh_head->field.sle_height - 1; \ do { \ - __skiplist_path_##decl##_t *pe = &path[i + 1]; \ - pe->pu = 0; \ + cur = &path[i + 1]; \ + cur->pu = 0; \ while (elm != slist->slh_tail && __skip_compare_nodes_##decl(slist, elm->field.sle_levels[i].next, n, slist->slh_aux) < 0) { \ - pe->in = i; \ - pe->pu += elm->field.sle_levels[0].hits; \ + cur->in = i; \ elm = elm->field.sle_levels[i].next; \ } \ - pe->node = elm; \ - pe->node->field.sle_levels[pe->in].hits += i ? 1 : 0; \ + cur->node = elm; \ + cur->node->field.sle_levels[cur->in].hits += i ? 1 : 0; \ + cur->pu += cur->node->field.sle_levels[0].hits; \ len++; \ } while (i--); \ elm = elm->field.sle_levels[0].next; \ if (__skip_compare_nodes_##decl(slist, elm, n, slist->slh_aux) == 0) { \ path[0].node = elm; \ path[0].node->field.sle_levels[0].hits++; \ + cur->pu += path[0].node->field.sle_levels[0].hits; \ slist->slh_head->entries.sle_levels[slist->slh_head->entries.sle_height].hits++; \ __skip_rebalance_##decl(slist, len, path); \ } \ -- 2.43.4 From dc30baea0a92a8f9bea6c0232c435f75e4615cc3 Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Tue, 28 May 2024 15:09:05 -0400 Subject: [PATCH 07/11] WIP --- include/sl.h | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/include/sl.h b/include/sl.h index 7ddef8e..8827231 100644 --- a/include/sl.h +++ b/include/sl.h @@ -678,7 +678,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li \ /* (a) Check the decent condition: \ * u_hits <= m_total_hits / (2 ^ (k_threshold - height of node)) \ - * When met we: \ + * When met we: \ * 1) traverse the path backward, and... \ * 2) propagate path[i].level[i] hits backward along path, and... \ * 3) adjust any forward pointers along the way, then... \ @@ -692,25 +692,21 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li /* 1) go backwards along path from where we are until head */ \ cur = &path[i]; \ cur_hits = cur->node->field.sle_levels[cur->in].hits; \ - do { \ - prv_node = (*(p + 1)).node->entries.sle_levels[path_u.in].next; \ - \ - /* 2) propagate hits */ \ - prv_hits = prv_node->field.sle_levels[cur->in].hits; \ - prv_node->field.sle_levels[cur->in].hits += 1; \ - cur_hits = prv_hits; \ - /* 3) adjust forward pointers */ \ - if (prv_node->field.sle_levels[cur->in].next == cur->node) \ - prv_node->field.sle_levels[cur->in].next = cur->node->field.sle_levels[cur->in].next; \ - cur++; \ - } while (cur->node != slist->slh_head); \ + prv_node = (*(p + 1)).node->entries.sle_levels[path_u.in].next; \ + /* 2) propagate hits */ \ + prv_hits = prv_node->field.sle_levels[cur->in].hits; \ + prv_node->field.sle_levels[cur->in].hits += 1; \ + cur_hits = prv_hits; \ + /* 3) adjust forward pointers */ \ + if (prv_node->field.sle_levels[cur->in].next == cur->node) \ + prv_node->field.sle_levels[cur->in].next = cur->node->field.sle_levels[cur->in].next; \ /* 4) reduce height by one */ \ cur->node->field.sle_height--; \ } \ } \ /* (b) Check the ascent condition: \ * path[i].pu + node_hits > hits total / (2 ^ (height of head - height of node - 1)) \ - * When met we: \ + * When met we: \ * 1) check the ascent condition, then iff true ... \ * 2) add a level, and ... \ * 3) set its hits to the prev node at intersection height \ -- 2.43.4 From b5ec8aca21afa788ed4d575f56710b52c398d49d Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Tue, 28 May 2024 22:46:47 -0400 Subject: [PATCH 08/11] WIP --- examples/ex2.c | 51 +++++++++++++++++------------ include/sl.h | 89 ++++++++++++++++++++++++++------------------------ 2 files changed, 77 insertions(+), 63 deletions(-) diff --git a/examples/ex2.c b/examples/ex2.c index fb24c4b..099fe4d 100644 --- a/examples/ex2.c +++ b/examples/ex2.c @@ -14,8 +14,8 @@ // --------------------------------------------------------------------------- #define TEST_ARRAY_SIZE 10 #define VALIDATE -//define SNAPSHOTS -//define TODO_RESTORE_SNAPSHOTS +// define SNAPSHOTS +// define TODO_RESTORE_SNAPSHOTS #define STABLE_SEED #define DOT @@ -189,24 +189,24 @@ int __xorshift32_state = 0; static uint32_t xorshift32() { - uint32_t x = __xorshift32_state; - if (x == 0) - x = 123456789; - x ^= x << 13; - x ^= x >> 17; - x ^= x << 5; - __xorshift32_state = x; - return x; + uint32_t x = __xorshift32_state; + if (x == 0) + x = 123456789; + x ^= x << 13; + x ^= x >> 17; + x ^= x << 5; + __xorshift32_state = x; + return x; } static void xorshift32_seed() { - // Seed the PRNG + // Seed the PRNG #ifdef STABLE_SEED - __xorshift32_state = 8675309; + __xorshift32_state = 8675309; #else - __xorshift32_state = (unsigned int)time(NULL) ^ getpid(); + __xorshift32_state = (unsigned int)time(NULL) ^ getpid(); #endif } @@ -358,7 +358,7 @@ main() rc = api_skip_put_ex(list, array[i], numeral); CHECK; #ifdef DOT - sprintf(msg, "put key: %d value: %s", i, numeral); + sprintf(msg, "put key: %d value: %s", array[i], numeral); api_skip_dot_ex(of, list, gen++, msg, sprintf_ex_node); #endif char *v = api_skip_get_ex(list, array[i]); @@ -369,24 +369,38 @@ main() to_upper(upper_numeral); api_skip_set_ex(list, array[i], upper_numeral); CHECK; + + for (size_t j = 0; j < api_skip_length_ex(list); j++) { + int n = xorshift32() % api_skip_length_ex(list); + api_skip_contains_ex(list, n); + CHECK; +#if 0 + sprintf(msg, "locate key: %d", n); + api_skip_dot_ex(of, list, gen++, msg, sprintf_ex_node); +#endif + } } numeral = int_to_roman_numeral(-1); api_skip_dup_ex(list, -1, numeral); CHECK; #ifdef DOT - sprintf(msg, "put dup key: %d value: %s", i, numeral); + sprintf(msg, "put dup key: %d value: %s", -1, numeral); api_skip_dot_ex(of, list, gen++, msg, sprintf_ex_node); #endif numeral = int_to_roman_numeral(1); api_skip_dup_ex(list, 1, numeral); CHECK; #ifdef DOT - sprintf(msg, "put dup key: %d value: %s", i, numeral); + sprintf(msg, "put dup key: %d value: %s", 1, numeral); api_skip_dot_ex(of, list, gen++, msg, sprintf_ex_node); #endif api_skip_del_ex(list, 0); CHECK; +#ifdef DOT + sprintf(msg, "deleted key: %d, value: %s", 0, numeral); + api_skip_dot_ex(of, list, gen++, msg, sprintf_ex_node); +#endif if (api_skip_get_ex(list, 0) != NULL) perror("found a deleted item!"); api_skip_del_ex(list, 0); @@ -400,11 +414,6 @@ main() api_skip_del_ex(list, key); CHECK; -#ifdef DOT - sprintf(msg, "deleted key: %d, value: %s", 0, numeral); - api_skip_dot_ex(of, list, gen++, msg, sprintf_ex_node); -#endif - numeral = int_to_roman_numeral(-(TEST_ARRAY_SIZE)); assert(strcmp(api_skip_pos_ex(list, SKIP_GTE, -(TEST_ARRAY_SIZE)-1)->value, numeral) == 0); free(numeral); diff --git a/include/sl.h b/include/sl.h index 8827231..2ca17a8 100644 --- a/include/sl.h +++ b/include/sl.h @@ -61,7 +61,7 @@ * A skip-list is a sorted list with O(log(n)) on average for most operations. * It is a probabilistic datastructure, meaning that it does not guarantee * O(log(n)), but it has been shown to approximate it over time. This - * implementation includes the rebalancing techniques that improve on that + * implementation includes the re-balancing techniques that improve on that * approximation using an adaptive technique called "splay-list". It is similar * to a standard skip-list, with the key distinction that the height of each * element adapts dynamically to its access rate: popular elements increase in @@ -231,12 +231,12 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li for (iter = (list)->slh_length, (elm) = (list)->slh_tail; ((elm) = (elm)->field.sle_prev) != (list)->slh_head; iter--) /* Iterate over the next pointers in a node from bottom to top (B2T) or top to bottom (T2B). */ -#define __SKIP_ALL_ENTRIES_T2B(field, elm) for (size_t lvl = slist->slh_head->entries.sle_height - 1; lvl != (size_t)-1; lvl--) +#define __SKIP_ALL_ENTRIES_T2B(field, elm) for (size_t lvl = slist->slh_head->field.sle_height - 1; lvl != (size_t)-1; lvl--) #define __SKIP_ENTRIES_T2B(field, elm) for (size_t lvl = elm->field.sle_height; lvl != (size_t)-1; lvl--) #define __SKIP_ENTRIES_T2B_FROM(field, elm, off) for (size_t lvl = off; lvl != (size_t)-1; lvl--) #define __SKIP_IS_LAST_ENTRY_T2B() if (lvl == 0) -#define __SKIP_ALL_ENTRIES_B2T(field, elm) for (size_t lvl = 0; lvl < slist->slh_head->entries.sle_height - 1; lvl++) +#define __SKIP_ALL_ENTRIES_B2T(field, elm) for (size_t lvl = 0; lvl < slist->slh_head->field.sle_height - 1; lvl++) #define __SKIP_ENTRIES_B2T(field, elm) for (size_t lvl = 0; lvl <= elm->field.sle_height; lvl++) #define __SKIP_ENTRIES_B2T_FROM(field, elm, off) for (size_t lvl = off; lvl <= elm->field.sle_height; lvl++) #define __SKIP_IS_LAST_ENTRY_B2T() if (lvl + 1 == elm->field.sle_height) @@ -635,8 +635,8 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li */ \ static void __skip_rebalance_##decl(decl##_t *slist, size_t len, __skiplist_path_##decl##_t path[]) \ { \ - size_t i, u_hits, hits_CHu = 0, hits_CHv = 0, delta_height, cur_hits, prv_hits; \ - size_t k_threshold, m_total_hits; \ + size_t i, lvl, u_hits, hits_CHu = 0, hits_CHv = 0, delta_height; \ + size_t k_threshold, m_total_hits, expected_height; \ double asc_cond, dsc_cond; \ __skiplist_path_##decl##_t *p, path_u, path_v, *cur; \ decl##_node_t *prv_node; \ @@ -644,6 +644,16 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li /* Total hits, `k`, across all nodes. */ \ m_total_hits = slist->slh_head->field.sle_levels[slist->slh_head->field.sle_height].hits; \ \ + /* Should we adjust the height? */ \ + if (m_total_hits > 0) { \ + expected_height = floor(log(m_total_hits) / log(2)); \ + if (expected_height > slist->slh_head->field.sle_height && expected_height < SKIPLIST_MAX_HEIGHT - 1) { \ + slist->slh_head->field.sle_height++; \ + slist->slh_head->field.sle_levels[slist->slh_head->field.sle_height].next = slist->slh_tail; \ + slist->slh_tail->field.sle_height = slist->slh_head->field.sle_height; \ + } \ + } \ + \ /* Height of the head node, should be equal to floor(log(m_total_hits)). */ \ k_threshold = slist->slh_head->field.sle_height; \ \ @@ -660,8 +670,8 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li if (path_u.node == slist->slh_head || path_u.node == slist->slh_tail) \ continue; \ path_v = *(p + 1); \ - if ((*(p + 1)).node->entries.sle_levels[path_u.in].next != path_u.node) { \ - path_v.node = (*(p + 1)).node->entries.sle_levels[path_u.in].next; \ + if ((*(p + 1)).node->field.sle_levels[path_u.in].next != path_u.node) { \ + path_v.node = (*(p + 1)).node->field.sle_levels[path_u.in].next; \ path_v.pu = 0; \ } \ \ @@ -688,21 +698,16 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li dsc_cond = m_total_hits / pow(2.0, delta_height); \ u_hits = hits_CHu + hits_CHv; \ if (u_hits <= dsc_cond && path_u.node->field.sle_height > 0) { \ - if (path_u.node->field.sle_prev != slist->slh_head) { \ - /* 1) go backwards along path from where we are until head */ \ - cur = &path[i]; \ - cur_hits = cur->node->field.sle_levels[cur->in].hits; \ - prv_node = (*(p + 1)).node->entries.sle_levels[path_u.in].next; \ - /* 2) propagate hits */ \ - prv_hits = prv_node->field.sle_levels[cur->in].hits; \ - prv_node->field.sle_levels[cur->in].hits += 1; \ - cur_hits = prv_hits; \ - /* 3) adjust forward pointers */ \ - if (prv_node->field.sle_levels[cur->in].next == cur->node) \ - prv_node->field.sle_levels[cur->in].next = cur->node->field.sle_levels[cur->in].next; \ - /* 4) reduce height by one */ \ - cur->node->field.sle_height--; \ - } \ + /* 1) go backwards along path from where we are until head */ \ + cur = &path[i]; \ + prv_node = (*(p + 1)).node->field.sle_levels[path_u.in].next; \ + /* 2) propagate hits */ \ + prv_node->field.sle_levels[cur->in].hits += 1; \ + /* 3) adjust forward pointers */ \ + if (prv_node->field.sle_levels[cur->in].next == cur->node) \ + prv_node->field.sle_levels[cur->in].next = cur->node->field.sle_levels[cur->in].next; \ + /* 4) reduce height by one */ \ + cur->node->field.sle_height--; \ } \ /* (b) Check the ascent condition: \ * path[i].pu + node_hits > hits total / (2 ^ (height of head - height of node - 1)) \ @@ -714,16 +719,16 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li */ \ /* 1) check ascent condition */ \ asc_cond = m_total_hits / pow(2.0, delta_height - 1); \ - if (path_u.pu > asc_cond && path_u.node->field.sle_height < SKIPLIST_MAX_HEIGHT) { \ + if (path_u.pu > asc_cond && path_u.node->field.sle_height < SKIPLIST_MAX_HEIGHT - 1) { \ /* 2) increase height by one */ \ - path_u.node->field.sle_height++; \ + lvl = ++path_u.node->field.sle_height; \ /* 3) update hit counter */ \ - path_u.node->field.sle_levels[path_v.in].hits = path_v.node->field.sle_levels[path_v.in].hits; \ + path_u.node->field.sle_levels[lvl].hits = path_v.node->field.sle_levels[lvl].hits; \ /* 4) reset the prev node hits to 0 */ \ - path_v.node->field.sle_levels[path_v.in].hits = 0; \ + path_v.node->field.sle_levels[lvl].hits = 0; \ /* 5) update forward pointers in the level */ \ - path_u.node->field.sle_levels[path_v.in].next = path_v.node->field.sle_levels[path_v.in].next; \ - path_v.node->field.sle_levels[path_v.in].next = path_u.node; \ + path_u.node->field.sle_levels[lvl].next = path_v.node->field.sle_levels[lvl].next; \ + path_v.node->field.sle_levels[lvl].next = path_u.node; \ } \ } \ } \ @@ -734,7 +739,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li * Locates a node that matches another node updating `path` and then \ * returning the length of that path + 1 to the node and the matching \ * node in path[0], or NULL at path[0] where there wasn't a match. \ - * sizeof(path) should be `slist->slh_head->entries.sle_height + 1` \ + * sizeof(path) should be `slist->slh_head->field.sle_height + 1` \ */ \ static size_t __skip_locate_##decl(decl##_t *slist, decl##_node_t *n, __skiplist_path_##decl##_t path[]) \ { \ @@ -767,7 +772,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li path[0].node = elm; \ path[0].node->field.sle_levels[0].hits++; \ cur->pu += path[0].node->field.sle_levels[0].hits; \ - slist->slh_head->entries.sle_levels[slist->slh_head->entries.sle_height].hits++; \ + slist->slh_head->field.sle_levels[slist->slh_head->field.sle_height].hits++; \ __skip_rebalance_##decl(slist, len, path); \ } \ return len; \ @@ -784,7 +789,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li { \ static __skiplist_path_##decl##_t apath[SKIPLIST_MAX_HEIGHT + 1]; \ int rc = 0; \ - size_t i, len, loc = 0, cur_height, new_height; \ + size_t i, len, loc = 0, current_height, new_height; \ decl##_node_t *node; \ __skiplist_path_##decl##_t *path = apath; \ \ @@ -801,12 +806,12 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li /* Don't insert, duplicate if flag not set. */ \ return -1; \ } \ + current_height = slist->slh_head->field.sle_height - 1; \ /* Coin toss to determine level of this new node [0, current max height) */ \ - cur_height = slist->slh_head->field.sle_height - 1; \ - new_height = __skip_toss_##decl(slist, cur_height); \ + new_height = __skip_toss_##decl(slist, current_height); \ new->field.sle_height = new_height; \ /* Trim the path to at most the new height for the new node. */ \ - for (i = cur_height + 1; i <= new_height; i++) { \ + for (i = current_height + 1; i <= new_height; i++) { \ path[i + 1].node = slist->slh_tail; \ } \ /* Ensure all next[] point to tail. */ \ @@ -933,7 +938,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li * -- skip_position_gt_ \ * \ * Position and return a cursor at the first node that is greater than \ - * the provided node `n`. If the largestkey is less than the key in `n` \ + * the provided node `n`. If the largest key is less than the key in `n` \ * return NULL. \ */ \ decl##_node_t *prefix##skip_position_gt_##decl(decl##_t *slist, decl##_node_t *query) \ @@ -1263,7 +1268,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li return 0; \ \ /* (a) alloc, ... */ \ - size_t sle_arr_sz = sizeof(struct __skiplist_##decl##_level) * slist->slh_head->entries.sle_height - 1; \ + size_t sle_arr_sz = sizeof(struct __skiplist_##decl##_level) * slist->slh_head->field.sle_height - 1; \ rc = prefix##skip_alloc_node_##decl(&dest); \ if (rc) \ return rc; \ @@ -1498,28 +1503,28 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li return n_err; \ } \ \ - if (slist->slh_head->entries.sle_height > SKIPLIST_MAX_HEIGHT) { \ + if (slist->slh_head->field.sle_height > SKIPLIST_MAX_HEIGHT) { \ __skip_integrity_failure_##decl("skiplist head height > SKIPLIST_MAX_HEIGHT\n"); \ n_err++; \ if (flags) \ return n_err; \ } \ \ - if (slist->slh_tail->entries.sle_height > SKIPLIST_MAX_HEIGHT) { \ + if (slist->slh_tail->field.sle_height > SKIPLIST_MAX_HEIGHT) { \ __skip_integrity_failure_##decl("skiplist tail height > SKIPLIST_MAX_HEIGHT\n"); \ n_err++; \ if (flags) \ return n_err; \ } \ \ - if (slist->slh_head->entries.sle_height != slist->slh_tail->entries.sle_height) { \ + if (slist->slh_head->field.sle_height != slist->slh_tail->field.sle_height) { \ __skip_integrity_failure_##decl("skiplist head & tail height are not equal\n"); \ n_err++; \ if (flags) \ return n_err; \ } \ \ - /* TODO: slh_head->entries.sle_height should == log(m) where m is the sum of all hits on all nodes */ \ + /* TODO: slh_head->field.sle_height should == log(m) where m is the sum of all hits on all nodes */ \ \ if (SKIPLIST_MAX_HEIGHT < 1) { \ __skip_integrity_failure_##decl("SKIPLIST_MAX_HEIGHT cannot be less than 1\n"); \ @@ -1576,9 +1581,9 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li { \ this = &node->field; \ \ - if (this->sle_height > slist->slh_head->entries.sle_height) { \ + if (this->sle_height > slist->slh_head->field.sle_height) { \ __skip_integrity_failure_##decl("the %lu node's [%p] height %lu is > head %lu\n", nth, (void *)node, this->sle_height, \ - slist->slh_head->entries.sle_height); \ + slist->slh_head->field.sle_height); \ n_err++; \ if (flags) \ return n_err; \ -- 2.43.4 From d4813c8ddb1923525a26de2defdabebb1b74304c Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Tue, 28 May 2024 23:12:59 -0400 Subject: [PATCH 09/11] WIP --- include/sl.h | 7 ------- 1 file changed, 7 deletions(-) diff --git a/include/sl.h b/include/sl.h index 2ca17a8..90eceee 100644 --- a/include/sl.h +++ b/include/sl.h @@ -832,13 +832,6 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li new->field.sle_levels[i].next = slist->slh_tail; \ } \ } \ - /* Ensure all slh_head->next[] above loc point to tail. */ \ - if (path[1].node == slist->slh_head) { \ - __SKIP_ENTRIES_B2T_FROM(field, slist->slh_head, loc + 1) \ - { \ - slist->slh_head->field.sle_levels[lvl].next = slist->slh_tail; \ - } \ - } \ /* Adjust the previous pointers in the nodes. */ \ new->field.sle_prev = path[1].node; \ new->field.sle_levels[0].next->field.sle_prev = new; \ -- 2.43.4 From fa0fd4b55233a7a13a2740fdefadaed88209b07f Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Wed, 29 May 2024 15:26:42 -0400 Subject: [PATCH 10/11] WIP --- examples/ex2.c | 18 +++++++++---- include/sl.h | 73 +++++++++++++++++++++++++++----------------------- 2 files changed, 53 insertions(+), 38 deletions(-) diff --git a/examples/ex2.c b/examples/ex2.c index 099fe4d..2517feb 100644 --- a/examples/ex2.c +++ b/examples/ex2.c @@ -16,7 +16,7 @@ #define VALIDATE // define SNAPSHOTS // define TODO_RESTORE_SNAPSHOTS -#define STABLE_SEED +//define STABLE_SEED #define DOT #ifdef DOT @@ -362,23 +362,31 @@ main() api_skip_dot_ex(of, list, gen++, msg, sprintf_ex_node); #endif char *v = api_skip_get_ex(list, array[i]); +#ifdef DOT + sprintf(msg, "get key: %d", array[i]); + api_skip_dot_ex(of, list, gen++, msg, sprintf_ex_node); +#endif CHECK; char *upper_numeral = calloc(1, strlen(v) + 1); strncpy(upper_numeral, v, strlen(v)); assert(strncmp(v, upper_numeral, strlen(upper_numeral)) == 0); to_upper(upper_numeral); api_skip_set_ex(list, array[i], upper_numeral); +#ifdef DOT + sprintf(msg, "set key: %d value: %s", array[i], upper_numeral); + api_skip_dot_ex(of, list, gen++, msg, sprintf_ex_node); +#endif CHECK; for (size_t j = 0; j < api_skip_length_ex(list); j++) { int n = xorshift32() % api_skip_length_ex(list); api_skip_contains_ex(list, n); CHECK; -#if 0 - sprintf(msg, "locate key: %d", n); - api_skip_dot_ex(of, list, gen++, msg, sprintf_ex_node); -#endif } +#ifdef DOT + sprintf(msg, "locate all the keys!!!"); + api_skip_dot_ex(of, list, gen++, msg, sprintf_ex_node); +#endif } numeral = int_to_roman_numeral(-1); api_skip_dup_ex(list, -1, numeral); diff --git a/include/sl.h b/include/sl.h index 90eceee..f4182d5 100644 --- a/include/sl.h +++ b/include/sl.h @@ -247,8 +247,8 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li #define __SKIP_SUBTREE_CHu(decl, field, list, path, nth) \ for (decl##_node_t *elm = path[nth].node; elm != path[nth].node->field.sle_levels[0].next; elm = elm->field.sle_levels[0].next) /* Iterate over a subtree starting at provided path element, u = path.in */ -#define __SKIP_SUBTREE_CHux(decl, field, list, path, level) \ - for (decl##_node_t *elm = path.node->field.sle_levels[level].next->field.sle_prev; elm != path.node->field.sle_prev; elm = elm->field.sle_prev) +#define __SKIP_SUBTREE_CHux(decl, field, list, node, level) \ + for (decl##_node_t *elm = node->field.sle_levels[level].next->field.sle_prev; elm != node->field.sle_prev; elm = elm->field.sle_prev) /* * Skiplist declarations and access methods. @@ -638,8 +638,8 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li size_t i, lvl, u_hits, hits_CHu = 0, hits_CHv = 0, delta_height; \ size_t k_threshold, m_total_hits, expected_height; \ double asc_cond, dsc_cond; \ - __skiplist_path_##decl##_t *p, path_u, path_v, *cur; \ - decl##_node_t *prv_node; \ + __skiplist_path_##decl##_t *p, path_u, path_v; \ + decl##_node_t *node; \ \ /* Total hits, `k`, across all nodes. */ \ m_total_hits = slist->slh_head->field.sle_levels[slist->slh_head->field.sle_height].hits; \ @@ -650,6 +650,9 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li if (expected_height > slist->slh_head->field.sle_height && expected_height < SKIPLIST_MAX_HEIGHT - 1) { \ slist->slh_head->field.sle_height++; \ slist->slh_head->field.sle_levels[slist->slh_head->field.sle_height].next = slist->slh_tail; \ + slist->slh_head->field.sle_levels[slist->slh_head->field.sle_height].hits = \ + slist->slh_head->field.sle_levels[slist->slh_head->field.sle_height - 1].hits; \ + slist->slh_head->field.sle_levels[slist->slh_head->field.sle_height - 1].hits = 0; \ slist->slh_tail->field.sle_height = slist->slh_head->field.sle_height; \ } \ } \ @@ -670,18 +673,14 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li if (path_u.node == slist->slh_head || path_u.node == slist->slh_tail) \ continue; \ path_v = *(p + 1); \ - if ((*(p + 1)).node->field.sle_levels[path_u.in].next != path_u.node) { \ - path_v.node = (*(p + 1)).node->field.sle_levels[path_u.in].next; \ - path_v.pu = 0; \ - } \ \ /* Evaluate the subtree 'u'. */ \ - __SKIP_SUBTREE_CHux(decl, field, slist, path_u, path_u.in) \ + __SKIP_SUBTREE_CHux(decl, field, slist, path_u.node, path_u.in) \ { \ hits_CHu += elm->field.sle_levels[0].hits; \ } \ /* Evaluate the subtree 'v' at the same level. */ \ - __SKIP_SUBTREE_CHux(decl, field, slist, path_v, path_u.in) \ + __SKIP_SUBTREE_CHux(decl, field, slist, (*(p + 1)).node->field.sle_levels[path_u.in].next, path_u.in) \ { \ hits_CHv += elm->field.sle_levels[0].hits; \ } \ @@ -689,25 +688,30 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li /* (a) Check the decent condition: \ * u_hits <= m_total_hits / (2 ^ (k_threshold - height of node)) \ * When met we: \ - * 1) traverse the path backward, and... \ - * 2) propagate path[i].level[i] hits backward along path, and... \ - * 3) adjust any forward pointers along the way, then... \ + * 1) go backwards along path from where we are until head \ + * 2) adjust any forward pointers, then... \ + * 3) adjust hits, and... \ * 4) lower the path[i]'s node height by 1 \ */ \ delta_height = k_threshold - path_u.node->field.sle_height; \ dsc_cond = m_total_hits / pow(2.0, delta_height); \ u_hits = hits_CHu + hits_CHv; \ if (u_hits <= dsc_cond && path_u.node->field.sle_height > 0) { \ + lvl = path_u.node->field.sle_height; \ /* 1) go backwards along path from where we are until head */ \ - cur = &path[i]; \ - prv_node = (*(p + 1)).node->field.sle_levels[path_u.in].next; \ - /* 2) propagate hits */ \ - prv_node->field.sle_levels[cur->in].hits += 1; \ - /* 3) adjust forward pointers */ \ - if (prv_node->field.sle_levels[cur->in].next == cur->node) \ - prv_node->field.sle_levels[cur->in].next = cur->node->field.sle_levels[cur->in].next; \ + node = path_u.node; \ + do { \ + node = node->field.sle_prev; \ + /* 2) adjust forward pointers */ \ + if (node->field.sle_height >= lvl && node->field.sle_levels[lvl].next == path_u.node) { \ + node->field.sle_levels[lvl].next = path_u.node->field.sle_levels[lvl].next; \ + /* 3) adjust hits */ \ + node->field.sle_levels[lvl].hits += 1; \ + break; \ + } \ + } while (node != slist->slh_head); \ /* 4) reduce height by one */ \ - cur->node->field.sle_height--; \ + path_u.node->field.sle_height--; \ } \ /* (b) Check the ascent condition: \ * path[i].pu + node_hits > hits total / (2 ^ (height of head - height of node - 1)) \ @@ -719,14 +723,16 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li */ \ /* 1) check ascent condition */ \ asc_cond = m_total_hits / pow(2.0, delta_height - 1); \ - if (path_u.pu > asc_cond && path_u.node->field.sle_height < SKIPLIST_MAX_HEIGHT - 1) { \ - /* 2) increase height by one */ \ + if (path_u.pu > asc_cond && path_u.node->entries.sle_height < 64 - 1) { \ + if (path_u.node->field.sle_height + 1 > path_v.node->field.sle_height) { \ + continue; \ + } \ lvl = ++path_u.node->field.sle_height; \ - /* 3) update hit counter */ \ - path_u.node->field.sle_levels[lvl].hits = path_v.node->field.sle_levels[lvl].hits; \ - /* 4) reset the prev node hits to 0 */ \ - path_v.node->field.sle_levels[lvl].hits = 0; \ - /* 5) update forward pointers in the level */ \ + /* Don't adjust hits when the previous node on the path is the head. */ \ + if (path_v.node != slist->slh_head) { \ + path_u.node->field.sle_levels[lvl].hits = path_v.node->entries.sle_levels[lvl].hits; \ + path_v.node->field.sle_levels[lvl].hits = 0; \ + } \ path_u.node->field.sle_levels[lvl].next = path_v.node->field.sle_levels[lvl].next; \ path_v.node->field.sle_levels[lvl].next = path_u.node; \ } \ @@ -1155,6 +1161,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li /* Reduce the height of the head/tail nodes. */ \ for (i = 0; slist->slh_head->field.sle_levels[i].next != slist->slh_tail && i < SKIPLIST_MAX_HEIGHT; i++) \ ; \ + slist->slh_head->field.sle_levels[i].hits = slist->slh_head->field.sle_levels[slist->slh_head->field.sle_height].hits; \ slist->slh_head->field.sle_height = slist->slh_tail->field.sle_height = i; \ \ slist->slh_length--; \ @@ -1937,10 +1944,10 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li \ if (slist == NULL || fn == NULL) \ return nsg; \ - if (__skip_integrity_check_##decl(slist, 1) != 0) { \ + /*if (__skip_integrity_check_##decl(slist, 1) != 0) { \ perror("Skiplist failed integrity checks, impossible to diagram."); \ return -1; \ - } \ + }*/ \ if (nsg == 0) { \ fprintf(os, "digraph Skiplist {\n"); \ fprintf(os, "label = \"Skiplist.\"\n"); \ @@ -1950,9 +1957,9 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li } \ fprintf(os, "subgraph cluster%lu {\n", nsg); \ fprintf(os, "style=dashed\n"); \ - fprintf(os, "label=\"Skiplist iteration %lu", nsg); \ + fprintf(os, "label=\"Skiplist [%lu]", nsg); \ if (msg) \ - fprintf(os, ", %s", msg); \ + fprintf(os, " %s", msg); \ fprintf(os, "\"\n\n"); \ fprintf(os, "\"HeadNode%lu\" [\n", nsg); \ fprintf(os, "label = \""); \ @@ -2011,7 +2018,7 @@ void __attribute__((format(printf, 4, 5))) __skip_diag_(const char *file, int li } \ fflush(os); \ \ - /* The tail, sentinal node */ \ + /* The tail, sentinel node */ \ if (letitgo) { \ __skip_dot_write_node_##decl(os, nsg, NULL); \ fprintf(os, " [label = \""); \ -- 2.43.4 From 5521451bf381c4db48b5e885dcccffc755468628 Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Wed, 29 May 2024 15:42:50 -0400 Subject: [PATCH 11/11] WIP --- examples/ex2.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/examples/ex2.c b/examples/ex2.c index 2517feb..427967f 100644 --- a/examples/ex2.c +++ b/examples/ex2.c @@ -16,7 +16,7 @@ #define VALIDATE // define SNAPSHOTS // define TODO_RESTORE_SNAPSHOTS -//define STABLE_SEED +// define STABLE_SEED #define DOT #ifdef DOT @@ -315,8 +315,10 @@ main() if (rc) return rc; - /* Set the PRNG state to a known constant for reproducible generation, easing debugging. */ + /* Set the PRNG state to a known constant for reproducible generation, easing debugging. */ +#ifdef STABLE_SEED list->slh_prng_state = 12; +#endif #ifdef SNAPSHOTS api_skip_snapshots_init_ex(list); #endif @@ -378,12 +380,12 @@ main() #endif CHECK; - for (size_t j = 0; j < api_skip_length_ex(list); j++) { + for (size_t j = 0, k = api_skip_length_ex(list); j < k; j++) { int n = xorshift32() % api_skip_length_ex(list); - api_skip_contains_ex(list, n); + api_skip_contains_ex(list, array[n]); CHECK; } -#ifdef DOT +#if 0 sprintf(msg, "locate all the keys!!!"); api_skip_dot_ex(of, list, gen++, msg, sprintf_ex_node); #endif @@ -491,6 +493,18 @@ main() api_skip_release_snapshots_ex(list); #endif + for (size_t i = 0; i < 1000000; i++) { + for (size_t j = 0, k = api_skip_length_ex(list); j < k; j++) { + int n = xorshift32() % api_skip_length_ex(list); + api_skip_contains_ex(list, array[n]); + CHECK; + } + } +#ifdef DOT + sprintf(msg, "lots-o-contains later..."); + api_skip_dot_ex(of, list, gen++, msg, sprintf_ex_node); +#endif + #ifdef DOT api_skip_dot_end_ex(of, gen); fclose(of); -- 2.43.4