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

93 lines
2.9 KiB
C

#ifndef BLUELIB_HASHMAP_H_
#define BLUELIB_HASHMAP_H_
#include <blue/core/btree.h>
#include <blue/core/misc.h>
#include <blue/core/queue.h>
#include <blue/core/status.h>
#include <blue/object/object.h>
#include <blue/object/type.h>
#define B_HASHMAP(p) ((b_hashmap *)(p))
#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 struct b_hashmap b_hashmap;
typedef struct b_hashmap_key {
const void *key_data;
size_t key_size;
bool key_owned;
} b_hashmap_key;
typedef struct b_hashmap_value {
void *value_data;
size_t value_size;
bool value_owned;
} 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;
b_btree_node *_cbn;
b_queue_entry *_cqe;
} b_hashmap_iterator;
BLUE_API b_hashmap *b_hashmap_create(void);
BLUE_API b_hashmap *b_hashmap_create_with_items(const b_hashmap_item *items);
static inline b_hashmap *b_hashmap_retain(b_hashmap *hashmap)
{
return B_HASHMAP(b_retain(B_OBJECT(hashmap)));
}
static inline void b_hashmap_release(b_hashmap *hashmap)
{
b_release(B_OBJECT(hashmap));
}
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);
#endif