Refactor list, add attaching functional
This commit is contained in:
parent
57a8a08234
commit
925a8855ed
@ -3,7 +3,7 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../include/extra/list.h"
|
||||
#include "../include/list.h"
|
||||
|
||||
typedef vtype_list list_t;
|
||||
|
||||
@ -33,11 +33,11 @@ int main(int argc, char** argv) {
|
||||
|
||||
list_init(&list);
|
||||
|
||||
for (size_t i = 0; i < 28; ++i) {
|
||||
for (vtype_int32 i = 0; i < 28; ++i) {
|
||||
if (i%2) {
|
||||
list_push_back(&list, (vtype_int32)i);
|
||||
list_push_back(&list, i);
|
||||
} else {
|
||||
list_push_back(&list, (vtype_float)fl);
|
||||
list_push_back(&list, fl);
|
||||
fl += 0.05;
|
||||
}
|
||||
}
|
||||
|
@ -1,22 +0,0 @@
|
||||
/* This software is licensed by the MIT License, see LICENSE file */
|
||||
/* Copyright © 2022 Gregory Lirent */
|
||||
|
||||
#include "../list.h"
|
||||
|
||||
#ifndef LIBCDSB_EXTRA_LIST_H
|
||||
#define LIBCDSB_EXTRA_LIST_H
|
||||
|
||||
#define list_get_by_index(x, index, data, callback) libcdsb_list_get(x, index, data, callback, 0)
|
||||
#define list_pop_by_index(x, index, data, callback) libcdsb_list_get(x, index, data, callback, 1)
|
||||
#define list_remove_by_index(x, index) libcdsb_list_get(x, index, 0, 0, 1)
|
||||
|
||||
#define list_foreach(x, data, callback) libcdsb_list_foreach(x, data, callback, 0)
|
||||
|
||||
extern bool libcdsb_list_update(vtype_list* x, ssize_t index, const void* value, vtype type, int ins_direction) Nonnull__(1);
|
||||
extern size_t libcdsb_list_count(const vtype_list* s, const void* value, vtype type) Pure__ Warn_unused_result__ Nonnull__(1);
|
||||
|
||||
extern int libcdsb_list_find (vtype_list* x, const void* value, vtype type, void* data, list_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_list_get (vtype_list* x, ssize_t index, void* data, list_access_callback, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_list_foreach(vtype_list* x, void* data, list_access_callback, bool flush) Nonnull__(1,3);
|
||||
|
||||
#endif /* LIBCDSB_EXTRA_LIST_H */
|
100
include/list.h
100
include/list.h
@ -7,91 +7,45 @@
|
||||
#ifndef LIBCDSB_LIST_H
|
||||
#define LIBCDSB_LIST_H
|
||||
|
||||
/*#####################################################################################################################*/
|
||||
|
||||
typedef int (*list_access_callback)(void* value, ssize_t index, vtype type, void* data);
|
||||
|
||||
/*#####################################################################################################################*/
|
||||
|
||||
extern void list_init (vtype_list* x) Nonnull__(1);
|
||||
extern void list_extend (vtype_list* x, const vtype_list* s) Nonnull__(1,2);
|
||||
extern size_t list_slice (vtype_list* x, vtype_list* src, ssize_t index, size_t count, bool cut) Nonnull__(1,2);
|
||||
extern void list_sort (vtype_list* x) Nonnull__(1);
|
||||
extern void list_reverse(vtype_list* x) Nonnull__(1);
|
||||
|
||||
#define list_pop(x, value, data, callback) _LIBCDSB_Generic(libcdsb_list, find, value)(x, value, data, callback, 0, 1)
|
||||
#define list_find(x, value, data, callback) _LIBCDSB_Generic(libcdsb_list, find, value)(x, value, data, callback, 0, 0)
|
||||
#define list_rfind(x, value, data, callback) _LIBCDSB_Generic(libcdsb_list, find, value)(x, value, data, callback, 1, 0)
|
||||
#define list_countof(x, value) _LIBCDSB_Generic(libcdsb_list, count, value)(x, value)
|
||||
#define list_remove(x, value) list_pop(x, value, 0, 0)
|
||||
#define in_list(x, value) (list_find(x, value, 0, 0) == 0)
|
||||
/*#####################################################################################################################*/
|
||||
|
||||
#define list_insert(x, index, value) _LIBCDSB_Generic(libcdsb_list, update, value)(x, index, value, -1)
|
||||
#define list_replace(x, index, value) _LIBCDSB_Generic(libcdsb_list, update, value)(x, index, value, 0)
|
||||
#define list_push_back(x, value) _LIBCDSB_Generic(libcdsb_list, update, value)(x, -1, value, 1)
|
||||
#define list_push_front(x, value) _LIBCDSB_Generic(libcdsb_list, update, value)(x, 0, value, -1)
|
||||
#define list_pop(x, value, data, callback) libcdsb_list_find (x, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value), data, callback, 0, 1)
|
||||
#define list_get list_find
|
||||
#define list_pop_by_index(x, index, data, callback) libcdsb_list_get (x, index, data, callback, 1)
|
||||
#define list_get_by_index(x, index, data, callback) libcdsb_list_get (x, index, data, callback, 0)
|
||||
#define list_find(x, value, data, callback) libcdsb_list_find (x, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value), data, callback, 0, 0)
|
||||
#define list_rfind(x, value, data, callback) libcdsb_list_find (x, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value), data, callback, 1, 0)
|
||||
#define list_countof(x, value) libcdsb_list_count (x, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value))
|
||||
#define list_insert(x, index, value) libcdsb_list_insert (x, index, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value), -1)
|
||||
#define list_replace(x, index, value) libcdsb_list_insert (x, index, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value), 0)
|
||||
#define list_push_back(x, value) libcdsb_list_insert (x, -1, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value), 1)
|
||||
#define list_push_front(x, value) libcdsb_list_insert (x, 0, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value), -1)
|
||||
#define list_attach_back(x, value) libcdsb_list_attach (x, -1, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value), 1)
|
||||
#define list_attach_front(x, value) libcdsb_list_attach (x, 0, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value), -1)
|
||||
#define list_foreach(x, data, callback) libcdsb_list_foreach(x, data, callback, 0)
|
||||
#define list_remove(x, value) list_pop (x, value, 0, 0)
|
||||
#define list_remove_by_index(x, index) list_pop_by_index (x, index, 0, 0)
|
||||
|
||||
#define in_list(x, value) (list_get(x, value, 0, 0) == 0)
|
||||
|
||||
/*#####################################################################################################################*/
|
||||
|
||||
extern int libcdsb_list_find_pointer(vtype_list* x, const void* value, void* data, list_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_list_find_cstring(vtype_list* x, const char* value, void* data, list_access_callback, bool reverse, bool cut) Nonnull__(1,2);
|
||||
extern int libcdsb_list_find_string (vtype_list* x, const vtype_string* value, void* data, list_access_callback, bool reverse, bool cut) Nonnull__(1,2);
|
||||
extern int libcdsb_list_find_array (vtype_list* x, const vtype_array* value, void* data, list_access_callback, bool reverse, bool cut) Nonnull__(1,2);
|
||||
extern int libcdsb_list_find_list (vtype_list* x, const vtype_list* value, void* data, list_access_callback, bool reverse, bool cut) Nonnull__(1,2);
|
||||
extern int libcdsb_list_find_map (vtype_list* x, const vtype_map* value, void* data, list_access_callback, bool reverse, bool cut) Nonnull__(1,2);
|
||||
extern int libcdsb_list_find_vset (vtype_list* x, const vtype_set* value, void* data, list_access_callback, bool reverse, bool cut) Nonnull__(1,2);
|
||||
extern int libcdsb_list_find_dict (vtype_list* x, const vtype_dict* value, void* data, list_access_callback, bool reverse, bool cut) Nonnull__(1,2);
|
||||
extern int libcdsb_list_find_boolean(vtype_list* x, vtype_bool value, void* data, list_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_list_find_int8 (vtype_list* x, vtype_int8 value, void* data, list_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_list_find_int16 (vtype_list* x, vtype_int16 value, void* data, list_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_list_find_int32 (vtype_list* x, vtype_int32 value, void* data, list_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_list_find_int64 (vtype_list* x, vtype_int64 value, void* data, list_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_list_find_uint8 (vtype_list* x, vtype_uint8 value, void* data, list_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_list_find_uint16 (vtype_list* x, vtype_uint16 value, void* data, list_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_list_find_uint32 (vtype_list* x, vtype_uint32 value, void* data, list_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_list_find_uint64 (vtype_list* x, vtype_uint64 value, void* data, list_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_list_find_float (vtype_list* x, vtype_float value, void* data, list_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_list_find_double (vtype_list* x, vtype_double value, void* data, list_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_list_find_ldouble(vtype_list* x, vtype_ldouble value, void* data, list_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
extern bool libcdsb_list_insert (vtype_list* x, ssize_t index, const void* value, vtype type, int ins_direction) Nonnull__(1);
|
||||
extern bool libcdsb_list_attach (vtype_list* x, ssize_t index, const void* value, vtype type, int ins_direction) Nonnull__(1);
|
||||
extern int libcdsb_list_find (vtype_list* x, const void* value, vtype type, void* data, list_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_list_get (vtype_list* x, ssize_t index, void* data, list_access_callback, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_list_foreach (vtype_list* x, void* data, list_access_callback, bool flush) Nonnull__(1,3);
|
||||
|
||||
extern size_t libcdsb_list_count_pointer(const vtype_list* s, const void* value) Nonnull__(1);
|
||||
extern size_t libcdsb_list_count_cstring(const vtype_list* s, const char* value) Nonnull__(1,2);
|
||||
extern size_t libcdsb_list_count_string (const vtype_list* s, const vtype_string* value) Nonnull__(1,2);
|
||||
extern size_t libcdsb_list_count_array (const vtype_list* s, const vtype_array* value) Nonnull__(1,2);
|
||||
extern size_t libcdsb_list_count_list (const vtype_list* s, const vtype_list* value) Nonnull__(1,2);
|
||||
extern size_t libcdsb_list_count_map (const vtype_list* s, const vtype_map* value) Nonnull__(1,2);
|
||||
extern size_t libcdsb_list_count_vset (const vtype_list* s, const vtype_set* value) Nonnull__(1,2);
|
||||
extern size_t libcdsb_list_count_dict (const vtype_list* s, const vtype_dict* value) Nonnull__(1,2);
|
||||
extern size_t libcdsb_list_count_boolean(const vtype_list* s, vtype_bool value) Nonnull__(1);
|
||||
extern size_t libcdsb_list_count_int8 (const vtype_list* s, vtype_int8 value) Nonnull__(1);
|
||||
extern size_t libcdsb_list_count_int16 (const vtype_list* s, vtype_int16 value) Nonnull__(1);
|
||||
extern size_t libcdsb_list_count_int32 (const vtype_list* s, vtype_int32 value) Nonnull__(1);
|
||||
extern size_t libcdsb_list_count_int64 (const vtype_list* s, vtype_int64 value) Nonnull__(1);
|
||||
extern size_t libcdsb_list_count_uint8 (const vtype_list* s, vtype_uint8 value) Nonnull__(1);
|
||||
extern size_t libcdsb_list_count_uint16 (const vtype_list* s, vtype_uint16 value) Nonnull__(1);
|
||||
extern size_t libcdsb_list_count_uint32 (const vtype_list* s, vtype_uint32 value) Nonnull__(1);
|
||||
extern size_t libcdsb_list_count_uint64 (const vtype_list* s, vtype_uint64 value) Nonnull__(1);
|
||||
extern size_t libcdsb_list_count_float (const vtype_list* s, vtype_float value) Nonnull__(1);
|
||||
extern size_t libcdsb_list_count_double (const vtype_list* s, vtype_double value) Nonnull__(1);
|
||||
extern size_t libcdsb_list_count_ldouble(const vtype_list* s, vtype_ldouble value) Nonnull__(1);
|
||||
|
||||
extern bool libcdsb_list_update_pointer(vtype_list* x, ssize_t index, const void* value, int ins_direction) Nonnull__(1);
|
||||
extern bool libcdsb_list_update_cstring(vtype_list* x, ssize_t index, const char* value, int ins_direction) Nonnull__(1,3);
|
||||
extern bool libcdsb_list_update_string (vtype_list* x, ssize_t index, const vtype_string* value, int ins_direction) Nonnull__(1,3);
|
||||
extern bool libcdsb_list_update_array (vtype_list* x, ssize_t index, const vtype_array* value, int ins_direction) Nonnull__(1,3);
|
||||
extern bool libcdsb_list_update_list (vtype_list* x, ssize_t index, const vtype_list* value, int ins_direction) Nonnull__(1,3);
|
||||
extern bool libcdsb_list_update_map (vtype_list* x, ssize_t index, const vtype_map* value, int ins_direction) Nonnull__(1,3);
|
||||
extern bool libcdsb_list_update_vset (vtype_list* x, ssize_t index, const vtype_set* value, int ins_direction) Nonnull__(1,3);
|
||||
extern bool libcdsb_list_update_dict (vtype_list* x, ssize_t index, const vtype_dict* value, int ins_direction) Nonnull__(1,3);
|
||||
extern bool libcdsb_list_update_boolean(vtype_list* x, ssize_t index, vtype_bool value, int ins_direction) Nonnull__(1);
|
||||
extern bool libcdsb_list_update_int8 (vtype_list* x, ssize_t index, vtype_int8 value, int ins_direction) Nonnull__(1);
|
||||
extern bool libcdsb_list_update_int16 (vtype_list* x, ssize_t index, vtype_int16 value, int ins_direction) Nonnull__(1);
|
||||
extern bool libcdsb_list_update_int32 (vtype_list* x, ssize_t index, vtype_int32 value, int ins_direction) Nonnull__(1);
|
||||
extern bool libcdsb_list_update_int64 (vtype_list* x, ssize_t index, vtype_int64 value, int ins_direction) Nonnull__(1);
|
||||
extern bool libcdsb_list_update_uint8 (vtype_list* x, ssize_t index, vtype_uint8 value, int ins_direction) Nonnull__(1);
|
||||
extern bool libcdsb_list_update_uint16 (vtype_list* x, ssize_t index, vtype_uint16 value, int ins_direction) Nonnull__(1);
|
||||
extern bool libcdsb_list_update_uint32 (vtype_list* x, ssize_t index, vtype_uint32 value, int ins_direction) Nonnull__(1);
|
||||
extern bool libcdsb_list_update_uint64 (vtype_list* x, ssize_t index, vtype_uint64 value, int ins_direction) Nonnull__(1);
|
||||
extern bool libcdsb_list_update_float (vtype_list* x, ssize_t index, vtype_float value, int ins_direction) Nonnull__(1);
|
||||
extern bool libcdsb_list_update_double (vtype_list* x, ssize_t index, vtype_double value, int ins_direction) Nonnull__(1);
|
||||
extern bool libcdsb_list_update_ldouble(vtype_list* x, ssize_t index, vtype_ldouble value, int ins_direction) Nonnull__(1);
|
||||
extern size_t libcdsb_list_count(const vtype_list* s, const void* value, vtype type) Pure__ Warn_unused_result__ Nonnull__(1);
|
||||
|
||||
#endif /* LIBCDSB_LIST_H */
|
||||
|
@ -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;
|
@ -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
src/list/comparison.c
Normal file
36
src/list/comparison.c
Normal 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
src/list/compute.c
Normal file
57
src/list/compute.c
Normal 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,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;
|
||||
|
@ -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,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
src/list/memory.c
Normal file
25
src/list/memory.c
Normal 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
src/list/modify.c
Normal file
107
src/list/modify.c
Normal 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;
|
||||
}
|
@ -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 "../../include/random.h"
|
||||
#include "../../include/test.h"
|
||||
|
Loading…
Reference in New Issue
Block a user