Refactor list, add attaching functional

This commit is contained in:
2022-08-19 16:46:07 +03:00
parent 57a8a08234
commit 925a8855ed
13 changed files with 261 additions and 336 deletions
-71
View File
@@ -18,79 +18,8 @@ static void lnode_cut(list_t* s, lnode_t* cur) {
free(cur);
}
/*#####################################################################################################################*/
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;
}
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;
}
int libcdsb_list_get(vtype_list* x, ssize_t i, void* _, list_access_callback callback, bool cut) {
ldir_t dir;
-93
View File
@@ -1,93 +0,0 @@
/* 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;
}
}
}
+36
View File
@@ -0,0 +1,36 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "include.h"
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;
}
}
}
+57
View File
@@ -0,0 +1,57 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "include.h"
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;
}
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;
}
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;
}
+3 -4
View File
@@ -3,8 +3,6 @@
#include "include.h"
/*#####################################################################################################################*/
static void init_first(list_t* x, vnode_t v, vtype t) {
lnode_t* node = malloc(sizeof(*node));
@@ -17,6 +15,7 @@ static void init_first(list_t* x, vnode_t v, vtype t) {
x->last = node;
}
static void push_next(list_t* x, vnode_t v, vtype t) {
lnode_t* node = malloc(sizeof(*node));
@@ -50,6 +49,7 @@ list_t list_copy(const list_t* s) {
return x;
}
list_t* list_duplicate(const list_t* s) {
list_t* x;
lnode_t* c;
@@ -69,6 +69,7 @@ list_t* list_duplicate(const list_t* s) {
return x;
}
void list_copy_init(list_t* x, const list_t* s) {
lnode_t* c;
@@ -85,7 +86,6 @@ void list_copy_init(list_t* x, const list_t* s) {
}
}
/*#####################################################################################################################*/
void list_extend(list_t* x, const list_t* s) {
lnode_t* c;
@@ -109,7 +109,6 @@ void list_extend(list_t* x, const list_t* s) {
}
size_t list_slice(list_t* x, list_t* s, ssize_t i, size_t n, _Bool cut) {
lnode_t* c;
-67
View File
@@ -1,67 +0,0 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "include.h"
int libcdsb_list_find_pointer(list_t* x, const void* v, void* _, list_access_callback cb, bool r, bool cut) { return libcdsb_list_find(x, &v, vtypeof(&v), _, cb, r, cut); }
int libcdsb_list_find_cstring(list_t* x, const char* v, void* _, list_access_callback cb, bool r, bool cut) { return libcdsb_list_find(x, &v, vtypeof(&v), _, cb, r, cut); }
int libcdsb_list_find_string (list_t* x, const str_t* v, void* _, list_access_callback cb, bool r, bool cut) { return libcdsb_list_find(x, v, vtypeof( v), _, cb, r, cut); }
int libcdsb_list_find_array (list_t* x, const arr_t* v, void* _, list_access_callback cb, bool r, bool cut) { return libcdsb_list_find(x, v, vtypeof( v), _, cb, r, cut); }
int libcdsb_list_find_list (list_t* x, const list_t* v, void* _, list_access_callback cb, bool r, bool cut) { return libcdsb_list_find(x, v, vtypeof( v), _, cb, r, cut); }
int libcdsb_list_find_map (list_t* x, const map_t* v, void* _, list_access_callback cb, bool r, bool cut) { return libcdsb_list_find(x, v, vtypeof( v), _, cb, r, cut); }
int libcdsb_list_find_vset (list_t* x, const set_t* v, void* _, list_access_callback cb, bool r, bool cut) { return libcdsb_list_find(x, v, vtypeof( v), _, cb, r, cut); }
int libcdsb_list_find_dict (list_t* x, const dict_t* v, void* _, list_access_callback cb, bool r, bool cut) { return libcdsb_list_find(x, v, vtypeof( v), _, cb, r, cut); }
int libcdsb_list_find_boolean(list_t* x, bool v, void* _, list_access_callback cb, bool r, bool cut) { return libcdsb_list_find(x, &v, vtypeof(&v), _, cb, r, cut); }
int libcdsb_list_find_int8 (list_t* x, s8_t v, void* _, list_access_callback cb, bool r, bool cut) { return libcdsb_list_find(x, &v, vtypeof(&v), _, cb, r, cut); }
int libcdsb_list_find_int16 (list_t* x, s16_t v, void* _, list_access_callback cb, bool r, bool cut) { return libcdsb_list_find(x, &v, vtypeof(&v), _, cb, r, cut); }
int libcdsb_list_find_int32 (list_t* x, s32_t v, void* _, list_access_callback cb, bool r, bool cut) { return libcdsb_list_find(x, &v, vtypeof(&v), _, cb, r, cut); }
int libcdsb_list_find_int64 (list_t* x, s64_t v, void* _, list_access_callback cb, bool r, bool cut) { return libcdsb_list_find(x, &v, vtypeof(&v), _, cb, r, cut); }
int libcdsb_list_find_uint8 (list_t* x, u8_t v, void* _, list_access_callback cb, bool r, bool cut) { return libcdsb_list_find(x, &v, vtypeof(&v), _, cb, r, cut); }
int libcdsb_list_find_uint16 (list_t* x, u16_t v, void* _, list_access_callback cb, bool r, bool cut) { return libcdsb_list_find(x, &v, vtypeof(&v), _, cb, r, cut); }
int libcdsb_list_find_uint32 (list_t* x, u32_t v, void* _, list_access_callback cb, bool r, bool cut) { return libcdsb_list_find(x, &v, vtypeof(&v), _, cb, r, cut); }
int libcdsb_list_find_uint64 (list_t* x, u64_t v, void* _, list_access_callback cb, bool r, bool cut) { return libcdsb_list_find(x, &v, vtypeof(&v), _, cb, r, cut); }
int libcdsb_list_find_float (list_t* x, fl_t v, void* _, list_access_callback cb, bool r, bool cut) { return libcdsb_list_find(x, &v, vtypeof(&v), _, cb, r, cut); }
int libcdsb_list_find_double (list_t* x, dbl_t v, void* _, list_access_callback cb, bool r, bool cut) { return libcdsb_list_find(x, &v, vtypeof(&v), _, cb, r, cut); }
int libcdsb_list_find_ldouble(list_t* x, ldbl_t v, void* _, list_access_callback cb, bool r, bool cut) { return libcdsb_list_find(x, &v, vtypeof(&v), _, cb, r, cut); }
size_t libcdsb_list_count_pointer(const list_t* s, const void* v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); }
size_t libcdsb_list_count_cstring(const list_t* s, const char* v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); }
size_t libcdsb_list_count_string (const list_t* s, const str_t* v) { return libcdsb_list_count((void*)s, v, vtypeof( v)); }
size_t libcdsb_list_count_array (const list_t* s, const arr_t* v) { return libcdsb_list_count((void*)s, v, vtypeof( v)); }
size_t libcdsb_list_count_list (const list_t* s, const list_t* v) { return libcdsb_list_count((void*)s, v, vtypeof( v)); }
size_t libcdsb_list_count_map (const list_t* s, const map_t* v) { return libcdsb_list_count((void*)s, v, vtypeof( v)); }
size_t libcdsb_list_count_vset (const list_t* s, const set_t* v) { return libcdsb_list_count((void*)s, v, vtypeof( v)); }
size_t libcdsb_list_count_dict (const list_t* s, const dict_t* v) { return libcdsb_list_count((void*)s, v, vtypeof( v)); }
size_t libcdsb_list_count_boolean(const list_t* s, bool v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); }
size_t libcdsb_list_count_int8 (const list_t* s, s8_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); }
size_t libcdsb_list_count_int16 (const list_t* s, s16_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); }
size_t libcdsb_list_count_int32 (const list_t* s, s32_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); }
size_t libcdsb_list_count_int64 (const list_t* s, s64_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); }
size_t libcdsb_list_count_uint8 (const list_t* s, u8_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); }
size_t libcdsb_list_count_uint16 (const list_t* s, u16_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); }
size_t libcdsb_list_count_uint32 (const list_t* s, u32_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); }
size_t libcdsb_list_count_uint64 (const list_t* s, u64_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); }
size_t libcdsb_list_count_float (const list_t* s, fl_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); }
size_t libcdsb_list_count_double (const list_t* s, dbl_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); }
size_t libcdsb_list_count_ldouble(const list_t* s, ldbl_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); }
bool libcdsb_list_update_pointer(list_t* x, ssize_t i, const void* v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins); }
bool libcdsb_list_update_cstring(list_t* x, ssize_t i, const char* v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins); }
bool libcdsb_list_update_string (list_t* x, ssize_t i, const str_t* v, int ins) { return libcdsb_list_update(x, i, v, vtypeof( v), ins); }
bool libcdsb_list_update_array (list_t* x, ssize_t i, const arr_t* v, int ins) { return libcdsb_list_update(x, i, v, vtypeof( v), ins); }
bool libcdsb_list_update_list (list_t* x, ssize_t i, const list_t* v, int ins) { return libcdsb_list_update(x, i, v, vtypeof( v), ins); }
bool libcdsb_list_update_map (list_t* x, ssize_t i, const map_t* v, int ins) { return libcdsb_list_update(x, i, v, vtypeof( v), ins); }
bool libcdsb_list_update_vset (list_t* x, ssize_t i, const set_t* v, int ins) { return libcdsb_list_update(x, i, v, vtypeof( v), ins); }
bool libcdsb_list_update_dict (list_t* x, ssize_t i, const dict_t* v, int ins) { return libcdsb_list_update(x, i, v, vtypeof( v), ins); }
bool libcdsb_list_update_boolean(list_t* x, ssize_t i, bool v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins); }
bool libcdsb_list_update_int8 (list_t* x, ssize_t i, s8_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins); }
bool libcdsb_list_update_int16 (list_t* x, ssize_t i, s16_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins); }
bool libcdsb_list_update_int32 (list_t* x, ssize_t i, s32_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins); }
bool libcdsb_list_update_int64 (list_t* x, ssize_t i, s64_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins); }
bool libcdsb_list_update_uint8 (list_t* x, ssize_t i, u8_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins); }
bool libcdsb_list_update_uint16 (list_t* x, ssize_t i, u16_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins); }
bool libcdsb_list_update_uint32 (list_t* x, ssize_t i, u32_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins); }
bool libcdsb_list_update_uint64 (list_t* x, ssize_t i, u64_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins); }
bool libcdsb_list_update_float (list_t* x, ssize_t i, fl_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins); }
bool libcdsb_list_update_double (list_t* x, ssize_t i, dbl_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins); }
bool libcdsb_list_update_ldouble(list_t* x, ssize_t i, ldbl_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins); }
+1 -1
View File
@@ -1,7 +1,7 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "../../include/extra/list.h"
#include "../../include/list.h"
#include "../__internal/vnode.h"
#ifndef LIBCDSB_SRC_LIST_INCLUDE_H
+25
View File
@@ -0,0 +1,25 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "include.h"
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));
}
+107
View File
@@ -0,0 +1,107 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "include.h"
bool libcdsb_list_insert(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;
}
bool libcdsb_list_attach(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);
if (t < VTYPE_STRING) {
c->node = vnode_create(v, t);
} else if (sizeof(str_t) == sizeof(void*) && t == VTYPE_STRING) {
c->node = *(char**)v;
} else {
c->node = malloc(vtype_size(t));
memcpy(c->node, v, vtype_size(t));
}
c->type = t;
return true;
}