WIP snaps, pt6; serialization p2
This commit is contained in:
parent
da82f6bd62
commit
efd1933c62
|
@ -43,17 +43,27 @@ struct slex_node {
|
||||||
*/
|
*/
|
||||||
SKIPLIST_DECL(
|
SKIPLIST_DECL(
|
||||||
slex, api_, entries,
|
slex, api_, entries,
|
||||||
/* free node */ { (void)node; },
|
/* free node */ { free(node->value); },
|
||||||
/* update node */ { node->value = new->value; },
|
/* update node */ { node->value = new->value; },
|
||||||
/* snapshot node */
|
/* snapshot node */
|
||||||
{
|
{
|
||||||
new->key = node->key;
|
new->key = node->key;
|
||||||
new->value = strncpy(new->value, node->value, strlen(node->value));
|
char *nv = calloc(strlen(node->value) + 1, sizeof(char));
|
||||||
|
if (nv == NULL)
|
||||||
|
return NULL; /* leaks some memory... TODO */
|
||||||
|
new->value = strncpy(nv, node->value, strlen(node->value));
|
||||||
},
|
},
|
||||||
/* size in bytes of the content stored in an entry by you */
|
/* size in bytes of the content stored in an entry by you */
|
||||||
{
|
{ size = strlen(node->value) + 1; })
|
||||||
size = strlen(node->value) + 1;
|
|
||||||
})
|
/* Optional: Create the functions used to visualize a Skiplist (DOT/Graphviz) */
|
||||||
|
SKIPLIST_DECL_DOT(slex, api_, entries)
|
||||||
|
|
||||||
|
void
|
||||||
|
sprintf_slex_node(slex_node_t *node, char *buf)
|
||||||
|
{
|
||||||
|
sprintf(buf, "%d:%s", node->key, node->value);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Getter
|
* Getter
|
||||||
|
@ -97,23 +107,31 @@ __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) {
|
static char *
|
||||||
|
to_lower(char *str)
|
||||||
|
{
|
||||||
char *p = str;
|
char *p = str;
|
||||||
for ( ; *p; ++p) *p = *p >= 'A' && *p <= 'Z' ? *p|0x60 : *p;
|
for (; *p; ++p)
|
||||||
|
*p = *p >= 'A' && *p <= 'Z' ? *p | 0x60 : *p;
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char* to_upper(char* str) {
|
static char *
|
||||||
|
to_upper(char *str)
|
||||||
|
{
|
||||||
char *p = str;
|
char *p = str;
|
||||||
for ( ; *p; ++p) *p = *p >= 'a' && *p <= 'z' ? *p&~0x20 : *p;
|
for (; *p; ++p)
|
||||||
|
*p = *p >= 'a' && *p <= 'z' ? *p & ~0x20 : *p;
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *int_to_roman_numeral(int num){
|
static char *
|
||||||
int del[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1}; // Key value in Roman counting
|
int_to_roman_numeral(int num)
|
||||||
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
|
int del[] = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 }; // Key value in Roman counting
|
||||||
// represented by its Roman numeral equivalent up to 3 repetitions. Therefore, 64 should be more than enough.
|
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));
|
char *res = (char *)calloc(64, sizeof(char));
|
||||||
int i = 0;
|
int i = 0;
|
||||||
if (num < 0) {
|
if (num < 0) {
|
||||||
|
@ -125,21 +143,23 @@ static char *int_to_roman_numeral(int num){
|
||||||
res[0] = '0';
|
res[0] = '0';
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
while (num){ //while input number is not zero
|
while (num) { // while input number is not zero
|
||||||
while (num/del[i]){ //while a number contains the largest key value possible
|
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
|
strcat(res, sym[i]); // append the symbol for this key value to res string
|
||||||
num -= del[i]; //subtract the key value from number
|
num -= del[i]; // subtract the key value from number
|
||||||
}
|
}
|
||||||
i++; //proceed to the next key value
|
i++; // proceed to the next key value
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
void shuffle(int *array, size_t n) {
|
void
|
||||||
|
shuffle(int *array, size_t n)
|
||||||
|
{
|
||||||
if (n > 1) {
|
if (n > 1) {
|
||||||
size_t i;
|
size_t i;
|
||||||
for (i = n - 1; i > 0; i--) {
|
for (i = n - 1; i > 0; i--) {
|
||||||
size_t j = (unsigned int)(rand() % (i+1)); /* NOLINT(*-msc50-cpp) */
|
size_t j = (unsigned int)(rand() % (i + 1)); /* NOLINT(*-msc50-cpp) */
|
||||||
int t = array[j];
|
int t = array[j];
|
||||||
array[j] = array[i];
|
array[j] = array[i];
|
||||||
array[i] = t;
|
array[i] = t;
|
||||||
|
@ -147,18 +167,6 @@ void shuffle(int *array, size_t n) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#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:%s", node->key, node->value);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define TEST_ARRAY_SIZE 50
|
#define TEST_ARRAY_SIZE 50
|
||||||
|
|
||||||
int
|
int
|
||||||
|
|
62
include/sl.h
62
include/sl.h
|
@ -17,16 +17,18 @@
|
||||||
* I'd like to thank others for thoughtfully licensing their work, the
|
* I'd like to thank others for thoughtfully licensing their work, the
|
||||||
* community of software engineers succeeds when we work together.
|
* community of software engineers succeeds when we work together.
|
||||||
*
|
*
|
||||||
* Portions of this code are derived from copyrighted work:
|
* Portions of this code are derived from other copyrighted works:
|
||||||
*
|
*
|
||||||
* - MIT LICENSE
|
* - MIT License
|
||||||
* - https://github.com/greensky00/skiplist
|
* - https://github.com/greensky00/skiplist
|
||||||
* 2017-2024 Jung-Sang Ahn <jungsang.ahn@gmail.com>
|
* 2017-2024 Jung-Sang Ahn <jungsang.ahn@gmail.com>
|
||||||
* - https://github.com/paulross/skiplist
|
* - https://github.com/paulross/skiplist
|
||||||
* Copyright (c) 2017-2023 Paul Ross
|
* Copyright (c) 2017-2023 Paul Ross <paulross@uky.edu>
|
||||||
* - gist skiplist.c
|
* - https://github.com/JP-Ellis/rust-skiplist
|
||||||
* - khash.h
|
* Copyright (c) 2015 Joshua Ellis <github@jpellis.me>
|
||||||
* - async_nif.h
|
* - Public Domain
|
||||||
|
* - https://gist.github.com/zhpengg/2873424
|
||||||
|
* Zhipeng Li <zhpeng.is@gmail.com>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef _SKIPLIST_H_
|
#ifndef _SKIPLIST_H_
|
||||||
|
@ -82,9 +84,51 @@
|
||||||
*
|
*
|
||||||
* References for this implementation include, but are not limited to:
|
* References for this implementation include, but are not limited to:
|
||||||
*
|
*
|
||||||
* - Skiplists: a probabilistic alternative to balanced trees.
|
* - Skip lists: a probabilistic alternative to balanced trees
|
||||||
* https://www.cl.cam.ac.uk/teaching/2005/Algorithms/skiplists.pdf
|
* @article{10.1145/78973.78977,
|
||||||
* 1990 William Pugh published:
|
* author = {Pugh, William},
|
||||||
|
* title = {Skip lists: a probabilistic alternative to balanced trees},
|
||||||
|
* year = {1990}, issue_date = {June 1990},
|
||||||
|
* publisher = {Association for Computing Machinery},
|
||||||
|
* address = {New York, NY, USA},
|
||||||
|
* volume = {33}, number = {6}, issn = {0001-0782},
|
||||||
|
* url = {https://doi.org/10.1145/78973.78977},
|
||||||
|
* doi = {10.1145/78973.78977},
|
||||||
|
* journal = {Commun. ACM}, month = {jun}, pages = {668–676}, numpages = {9},
|
||||||
|
* keywords = {trees, searching, data structures},
|
||||||
|
* download = {https://www.cl.cam.ac.uk/teaching/2005/Algorithms/skiplists.pdf}
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* - Tutorial: The Ubiquitous Skiplist, its Variants, and Applications in Modern Big Data Systems
|
||||||
|
* @article{Vadrevu2023TutorialTU,
|
||||||
|
* title={Tutorial: The Ubiquitous Skiplist, its Variants, and Applications in Modern Big Data Systems},
|
||||||
|
* author={Venkata Sai Pavan Kumar Vadrevu and Lu Xing and Walid G. Aref},
|
||||||
|
* journal={ArXiv},
|
||||||
|
* year={2023},
|
||||||
|
* volume={abs/2304.09983},
|
||||||
|
* url={https://api.semanticscholar.org/CorpusID:258236678},
|
||||||
|
* download={https://arxiv.org/pdf/2304.09983.pdf}
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* - The Splay-List: A Distribution-Adaptive Concurrent Skip-List
|
||||||
|
* @misc{aksenov2020splaylist,
|
||||||
|
* title={The Splay-List: A Distribution-Adaptive Concurrent Skip-List},
|
||||||
|
* author={Vitaly Aksenov and Dan Alistarh and Alexandra Drozdova and Amirkeivan Mohtashami},
|
||||||
|
* year={2020},
|
||||||
|
* eprint={2008.01009},
|
||||||
|
* archivePrefix={arXiv},
|
||||||
|
* primaryClass={cs.DC},
|
||||||
|
* download={https://arxiv.org/pdf/2008.01009.pdf}
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* - JellyFish: A Fast Skip List with MVCC},
|
||||||
|
* @article{Yeon2020JellyFishAF,
|
||||||
|
* title={JellyFish: A Fast Skip List with MVCC},
|
||||||
|
* author={Jeseong Yeon and Leeju Kim and Youil Han and Hyeon Gyu Lee and Eunji Lee and Bryan Suk Joon Kim},
|
||||||
|
* journal={Proceedings of the 21st International Middleware Conference},
|
||||||
|
* year={2020},
|
||||||
|
* url={https://api.semanticscholar.org/CorpusID:228086012}
|
||||||
|
* }
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
Loading…
Reference in a new issue