WIP snaps, pt4

This commit is contained in:
Gregory Burd 2024-03-21 12:30:37 -04:00
parent f2794ac93b
commit a5c49e1d36
3 changed files with 172 additions and 72 deletions

30
.editorconfig Normal file
View file

@ -0,0 +1,30 @@
root = true
[{*.{awk,c,dts,dtsi,dtso,h,mk,s,S},Kconfig,Makefile,Makefile.*}]
charset = utf-8
end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 4
[*.{json,py,rs}]
charset = utf-8
end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 4
# this must be below the general *.py to overwrite it
[tools/{perf,power,rcu,testing/kunit}/**.py,]
indent_style = tab
indent_size = 4
[*.yaml]
charset = utf-8
end_of_line = lf
trim_trailing_whitespace = unset
insert_final_newline = true
indent_style = space
indent_size = 2

View file

@ -1,9 +1,12 @@
#include <sys/time.h>
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
@ -31,7 +34,7 @@
*/ */
struct slex_node { struct slex_node {
int key; int key;
int value; char *value;
SKIPLIST_ENTRY(slex_node) entries; SKIPLIST_ENTRY(slex_node) entries;
}; };
@ -42,8 +45,15 @@ SKIPLIST_DECL(
slex, api_, entries, slex, api_, entries,
/* free node */ { (void)node; }, /* free node */ { (void)node; },
/* update node */ { node->value = new->value; }, /* update node */ { node->value = new->value; },
/* snapshot node */ { new->key = node->key; new->value = node->value; }, /* snapshot node */
int, /* into array */ { elm = node->value; }) {
new->key = node->key;
new->value = strncpy(new->value, node->value, strlen(node->value));
},
/* size in bytes of the content stored in an entry by you */
{
size = strlen(node->value) + 1;
})
/* /*
* Getter * Getter
@ -52,7 +62,7 @@ SKIPLIST_DECL(
* extract data from within your nodes. * extract data from within your nodes.
*/ */
SKIPLIST_GETTERS( SKIPLIST_GETTERS(
slex, api_, int, int, { query.key = key; }, { return node->value; }) slex, api_, int, char *, { query.key = key; }, { return node->value; })
/* /*
* Now we need a way to compare the nodes you defined above. * Now we need a way to compare the nodes you defined above.
@ -87,6 +97,56 @@ __slm_key_compare(slex_t *list, slex_node_t *a, slex_node_t *b, void *aux)
return 0; return 0;
} }
static char* to_lower(char* str) {
char *p = str;
for ( ; *p; ++p) *p = *p >= 'A' && *p <= 'Z' ? *p|0x60 : *p;
return str;
}
static char* to_upper(char* str) {
char *p = str;
for ( ; *p; ++p) *p = *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(64, sizeof(char));
int i = 0;
if (num < 0) {
res[0] = '-';
i++;
num = -num;
}
if (num == 0) {
res[0] = '0';
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)(rand() % (i+1)); /* NOLINT(*-msc50-cpp) */
int t = array[j];
array[j] = array[i];
array[i] = t;
}
}
}
#define DOT #define DOT
#ifdef DOT #ifdef DOT
/* Also declare the functions used to visualize a Sliplist (DOT/Graphviz) */ /* Also declare the functions used to visualize a Sliplist (DOT/Graphviz) */
@ -95,40 +155,53 @@ SKIPLIST_DECL_DOT(slex, api_, entries)
void void
sprintf_slex_node(slex_node_t *node, char *buf) sprintf_slex_node(slex_node_t *node, char *buf)
{ {
sprintf(buf, "%d:%d", node->key, node->value); sprintf(buf, "%d:%s", node->key, node->value);
} }
#endif #endif
#define TEST_ARRAY_SIZE 50
int int
main() main()
{ {
int rc = 0; int rc = 0;
/* Allocate and initialize a Skiplist. */ /* Allocate and initialize a Skiplist. */
slex_t *list = (slex_t *)malloc(sizeof(slex_t)); slex_t *list = (slex_t *)malloc(sizeof(slex_t));
if (list == NULL) { if (list == NULL)
rc = ENOMEM; return ENOMEM;
goto fail;
}
rc = api_skip_init_slex(list, 12, __slm_key_compare); rc = api_skip_init_slex(list, 12, __slm_key_compare);
if (rc) if (rc)
return rc; return rc;
struct slex_node *n; /* Test creating a snapshot of an empty Skiplist */
slex_snap_t *snap = api_skip_snapshot_slex(list);
slex_t *restored = api_skip_restore_snapshot_slex(snap, __skip_key_compare_slex);
api_skip_dispose_snapshot_slex(snap);
api_skip_destroy_slex(restored);
/* Insert 7 key/value pairs into the list. */ /* Insert 7 key/value pairs into the list. */
for (int i = -50; i <= 50; i++) { int amt = TEST_ARRAY_SIZE, asz = (amt * 2) + 1;
int v; int array[(TEST_ARRAY_SIZE * 2) + 1];
for (int j = 0, i = -amt; i <= amt; i++, j++)
array[j] = i;
shuffle(array, asz);
for (int i = 0; i <= asz; i++) {
struct slex_node *n;
char *v;
slex_node_t new; slex_node_t new;
rc = api_skip_alloc_node_slex(list, &n); rc = api_skip_alloc_node_slex(list, &n);
if (rc) if (rc)
return rc; return rc;
n->key = i; n->key = array[i];
n->value = i; n->value = to_lower(int_to_roman_numeral(array[i]));
api_skip_insert_slex(list, n); api_skip_insert_slex(list, n);
v = api_skip_get_slex(list, i); v = api_skip_get_slex(list, array[i]);
((void)v); ((void)v);
new.key = n->key; new.key = n->key;
new.value = n->value * 10; new.value = to_upper(n->value);
api_skip_update_slex(list, &new); api_skip_update_slex(list, &new);
} }
@ -136,24 +209,22 @@ main()
q.key = 0; q.key = 0;
api_skip_remove_slex(list, &q); api_skip_remove_slex(list, &q);
slex_snap_t *snap = api_skip_snapshot_slex(list); snap = api_skip_snapshot_slex(list);
slex_t *restored = api_skip_restore_snapshot_slex(snap, __skip_key_compare_slex); restored = api_skip_restore_snapshot_slex(snap, __skip_key_compare_slex);
api_skip_dispose_snapshot_slex(snap); api_skip_dispose_snapshot_slex(snap);
api_skip_destroy_slex(restored); api_skip_destroy_slex(restored);
// assert(api_skip_gte_slex(list, -3000000) == -20); assert(api_skip_gte_slex(list, -6) == int_to_roman_numeral(-5));
assert(api_skip_gte_slex(list, -2) == -20); assert(api_skip_gte_slex(list, -2) == int_to_roman_numeral(-2));
assert(api_skip_gte_slex(list, 0) == 10); assert(api_skip_gte_slex(list, 0) == int_to_roman_numeral(0));
// assert(api_skip_gte_slex(list, 0) == 0); assert(api_skip_gte_slex(list, 2) == int_to_roman_numeral(2));
assert(api_skip_gte_slex(list, 2) == 20); assert(api_skip_gte_slex(list, 6) == int_to_roman_numeral(0));
assert(api_skip_gte_slex(list, 30000000) == 0);
assert(api_skip_lte_slex(list, -3000000) == 0); assert(api_skip_lte_slex(list, -6) == int_to_roman_numeral(0));
assert(api_skip_lte_slex(list, -2) == -20); assert(api_skip_lte_slex(list, -2) == int_to_roman_numeral(-2));
assert(api_skip_lte_slex(list, 0) == -10); assert(api_skip_lte_slex(list, 0) == int_to_roman_numeral(-10));
// assert(api_skip_lte_slex(list, 0) == 0); assert(api_skip_lte_slex(list, 2) == int_to_roman_numeral(2));
assert(api_skip_lte_slex(list, 2) == 20); assert(api_skip_lte_slex(list, 6) == int_to_roman_numeral(20));
// assert(api_skip_lte_slex(list, 30000000) == 20);
FILE *of = fopen("/tmp/slm.dot", "w"); FILE *of = fopen("/tmp/slm.dot", "w");
if (!of) { if (!of) {
@ -165,6 +236,5 @@ main()
api_skip_destroy_slex(list); api_skip_destroy_slex(list);
fail:;
return rc; return rc;
} }

View file

@ -128,7 +128,7 @@
fn \ fn \
} }
#define SKIPLIST_DECL(decl, prefix, field, free_node_blk, update_node_blk, snap_node_blk, array_type, into_array_blk) \ #define SKIPLIST_DECL(decl, prefix, field, free_node_blk, update_node_blk, snap_node_blk, sizeof_entry_blk) \
\ \
/* Skiplist node type */ \ /* Skiplist node type */ \
typedef struct decl##_node decl##_node_t; \ typedef struct decl##_node decl##_node_t; \
@ -142,11 +142,15 @@
decl##_node_t *slh_tail; \ decl##_node_t *slh_tail; \
} decl##_t; \ } decl##_t; \
\ \
/* Snapshot of a Skiplist */ \
typedef struct decl##_snap { \ typedef struct decl##_snap { \
decl##_t list; \ decl##_t list; \
decl##_node_t **nodes; \ decl##_node_t *nodes; \
size_t bytes; \
} decl##_snap_t; \ } decl##_snap_t; \
\ \
/* Path taken to find an element in the Skiplist, path[0] is the match if \
one exists */ \
struct __##decl##_path { \ struct __##decl##_path { \
size_t cap; \ size_t cap; \
size_t len; \ size_t len; \
@ -274,13 +278,13 @@
/* -- skip_head_ */ \ /* -- skip_head_ */ \
decl##_node_t *prefix##skip_head_##decl(decl##_t *slist) \ decl##_node_t *prefix##skip_head_##decl(decl##_t *slist) \
{ \ { \
return slist->slh_head->field.sle.next[0]; \ return slist->slh_head->field.sle.next[0] == slist->slh_tail ? NULL : slist->slh_head->field.sle.next[0]; \
} \ } \
\ \
/* -- skip_tail_ */ \ /* -- skip_tail_ */ \
decl##_node_t *prefix##skip_tail_##decl(decl##_t *slist) \ decl##_node_t *prefix##skip_tail_##decl(decl##_t *slist) \
{ \ { \
return slist->slh_tail->field.sle.prev; \ return slist->slh_tail->field.sle.prev == slist->slh_head->field.sle.next[0] ? NULL : slist->slh_tail->field.sle.prev; \
} \ } \
\ \
/* -- __skip_locate_ \ /* -- __skip_locate_ \
@ -577,30 +581,39 @@
*/ \ */ \
decl##_snap_t *prefix##skip_snapshot_##decl(decl##_t *slist) \ decl##_snap_t *prefix##skip_snapshot_##decl(decl##_t *slist) \
{ \ { \
size_t i; \ size_t *sz, size, bytes, i; \
uintptr_t offset; \
decl##_snap_t *snap; \ decl##_snap_t *snap; \
decl##_node_t *node, *new; \ decl##_node_t *node, *new; \
\ \
if (slist == NULL) \ if (slist == NULL) \
return 0; \
if (prefix##skip_size_##decl(slist) == 0) \
return 0; \ return 0; \
\ \
snap = (decl##_snap_t *)calloc(1, sizeof(decl##_snap_t) + (slist->length * sizeof(decl##_node_t))); \ bytes = sizeof(decl##_snap_t) + (slist->length * sizeof(decl##_node_t)); \
node = prefix##skip_head_##decl(slist); \
while (node) { \
sizeof_entry_blk; \
bytes += sizeof(size_t); \
bytes += size; \
node = prefix##skip_next_node_##decl(slist, node); \
} \
snap = (decl##_snap_t *)calloc(1, bytes); \
if (snap == NULL) \ if (snap == NULL) \
return NULL; \ return NULL; \
\ \
snap->bytes = bytes; \
snap->list.length = slist->length; \ snap->list.length = slist->length; \
snap->list.max = slist->max; \ snap->list.max = slist->max; \
snap->nodes = (decl##_node_t **)(snap + sizeof(decl##_snap_t)); \ snap->nodes = (decl##_node_t *)(snap + sizeof(decl##_snap_t)); \
\ \
node = prefix##skip_head_##decl(slist); \
i = 0; \ i = 0; \
do { \ node = prefix##skip_head_##decl(slist); \
new = (decl##_node_t *)snap->nodes + (i * sizeof(decl##_node_t)); \ while (node) { \
decl##_node_t *n = (decl##_node_t *)snap->nodes + (i++ * sizeof(decl##_node_t)); \
new = (decl##_node_t *)&n; \
snap_node_blk; \ snap_node_blk; \
node = prefix##skip_next_node_##decl(slist, node); \ node = prefix##skip_next_node_##decl(slist, node); \
} while (++i < slist->length); \ } \
return snap; \ return snap; \
} \ } \
\ \
@ -618,6 +631,9 @@
if (slist == NULL) \ if (slist == NULL) \
return NULL; \ return NULL; \
\ \
slist->cmp = cmp; \
slist->max = snap->list.max; \
\
rc = prefix##skip_alloc_node_##decl(slist, &slist->slh_head); \ rc = prefix##skip_alloc_node_##decl(slist, &slist->slh_head); \
if (rc) \ if (rc) \
goto fail; \ goto fail; \
@ -625,10 +641,6 @@
if (rc) \ if (rc) \
goto fail; \ goto fail; \
\ \
slist->cmp = cmp; \
slist->length = snap->list.length; \
slist->max = snap->list.max; \
\
slist->slh_head->field.sle.len = 0; \ slist->slh_head->field.sle.len = 0; \
for (i = 0; i < slist->max; i++) \ for (i = 0; i < slist->max; i++) \
slist->slh_head->field.sle.next[i] = slist->slh_tail; \ slist->slh_head->field.sle.next[i] = slist->slh_tail; \
@ -640,13 +652,15 @@
slist->slh_tail->field.sle.prev = slist->slh_head; \ slist->slh_tail->field.sle.prev = slist->slh_head; \
\ \
i = 0; \ i = 0; \
do { \ while (snap->list.length > 0) { \
decl##_node_t *n = (decl##_node_t *)snap->nodes + (i++ * sizeof(decl##_node_t)); \
node = (decl##_node_t *)&n; \
rc = prefix##skip_alloc_node_##decl(slist, &new); \ rc = prefix##skip_alloc_node_##decl(slist, &new); \
node = (decl##_node_t *)snap->nodes + (i * sizeof(decl##_node_t)); \
snap_node_blk; \ snap_node_blk; \
__skip_insert_##decl(slist, new, 1); \ __skip_insert_##decl(slist, new, 1); \
} while (++i < slist->length); \ snap->list.length--; \
return 0; \ } \
return slist; \
fail:; \ fail:; \
if (slist->slh_head) \ if (slist->slh_head) \
free(slist->slh_head); \ free(slist->slh_head); \
@ -658,31 +672,17 @@
/* -- skip_dispose_snapshot_ */ \ /* -- skip_dispose_snapshot_ */ \
void prefix##skip_dispose_snapshot_##decl(decl##_snap_t *snap) \ void prefix##skip_dispose_snapshot_##decl(decl##_snap_t *snap) \
{ \ { \
size_t i; \ decl##_node_t *node; \
decl##_node_t *node = snap->nodes[0]; \
\ \
i = 0; \ node = (decl##_node_t *)snap->nodes; \
do { \ while (snap->list.length > 0) { \
node = (decl##_node_t *)snap->nodes[i]; \
free_node_blk; \ free_node_blk; \
} while (++i < snap->list.length); \ node += sizeof(decl##_node_t); \
snap->list.length--; \
} \
free(snap); \ free(snap); \
} \ } \
\ \
/* -- skip_to_array_ */ \
int prefix##skip_to_array_##decl(decl##_t *slist) \
{ \
((void)slist); /* TODO array_type into_array_blk */ \
return 0; \
} \
\
/* -- skip_from_array_ */ \
int prefix##skip_from_array_##decl(decl##_t *slist) \
{ \
((void)slist); /* TODO */ \
return 0; \
} \
\
/* -- __skip_integrity_check_ */ \ /* -- __skip_integrity_check_ */ \
static int __skip_integrity_check_##decl(decl##_t *slist) \ static int __skip_integrity_check_##decl(decl##_t *slist) \
{ \ { \