Files
bluelib/object/include/blue/object/dict.h
Max Wash be47176524 object: dict: use b_strings as keys rather than plain c-strings.
now that strings can contain UTF-8 codepoints and null chars, the b_dict
api has been enhanced to accept keys as b_strings as well as regular c-strings.

keys are now stored as b_strings internally, to allow a wider range of
keys to be used.
2025-09-22 10:50:07 +01:00

77 lines
2.4 KiB
C

#ifndef BLUELIB_DICT_H_
#define BLUELIB_DICT_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>
struct b_string;
#define B_DICT(p) ((b_dict *)(p))
#define B_DICT_ITEM(k, v) \
{ \
.key = (k), .value = (v) \
}
#define B_DICT_ITEM_END \
{ \
.key = NULL, .value = NULL \
}
#define b_dict_foreach(it, dict) \
for (int z__b_unique_name() = b_dict_iterator_begin(dict, it); \
(it)->key != NULL; b_dict_iterator_next(it))
typedef struct b_dict b_dict;
typedef struct b_dict_iterator {
b_iterator _base;
size_t i;
const struct b_string *key;
b_object *value;
b_dict *_d;
b_btree_node *_cbn;
b_queue_entry *_cqe;
} b_dict_iterator;
typedef struct b_dict_item {
const char *key;
b_object *value;
} b_dict_item;
BLUE_API b_dict *b_dict_create(void);
BLUE_API b_dict *b_dict_create_with_items(const b_dict_item *items);
static inline b_dict *b_dict_retain(b_dict *dict)
{
return B_DICT(b_retain(B_OBJECT(dict)));
}
static inline void b_dict_release(b_dict *dict)
{
b_release(B_OBJECT(dict));
}
BLUE_API b_status b_dict_put(b_dict *dict, const char *key, b_object *value);
BLUE_API b_status b_dict_put_sk(
b_dict *dict, const struct b_string *key, b_object *value);
BLUE_API b_object *b_dict_at(const b_dict *dict, const char *key);
BLUE_API b_object *b_dict_at_sk(const b_dict *dict, const struct b_string *key);
BLUE_API b_object *b_dict_get(b_dict *dict, const char *key);
BLUE_API b_object *b_dict_get_sk(b_dict *dict, const struct b_string *key);
BLUE_API bool b_dict_has_key(const b_dict *dict, const char *key);
BLUE_API bool b_dict_has_skey(const b_dict *dict, const struct b_string *key);
BLUE_API size_t b_dict_get_size(const b_dict *dict);
BLUE_API bool b_dict_is_empty(const b_dict *dict);
BLUE_API int b_dict_iterator_begin(b_dict *dict, b_dict_iterator *it);
BLUE_API bool b_dict_iterator_next(b_dict_iterator *it);
BLUE_API b_status b_dict_iterator_erase(b_dict_iterator *it);
BLUE_API bool b_dict_iterator_is_valid(const b_dict_iterator *it);
#endif