/* This software is licensed by the MIT License, see LICENSE file */ /* Copyright © 2022 Gregory Lirent */ #include "include.h" /*#####################################################################################################################*/ static void lnode_cut(val_t* x, list_t* s, lnode_t* cur) { if (!is_null(x)) { value_set(x, cur->node, cur->type, VF_WRITEABLE|VF_REMOVABLE); } else vnode_free(&cur->node, cur->type); if (!is_null(cur->prev)) { cur->prev->next = cur->next; } else s->first = cur->next; if (!is_null(cur->next)) { cur->next->prev = cur->prev; } else s->last = cur->prev; free(cur); } /*#####################################################################################################################*/ ssize_t libcdsb_list_get(val_t* x, list_t* s, ssize_t i, _Bool cut) { ldir_t dir; lnode_t* c; size_t n; if (i < 0) { n = (i = ~i); dir = LD_PREV; } else { n = i; dir = LD_NEXT; } c = ldir_dir((lnode_t*)s, dir); if (!is_null(x)) memset(x, 0, sizeof(*x)); while (n && !is_null(c)) { c = ldir_dir(c, dir); --n; } if (n || is_null(c)) return -1; if (!cut && !is_null(x)) { value_set(x, &c->node, c->type, VF_WRITEABLE|VF_CHANGEABLE); } else if (cut) lnode_cut(x, s, c); return i; } /*#####################################################################################################################*/ ssize_t libcdsb_list_find(val_t* x, list_t* s, const void* v, vtype t, _Bool r, _Bool cut) { ldir_t dir = r ? LD_PREV : LD_NEXT; lnode_t* c; ssize_t i; int cmp; c = ldir_dir((lnode_t*)s, dir); i = 0; if (!is_null(x)) memset(x, 0, sizeof(*x)); while (!is_null(c)) { cmp = vtype_compare(vnode_peek(&c->node, c->type), c->type, v, t); if (cmp == 0) { if (!cut && !is_null(x)) { value_set(x, &c->node, c->type, VF_WRITEABLE|VF_CHANGEABLE); } else if (cut) lnode_cut(x, s, c); return i; } c = ldir_dir(c, dir); ++i; } return -1; } size_t libcdsb_list_count(const list_t* s, const void* v, vtype t) { lnode_t* c; size_t n; int cmp; c = s->first; n = 0; while (!is_null(c)) { cmp = vtype_compare(vnode_peek(&c->node, c->type), c->type, v, t); if (cmp == 0) ++n; c = c->next; } return n; } /*#####################################################################################################################*/ _Bool libcdsb_list_update(list_t* x, ssize_t i, const void* v, vtype t, int ins) { ldir_t dir; lnode_t* c; if (i < 0) { i = ~i; dir = LD_PREV; } else dir = LD_NEXT; c = ldir_dir((lnode_t*)x, dir); while (i && !is_null(c)) { c = ldir_dir(c, dir); --i; } if (i && dir == LD_PREV) { c = x->first; } else if (i) return false; if (is_null(c)) { x->first = x->last = c = calloc(sizeof(*c), 1); } else if (ins) { lnode_t *v = malloc(sizeof(*v)); dir = (ins < 0) ? LD_PREV : LD_NEXT; ldir_dir(v, dir) = ldir_dir(c, dir); ldir_inv(v, dir) = c; c = v; if (!is_null(ldir_dir(c, dir))) { ldir_inv(ldir_dir(c, dir), dir) = c; } else ldir_inv((lnode_t*)x, dir) = c; ldir_dir(ldir_inv(c, dir), dir) = c; } else vnode_free(&c->node, c->type); c->node = vnode_create(v, t); c->type = t; return true; } /*#####################################################################################################################*/ int libcdsb_list_foreach(const vtype_list* x, void* data, int (*callback)(void* value, ssize_t index, vtype type, void* data)) { lnode_t* c; size_t n; int r; c = x->first; n = 0; while (!is_null(c)) { if ((r = callback(vnode_peek(&c->node, c->type), n, c->type, data)) != 0) return r; c = c->next; ++n; } return 0; }