This repository has been archived on 2022-01-16. You can view files and clone it, but cannot push or open issues or pull requests.
corelibs/src/llist/llist.c

228 lines
5.6 KiB
C

/*
* This file is part of corelibs. (https://git.redxen.eu/corelibs)
* Copyright (c) 2021 Alex-David Denes
*
* corelibs is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* corelibs is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with corelibs. If not, see <https://www.gnu.org/licenses/>.
*/
#include "llist/llist.h"
#include <stdarg.h> // va_args
#include <stdint.h> // uintmax_t
#include <stdlib.h> // malloc() free()
static cl_error_t
corelibs_llist_create (void *, void (*) (void *), cl_llist_t **),
corelibs_llist_link (uintmax_t, ...),
corelibs_llist_rep (cl_llist_t *, cl_llist_t *),
corelibs_llist_free_list (cl_llist_t *),
corelibs_llist_free_elem (cl_llist_t *),
corelibs_llist_free_cont (cl_llist_t *),
corelibs_llist_set_cont (cl_llist_t *, void *),
corelibs_llist_set_free (cl_llist_t *, void (*) (void *)),
corelibs_llist_get_next (const cl_llist_t *, cl_llist_t **),
corelibs_llist_get_prev (const cl_llist_t *, cl_llist_t **),
corelibs_llist_get_cont (const cl_llist_t *, void **);
static void corelibs_llist_remove (cl_llist_t *);
struct cl_llist_t {
struct cl_llist_t *next, *prev;
void * cont;
void (*free) (void *);
};
struct corelibs_llist_interface const cl_llist = {
.create = corelibs_llist_create,
.link = corelibs_llist_link,
.rep = corelibs_llist_rep,
.free = {
.list = corelibs_llist_free_list,
.elem = corelibs_llist_free_elem,
.cont = corelibs_llist_free_cont,
},
.set = {
.cont = corelibs_llist_set_cont,
.free = corelibs_llist_set_free,
},
.get = {
.prev = corelibs_llist_get_prev,
.cont = corelibs_llist_get_cont,
.next = corelibs_llist_get_next,
},
};
static cl_error_t
corelibs_llist_create (void *c, void (*const f) (void *), cl_llist_t **s) {
cl_error_t err = cl_error.err.ok;
if (s == NULL) err = cl_error.err.mem.null;
if (!err) {
cl_llist_t *cur = malloc (sizeof (*cur));
if (cur != NULL) {
cur->prev = NULL;
cur->next = NULL;
cur->cont = c;
cur->free = f;
*s = cur;
} else {
err = cl_error.err.mem.alloc;
}
}
return err;
}
static cl_error_t
corelibs_llist_free_list (cl_llist_t *l) {
cl_error_t err = cl_error.err.ok;
if (l == NULL) err = cl_error.err.mem.null;
if (!err) {
// Free previous elements if we have any
if (l->prev != NULL) {
for (cl_llist_t *p = l->prev; p != NULL;) {
cl_llist_t *c = p;
p = p->prev;
corelibs_llist_remove (c);
}
}
// Free current and next elements
for (cl_llist_t *p = l; p != NULL;) {
cl_llist_t *c = p;
p = p->next;
corelibs_llist_remove (c);
}
}
return err;
}
static cl_error_t
corelibs_llist_free_elem (cl_llist_t *l) {
cl_error_t err = cl_error.err.ok;
if (l == NULL) err = cl_error.err.mem.null;
if (!err) {
corelibs_llist_remove (l);
}
return err;
}
static cl_error_t
corelibs_llist_free_cont (cl_llist_t *l) {
cl_error_t err = cl_error.err.ok;
if (l == NULL) err = cl_error.err.mem.null;
if (!err) {
if (l->free != NULL) l->free (l->cont);
l->free = NULL;
l->cont = NULL;
}
return err;
}
static cl_error_t
corelibs_llist_link (uintmax_t cnt, ...) {
cl_error_t err = cl_error.err.ok;
if (cnt <= 1) err = cl_error.err.args.mis;
if (!err) {
va_list ap;
va_start (ap, cnt);
uintmax_t ccnt = 0;
cl_llist_t *a = NULL, *b = NULL;
while (ccnt < cnt) {
a = b;
b = va_arg (ap, cl_llist_t *);
if (ccnt < 1) continue; // Fill at least first 2 slots
if (a != NULL) a->next = b;
if (b != NULL) b->prev = a;
ccnt++;
}
va_end (ap);
}
return err;
}
static cl_error_t
corelibs_llist_rep (cl_llist_t *dest, cl_llist_t *src) {
cl_error_t err = cl_error.err.ok;
if (src == NULL || dest == NULL) err = cl_error.err.mem.null;
if (!err) {
if (dest->prev != NULL) dest->prev->next = src;
if (dest->next != NULL) dest->next->prev = src;
corelibs_llist_free_elem (dest);
}
return err;
}
static cl_error_t
corelibs_llist_get_next (const cl_llist_t *e, cl_llist_t **save) {
cl_error_t err = cl_error.err.ok;
if (e == NULL || save == NULL) err = cl_error.err.mem.null;
if (e->next == NULL) err = cl_error.err.undef;
if (!err) {
*save = e->next;
}
return err;
}
static cl_error_t
corelibs_llist_get_prev (const cl_llist_t *e, cl_llist_t **save) {
cl_error_t err = cl_error.err.ok;
if (e == NULL || save == NULL) err = cl_error.err.mem.null;
if (e->prev == NULL) err = cl_error.err.undef;
if (!err) {
*save = e->prev;
}
return err;
}
static cl_error_t
corelibs_llist_get_cont (const cl_llist_t *e, void **save) {
cl_error_t err = cl_error.err.ok;
if (e == NULL || save == NULL) err = cl_error.err.mem.null;
if (!err) {
*save = e->cont;
}
return err;
}
static cl_error_t
corelibs_llist_set_cont (cl_llist_t *e, void *i) {
cl_error_t err = cl_error.err.ok;
if (e == NULL) err = cl_error.err.mem.null;
if (!err) {
e->cont = i;
}
return err;
}
static cl_error_t
corelibs_llist_set_free (cl_llist_t *e, void (*const f) (void *)) {
cl_error_t err = cl_error.err.ok;
if (e == NULL) err = cl_error.err.mem.null;
if (!err) {
e->free = f;
}
return err;
}
// Private function
static void
corelibs_llist_remove (cl_llist_t *e) {
corelibs_llist_link (2, e->prev, e->next);
if (e->free != NULL) e->free (e->cont);
free (e);
}