#include #include #include #include #include #include #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; }; struct b_dict_iterator_p { size_t i; b_dict_item item; b_dict *_d; struct b_dict_p *_d_p; b_btree_node *_cbn; b_queue_entry *_cqe; }; /*** 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; } struct b_queue_entry *entry = b_queue_first(&bucket->bk_items); while (entry) { struct b_dict_bucket_item *item = b_unbox(struct b_dict_bucket_item, entry, bi_entry); if (!strcmp(b_string_ptr(item->bi_str), key)) { return item->bi_value; } entry = b_queue_next(entry); } 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; } struct b_queue_entry *entry = b_queue_first(&bucket->bk_items); while (entry) { struct b_dict_bucket_item *item = b_unbox(struct b_dict_bucket_item, entry, bi_entry); if (b_string_compare(item->bi_str, key)) { return item->bi_value; } entry = b_queue_next(entry); } 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; #if 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++; } } #endif 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 *********************************************************/ #if 0 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; } #endif 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; struct b_btree_node *node = b_btree_first(&dict->d_buckets); while (node) { struct b_dict_bucket *bucket = b_unbox(struct b_dict_bucket, node, bk_node); struct b_btree_node *next_node = b_btree_next(node); b_btree_delete(&dict->d_buckets, node); struct b_queue_entry *entry = b_queue_first(&bucket->bk_items); while (entry) { struct b_dict_bucket_item *item = b_unbox( struct b_dict_bucket_item, entry, bi_entry); struct b_queue_entry *next_entry = b_queue_next(entry); b_queue_delete(&bucket->bk_items, entry); free(item->bi_str); b_object_unref(item->bi_value); free(item); entry = next_entry; } free(bucket); node = next_node; } } static void dict_to_string(const b_object *obj, 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); size_t i = 0; struct b_btree_node *node = b_btree_first(&dict->d_buckets); while (node) { struct b_dict_bucket *bucket = b_unbox(struct b_dict_bucket, node, bk_node); struct b_queue_entry *entry = b_queue_first(&bucket->bk_items); while (entry) { struct b_dict_bucket_item *item = b_unbox( struct b_dict_bucket_item, entry, bi_entry); b_object_to_string(item->bi_str, out); b_stream_write_string(out, ": ", NULL); bool is_string = b_object_is_type(item->bi_value, B_TYPE_STRING); if (is_string) { b_stream_write_char(out, '"'); } b_object_to_string(item->bi_value, out); if (is_string) { b_stream_write_char(out, '"'); } if (i < len - 1) { b_stream_write_string(out, ",", NULL); } b_stream_write_char(out, '\n'); entry = b_queue_next(entry); i++; } node = b_btree_next(node); } b_stream_pop_indent(out); b_stream_write_char(out, '}'); } /*** ITERATOR FUNCTIONS *******************************************************/ static b_iterator *iterable_begin(b_dict *dict) { b_iterator *it_obj = b_object_create(B_TYPE_DICT_ITERATOR); if (!it_obj) { return NULL; } struct b_dict_iterator_p *it = b_object_get_private(it_obj, B_TYPE_DICT_ITERATOR); 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->item.key = NULL; it->item.value = NULL; b_iterator_set_status(it_obj, B_ERR_NO_DATA); return it_obj; } 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->item.key = NULL; it->item.value = NULL; b_iterator_set_status(it_obj, B_ERR_NO_DATA); return it_obj; } 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->item.key = NULL; it->item.value = NULL; b_iterator_set_status(it_obj, B_ERR_NO_DATA); return it_obj; } it->item.key = first_item->bi_str; it->item.value = first_item->bi_value; it->_d = dict; it->_cbn = first_node; it->_cqe = first_entry; return it_obj; } static const b_iterator *iterable_cbegin(const b_dict *dict) { b_iterator *it_obj = b_object_create(B_TYPE_DICT_ITERATOR); if (!it_obj) { return NULL; } struct b_dict_iterator_p *it = b_object_get_private(it_obj, B_TYPE_DICT_ITERATOR); it->i = 0; it->_d = (b_dict *)dict; it->_d_p = b_object_get_private(dict, B_TYPE_DICT); if (dict_is_empty(it->_d_p)) { it->item.key = NULL; it->item.value = NULL; b_iterator_set_status(it_obj, B_ERR_NO_DATA); return it_obj; } 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->item.key = NULL; it->item.value = NULL; b_iterator_set_status(it_obj, B_ERR_NO_DATA); return it_obj; } 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->item.key = NULL; it->item.value = NULL; b_iterator_set_status(it_obj, B_ERR_NO_DATA); return it_obj; } it->item.key = first_item->bi_str; it->item.value = first_item->bi_value; it->_cbn = first_node; it->_cqe = first_entry; return it_obj; } static enum b_status iterator_move_next(const b_iterator *obj) { struct b_dict_iterator_p *it = b_object_get_private(obj, B_TYPE_DICT_ITERATOR); 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->item.key = NULL; it->item.value = NULL; return B_ERR_NO_DATA; } struct b_dict_bucket_item *next_item = b_unbox(struct b_dict_bucket_item, next_entry, bi_entry); if (!next_item) { it->item.key = NULL; it->item.value = NULL; return B_ERR_NO_DATA; } it->i++; it->item.key = next_item->bi_str; it->item.value = next_item->bi_value; it->_cbn = next_node; it->_cqe = next_entry; return B_SUCCESS; } static enum b_status iterator_erase(b_iterator *obj) { struct b_dict_iterator_p *it = b_object_get_private(obj, B_TYPE_DICT_ITERATOR); if ((it->item.key || it->item.value) && !(it->_cbn && it->_cqe)) { return B_ERR_BAD_STATE; } if (!it->item.key || !it->_cqe) { return B_ERR_NO_DATA; } 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->item.key = NULL; it->item.value = NULL; return B_ERR_NO_DATA; } 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->item.key = next_item->bi_str; it->item.value = next_item->bi_value; it->_cbn = next_node; it->_cqe = next_entry; } else { it->item.key = NULL; it->item.value = NULL; it->_cbn = NULL; it->_cqe = NULL; } return B_SUCCESS; } static b_iterator_value iterator_get_value(b_iterator *obj) { struct b_dict_iterator_p *it = b_object_get_private(obj, B_TYPE_DICT_ITERATOR); return B_ITERATOR_VALUE_PTR(&it->item); } static const b_iterator_value iterator_get_cvalue(const b_iterator *obj) { struct b_dict_iterator_p *it = b_object_get_private(obj, B_TYPE_DICT_ITERATOR); return B_ITERATOR_VALUE_CPTR(&it->item); } /*** CLASS DEFINITION *********************************************************/ // ---- b_dict 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_INTERFACE_BEGIN(b_iterable, B_TYPE_ITERABLE) B_INTERFACE_ENTRY(it_begin) = iterable_begin; B_INTERFACE_ENTRY(it_cbegin) = iterable_cbegin; B_TYPE_CLASS_INTERFACE_END(b_iterable, B_TYPE_ITERABLE) 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_IMPLEMENTS(B_TYPE_ITERABLE); B_TYPE_INSTANCE_PRIVATE(struct b_dict_p); B_TYPE_INSTANCE_INIT(dict_init); B_TYPE_INSTANCE_FINI(dict_fini); B_TYPE_DEFINITION_END(b_dict) // ---- b_dict_iterator DEFINITION B_TYPE_CLASS_DEFINITION_BEGIN(b_dict_iterator) B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT) B_INTERFACE_ENTRY(to_string) = NULL; B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT) B_TYPE_CLASS_INTERFACE_BEGIN(b_iterator, B_TYPE_ITERATOR) B_INTERFACE_ENTRY(it_move_next) = iterator_move_next; B_INTERFACE_ENTRY(it_erase) = iterator_erase; B_INTERFACE_ENTRY(it_get_value) = iterator_get_value; B_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue; B_TYPE_CLASS_INTERFACE_END(b_iterator, B_TYPE_ITERATOR) B_TYPE_CLASS_DEFINITION_END(b_dict_iterator) B_TYPE_DEFINITION_BEGIN(b_dict_iterator) B_TYPE_ID(0x9ea96701, 0x1713, 0x4a3e, 0xbf63, 0xdc856b456f3b); B_TYPE_EXTENDS(B_TYPE_ITERATOR); B_TYPE_CLASS(b_dict_iterator_class); B_TYPE_INSTANCE_PRIVATE(struct b_dict_iterator_p); B_TYPE_DEFINITION_END(b_dict_iterator)