libcdsb/src/list/base.c

94 lines
1.9 KiB
C

/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "include.h"
/*#####################################################################################################################*/
hash_t list_hash(const list_t* s) {
hash_t hash = 0;
if (!is_null(s->first)) {
hash = vnode_hash(&s->first->node, s->first->type);
if (s->first != s->last) {
hash += vnode_hash(&s->first->node, s->first->type);
hash ^= list_size(s);
} else hash ^= 1;
}
return hash + VTYPE_LIST;
}
void list_init(list_t* x) {
memset(x, 0, sizeof(*x));
}
void list_free(list_t* x) {
lnode_t* c;
lnode_t* next;
c = x->first;
while (!is_null(c)) {
next = c->next;
vnode_free(&c->node, c->type);
free(c);
c = next;
}
memset(x, 0, sizeof(*x));
}
/*#####################################################################################################################*/
size_t list_size(const list_t* x) {
lnode_t* c;
size_t n;
c = x->first;
n = 0;
while (!is_null(c)) {
c = c->next;
++n;
}
return n;
}
/*#####################################################################################################################*/
int list_compare(const list_t* s0, const list_t* s1) {
lnode_t *c0, *c1;
int c;
if (s0 == s1) return 0;
c0 = s0->first;
c1 = s1->first;
for (;;) {
if (is_null(c0) || is_null(c1)) {
return (c0 == c1) ? 0 : (ssize_t)c0 - (ssize_t)c1;
}
c = lnode_compare(c0, c1);
if (c != 0) break;
c0 = c0->next;
c1 = c1->next;
}
for (;;) {
c0 = c0->next;
c1 = c1->next;
if (is_null(c0) || is_null(c1)) {
return (c0 == c1) ? 0 : (ssize_t)c0 - (ssize_t)c1;
}
}
}