fx.collections: hashtable: implement clone()

This commit is contained in:
2026-07-12 22:13:36 +01:00
parent ad27b0cf73
commit 5c837abc6c
+51
View File
@@ -546,6 +546,56 @@ static fx_status to_string(
return FX_SUCCESS;
}
static fx_status clone(const fx_value *src_v, fx_value *dest_v)
{
fx_hashtable *src_table = NULL, *dest_table = NULL;
fx_value_get_object(src_v, &src_table);
dest_table = fx_hashtable_create();
if (!dest_table) {
return FX_ERR_NO_MEMORY;
}
struct fx_hashtable_p *src_p
= fx_object_get_private(src_table, FX_TYPE_HASHTABLE);
struct fx_hashtable_p *dest_p
= fx_object_get_private(dest_table, FX_TYPE_HASHTABLE);
dest_p->t_count = src_p->t_count;
dest_p->t_max_index = src_p->t_max_index;
size_t capacity = primes[dest_p->t_max_index];
dest_p->t_items = calloc(capacity, sizeof *dest_p->t_items);
if (!dest_p->t_items) {
fx_hashtable_unref(dest_table);
return FX_ERR_NO_MEMORY;
}
for (size_t i = 0; i < capacity; i++) {
if (!src_p->t_items[i].i_item) {
continue;
}
fx_hashtable *new_item
= fx_object_create(FX_TYPE_HASHTABLE_ITEM);
if (!new_item) {
fx_hashtable_unref(dest_table);
return FX_ERR_NO_MEMORY;
}
struct fx_hashtable_item_p *src_item_p = fx_object_get_private(
src_p->t_items[i].i_item,
FX_TYPE_HASHTABLE_ITEM);
struct fx_hashtable_item_p *new_item_p = fx_object_get_private(
new_item,
FX_TYPE_HASHTABLE_ITEM);
new_item_p->i_key = fx_value_copy_return(src_item_p->i_key);
new_item_p->i_value = fx_value_copy_return(src_item_p->i_value);
dest_p->t_items[i].i_item = new_item;
}
*dest_v = FX_VALUE_OBJECT(dest_table);
return FX_SUCCESS;
}
static fx_status get_key(
const fx_value *item_value,
const fx_property *prop,
@@ -655,6 +705,7 @@ static const fx_value *iterator_get_value(const fx_iterator *obj)
FX_TYPE_CLASS_BEGIN(fx_hashtable)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = to_string;
FX_INTERFACE_ENTRY(clone) = clone;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)