fx.collections: update interface implementations
This commit is contained in:
+72
-52
@@ -112,14 +112,18 @@ static bool get_next_node(
|
||||
struct fx_bst_node **out_next_node,
|
||||
struct fx_queue_entry **out_next_entry)
|
||||
{
|
||||
struct fx_hashmap_bucket *cur_bucket
|
||||
= fx_unbox(struct fx_hashmap_bucket, cur_node, bk_node);
|
||||
struct fx_hashmap_bucket *cur_bucket = fx_unbox(
|
||||
struct fx_hashmap_bucket,
|
||||
cur_node,
|
||||
bk_node);
|
||||
if (!cur_bucket) {
|
||||
return false;
|
||||
}
|
||||
|
||||
struct fx_hashmap_bucket_item *cur_item
|
||||
= fx_unbox(struct fx_hashmap_bucket_item, cur_entry, bi_entry);
|
||||
struct fx_hashmap_bucket_item *cur_item = fx_unbox(
|
||||
struct fx_hashmap_bucket_item,
|
||||
cur_entry,
|
||||
bi_entry);
|
||||
if (!cur_item) {
|
||||
return false;
|
||||
}
|
||||
@@ -146,8 +150,10 @@ static bool get_next_node(
|
||||
}
|
||||
}
|
||||
|
||||
struct fx_hashmap_bucket_item *next_item
|
||||
= fx_unbox(struct fx_hashmap_bucket_item, next_entry, bi_entry);
|
||||
struct fx_hashmap_bucket_item *next_item = fx_unbox(
|
||||
struct fx_hashmap_bucket_item,
|
||||
next_entry,
|
||||
bi_entry);
|
||||
if (!next_item) {
|
||||
return false;
|
||||
}
|
||||
@@ -186,8 +192,9 @@ static fx_status hashmap_put(
|
||||
const fx_hashmap_value *value)
|
||||
{
|
||||
uint64_t hash = hash_key(key);
|
||||
struct fx_hashmap_bucket *bucket
|
||||
= get_bucket(&hashmap->h_buckets, hash);
|
||||
struct fx_hashmap_bucket *bucket = get_bucket(
|
||||
&hashmap->h_buckets,
|
||||
hash);
|
||||
|
||||
if (!bucket) {
|
||||
bucket = create_bucket();
|
||||
@@ -234,8 +241,9 @@ static const struct fx_hashmap_value *hashmap_get(
|
||||
{
|
||||
uint64_t hash = hash_key(key);
|
||||
|
||||
struct fx_hashmap_bucket *bucket
|
||||
= get_bucket(&hashmap->h_buckets, hash);
|
||||
struct fx_hashmap_bucket *bucket = get_bucket(
|
||||
&hashmap->h_buckets,
|
||||
hash);
|
||||
if (!bucket) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -262,8 +270,9 @@ static bool hashmap_has_key(
|
||||
const fx_hashmap_key *key)
|
||||
{
|
||||
uint64_t hash = hash_key(key);
|
||||
struct fx_hashmap_bucket *bucket
|
||||
= get_bucket(&hashmap->h_buckets, hash);
|
||||
struct fx_hashmap_bucket *bucket = get_bucket(
|
||||
&hashmap->h_buckets,
|
||||
hash);
|
||||
if (!bucket) {
|
||||
return false;
|
||||
}
|
||||
@@ -293,8 +302,10 @@ static size_t hashmap_get_size(const struct fx_hashmap_p *hashmap)
|
||||
static bool hashmap_is_empty(const struct fx_hashmap_p *hashmap)
|
||||
{
|
||||
fx_bst_node *first_node = fx_bst_first(&hashmap->h_buckets);
|
||||
struct fx_hashmap_bucket *first_bucket
|
||||
= fx_unbox(struct fx_hashmap_bucket, first_node, bk_node);
|
||||
struct fx_hashmap_bucket *first_bucket = fx_unbox(
|
||||
struct fx_hashmap_bucket,
|
||||
first_node,
|
||||
bk_node);
|
||||
if (!first_bucket) {
|
||||
return true;
|
||||
}
|
||||
@@ -358,8 +369,9 @@ fx_hashmap *fx_hashmap_create_with_items(const fx_hashmap_item *items)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fx_hashmap_p *p
|
||||
= fx_object_get_private(hashmap, FX_TYPE_HASHMAP);
|
||||
struct fx_hashmap_p *p = fx_object_get_private(
|
||||
hashmap,
|
||||
FX_TYPE_HASHMAP);
|
||||
|
||||
for (size_t i = 0; items[i].key.key_data && items[i].key.key_size;
|
||||
i++) {
|
||||
@@ -410,10 +422,11 @@ bool fx_hashmap_is_empty(const fx_hashmap *hashmap)
|
||||
|
||||
fx_iterator *fx_hashmap_begin(fx_hashmap *hashmap)
|
||||
{
|
||||
fx_hashmap_iterator *it_obj
|
||||
= fx_object_create(FX_TYPE_HASHMAP_ITERATOR);
|
||||
struct fx_hashmap_iterator_p *it
|
||||
= fx_object_get_private(it_obj, FX_TYPE_HASHMAP_ITERATOR);
|
||||
fx_hashmap_iterator *it_obj = fx_object_create(
|
||||
FX_TYPE_HASHMAP_ITERATOR);
|
||||
struct fx_hashmap_iterator_p *it = fx_object_get_private(
|
||||
it_obj,
|
||||
FX_TYPE_HASHMAP_ITERATOR);
|
||||
|
||||
it->_h = hashmap;
|
||||
it->_h_p = fx_object_get_private(hashmap, FX_TYPE_HASHMAP);
|
||||
@@ -426,16 +439,18 @@ fx_iterator *fx_hashmap_begin(fx_hashmap *hashmap)
|
||||
}
|
||||
|
||||
struct fx_bst_node *first_node = fx_bst_first(&it->_h_p->h_buckets);
|
||||
struct fx_hashmap_bucket *first_bucket
|
||||
= fx_unbox(struct fx_hashmap_bucket, first_node, bk_node);
|
||||
struct fx_hashmap_bucket *first_bucket = fx_unbox(
|
||||
struct fx_hashmap_bucket,
|
||||
first_node,
|
||||
bk_node);
|
||||
if (!first_bucket) {
|
||||
memset(&it->item, 0x0, sizeof it->item);
|
||||
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
|
||||
return it_obj;
|
||||
}
|
||||
|
||||
struct fx_queue_entry *first_entry
|
||||
= fx_queue_first(&first_bucket->bk_items);
|
||||
struct fx_queue_entry *first_entry = fx_queue_first(
|
||||
&first_bucket->bk_items);
|
||||
struct fx_hashmap_bucket_item *first_item = fx_unbox(
|
||||
struct fx_hashmap_bucket_item,
|
||||
first_entry,
|
||||
@@ -473,8 +488,10 @@ static void hashmap_fini(fx_object *obj, void *priv)
|
||||
|
||||
struct fx_bst_node *node = fx_bst_first(&map->h_buckets);
|
||||
while (node) {
|
||||
struct fx_hashmap_bucket *b
|
||||
= fx_unbox(struct fx_hashmap_bucket, node, bk_node);
|
||||
struct fx_hashmap_bucket *b = fx_unbox(
|
||||
struct fx_hashmap_bucket,
|
||||
node,
|
||||
bk_node);
|
||||
struct fx_bst_node *next_node = fx_bst_next(node);
|
||||
fx_bst_delete(&map->h_buckets, node);
|
||||
|
||||
@@ -484,8 +501,8 @@ static void hashmap_fini(fx_object *obj, void *priv)
|
||||
struct fx_hashmap_bucket_item,
|
||||
entry,
|
||||
bi_entry);
|
||||
struct fx_queue_entry *next_entry
|
||||
= fx_queue_next(entry);
|
||||
struct fx_queue_entry *next_entry = fx_queue_next(
|
||||
entry);
|
||||
fx_queue_delete(&b->bk_items, entry);
|
||||
|
||||
if (map->h_key_dtor) {
|
||||
@@ -510,8 +527,9 @@ static void hashmap_fini(fx_object *obj, void *priv)
|
||||
|
||||
static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_hashmap_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_HASHMAP_ITERATOR);
|
||||
struct fx_hashmap_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_TYPE_HASHMAP_ITERATOR);
|
||||
|
||||
struct fx_bst_node *next_node;
|
||||
struct fx_queue_entry *next_entry;
|
||||
@@ -520,8 +538,10 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
struct fx_hashmap_bucket_item *next_item
|
||||
= fx_unbox(struct fx_hashmap_bucket_item, next_entry, bi_entry);
|
||||
struct fx_hashmap_bucket_item *next_item = fx_unbox(
|
||||
struct fx_hashmap_bucket_item,
|
||||
next_entry,
|
||||
bi_entry);
|
||||
|
||||
if (!next_item) {
|
||||
memset(&it->item, 0x0, sizeof it->item);
|
||||
@@ -540,8 +560,9 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
|
||||
|
||||
static enum fx_status iterator_erase(fx_iterator *obj)
|
||||
{
|
||||
struct fx_hashmap_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_HASHMAP_ITERATOR);
|
||||
struct fx_hashmap_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_TYPE_HASHMAP_ITERATOR);
|
||||
|
||||
if ((it->item.key.key_data || it->item.value.value_data)
|
||||
&& !(it->_cbn && it->_cqe)) {
|
||||
@@ -559,13 +580,19 @@ static enum fx_status iterator_erase(fx_iterator *obj)
|
||||
return FX_ERR_NO_DATA;
|
||||
}
|
||||
|
||||
struct fx_hashmap_bucket *cur_bucket
|
||||
= fx_unbox(struct fx_hashmap_bucket, it->_cbn, bk_node);
|
||||
struct fx_hashmap_bucket_item *cur_item
|
||||
= fx_unbox(struct fx_hashmap_bucket_item, it->_cqe, bi_entry);
|
||||
struct fx_hashmap_bucket *cur_bucket = fx_unbox(
|
||||
struct fx_hashmap_bucket,
|
||||
it->_cbn,
|
||||
bk_node);
|
||||
struct fx_hashmap_bucket_item *cur_item = fx_unbox(
|
||||
struct fx_hashmap_bucket_item,
|
||||
it->_cqe,
|
||||
bi_entry);
|
||||
|
||||
struct fx_hashmap_bucket_item *next_item
|
||||
= fx_unbox(struct fx_hashmap_bucket_item, next_entry, bi_entry);
|
||||
struct fx_hashmap_bucket_item *next_item = fx_unbox(
|
||||
struct fx_hashmap_bucket_item,
|
||||
next_entry,
|
||||
bi_entry);
|
||||
|
||||
fx_status status = delete_item(it->_h_p, cur_bucket, cur_item);
|
||||
if (FX_ERR(status)) {
|
||||
@@ -590,18 +617,12 @@ static enum fx_status iterator_erase(fx_iterator *obj)
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_iterator_value iterator_get_value(fx_iterator *obj)
|
||||
static fx_value iterator_get_value(const fx_iterator *obj)
|
||||
{
|
||||
struct fx_hashmap_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_HASHMAP_ITERATOR);
|
||||
return FX_ITERATOR_VALUE_PTR(&it->item);
|
||||
}
|
||||
|
||||
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
|
||||
{
|
||||
const struct fx_hashmap_iterator_p *it
|
||||
= fx_object_get_private(obj, FX_TYPE_HASHMAP_ITERATOR);
|
||||
return FX_ITERATOR_VALUE_CPTR(&it->item);
|
||||
struct fx_hashmap_iterator_p *it = fx_object_get_private(
|
||||
obj,
|
||||
FX_TYPE_HASHMAP_ITERATOR);
|
||||
return FX_POINTER(&it->item);
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
@@ -637,7 +658,6 @@ FX_TYPE_CLASS_BEGIN(fx_hashmap_iterator)
|
||||
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
|
||||
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
|
||||
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
|
||||
FX_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
|
||||
FX_TYPE_CLASS_END(fx_hashmap_iterator)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user