From 744acee09af1307a7ceafe076d23d47f668f09e0 Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Thu, 21 Mar 2024 17:34:34 -0400 Subject: [PATCH] pos gt/lt working; added dup put kv api --- examples/slm.c | 18 ++++++++++++++++-- include/sl.h | 37 ++++++++++++++++++++++++------------- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/examples/slm.c b/examples/slm.c index 7396006..14f6299 100644 --- a/examples/slm.c +++ b/examples/slm.c @@ -203,6 +203,8 @@ main() char *v = api_skip_get_slex(list, array[i]); api_skip_set_slex(list, array[i], to_upper(v)); } + api_skip_dup_slex(list, -1, int_to_roman_numeral(-1)); + api_skip_dup_slex(list, 1, int_to_roman_numeral(1)); api_skip_del_slex(list, 0); @@ -213,13 +215,25 @@ main() api_skip_destroy_slex(restored); #endif - assert(strcmp(api_skip_pos_slex(list, SKIP_GTE, -(TEST_ARRAY_SIZE) -1)->value, int_to_roman_numeral(-(TEST_ARRAY_SIZE))) == 0); + assert(strcmp(api_skip_pos_slex(list, SKIP_GTE, -(TEST_ARRAY_SIZE)-1)->value, int_to_roman_numeral(-(TEST_ARRAY_SIZE))) == 0); assert(strcmp(api_skip_pos_slex(list, SKIP_GTE, -2)->value, int_to_roman_numeral(-2)) == 0); assert(strcmp(api_skip_pos_slex(list, SKIP_GTE, 0)->value, int_to_roman_numeral(1)) == 0); assert(strcmp(api_skip_pos_slex(list, SKIP_GTE, 2)->value, int_to_roman_numeral(2)) == 0); assert(api_skip_pos_slex(list, SKIP_GTE, (TEST_ARRAY_SIZE + 1)) == NULL); - assert(api_skip_pos_slex(list, SKIP_LTE, -(TEST_ARRAY_SIZE) - 1) == NULL); + assert(strcmp(api_skip_pos_slex(list, SKIP_GT, -(TEST_ARRAY_SIZE)-1)->value, int_to_roman_numeral(-(TEST_ARRAY_SIZE))) == 0); + assert(strcmp(api_skip_pos_slex(list, SKIP_GT, -2)->value, int_to_roman_numeral(-1)) == 0); + assert(strcmp(api_skip_pos_slex(list, SKIP_GT, 0)->value, int_to_roman_numeral(1)) == 0); + assert(strcmp(api_skip_pos_slex(list, SKIP_GT, 1)->value, int_to_roman_numeral(2)) == 0); + assert(api_skip_pos_slex(list, SKIP_GT, TEST_ARRAY_SIZE) == NULL); + + assert(api_skip_pos_slex(list, SKIP_LT, -(TEST_ARRAY_SIZE)) == NULL); + assert(strcmp(api_skip_pos_slex(list, SKIP_LT, -1)->value, int_to_roman_numeral(-2)) == 0); + assert(strcmp(api_skip_pos_slex(list, SKIP_LT, 0)->value, int_to_roman_numeral(-1)) == 0); + assert(strcmp(api_skip_pos_slex(list, SKIP_LT, 2)->value, int_to_roman_numeral(1)) == 0); + assert(strcmp(api_skip_pos_slex(list, SKIP_LT, (TEST_ARRAY_SIZE + 1))->value, int_to_roman_numeral(TEST_ARRAY_SIZE)) == 0); + + assert(api_skip_pos_slex(list, SKIP_LTE, -(TEST_ARRAY_SIZE)-1) == NULL); assert(strcmp(api_skip_pos_slex(list, SKIP_LTE, -2)->value, int_to_roman_numeral(-2)) == 0); assert(strcmp(api_skip_pos_slex(list, SKIP_LTE, 0)->value, int_to_roman_numeral(-1)) == 0); assert(strcmp(api_skip_pos_slex(list, SKIP_LTE, 2)->value, int_to_roman_numeral(2)) == 0); diff --git a/include/sl.h b/include/sl.h index 3e65856..2de123a 100644 --- a/include/sl.h +++ b/include/sl.h @@ -483,7 +483,7 @@ return elm; \ } \ \ - /* -- skip_position_gt TODO \ + /* -- 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. \ @@ -509,7 +509,7 @@ do { \ elm = elm->field.sle.next[0]; \ cmp = __skip_key_compare_##decl(slist, elm, n, slist->aux); \ - } while (cmp < 0); \ + } while (cmp <= 0); \ return elm; \ } \ \ @@ -543,12 +543,12 @@ do { \ elm = elm->field.sle.prev; \ cmp = __skip_key_compare_##decl(slist, elm, n, slist->aux); \ - } while (cmp > 0); \ + } while (cmp >= 0); \ } \ return elm; \ } \ \ - /* -- skip_position_lt TODO \ + /* -- 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. \ * \ @@ -571,14 +571,10 @@ elm = elm->field.sle.next[i]; \ } while (i--); \ elm = elm->field.sle.next[0]; \ - if (__skip_key_compare_##decl(slist, elm, n, slist->aux) == 0) { \ - return elm; \ - } else { \ - do { \ - elm = elm->field.sle.prev; \ - cmp = __skip_key_compare_##decl(slist, elm, n, slist->aux); \ - } while (cmp > 0); \ - } \ + do { \ + elm = elm->field.sle.prev; \ + cmp = __skip_key_compare_##decl(slist, elm, n, slist->aux); \ + } while (cmp >= 0); \ return elm; \ } \ \ @@ -634,7 +630,7 @@ return -1; \ } \ \ - /* -- skip_remove_ */ \ + /* -- skip_remove_ BUG: prev pointer wrong!*/ \ int prefix##skip_remove_##decl(decl##_t *slist, decl##_node_t *n) \ { \ static decl##_node_t apath[SKIPLIST_MAX_HEIGHT + 1]; \ @@ -967,6 +963,21 @@ return rc; \ } \ \ + int prefix##skip_dup_##decl(decl##_t *slist, ktype key, vtype value) \ + { \ + int rc; \ + decl##_node_t *node; \ + rc = prefix##skip_alloc_node_##decl(slist, &node); \ + if (rc) \ + return rc; \ + node->key = key; \ + node->value = value; \ + rc = prefix##skip_insert_dup_##decl(slist, node); \ + if (rc) \ + prefix##skip_free_node_##decl(node); \ + return rc; \ + } \ + \ int prefix##skip_set_##decl(decl##_t *slist, ktype key, vtype value) \ { \ decl##_node_t node; \