fx.collections: replace fx_dict with fx_hashtable

This commit is contained in:
2026-05-25 17:27:59 +01:00
parent 6efe473c11
commit d84d56681f
5 changed files with 744 additions and 796 deletions
@@ -1,64 +0,0 @@
#ifndef FX_DS_DICT_H_
#define FX_DS_DICT_H_
#include <fx/bst.h>
#include <fx/macros.h>
#include <fx/misc.h>
#include <fx/queue.h>
#include <fx/status.h>
#include <fx/string.h>
FX_DECLS_BEGIN;
#define FX_TYPE_DICT (fx_dict_get_type())
#define FX_TYPE_DICT_ITERATOR (fx_dict_iterator_get_type())
struct fx_dict_p;
FX_DECLARE_TYPE(fx_dict);
FX_DECLARE_TYPE(fx_dict_iterator);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_dict)
FX_TYPE_CLASS_DECLARATION_END(fx_dict)
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_dict_iterator)
FX_TYPE_CLASS_DECLARATION_END(fx_dict_iterator)
#define FX_DICT_ITEM(k, v) {.key = (k), .value = (v)}
#define FX_DICT_ITEM_END {.key = NULL, .value = NULL}
#define fx_dict_foreach(it, dict) \
for (int z__fx_unique_name() = fx_dict_iterator_begin(dict, it); \
(it)->key != NULL; \
fx_dict_iterator_next(it))
typedef struct fx_dict_item {
const fx_string *key;
fx_object *value;
} fx_dict_item;
FX_API fx_type_id fx_dict_get_type(void);
FX_API fx_type_id fx_dict_iterator_get_type(void);
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_dict, FX_TYPE_DICT);
#if 0
FX_API fx_dict *fx_dict_create_with_items(const fx_dict_item *items);
#endif
FX_API fx_status fx_dict_put(fx_dict *dict, const char *key, fx_object *value);
FX_API fx_status
fx_dict_put_sk(fx_dict *dict, const fx_string *key, fx_object *value);
FX_API fx_object *fx_dict_at(const fx_dict *dict, const char *key);
FX_API fx_object *fx_dict_at_sk(const fx_dict *dict, const fx_string *key);
FX_API fx_object *fx_dict_get(fx_dict *dict, const char *key);
FX_API fx_object *fx_dict_get_sk(fx_dict *dict, const fx_string *key);
FX_API bool fx_dict_has_key(const fx_dict *dict, const char *key);
FX_API bool fx_dict_has_skey(const fx_dict *dict, const fx_string *key);
FX_API size_t fx_dict_get_size(const fx_dict *dict);
FX_API bool fx_dict_is_empty(const fx_dict *dict);
FX_DECLS_END;
#endif
@@ -0,0 +1,55 @@
#ifndef FX_COLLECTIONS_HASHTABLE_H_
#define FX_COLLECTIONS_HASHTABLE_H_
#include <fx/bst.h>
#include <fx/iterator.h>
#include <fx/macros.h>
#include <fx/misc.h>
#include <fx/queue.h>
#include <fx/status.h>
#include <fx/value.h>
#include <stddef.h>
FX_DECLS_BEGIN;
#define FX_TYPE_HASHTABLE (fx_hashtable_get_type())
#define FX_TYPE_HASHTABLE_ITERATOR (fx_hashtable_iterator_get_type())
#define FX_TYPE_HASHTABLE_ITEM (fx_hashtable_item_get_type())
FX_DECLARE_TYPE(fx_hashtable);
FX_DECLARE_TYPE(fx_hashtable_iterator);
FX_DECLARE_TYPE(fx_hashtable_item);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_hashtable)
FX_TYPE_CLASS_DECLARATION_END(fx_hashtable)
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_hashtable_iterator)
FX_TYPE_CLASS_DECLARATION_END(fx_hashtable_iterator)
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_hashtable_item)
FX_TYPE_CLASS_DECLARATION_END(fx_hashtable_item)
FX_API fx_type_id fx_hashtable_get_type(void);
FX_API fx_type_id fx_hashtable_iterator_get_type(void);
FX_API fx_type_id fx_hashtable_item_get_type(void);
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_hashtable, FX_TYPE_HASHTABLE);
FX_API fx_hashtable *fx_hashtable_create(void);
FX_API fx_status
fx_hashtable_put(fx_hashtable *hashtable, fx_value *key, fx_value *value);
FX_API const fx_value *fx_hashtable_get(
const fx_hashtable *hashtable,
const fx_value *key);
FX_API size_t fx_hashtable_get_count(const fx_hashtable *hashtable);
FX_API bool fx_hashtable_is_empty(const fx_hashtable *hashtable);
FX_API fx_iterator *fx_hashtable_begin(fx_hashtable *hashtable);
FX_API fx_value fx_hashtable_item_get_key(const fx_hashtable_item *item);
FX_API fx_value fx_hashtable_item_get_value(const fx_hashtable_item *item);
FX_DECLS_END;
#endif