Files
fx/test/ds/trees.c

126 lines
2.7 KiB
C
Raw Permalink Normal View History

2026-03-16 15:11:29 +00:00
#include <fx/core/bst.h>
2026-03-16 10:35:43 +00:00
#include <fx/core/iterator.h>
#include <fx/ds/dict.h>
#include <fx/ds/number.h>
#include <fx/ds/tree.h>
2024-10-24 21:33:19 +01:00
#include <stdio.h>
#define NITEMS 16
struct tree_item {
int value;
2026-03-16 10:35:43 +00:00
fx_tree_node node;
2024-10-24 21:33:19 +01:00
};
2026-03-16 15:11:29 +00:00
struct bst_item {
2024-10-24 21:33:19 +01:00
int value;
2026-03-16 10:35:43 +00:00
fx_bst_node node;
2024-10-24 21:33:19 +01:00
};
2026-03-16 15:11:29 +00:00
FX_BST_DEFINE_SIMPLE_GET(struct bst_item, int, node, value, get_node)
FX_BST_DEFINE_SIMPLE_INSERT(struct bst_item, node, value, put_node)
2024-10-24 21:33:19 +01:00
int main(void)
{
2026-03-16 10:35:43 +00:00
fx_dict *dict = fx_dict_create();
fx_dict_put(dict, "hello", FX_RV_INT(32));
fx_dict_put(dict, "world", FX_RV_INT(64));
fx_dict_put(dict, "more", FX_RV_INT(128));
fx_dict_put(dict, "other", FX_RV_INT(256));
2024-10-24 21:33:19 +01:00
2026-03-16 10:35:43 +00:00
fx_iterator *it = fx_iterator_begin(dict);
2024-10-24 21:33:19 +01:00
2025-11-01 10:04:41 +00:00
size_t i = 0;
2026-03-16 10:35:43 +00:00
fx_foreach(fx_dict_item *, item, it)
2024-10-24 21:33:19 +01:00
{
2026-03-16 10:35:43 +00:00
printf("item %zu: %s=%d\n", i++, fx_string_ptr(item->key),
fx_number_get_int(item->value));
2024-10-24 21:33:19 +01:00
}
2026-03-16 10:35:43 +00:00
fx_iterator_unref(it);
2025-11-01 10:04:41 +00:00
2026-03-16 10:35:43 +00:00
fx_tree *tree = fx_tree_create();
2024-10-24 21:33:19 +01:00
struct tree_item items2[NITEMS];
for (int i = 0; i < NITEMS; i++) {
items2[i].value = i;
2026-03-16 10:35:43 +00:00
items2[i].node = FX_TREE_NODE_INIT;
2024-10-24 21:33:19 +01:00
}
2026-03-16 10:35:43 +00:00
fx_tree_set_root(tree, &items2[0].node);
2024-10-24 21:33:19 +01:00
2026-03-16 10:35:43 +00:00
fx_tree_node_add_child(&items2[0].node, &items2[1].node);
fx_tree_node_add_child(&items2[0].node, &items2[2].node);
fx_tree_node_add_child(&items2[0].node, &items2[3].node);
fx_tree_node_add_child(&items2[0].node, &items2[7].node);
fx_tree_node_add_child(&items2[1].node, &items2[4].node);
fx_tree_node_add_child(&items2[1].node, &items2[5].node);
fx_tree_node_add_child(&items2[4].node, &items2[6].node);
2024-10-24 21:33:19 +01:00
2025-11-01 10:04:41 +00:00
#if 0
2026-03-16 10:35:43 +00:00
it = fx_iterator_begin(tree);
fx_tree_iterator it2;
fx_tree_foreach(&it2, tree)
2024-10-24 21:33:19 +01:00
{
2026-03-16 10:35:43 +00:00
struct tree_item *item = fx_unbox(struct tree_item, it2.node, node);
2024-10-24 21:33:19 +01:00
for (size_t i = 0; i < it2.depth; i++) {
fputs(" ", stdout);
}
printf("%u\n", item->value);
}
2026-03-16 15:11:29 +00:00
fx_bst bst = {0};
struct bst_item items3[NITEMS] = {0};
2024-10-24 21:33:19 +01:00
for (int i = 0; i < NITEMS; i++) {
items3[i].value = i;
2026-03-16 15:11:29 +00:00
put_node(&bst, &items3[i]);
2024-10-24 21:33:19 +01:00
}
printf("\n\n");
2026-03-16 10:35:43 +00:00
fx_bst_iterator it3;
2026-03-16 15:11:29 +00:00
fx_bst_foreach (&it3, &bst) {
struct bst_item *item
= fx_unbox(struct bst_item, it3.node, node);
2024-10-24 21:33:19 +01:00
for (size_t i = 0; i < it3.depth; i++) {
fputs(" ", stdout);
}
printf("%d\n", item->value);
}
2026-03-16 15:11:29 +00:00
fx_bst_iterator_begin(&bst, &it3);
2026-03-16 10:35:43 +00:00
while (fx_bst_iterator_is_valid(&it3)) {
2026-03-16 15:11:29 +00:00
struct bst_item *item
= fx_unbox(struct bst_item, it3.node, node);
2024-10-24 21:33:19 +01:00
if (item->value == 9) {
2026-03-16 10:35:43 +00:00
fx_bst_iterator_erase(&it3);
2024-10-24 21:33:19 +01:00
} else {
2026-03-16 10:35:43 +00:00
fx_bst_iterator_next(&it3);
2024-10-24 21:33:19 +01:00
}
}
printf("\n\n");
2026-03-16 15:11:29 +00:00
fx_bst_foreach (&it3, &bst) {
struct bst_item *item
= fx_unbox(struct bst_item, it3.node, node);
2024-10-24 21:33:19 +01:00
for (size_t i = 0; i < it3.depth; i++) {
fputs(" ", stdout);
}
printf("%d\n", item->value);
}
2026-03-16 10:35:43 +00:00
fx_tree_unref(tree);
2025-11-01 10:04:41 +00:00
#endif
2026-03-16 10:35:43 +00:00
fx_dict_unref(dict);
2024-10-27 19:43:05 +00:00
2024-10-24 21:33:19 +01:00
return 0;
}