Files
fx/fx.collections/include/fx/collections/hashmap.h
T

91 lines
2.6 KiB
C

#ifndef FX_DS_HASHMAP_H_
#define FX_DS_HASHMAP_H_
#include <fx/bst.h>
#include <fx/macros.h>
#include <fx/misc.h>
#include <fx/queue.h>
#include <fx/status.h>
#include <stddef.h>
FX_DECLS_BEGIN;
struct fx_hashmap_p;
#define FX_TYPE_HASHMAP (fx_hashmap_get_type())
#define FX_TYPE_HASHMAP_ITERATOR (fx_hashmap_iterator_get_type())
FX_DECLARE_TYPE(fx_hashmap);
FX_DECLARE_TYPE(fx_hashmap_iterator);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_hashmap)
FX_TYPE_CLASS_DECLARATION_END(fx_hashmap)
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_hashmap_iterator)
FX_TYPE_CLASS_DECLARATION_END(fx_hashmap_iterator)
#define FX_HASHMAP_KEY(k, ks) {.key_data = (k), .key_size = (ks)}
#define FX_HASHMAP_VALUE(v, vs) {.value_data = (v), .value_size = (vs)}
#define FX_HASHMAP_ITEM(k, ks, v, vs) \
{.key = FX_HASHMAP_KEY(k, ks), .value = FX_HASHMAP_VALUE(v, vs)}
#define FX_HASHMAP_ITEM_END {.key = {0}, .value = {0}}
#define fx_hashmap_foreach(it, hashmap) \
for (int z__fx_unique_name() = fx_hashmap_iterator_begin(hashmap, it); \
(it)->key != NULL; \
fx_hashmap_iterator_next(it))
typedef void (*fx_hashmap_key_destructor)(void *);
typedef void (*fx_hashmap_value_destructor)(void *);
typedef enum fx_hashmap_key_flags {
FX_HASHMAP_KEY_F_INTVALUE = 0x01u,
} fx_hashmap_key_flags;
typedef struct fx_hashmap_key {
fx_hashmap_key_flags key_flags;
const void *key_data;
size_t key_size;
} fx_hashmap_key;
typedef struct fx_hashmap_value {
void *value_data;
size_t value_size;
} fx_hashmap_value;
typedef struct fx_hashmap_item {
fx_hashmap_key key;
fx_hashmap_value value;
} fx_hashmap_item;
FX_API fx_type fx_hashmap_get_type(void);
FX_API fx_type fx_hashmap_iterator_get_type(void);
FX_API fx_hashmap *fx_hashmap_create(
fx_hashmap_key_destructor key_dtor,
fx_hashmap_value_destructor value_dtor);
FX_API fx_hashmap *fx_hashmap_create_with_items(const fx_hashmap_item *items);
FX_API fx_status fx_hashmap_put(
fx_hashmap *hashmap,
const fx_hashmap_key *key,
const fx_hashmap_value *value);
FX_API const fx_hashmap_value *fx_hashmap_get(
const fx_hashmap *hashmap,
const fx_hashmap_key *key);
FX_API bool fx_hashmap_has_key(
const fx_hashmap *hashmap,
const fx_hashmap_key *key);
FX_API size_t fx_hashmap_get_size(const fx_hashmap *hashmap);
FX_API bool fx_hashmap_is_empty(const fx_hashmap *hashmap);
FX_API fx_iterator *fx_hashmap_begin(fx_hashmap *hashmap);
FX_API const fx_iterator *fx_hashmap_cbegin(const fx_hashmap *hashmap);
FX_DECLS_END;
#endif