Add list base
This commit is contained in:
+109
@@ -0,0 +1,109 @@
|
||||
/* This software is licensed by the MIT License, see LICENSE file */
|
||||
/* Copyright © 2022 Gregory Lirent */
|
||||
|
||||
#include "include.h"
|
||||
|
||||
/*#####################################################################################################################*/
|
||||
|
||||
static void init_first(list_t* x, vnode_t v, vtype t) {
|
||||
lnode_t* node = malloc(sizeof(*node));
|
||||
|
||||
node->next = nullptr;
|
||||
node->prev = nullptr;
|
||||
node->node = v;
|
||||
node->type = t;
|
||||
|
||||
x->first = node;
|
||||
x->last = node;
|
||||
}
|
||||
|
||||
static void push_next(list_t* x, vnode_t v, vtype t) {
|
||||
lnode_t* node = malloc(sizeof(*node));
|
||||
|
||||
node->next = nullptr;
|
||||
node->prev = x->last;
|
||||
node->node = v;
|
||||
node->type = t;
|
||||
|
||||
x->last = node;
|
||||
x->last->next = node;
|
||||
}
|
||||
|
||||
/*#####################################################################################################################*/
|
||||
|
||||
list_t list_copy(const list_t* s) {
|
||||
list_t x;
|
||||
lnode_t* c;
|
||||
|
||||
c = s->first;
|
||||
memset(&x, 0, sizeof(x));
|
||||
|
||||
if (is_null(c))
|
||||
return x;
|
||||
|
||||
init_first(&x, vnode_duplicate(&c->node, c->type), c->type);
|
||||
|
||||
while (!is_null(c = c->next)) {
|
||||
push_next(&x, vnode_duplicate(&c->node, c->type), c->type);
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
list_t* list_duplicate(const list_t* s) {
|
||||
list_t* x;
|
||||
lnode_t* c;
|
||||
|
||||
c = s->first;
|
||||
x = calloc(sizeof(*x), 1);
|
||||
|
||||
if (is_null(c))
|
||||
return x;
|
||||
|
||||
init_first(x, vnode_duplicate(&c->node, c->type), c->type);
|
||||
|
||||
while (!is_null(c = c->next)) {
|
||||
push_next(x, vnode_duplicate(&c->node, c->type), c->type);
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
void list_copy_init(list_t* x, const list_t* s) {
|
||||
lnode_t* c;
|
||||
|
||||
c = s->first;
|
||||
memset(x, 0, sizeof(*x));
|
||||
|
||||
if (is_null(c))
|
||||
return;
|
||||
|
||||
init_first(x, vnode_duplicate(&c->node, c->type), c->type);
|
||||
|
||||
while (!is_null(c = c->next)) {
|
||||
push_next(x, vnode_duplicate(&c->node, c->type), c->type);
|
||||
}
|
||||
}
|
||||
|
||||
/*#####################################################################################################################*/
|
||||
|
||||
void list_extend(list_t* x, const list_t* s) {
|
||||
lnode_t* c;
|
||||
|
||||
c = s->first;
|
||||
|
||||
if (is_null(c))
|
||||
return;
|
||||
|
||||
if (is_null(x->first)) {
|
||||
init_first(x, vnode_duplicate(&c->node, c->type), c->type);
|
||||
c = c->next;
|
||||
|
||||
if (is_null(c))
|
||||
return;
|
||||
}
|
||||
|
||||
do {
|
||||
push_next(x, vnode_duplicate(&c->node, c->type), c->type);
|
||||
} while (!is_null(c = c->next));
|
||||
}
|
||||
Reference in New Issue
Block a user