fx.collections: update interface implementations

This commit is contained in:
2026-05-25 17:30:08 +01:00
parent 0be82c63fa
commit 97fe00d4a1
4 changed files with 141 additions and 94 deletions
+10 -3
View File
@@ -1,6 +1,7 @@
#include <fx/bitop.h>
#include <fx/collections/bitmap.h>
#include <fx/stream.h>
#include <fx/value.h>
#include <string.h>
#define BITS_PER_WORD (8 * sizeof(bitmap_word_t))
@@ -306,10 +307,14 @@ static void bitmap_fini(fx_object *obj, void *priv)
struct fx_bitmap_p *map = priv;
}
static void bitmap_to_string(const fx_object *obj, fx_stream *out)
static fx_status bitmap_to_string(
const fx_value *obj,
fx_stream *out,
const char *format)
{
const struct fx_bitmap_p *map
= fx_object_get_private(obj, FX_TYPE_BITMAP);
const struct fx_bitmap_p *map = fx_object_get_private(
obj->v_object,
FX_TYPE_BITMAP);
unsigned char *bytes = (unsigned char *)map->map_words;
size_t nr_bytes = map->map_nr_words * sizeof(bitmap_word_t);
@@ -339,6 +344,8 @@ static void bitmap_to_string(const fx_object *obj, fx_stream *out)
fx_stream_write_fmt(out, NULL, "%c", (c & mask) ? '1' : '0');
}
return FX_SUCCESS;
}
/*** CLASS DEFINITION *********************************************************/
+10 -2
View File
@@ -1,6 +1,7 @@
#include <fx/collections/datetime.h>
#include <fx/stream.h>
#include <fx/string.h>
#include <fx/value.h>
/*** PRIVATE DATA *************************************************************/
@@ -566,9 +567,14 @@ static void datetime_fini(fx_object *obj, void *priv)
struct fx_datetime_p *dt = priv;
}
static void _datetime_to_string(const fx_object *obj, fx_stream *out)
static fx_status _datetime_to_string(
const fx_value *obj,
fx_stream *out,
const char *format)
{
struct fx_datetime_p *dt = fx_object_get_private(obj, FX_TYPE_DATETIME);
struct fx_datetime_p *dt = fx_object_get_private(
obj->v_object,
FX_TYPE_DATETIME);
if (dt->dt_has_date) {
fx_stream_write_fmt(
@@ -607,6 +613,8 @@ static void _datetime_to_string(const fx_object *obj, fx_stream *out)
dt->dt_zone_offset_minute);
}
}
return FX_SUCCESS;
}
/*** CLASS DEFINITION *********************************************************/
+72 -52
View File
@@ -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)
+49 -37
View File
@@ -41,8 +41,10 @@ static void *list_first_item(const struct fx_list_p *q)
return NULL;
}
struct fx_list_entry *list_entry
= fx_unbox(struct fx_list_entry, entry, e_entry);
struct fx_list_entry *list_entry = fx_unbox(
struct fx_list_entry,
entry,
e_entry);
return list_entry->e_data;
}
@@ -55,8 +57,10 @@ static void *list_last_item(const struct fx_list_p *q)
return NULL;
}
struct fx_list_entry *list_entry
= fx_unbox(struct fx_list_entry, entry, e_entry);
struct fx_list_entry *list_entry = fx_unbox(
struct fx_list_entry,
entry,
e_entry);
return list_entry->e_data;
}
@@ -69,8 +73,10 @@ static struct fx_list_entry *list_first_entry(const struct fx_list_p *q)
return NULL;
}
struct fx_list_entry *list_entry
= fx_unbox(struct fx_list_entry, entry, e_entry);
struct fx_list_entry *list_entry = fx_unbox(
struct fx_list_entry,
entry,
e_entry);
return list_entry;
}
@@ -83,8 +89,10 @@ static struct fx_list_entry *list_last_entry(const struct fx_list_p *q)
return NULL;
}
struct fx_list_entry *list_entry
= fx_unbox(struct fx_list_entry, entry, e_entry);
struct fx_list_entry *list_entry = fx_unbox(
struct fx_list_entry,
entry,
e_entry);
return list_entry;
}
@@ -169,8 +177,10 @@ static void *list_pop_front(struct fx_list_p *q)
return NULL;
}
struct fx_list_entry *list_entry
= fx_unbox(struct fx_list_entry, entry, e_entry);
struct fx_list_entry *list_entry = fx_unbox(
struct fx_list_entry,
entry,
e_entry);
void *item = list_entry->e_data;
free(list_entry);
@@ -187,8 +197,10 @@ static void *list_pop_back(struct fx_list_p *q)
return NULL;
}
struct fx_list_entry *list_entry
= fx_unbox(struct fx_list_entry, entry, e_entry);
struct fx_list_entry *list_entry = fx_unbox(
struct fx_list_entry,
entry,
e_entry);
void *item = list_entry->e_data;
free(list_entry);
@@ -202,8 +214,10 @@ static struct fx_list_entry *find_item(struct fx_list_p *list, void *item)
{
struct fx_queue_entry *entry = fx_queue_first(&list->l_queue);
while (entry) {
struct fx_list_entry *list_entry
= fx_unbox(struct fx_list_entry, entry, e_entry);
struct fx_list_entry *list_entry = fx_unbox(
struct fx_list_entry,
entry,
e_entry);
if (list_entry->e_data == item) {
return list_entry;
@@ -246,8 +260,10 @@ static void list_delete_all(struct fx_list_p *q)
{
struct fx_queue_entry *entry = fx_queue_first(&q->l_queue);
while (entry) {
struct fx_list_entry *list_entry
= fx_unbox(struct fx_list_entry, entry, e_entry);
struct fx_list_entry *list_entry = fx_unbox(
struct fx_list_entry,
entry,
e_entry);
struct fx_queue_entry *next = fx_queue_next(entry);
free(list_entry);
@@ -388,8 +404,9 @@ void *fx_list_entry_value(const struct fx_list_entry *entry)
fx_iterator *fx_list_begin(fx_list *q)
{
fx_list_iterator *it_obj = fx_object_create(FX_TYPE_LIST_ITERATOR);
struct fx_list_iterator_p *it
= fx_object_get_private(it_obj, FX_TYPE_LIST_ITERATOR);
struct fx_list_iterator_p *it = fx_object_get_private(
it_obj,
FX_TYPE_LIST_ITERATOR);
it->_q = q;
it->_q_p = fx_object_get_private(q, FX_TYPE_LIST);
@@ -407,8 +424,9 @@ fx_iterator *fx_list_begin(fx_list *q)
const fx_iterator *fx_list_cbegin(const fx_list *q)
{
fx_list_iterator *it_obj = fx_object_create(FX_TYPE_LIST_ITERATOR);
struct fx_list_iterator_p *it
= fx_object_get_private(it_obj, FX_TYPE_LIST_ITERATOR);
struct fx_list_iterator_p *it = fx_object_get_private(
it_obj,
FX_TYPE_LIST_ITERATOR);
it->_q = (fx_list *)q;
it->_q_p = fx_object_get_private(q, FX_TYPE_LIST);
@@ -440,8 +458,9 @@ static void list_fini(fx_object *obj, void *priv)
static enum fx_status iterator_move_next(const fx_iterator *obj)
{
struct fx_list_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_LIST_ITERATOR);
struct fx_list_iterator_p *it = fx_object_get_private(
obj,
FX_TYPE_LIST_ITERATOR);
if (!it->_q_entry) {
it->entry = NULL;
@@ -467,8 +486,9 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
static enum fx_status iterator_erase(fx_iterator *obj)
{
struct fx_list_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_LIST_ITERATOR);
struct fx_list_iterator_p *it = fx_object_get_private(
obj,
FX_TYPE_LIST_ITERATOR);
if (!it->entry || !it->_q_entry) {
return FX_ERR_OUT_OF_BOUNDS;
@@ -488,20 +508,13 @@ 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_list_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_LIST_ITERATOR);
struct fx_list_iterator_p *it = fx_object_get_private(
obj,
FX_TYPE_LIST_ITERATOR);
return FX_ITERATOR_VALUE_PTR(it->item);
}
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
{
struct fx_list_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_LIST_ITERATOR);
return FX_ITERATOR_VALUE_CPTR(it->item);
return FX_POINTER(it->item);
}
/*** CLASS DEFINITION *********************************************************/
@@ -537,7 +550,6 @@ FX_TYPE_CLASS_BEGIN(fx_list_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_list_iterator)