diff --git a/include/extra/dict.h b/include/extra/dict.h index 972b77b..1f8edc5 100644 --- a/include/extra/dict.h +++ b/include/extra/dict.h @@ -14,3 +14,16 @@ extern int libcdsb_dict_foreach (vtype_dict* x, void* data, dict_access_ca extern bool libcdsb_dict_shrink_to_fit(vtype_dict* x) Nonnull__(1); #endif /* LIBCDSB_EXTRA_DICT_H */ + +#if defined(LIBCDSB_LIST_H) && !defined(LIBCDSB_EXTRA_DICT_H_EXT) +#define LIBCDSB_EXTRA_DICT_H_EXT + +#define dict_copy_keys libcdsb_dict_copy_keys +#define dict_duplicate_keys libcdsb_dict_duplicate_keys +#define dict_init_keys libcdsb_dict_init_keys + +extern vtype_list libcdsb_dict_copy_keys (const vtype_dict* s) Nonnull__(1); +extern vtype_list* libcdsb_dict_duplicate_keys(const vtype_dict* s) Nonnull__(1); +extern void libcdsb_dict_init_keys (vtype_list* x, const vtype_dict* s) Nonnull__(1,2); + +#endif /* LIBCDSB_EXTRA_DICT_H_EXT */ diff --git a/src/dict/extra.c b/src/dict/extra.c index 81d645c..5732977 100644 --- a/src/dict/extra.c +++ b/src/dict/extra.c @@ -1,6 +1,7 @@ /* This software is licensed by the MIT License, see LICENSE file */ /* Copyright © 2022 Gregory Lirent */ +#include "../../include/extra/list.h" #include "include.h" /*#####################################################################################################################*/ @@ -167,3 +168,69 @@ int libcdsb_dict_foreach(dict_t* x, void* dt, dict_access_callback callback, boo end_: return r; } + +/*#####################################################################################################################*/ + +vtype_list libcdsb_dict_copy_keys(const vtype_dict* s) { + vtype_list x; + dnode_t *c; + size_t i; + + i = s->capacity; + + list_init(&x); + + while (i--) { + c = s->nodes[i]; + + while (!is_null(c)) { + libcdsb_list_update(&x, -1, vnode_peek(&c->key, c->key_type), c->key_type, 1); + c = c->prev; + } + } + + return x; +} + + +vtype_list* libcdsb_dict_duplicate_keys(const vtype_dict* s) { + vtype_list* x; + dnode_t *c; + size_t i; + + x = malloc(sizeof(*x)); + i = s->capacity; + + list_init(x); + + while (i--) { + c = s->nodes[i]; + + while (!is_null(c)) { + libcdsb_list_update(x, -1, vnode_peek(&c->key, c->key_type), c->key_type, 1); + c = c->prev; + } + } + + return x; +} + + +void libcdsb_dict_init_keys(vtype_list* x, const vtype_dict* s) { + dnode_t *c; + size_t i; + + x = malloc(sizeof(*x)); + i = s->capacity; + + list_init(x); + + while (i--) { + c = s->nodes[i]; + + while (!is_null(c)) { + libcdsb_list_update(x, -1, vnode_peek(&c->key, c->key_type), c->key_type, 1); + c = c->prev; + } + } +}