615 lines
13 KiB
C
615 lines
13 KiB
C
#include <blue/core/status.h>
|
|
#include <blue/core/stream.h>
|
|
#include <blue/ds/dict.h>
|
|
#include <blue/ds/string.h>
|
|
#include <stdbool.h>
|
|
#include <stdlib.h>
|
|
|
|
#define HASH_OFFSET_BASIS 0xcbf29ce484222325
|
|
#define HASH_PRIME 0x100000001b3
|
|
|
|
/*** PRIVATE DATA *************************************************************/
|
|
|
|
struct b_dict_bucket_item {
|
|
b_queue_entry bi_entry;
|
|
b_string *bi_str;
|
|
b_object *bi_value;
|
|
};
|
|
|
|
struct b_dict_bucket {
|
|
b_btree_node bk_node;
|
|
uint64_t bk_hash;
|
|
b_queue bk_items;
|
|
};
|
|
|
|
struct b_dict_p {
|
|
b_btree d_buckets;
|
|
};
|
|
|
|
/*** MISC FUNCTIONS ***********************************************************/
|
|
|
|
static B_BTREE_DEFINE_SIMPLE_GET(
|
|
struct b_dict_bucket, uint64_t, bk_node, bk_hash, get_bucket);
|
|
static B_BTREE_DEFINE_SIMPLE_INSERT(
|
|
struct b_dict_bucket, bk_node, bk_hash, put_bucket);
|
|
|
|
uint64_t b_cstr_hash(const char *s)
|
|
{
|
|
uint64_t hash = HASH_OFFSET_BASIS;
|
|
|
|
for (size_t i = 0; s[i]; i++) {
|
|
hash *= HASH_PRIME;
|
|
hash ^= s[i];
|
|
}
|
|
|
|
return hash;
|
|
}
|
|
|
|
/*** PRIVATE FUNCTIONS ********************************************************/
|
|
|
|
static struct b_dict_bucket *create_bucket(void)
|
|
{
|
|
struct b_dict_bucket *bucket = malloc(sizeof *bucket);
|
|
if (!bucket) {
|
|
return NULL;
|
|
}
|
|
|
|
memset(bucket, 0x0, sizeof *bucket);
|
|
return bucket;
|
|
}
|
|
|
|
static struct b_dict_bucket_item *create_bucket_item(void)
|
|
{
|
|
struct b_dict_bucket_item *item = malloc(sizeof *item);
|
|
if (!item) {
|
|
return NULL;
|
|
}
|
|
|
|
memset(item, 0x0, sizeof *item);
|
|
return item;
|
|
}
|
|
|
|
static b_status dict_put(struct b_dict_p *dict, const char *key, b_object *value)
|
|
{
|
|
uint64_t hash = b_cstr_hash(key);
|
|
struct b_dict_bucket *bucket = get_bucket(&dict->d_buckets, hash);
|
|
if (!bucket) {
|
|
bucket = create_bucket();
|
|
if (!bucket) {
|
|
return B_ERR_NO_MEMORY;
|
|
}
|
|
|
|
bucket->bk_hash = hash;
|
|
put_bucket(&dict->d_buckets, bucket);
|
|
}
|
|
|
|
struct b_dict_bucket_item *item = create_bucket_item();
|
|
if (!item) {
|
|
return B_ERR_NO_MEMORY;
|
|
}
|
|
|
|
item->bi_str = b_string_create_from_cstr(key);
|
|
item->bi_value = b_object_ref(value);
|
|
|
|
b_queue_push_back(&bucket->bk_items, &item->bi_entry);
|
|
|
|
return B_SUCCESS;
|
|
}
|
|
|
|
static b_status dict_put_sk(
|
|
struct b_dict_p *dict, const b_string *key, b_object *value)
|
|
{
|
|
uint64_t hash = b_string_hash(key);
|
|
struct b_dict_bucket *bucket = get_bucket(&dict->d_buckets, hash);
|
|
if (!bucket) {
|
|
bucket = create_bucket();
|
|
if (!bucket) {
|
|
return B_ERR_NO_MEMORY;
|
|
}
|
|
|
|
bucket->bk_hash = hash;
|
|
put_bucket(&dict->d_buckets, bucket);
|
|
}
|
|
|
|
struct b_dict_bucket_item *item = create_bucket_item();
|
|
if (!item) {
|
|
return B_ERR_NO_MEMORY;
|
|
}
|
|
|
|
item->bi_str = b_string_duplicate(key);
|
|
item->bi_value = b_object_ref(value);
|
|
|
|
b_queue_push_back(&bucket->bk_items, &item->bi_entry);
|
|
|
|
return B_SUCCESS;
|
|
}
|
|
|
|
static b_object *dict_at(const struct b_dict_p *dict, const char *key)
|
|
{
|
|
uint64_t hash = b_cstr_hash(key);
|
|
struct b_dict_bucket *bucket = get_bucket(&dict->d_buckets, hash);
|
|
if (!bucket) {
|
|
return NULL;
|
|
}
|
|
|
|
b_queue_iterator it;
|
|
b_queue_foreach (&it, &bucket->bk_items) {
|
|
struct b_dict_bucket_item *item
|
|
= b_unbox(struct b_dict_bucket_item, it.entry, bi_entry);
|
|
|
|
if (!strcmp(b_string_ptr(item->bi_str), key)) {
|
|
return item->bi_value;
|
|
}
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
static b_object *dict_at_sk(const struct b_dict_p *dict, const b_string *key)
|
|
{
|
|
uint64_t hash = b_string_hash(key);
|
|
struct b_dict_bucket *bucket = get_bucket(&dict->d_buckets, hash);
|
|
if (!bucket) {
|
|
return NULL;
|
|
}
|
|
|
|
b_queue_iterator it;
|
|
b_queue_foreach (&it, &bucket->bk_items) {
|
|
struct b_dict_bucket_item *item
|
|
= b_unbox(struct b_dict_bucket_item, it.entry, bi_entry);
|
|
|
|
if (b_string_compare(item->bi_str, key)) {
|
|
return item->bi_value;
|
|
}
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
static b_object *dict_get(struct b_dict_p *dict, const char *key)
|
|
{
|
|
b_object *value = dict_at(dict, key);
|
|
if (value) {
|
|
b_object_ref(value);
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
static b_object *dict_get_sk(struct b_dict_p *dict, const b_string *key)
|
|
{
|
|
b_object *value = dict_at_sk(dict, key);
|
|
if (value) {
|
|
b_object_ref(value);
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
static bool dict_has_key(const struct b_dict_p *dict, const char *key)
|
|
{
|
|
return dict_at(dict, key) != NULL;
|
|
}
|
|
|
|
static bool dict_has_skey(const struct b_dict_p *dict, const b_string *key)
|
|
{
|
|
return dict_at_sk(dict, key) != NULL;
|
|
}
|
|
|
|
static size_t dict_get_size(const struct b_dict_p *dict)
|
|
{
|
|
size_t count = 0;
|
|
b_btree_iterator it1;
|
|
b_queue_iterator it2;
|
|
|
|
b_btree_foreach (&it1, &dict->d_buckets) {
|
|
struct b_dict_bucket *bucket
|
|
= b_unbox(struct b_dict_bucket, it1.node, bk_node);
|
|
|
|
b_queue_foreach (&it2, &bucket->bk_items) {
|
|
count++;
|
|
}
|
|
}
|
|
|
|
return count;
|
|
}
|
|
|
|
static bool dict_is_empty(const struct b_dict_p *dict)
|
|
{
|
|
b_btree_node *first_node = b_btree_first(&dict->d_buckets);
|
|
struct b_dict_bucket *first_bucket
|
|
= b_unbox(struct b_dict_bucket, first_node, bk_node);
|
|
if (!first_bucket) {
|
|
return true;
|
|
}
|
|
|
|
b_queue_entry *first_entry = b_queue_first(&first_bucket->bk_items);
|
|
struct b_dict_bucket_item *first_item
|
|
= b_unbox(struct b_dict_bucket_item, first_entry, bi_entry);
|
|
if (!first_item) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
static bool get_next_node(
|
|
struct b_btree_node *cur_node, struct b_queue_entry *cur_entry,
|
|
struct b_btree_node **out_next_node, struct b_queue_entry **out_next_entry)
|
|
{
|
|
struct b_dict_bucket *cur_bucket
|
|
= b_unbox(struct b_dict_bucket, cur_node, bk_node);
|
|
if (!cur_bucket) {
|
|
return false;
|
|
}
|
|
|
|
struct b_dict_bucket_item *cur_item
|
|
= b_unbox(struct b_dict_bucket_item, cur_entry, bi_entry);
|
|
if (!cur_item) {
|
|
return false;
|
|
}
|
|
|
|
struct b_btree_node *next_node = cur_node;
|
|
struct b_queue_entry *next_entry = b_queue_next(cur_entry);
|
|
if (!next_entry) {
|
|
next_node = b_btree_next(cur_node);
|
|
if (!next_node) {
|
|
return false;
|
|
}
|
|
|
|
struct b_dict_bucket *next_bucket
|
|
= b_unbox(struct b_dict_bucket, next_node, bk_node);
|
|
if (!next_bucket) {
|
|
return false;
|
|
}
|
|
|
|
next_entry = b_queue_first(&next_bucket->bk_items);
|
|
if (!next_entry) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
struct b_dict_bucket_item *next_item
|
|
= b_unbox(struct b_dict_bucket_item, next_entry, bi_entry);
|
|
if (!next_item) {
|
|
return false;
|
|
}
|
|
|
|
*out_next_node = next_node;
|
|
*out_next_entry = next_entry;
|
|
|
|
return true;
|
|
}
|
|
|
|
static b_status delete_item(
|
|
struct b_dict_p *dict, struct b_dict_bucket *bucket,
|
|
struct b_dict_bucket_item *item)
|
|
{
|
|
b_queue_delete(&bucket->bk_items, &item->bi_entry);
|
|
free(item);
|
|
|
|
if (b_queue_empty(&bucket->bk_items)) {
|
|
b_btree_delete(&dict->d_buckets, &bucket->bk_node);
|
|
free(bucket);
|
|
}
|
|
|
|
return B_SUCCESS;
|
|
}
|
|
|
|
/*** PUBLIC FUNCTIONS *********************************************************/
|
|
|
|
b_dict *b_dict_create_with_items(const b_dict_item *items)
|
|
{
|
|
b_dict *dict = b_dict_create();
|
|
if (!dict) {
|
|
return NULL;
|
|
}
|
|
|
|
struct b_dict_p *p = b_object_get_private(dict, B_TYPE_DICT);
|
|
|
|
for (size_t i = 0; items[i].key; i++) {
|
|
dict_put(p, items[i].key, items[i].value);
|
|
}
|
|
|
|
return dict;
|
|
}
|
|
b_status b_dict_put(b_dict *dict, const char *key, b_object *value)
|
|
{
|
|
B_CLASS_DISPATCH_STATIC(B_TYPE_DICT, dict_put, dict, key, value);
|
|
}
|
|
|
|
b_status b_dict_put_sk(b_dict *dict, const b_string *key, b_object *value)
|
|
{
|
|
B_CLASS_DISPATCH_STATIC(B_TYPE_DICT, dict_put_sk, dict, key, value);
|
|
}
|
|
|
|
b_object *b_dict_at(const b_dict *dict, const char *key)
|
|
{
|
|
B_CLASS_DISPATCH_STATIC(B_TYPE_DICT, dict_at, dict, key);
|
|
}
|
|
|
|
b_object *b_dict_at_sk(const b_dict *dict, const b_string *key)
|
|
{
|
|
B_CLASS_DISPATCH_STATIC(B_TYPE_DICT, dict_at_sk, dict, key);
|
|
}
|
|
|
|
b_object *b_dict_get(b_dict *dict, const char *key)
|
|
{
|
|
B_CLASS_DISPATCH_STATIC(B_TYPE_DICT, dict_get, dict, key);
|
|
}
|
|
|
|
b_object *b_dict_get_sk(b_dict *dict, const b_string *key)
|
|
{
|
|
B_CLASS_DISPATCH_STATIC(B_TYPE_DICT, dict_get_sk, dict, key);
|
|
}
|
|
|
|
bool b_dict_has_key(const b_dict *dict, const char *key)
|
|
{
|
|
B_CLASS_DISPATCH_STATIC(B_TYPE_DICT, dict_has_key, dict, key);
|
|
}
|
|
|
|
bool b_dict_has_skey(const b_dict *dict, const b_string *key)
|
|
{
|
|
B_CLASS_DISPATCH_STATIC(B_TYPE_DICT, dict_has_skey, dict, key);
|
|
}
|
|
|
|
size_t b_dict_get_size(const b_dict *dict)
|
|
{
|
|
B_CLASS_DISPATCH_STATIC_0(B_TYPE_DICT, dict_get_size, dict);
|
|
}
|
|
|
|
bool b_dict_is_empty(const b_dict *dict)
|
|
{
|
|
B_CLASS_DISPATCH_STATIC_0(B_TYPE_DICT, dict_is_empty, dict);
|
|
}
|
|
|
|
/*** PUBLIC ALIAS FUNCTIONS ***************************************************/
|
|
/*** VIRTUAL FUNCTIONS ********************************************************/
|
|
|
|
static void dict_init(b_object *obj, void *priv)
|
|
{
|
|
struct b_dict_p *dict = priv;
|
|
}
|
|
|
|
static void dict_fini(b_object *obj, void *priv)
|
|
{
|
|
struct b_dict_p *dict = priv;
|
|
|
|
b_btree_iterator tree_it;
|
|
b_btree_iterator_begin(&dict->d_buckets, &tree_it);
|
|
while (b_btree_iterator_is_valid(&tree_it)) {
|
|
struct b_dict_bucket *bucket
|
|
= b_unbox(struct b_dict_bucket, tree_it.node, bk_node);
|
|
b_btree_iterator_erase(&tree_it);
|
|
|
|
if (!bucket) {
|
|
continue;
|
|
}
|
|
|
|
b_queue_iterator bucket_it;
|
|
b_queue_iterator_begin(&bucket->bk_items, &bucket_it);
|
|
while (b_queue_iterator_is_valid(&bucket_it)) {
|
|
struct b_dict_bucket_item *item = b_unbox(
|
|
struct b_dict_bucket_item, bucket_it.entry,
|
|
bi_entry);
|
|
b_queue_iterator_erase(&bucket_it);
|
|
|
|
if (!item) {
|
|
continue;
|
|
}
|
|
|
|
free(item->bi_str);
|
|
b_object_unref(item->bi_value);
|
|
free(item);
|
|
}
|
|
|
|
free(bucket);
|
|
}
|
|
}
|
|
|
|
static void dict_to_string(const b_object *obj, struct b_stream *out)
|
|
{
|
|
struct b_dict_p *dict = b_object_get_private(obj, B_TYPE_DICT);
|
|
|
|
if (dict_is_empty(dict)) {
|
|
b_stream_write_string(out, "{}", NULL);
|
|
return;
|
|
}
|
|
|
|
b_stream_write_string(out, "{\n", NULL);
|
|
|
|
b_stream_push_indent(out, 1);
|
|
size_t len = dict_get_size(dict);
|
|
|
|
b_dict_iterator it;
|
|
b_dict_foreach(&it, (b_dict *)obj)
|
|
{
|
|
b_object_to_string(it.key, out);
|
|
b_stream_write_string(out, ": ", NULL);
|
|
|
|
bool is_string = b_object_is_type(it.value, B_TYPE_STRING);
|
|
|
|
if (is_string) {
|
|
b_stream_write_char(out, '"');
|
|
}
|
|
|
|
b_object_to_string(it.value, out);
|
|
|
|
if (is_string) {
|
|
b_stream_write_char(out, '"');
|
|
}
|
|
|
|
if (it.i < len - 1) {
|
|
b_stream_write_string(out, ",", NULL);
|
|
}
|
|
|
|
b_stream_write_char(out, '\n');
|
|
}
|
|
|
|
b_stream_pop_indent(out);
|
|
b_stream_write_char(out, '}');
|
|
}
|
|
|
|
/*** CLASS DEFINITION *********************************************************/
|
|
|
|
B_TYPE_CLASS_DEFINITION_BEGIN(b_dict)
|
|
B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT)
|
|
B_INTERFACE_ENTRY(to_string) = dict_to_string;
|
|
B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT)
|
|
B_TYPE_CLASS_DEFINITION_END(b_dict)
|
|
|
|
B_TYPE_DEFINITION_BEGIN(b_dict)
|
|
B_TYPE_ID(0xd2af61d9, 0xd0be, 0x4960, 0xbe3f, 0x509749814c10);
|
|
B_TYPE_CLASS(b_dict_class);
|
|
B_TYPE_INSTANCE_INIT(dict_init);
|
|
B_TYPE_INSTANCE_INIT(dict_fini);
|
|
B_TYPE_DEFINITION_END(b_dict)
|
|
|
|
/*** ITERATOR FUNCTIONS *******************************************************/
|
|
|
|
static bool dict_iterator_next(struct b_iterator *it)
|
|
{
|
|
return b_dict_iterator_next((struct b_dict_iterator *)it);
|
|
}
|
|
|
|
static b_status dict_iterator_erase(struct b_iterator *it)
|
|
{
|
|
return b_dict_iterator_erase((struct b_dict_iterator *)it);
|
|
}
|
|
|
|
static bool dict_iterator_is_valid(const struct b_iterator *it)
|
|
{
|
|
return b_dict_iterator_is_valid((struct b_dict_iterator *)it);
|
|
}
|
|
|
|
static struct b_iterator_ops it_ops = {
|
|
.it_next = dict_iterator_next,
|
|
.it_close = NULL,
|
|
.it_erase = dict_iterator_erase,
|
|
.it_is_valid = dict_iterator_is_valid,
|
|
};
|
|
|
|
int b_dict_iterator_begin(b_dict *dict, b_dict_iterator *it)
|
|
{
|
|
it->_base.it_ops = &it_ops;
|
|
|
|
it->i = 0;
|
|
it->_d = dict;
|
|
it->_d_p = b_object_get_private(dict, B_TYPE_DICT);
|
|
|
|
if (dict_is_empty(it->_d_p)) {
|
|
it->key = NULL;
|
|
it->value = NULL;
|
|
return -1;
|
|
}
|
|
|
|
b_btree_node *first_node = b_btree_first(&it->_d_p->d_buckets);
|
|
struct b_dict_bucket *first_bucket
|
|
= b_unbox(struct b_dict_bucket, first_node, bk_node);
|
|
if (!first_bucket) {
|
|
it->key = NULL;
|
|
it->value = NULL;
|
|
return -1;
|
|
}
|
|
|
|
b_queue_entry *first_entry = b_queue_first(&first_bucket->bk_items);
|
|
struct b_dict_bucket_item *first_item
|
|
= b_unbox(struct b_dict_bucket_item, first_entry, bi_entry);
|
|
if (!first_item) {
|
|
it->key = NULL;
|
|
it->value = NULL;
|
|
return -1;
|
|
}
|
|
|
|
it->key = first_item->bi_str;
|
|
it->value = first_item->bi_value;
|
|
|
|
it->_d = dict;
|
|
it->_cbn = first_node;
|
|
it->_cqe = first_entry;
|
|
|
|
return 0;
|
|
}
|
|
|
|
bool b_dict_iterator_next(b_dict_iterator *it)
|
|
{
|
|
struct b_btree_node *next_node;
|
|
struct b_queue_entry *next_entry;
|
|
if (!get_next_node(it->_cbn, it->_cqe, &next_node, &next_entry)) {
|
|
it->key = NULL;
|
|
it->value = NULL;
|
|
return -1;
|
|
}
|
|
|
|
struct b_dict_bucket_item *next_item
|
|
= b_unbox(struct b_dict_bucket_item, next_entry, bi_entry);
|
|
|
|
if (!next_item) {
|
|
it->key = NULL;
|
|
it->value = NULL;
|
|
return -1;
|
|
}
|
|
|
|
it->i++;
|
|
it->key = next_item->bi_str;
|
|
it->value = next_item->bi_value;
|
|
|
|
it->_cbn = next_node;
|
|
it->_cqe = next_entry;
|
|
|
|
return 0;
|
|
}
|
|
|
|
b_status b_dict_iterator_erase(struct b_dict_iterator *it)
|
|
{
|
|
if ((it->key || it->value) && !(it->_cbn && it->_cqe)) {
|
|
return B_ERR_BAD_STATE;
|
|
}
|
|
|
|
if (!it->key || !it->_cqe) {
|
|
return B_ERR_OUT_OF_BOUNDS;
|
|
}
|
|
|
|
struct b_btree_node *next_node;
|
|
struct b_queue_entry *next_entry;
|
|
if (!get_next_node(it->_cbn, it->_cqe, &next_node, &next_entry)) {
|
|
it->key = NULL;
|
|
it->value = NULL;
|
|
return false;
|
|
}
|
|
|
|
struct b_dict_bucket *cur_bucket
|
|
= b_unbox(struct b_dict_bucket, it->_cbn, bk_node);
|
|
struct b_dict_bucket_item *cur_item
|
|
= b_unbox(struct b_dict_bucket_item, it->_cqe, bi_entry);
|
|
|
|
struct b_dict_bucket_item *next_item
|
|
= b_unbox(struct b_dict_bucket_item, next_entry, bi_entry);
|
|
|
|
b_status status = delete_item(it->_d_p, cur_bucket, cur_item);
|
|
if (B_ERR(status)) {
|
|
return status;
|
|
}
|
|
|
|
if (next_item) {
|
|
it->key = next_item->bi_str;
|
|
it->value = next_item->bi_value;
|
|
|
|
it->_cbn = next_node;
|
|
it->_cqe = next_entry;
|
|
} else {
|
|
it->key = NULL;
|
|
it->value = NULL;
|
|
|
|
it->_cbn = NULL;
|
|
it->_cqe = NULL;
|
|
}
|
|
|
|
return B_SUCCESS;
|
|
}
|
|
|
|
bool b_dict_iterator_is_valid(const struct b_dict_iterator *it)
|
|
{
|
|
return it->key != NULL;
|
|
}
|