126 lines
2.7 KiB
C
126 lines
2.7 KiB
C
#include <fx/core/bst.h>
|
|
#include <fx/core/iterator.h>
|
|
#include <fx/ds/dict.h>
|
|
#include <fx/ds/number.h>
|
|
#include <fx/ds/tree.h>
|
|
#include <stdio.h>
|
|
|
|
#define NITEMS 16
|
|
|
|
struct tree_item {
|
|
int value;
|
|
fx_tree_node node;
|
|
};
|
|
|
|
struct bst_item {
|
|
int value;
|
|
fx_bst_node node;
|
|
};
|
|
|
|
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)
|
|
|
|
int main(void)
|
|
{
|
|
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));
|
|
|
|
fx_iterator *it = fx_iterator_begin(dict);
|
|
|
|
size_t i = 0;
|
|
fx_foreach(fx_dict_item *, item, it)
|
|
{
|
|
printf("item %zu: %s=%d\n", i++, fx_string_ptr(item->key),
|
|
fx_number_get_int(item->value));
|
|
}
|
|
|
|
fx_iterator_unref(it);
|
|
|
|
fx_tree *tree = fx_tree_create();
|
|
struct tree_item items2[NITEMS];
|
|
|
|
for (int i = 0; i < NITEMS; i++) {
|
|
items2[i].value = i;
|
|
items2[i].node = FX_TREE_NODE_INIT;
|
|
}
|
|
|
|
fx_tree_set_root(tree, &items2[0].node);
|
|
|
|
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);
|
|
|
|
#if 0
|
|
it = fx_iterator_begin(tree);
|
|
fx_tree_iterator it2;
|
|
fx_tree_foreach(&it2, tree)
|
|
{
|
|
struct tree_item *item = fx_unbox(struct tree_item, it2.node, node);
|
|
|
|
for (size_t i = 0; i < it2.depth; i++) {
|
|
fputs(" ", stdout);
|
|
}
|
|
|
|
printf("%u\n", item->value);
|
|
}
|
|
|
|
fx_bst bst = {0};
|
|
struct bst_item items3[NITEMS] = {0};
|
|
for (int i = 0; i < NITEMS; i++) {
|
|
items3[i].value = i;
|
|
put_node(&bst, &items3[i]);
|
|
}
|
|
|
|
printf("\n\n");
|
|
|
|
fx_bst_iterator it3;
|
|
fx_bst_foreach (&it3, &bst) {
|
|
struct bst_item *item
|
|
= fx_unbox(struct bst_item, it3.node, node);
|
|
|
|
for (size_t i = 0; i < it3.depth; i++) {
|
|
fputs(" ", stdout);
|
|
}
|
|
|
|
printf("%d\n", item->value);
|
|
}
|
|
|
|
fx_bst_iterator_begin(&bst, &it3);
|
|
while (fx_bst_iterator_is_valid(&it3)) {
|
|
struct bst_item *item
|
|
= fx_unbox(struct bst_item, it3.node, node);
|
|
|
|
if (item->value == 9) {
|
|
fx_bst_iterator_erase(&it3);
|
|
} else {
|
|
fx_bst_iterator_next(&it3);
|
|
}
|
|
}
|
|
|
|
printf("\n\n");
|
|
|
|
fx_bst_foreach (&it3, &bst) {
|
|
struct bst_item *item
|
|
= fx_unbox(struct bst_item, it3.node, node);
|
|
|
|
for (size_t i = 0; i < it3.depth; i++) {
|
|
fputs(" ", stdout);
|
|
}
|
|
|
|
printf("%d\n", item->value);
|
|
}
|
|
|
|
fx_tree_unref(tree);
|
|
#endif
|
|
fx_dict_unref(dict);
|
|
|
|
return 0;
|
|
}
|