From 804769d85cc414d3aa0efe32dc4e516a055930f5 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Wed, 24 Aug 2022 12:31:33 +0300 Subject: [PATCH] Fix dict keys copy implementation --- src/dict/compute.c | 10 ---------- src/dict/copy.c | 38 +++++++++++++++++++++++--------------- src/list/copy.c | 27 --------------------------- src/list/include.h | 25 +++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 52 deletions(-) diff --git a/src/dict/compute.c b/src/dict/compute.c index 2750de4..a22d4d6 100644 --- a/src/dict/compute.c +++ b/src/dict/compute.c @@ -3,16 +3,6 @@ #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) { dnode_t *min, *max; size_t i; diff --git a/src/dict/copy.c b/src/dict/copy.c index 312d7c7..5845b7e 100644 --- a/src/dict/copy.c +++ b/src/dict/copy.c @@ -1,7 +1,7 @@ /* This software is licensed by the MIT License, see LICENSE file */ /* Copyright © 2022 Gregory Lirent */ -#include "../../include/list.h" +#include "../list/include.h" #include "include.h" 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) { - vtype_list x; - dnode_t *c; - size_t i; +list_t libcdsb_dict_copy_keys(const dict_t* s) { + list_t x; + dnode_t *c; + size_t i; i = s->capacity; @@ -94,7 +94,11 @@ vtype_list libcdsb_dict_copy_keys(const vtype_dict* s) { c = s->nodes[i]; 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; } } @@ -103,10 +107,10 @@ vtype_list libcdsb_dict_copy_keys(const vtype_dict* s) { } -vtype_list* libcdsb_dict_duplicate_keys(const vtype_dict* s) { - vtype_list* x; - dnode_t *c; - size_t i; +list_t* libcdsb_dict_duplicate_keys(const dict_t* s) { + list_t* x; + dnode_t *c; + size_t i; x = malloc(sizeof(*x)); i = s->capacity; @@ -117,7 +121,9 @@ vtype_list* libcdsb_dict_duplicate_keys(const vtype_dict* s) { c = s->nodes[i]; 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; } } @@ -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) { - dnode_t *c; - size_t i; +void libcdsb_dict_init_keys(list_t* x, const dict_t* s) { + dnode_t *c; + size_t i; x = malloc(sizeof(*x)); i = s->capacity; @@ -139,7 +145,9 @@ void libcdsb_dict_init_keys(vtype_list* x, const vtype_dict* s) { c = s->nodes[i]; 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; } } diff --git a/src/list/copy.c b/src/list/copy.c index 34e5b77..ed6b03d 100644 --- a/src/list/copy.c +++ b/src/list/copy.c @@ -3,33 +3,6 @@ #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 x; lnode_t* c; diff --git a/src/list/include.h b/src/list/include.h index 463e872..f10b0a8 100644 --- a/src/list/include.h +++ b/src/list/include.h @@ -20,6 +20,31 @@ typedef struct libcdsb_list_node { vtype type; } 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_inv(cur, d) (&((cur)->prev))[(d)&1]