Files
bluelib/ds/include/blue/ds/hashmap.h

92 lines
2.6 KiB
C

#ifndef BLUE_DS_HASHMAP_H_
#define BLUE_DS_HASHMAP_H_
#include <blue/core/btree.h>
#include <blue/core/macros.h>
#include <blue/core/misc.h>
#include <blue/core/queue.h>
#include <blue/core/status.h>
#include <stddef.h>
B_DECLS_BEGIN;
struct b_hashmap_p;
#define B_TYPE_HASHMAP (b_hashmap_get_type())
B_DECLARE_TYPE(b_hashmap);
B_TYPE_CLASS_DECLARATION_BEGIN(b_hashmap)
B_TYPE_CLASS_DECLARATION_END(b_hashmap)
#define B_HASHMAP_KEY(k, ks) {.key_data = (k), .key_size = (ks)}
#define B_HASHMAP_VALUE(v, vs) {.value_data = (v), .value_size = (vs)}
#define B_HASHMAP_ITEM(k, ks, v, vs) \
{.key = B_HASHMAP_KEY(k, ks), .value = B_HASHMAP_VALUE(v, vs)}
#define B_HASHMAP_ITEM_END {.key = {0}, .value = {0}}
#define b_hashmap_foreach(it, hashmap) \
for (int z__b_unique_name() = b_hashmap_iterator_begin(hashmap, it); \
(it)->key != NULL; b_hashmap_iterator_next(it))
typedef void (*b_hashmap_key_destructor)(void *);
typedef void (*b_hashmap_value_destructor)(void *);
typedef enum b_hashmap_key_flags {
B_HASHMAP_KEY_F_INTVALUE = 0x01u,
} b_hashmap_key_flags;
typedef struct b_hashmap_key {
b_hashmap_key_flags key_flags;
const void *key_data;
size_t key_size;
} b_hashmap_key;
typedef struct b_hashmap_value {
void *value_data;
size_t value_size;
} b_hashmap_value;
typedef struct b_hashmap_item {
b_hashmap_key key;
b_hashmap_value value;
} b_hashmap_item;
typedef struct b_hashmap_iterator {
b_iterator _base;
size_t i;
const b_hashmap_key *key;
const b_hashmap_value *value;
b_hashmap *_h;
struct b_hashmap_p *_h_p;
b_btree_node *_cbn;
b_queue_entry *_cqe;
} b_hashmap_iterator;
BLUE_API b_type b_hashmap_get_type(void);
BLUE_API b_hashmap *b_hashmap_create(
b_hashmap_key_destructor key_dtor, b_hashmap_value_destructor value_dtor);
BLUE_API b_hashmap *b_hashmap_create_with_items(const b_hashmap_item *items);
BLUE_API b_status b_hashmap_put(
b_hashmap *hashmap, const b_hashmap_key *key, const b_hashmap_value *value);
BLUE_API const b_hashmap_value *b_hashmap_get(
const b_hashmap *hashmap, const b_hashmap_key *key);
BLUE_API bool b_hashmap_has_key(const b_hashmap *hashmap, const b_hashmap_key *key);
BLUE_API size_t b_hashmap_get_size(const b_hashmap *hashmap);
BLUE_API bool b_hashmap_is_empty(const b_hashmap *hashmap);
BLUE_API int b_hashmap_iterator_begin(b_hashmap *hashmap, b_hashmap_iterator *it);
BLUE_API bool b_hashmap_iterator_next(b_hashmap_iterator *it);
BLUE_API b_status b_hashmap_iterator_erase(b_hashmap_iterator *it);
BLUE_API bool b_hashmap_iterator_is_valid(const b_hashmap_iterator *it);
B_DECLS_END;
#endif