From d327c256aff092a9426ad11bf746669c7c2e8a85 Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Tue, 28 May 2024 11:05:39 -0400 Subject: [PATCH] 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) \