This commit is contained in:
Gregory Burd 2024-03-17 14:12:56 -04:00
parent 1245d72580
commit 8172a163cd
2 changed files with 19 additions and 13 deletions

View file

@ -48,7 +48,7 @@ int main() {
#define STATIC_INIT
#ifdef STATIC_INIT
skip_t _list = SKIP_HEAD_DEFAULT_INITIALIZER(__skip_cmp_entry);
_list.slh_tail = (struct entry *)_list.slh_head; // TODO...
_list.slh_tail = (struct entry *)&_list.slh_head; // TODO...
skip_t *list = &_list;
#else /* Dynamic allocation, init. */
skip_t *list = (skip_t *)malloc(sizeof(skip_t));
@ -56,13 +56,17 @@ int main() {
#endif
/* Insert 10 key/value pairs into the list. */
struct entry *n;
for (int i = 0; i < 10; i++) {
struct entry *n;
SKIP_ALLOC_NODE(list, n, entry, entries);
n->key = i;
n->value = i;
SKIP_INSERT(list, entry, n, entries);
SKIP_ALLOC_NODE(list, n, entry, entries);
n->key = i;
n->value = i;
SKIP_INSERT(list, entry, n, entries);
}
SKIP_ALLOC_NODE(list, n, entry, entries);
n->key = -1;
n->value = -1;
SKIP_INSERT(list, entry, n, entries);
#if 0
/* Delete a specific element in the list. */

View file

@ -217,7 +217,7 @@ struct sl_trace {
(var) = (struct type *)calloc(1, sizeof(struct type) + amt + 3); \
if ((var) != NULL) { \
(var)->field.sle_prev = (struct type *)(var) + sizeof(struct type); \
(var)->field.sle_next = (struct type **)(var) + sizeof(struct type) + (sizeof(struct type *) * 3); \
(var)->field.sle_next = (struct type **)((var)->field.sle_prev + (sizeof(void *) * 3)); \
ARRAY_SET_SIZE((var)->field.sle_next, (head)->max); \
ARRAY_SET_LENGTH((var)->field.sle_next, 0); \
} \
@ -235,13 +235,14 @@ struct sl_trace {
#define SKIP_INSERT(head, type, listelm, field) do { \
struct type *__elm = SKIP_FIRST(head); \
unsigned int __i = (head)->level; \
unsigned int __i; \
struct type **__path; \
if (__elm == NULL) { \
if ((listelm) == NULL) break; \
if (__elm == NULL) { \
/* Empty list, setup header and add first element. */ \
ARRAY_ALLOC((head)->slh_head, type, (head)->max); \
ARRAY_FORALL(__j, (head)->slh_head) { \
(head)->slh_head[__j] = (head)->slh_tail; \
ARRAY_ALLOC((head)->slh_head, type, (head)->max); \
ARRAY_FORALL(__i, (head)->slh_head) { \
(head)->slh_head[__i] = (head)->slh_tail; \
} \
(head)->slh_head[0] = (listelm); \
(head)->slh_tail = (listelm); \
@ -250,11 +251,12 @@ struct sl_trace {
(head)->length = 1; \
break; \
} \
__i = (head)->level; \
ARRAY_ALLOC(__path, type, (head)->max); \
if (__path == NULL) break; /* ENOMEM */ \
/* Find the position in the list where this element belongs. */ \
do { \
while(__elm && (head)->cmp((head), __elm->field.sle_next[__i], __elm, (head)->aux) < 0) \
while(__elm && (head)->cmp((head), __elm->field.sle_next[__i], (listelm), (head)->aux) < 0) \
__elm = __elm->field.sle_next[__i]; \
__path[__i] = __elm; \
} while(__i--); \