2024-03-19 01:19:26 +00:00
|
|
|
#include <errno.h>
|
2024-03-18 18:58:35 +00:00
|
|
|
#include <stddef.h>
|
2024-03-17 14:40:59 +00:00
|
|
|
#include <stdint.h>
|
2024-03-19 01:19:26 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2024-03-17 14:40:59 +00:00
|
|
|
#include "../include/sl.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SKIPLIST EXAMPLE:
|
|
|
|
*
|
|
|
|
* This example creates a Skiplist keys and values are integers.
|
2024-03-18 15:06:49 +00:00
|
|
|
* 'slex' - meaning: SkipList EXample
|
2024-03-17 14:40:59 +00:00
|
|
|
*/
|
|
|
|
|
2024-03-18 18:58:35 +00:00
|
|
|
/*
|
|
|
|
* 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 -> int, 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.
|
|
|
|
*/
|
2024-03-18 15:06:49 +00:00
|
|
|
struct slex_node {
|
2024-03-17 14:40:59 +00:00
|
|
|
int key;
|
|
|
|
int value;
|
2024-03-18 15:06:49 +00:00
|
|
|
SKIP_ENTRY(slex_node) entries;
|
2024-03-17 14:40:59 +00:00
|
|
|
};
|
|
|
|
|
2024-03-18 18:58:35 +00:00
|
|
|
/*
|
|
|
|
* Generate all the access functions for our type of Skiplist.
|
2024-03-19 13:41:36 +00:00
|
|
|
*/
|
2024-03-19 16:33:11 +00:00
|
|
|
SKIPLIST_DECL(
|
|
|
|
slex, api_, entries,
|
|
|
|
/* free node */ { (void)node; },
|
|
|
|
/* update node */ { node->value = new->value; })
|
2024-03-19 13:41:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Getter
|
|
|
|
* 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_GETTER(
|
|
|
|
slex, api_, get, int, int, { query.key = key; }, { return node->value; })
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now we need a way to compare the nodes you defined above.
|
|
|
|
* Let's create a function with four arguments:
|
2024-03-18 18:58:35 +00:00
|
|
|
* - 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.
|
|
|
|
*
|
2024-03-19 13:41:36 +00:00
|
|
|
* Your function should result in a return statement:
|
2024-03-18 18:58:35 +00:00
|
|
|
* a < b : return -1
|
|
|
|
* a == b : return 0
|
|
|
|
* a > b : return 1
|
|
|
|
*
|
|
|
|
* This result provides the ordering within the Skiplist. Sometimes
|
2024-03-19 13:41:36 +00:00
|
|
|
* 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
|
2024-03-18 18:58:35 +00:00
|
|
|
* list or when `a == b`. In those cases the comparison function
|
|
|
|
* returns before using the code in your block, don't panic. :)
|
|
|
|
*/
|
2024-03-19 01:19:26 +00:00
|
|
|
int
|
|
|
|
__slm_key_compare(slex_t *list, slex_node_t *a, slex_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;
|
|
|
|
}
|
2024-03-17 14:40:59 +00:00
|
|
|
|
2024-03-19 01:19:26 +00:00
|
|
|
#define DOT
|
|
|
|
#ifdef DOT
|
|
|
|
/* Also declare the functions used to visualize a Sliplist (DOT/Graphviz) */
|
|
|
|
SKIPLIST_DECL_DOT(slex, api_, entries)
|
|
|
|
|
|
|
|
void
|
|
|
|
sprintf_slex_node(slex_node_t *node, char *buf)
|
|
|
|
{
|
|
|
|
sprintf(buf, "%d:%d", node->key, node->value);
|
2024-03-18 20:15:01 +00:00
|
|
|
}
|
2024-03-19 01:19:26 +00:00
|
|
|
#endif
|
2024-03-18 20:15:01 +00:00
|
|
|
|
2024-03-19 01:19:26 +00:00
|
|
|
int
|
|
|
|
main()
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
/* Allocate and initialize a Skiplist. */
|
2024-03-18 15:06:49 +00:00
|
|
|
slex_t *list = (slex_t *)malloc(sizeof(slex_t));
|
2024-03-19 01:19:26 +00:00
|
|
|
if (list == NULL) {
|
|
|
|
rc = ENOMEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
rc = api_skip_init_slex(list, 12, 4, __slm_key_compare);
|
2024-03-19 13:41:36 +00:00
|
|
|
if (rc)
|
|
|
|
return rc;
|
2024-03-17 14:40:59 +00:00
|
|
|
|
2024-03-18 15:06:49 +00:00
|
|
|
struct slex_node *n;
|
|
|
|
|
2024-03-19 01:19:26 +00:00
|
|
|
/* Insert 7 key/value pairs into the list. */
|
|
|
|
for (int i = -2; i <= 2; i++) {
|
2024-03-19 16:33:11 +00:00
|
|
|
int v;
|
|
|
|
slex_node_t new;
|
2024-03-19 13:41:36 +00:00
|
|
|
rc = api_skip_alloc_node_slex(list, &n);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
2024-03-19 01:19:26 +00:00
|
|
|
n->key = i;
|
|
|
|
n->value = i;
|
|
|
|
api_skip_insert_slex(list, n);
|
2024-03-19 16:33:11 +00:00
|
|
|
v = api_skip_get_slex(list, i);
|
|
|
|
((void)v);
|
|
|
|
new.key = n->key;
|
|
|
|
new.value = n->value * 10;
|
|
|
|
api_skip_update_slex(list, &new);
|
2024-03-19 01:19:26 +00:00
|
|
|
}
|
|
|
|
|
2024-03-19 16:33:11 +00:00
|
|
|
slex_node_t q;
|
|
|
|
q.key = 0;
|
|
|
|
api_skip_remove_slex(list, &q);
|
|
|
|
|
2024-03-19 01:19:26 +00:00
|
|
|
FILE *of = fopen("/tmp/slm.dot", "w");
|
2024-03-18 20:15:01 +00:00
|
|
|
if (!of) {
|
2024-03-19 01:19:26 +00:00
|
|
|
perror("Failed to open file /tmp/slm.dot");
|
|
|
|
return EXIT_FAILURE;
|
2024-03-18 20:15:01 +00:00
|
|
|
}
|
|
|
|
api_skip_dot_slex(of, list, sprintf_slex_node);
|
|
|
|
fclose(of);
|
2024-03-18 19:42:59 +00:00
|
|
|
|
2024-03-19 16:57:29 +00:00
|
|
|
api_skip_destroy_slex(list);
|
|
|
|
|
2024-03-17 14:40:59 +00:00
|
|
|
#if 0
|
|
|
|
/* Delete a specific element in the list. */
|
2024-03-18 15:06:49 +00:00
|
|
|
struct slex_node query;
|
2024-03-17 14:40:59 +00:00
|
|
|
query.key = 4;
|
2024-03-18 15:06:49 +00:00
|
|
|
struct slex_node *removed = SKIP_REMOVE(list, q, entries);
|
2024-03-17 14:40:59 +00:00
|
|
|
free(removed);
|
|
|
|
/* Forward traversal. */
|
|
|
|
SKIP_FOREACH(np, &head, entries)
|
|
|
|
np-> ...
|
|
|
|
/* Reverse traversal. */
|
|
|
|
SKIP_FOREACH_REVERSE(np, &head, skiphead, entries)
|
|
|
|
np-> ...
|
|
|
|
/* Skiplist Deletion. */
|
|
|
|
while (!SKIP_EMPTY(&head)) {
|
|
|
|
n1 = SKIP_FIRST(&head);
|
|
|
|
SKIP_REMOVE(&head, n1, entries);
|
|
|
|
free(n1);
|
|
|
|
}
|
|
|
|
/* Faster Skiplist Deletion. */
|
|
|
|
n1 = SKIP_FIRST(&head);
|
|
|
|
while (n1 != NULL) {
|
|
|
|
n2 = SKIP_NEXT(n1, entries);
|
|
|
|
free(n1);
|
|
|
|
n1 = n2;
|
|
|
|
}
|
|
|
|
SKIP_INIT(&head);
|
|
|
|
#endif
|
2024-03-19 01:19:26 +00:00
|
|
|
fail:;
|
|
|
|
return rc;
|
2024-03-17 14:40:59 +00:00
|
|
|
}
|