Fix dict keys copy implementation

This commit is contained in:
Gregory Lirent 2022-08-24 12:31:33 +03:00
parent 1d8e8efe90
commit 804769d85c
4 changed files with 48 additions and 52 deletions

View File

@ -3,16 +3,6 @@
#include "include.h" #include "include.h"
size_t dict_size(const dict_t* x) {
return x->size;
}
size_t dict_capacity(const dict_t* x) {
return x->capacity;
}
hash_t dict_hash(const dict_t* s) { hash_t dict_hash(const dict_t* s) {
dnode_t *min, *max; dnode_t *min, *max;
size_t i; size_t i;

View File

@ -1,7 +1,7 @@
/* This software is licensed by the MIT License, see LICENSE file */ /* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */ /* Copyright © 2022 Gregory Lirent */
#include "../../include/list.h" #include "../list/include.h"
#include "include.h" #include "include.h"
static inline dnode_t* libcdsb_builtin_duplicate(const dnode_t* s, dnode_t* p) { static inline dnode_t* libcdsb_builtin_duplicate(const dnode_t* s, dnode_t* p) {
@ -81,10 +81,10 @@ void dict_copy_init(dict_t* x, const dict_t* s) {
} }
vtype_list libcdsb_dict_copy_keys(const vtype_dict* s) { list_t libcdsb_dict_copy_keys(const dict_t* s) {
vtype_list x; list_t x;
dnode_t *c; dnode_t *c;
size_t i; size_t i;
i = s->capacity; i = s->capacity;
@ -94,7 +94,11 @@ vtype_list libcdsb_dict_copy_keys(const vtype_dict* s) {
c = s->nodes[i]; c = s->nodes[i];
while (!is_null(c)) { while (!is_null(c)) {
libcdsb_list_insert(&x, -1, vnode_peek(&c->key, c->key_type), c->key_type, 1);
if (is_null(x.first)) {
libcdsb_builtin_init(&x, vnode_duplicate(&c->key, c->key_type), c->key_type);
} else libcdsb_builtin_push(&x, vnode_duplicate(&c->key, c->key_type), c->key_type);
c = c->prev; c = c->prev;
} }
} }
@ -103,10 +107,10 @@ vtype_list libcdsb_dict_copy_keys(const vtype_dict* s) {
} }
vtype_list* libcdsb_dict_duplicate_keys(const vtype_dict* s) { list_t* libcdsb_dict_duplicate_keys(const dict_t* s) {
vtype_list* x; list_t* x;
dnode_t *c; dnode_t *c;
size_t i; size_t i;
x = malloc(sizeof(*x)); x = malloc(sizeof(*x));
i = s->capacity; i = s->capacity;
@ -117,7 +121,9 @@ vtype_list* libcdsb_dict_duplicate_keys(const vtype_dict* s) {
c = s->nodes[i]; c = s->nodes[i];
while (!is_null(c)) { while (!is_null(c)) {
libcdsb_list_insert(x, -1, vnode_peek(&c->key, c->key_type), c->key_type, 1); if (is_null(x->first)) {
libcdsb_builtin_init(x, vnode_duplicate(&c->key, c->key_type), c->key_type);
} else libcdsb_builtin_push(x, vnode_duplicate(&c->key, c->key_type), c->key_type);
c = c->prev; c = c->prev;
} }
} }
@ -126,9 +132,9 @@ vtype_list* libcdsb_dict_duplicate_keys(const vtype_dict* s) {
} }
void libcdsb_dict_init_keys(vtype_list* x, const vtype_dict* s) { void libcdsb_dict_init_keys(list_t* x, const dict_t* s) {
dnode_t *c; dnode_t *c;
size_t i; size_t i;
x = malloc(sizeof(*x)); x = malloc(sizeof(*x));
i = s->capacity; i = s->capacity;
@ -139,7 +145,9 @@ void libcdsb_dict_init_keys(vtype_list* x, const vtype_dict* s) {
c = s->nodes[i]; c = s->nodes[i];
while (!is_null(c)) { while (!is_null(c)) {
libcdsb_list_insert(x, -1, vnode_peek(&c->key, c->key_type), c->key_type, 1); if (is_null(x->first)) {
libcdsb_builtin_init(x, vnode_duplicate(&c->key, c->key_type), c->key_type);
} else libcdsb_builtin_push(x, vnode_duplicate(&c->key, c->key_type), c->key_type);
c = c->prev; c = c->prev;
} }
} }

View File

@ -3,33 +3,6 @@
#include "include.h" #include "include.h"
static void libcdsb_builtin_init(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 libcdsb_builtin_push(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->next = node;
x->last = node;
}
/*#####################################################################################################################*/
list_t list_copy(const list_t* s) { list_t list_copy(const list_t* s) {
list_t x; list_t x;
lnode_t* c; lnode_t* c;

View File

@ -20,6 +20,31 @@ typedef struct libcdsb_list_node {
vtype type; vtype type;
} lnode_t; } lnode_t;
ainline(void libcdsb_builtin_init(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;
}
ainline(void libcdsb_builtin_push(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->next = node;
x->last = node;
}
#define ldir_dir(cur, d) (&((cur)->prev))[(d)>>1] #define ldir_dir(cur, d) (&((cur)->prev))[(d)>>1]
#define ldir_inv(cur, d) (&((cur)->prev))[(d)&1] #define ldir_inv(cur, d) (&((cur)->prev))[(d)&1]