From 77a01e9b227158ab045dded94f30cd6cea7760db Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Mon, 4 Mar 2024 12:26:22 -0500 Subject: [PATCH] sexy --- libs/BLT | 2 +- tests/include/lilfbtf/test2.h | 1 + tests/src/tests2.cpp | 123 ++++++++++++++++++++++++++++++---- 3 files changed, 113 insertions(+), 13 deletions(-) diff --git a/libs/BLT b/libs/BLT index b7d69bd..fe9cd9a 160000 --- a/libs/BLT +++ b/libs/BLT @@ -1 +1 @@ -Subproject commit b7d69bdcbb4e9bb61650c7d85e7ae444cb21ed74 +Subproject commit fe9cd9a6efcb4636172bea5940da5071c7ecf1e7 diff --git a/tests/include/lilfbtf/test2.h b/tests/include/lilfbtf/test2.h index b95c04c..fc9119d 100644 --- a/tests/include/lilfbtf/test2.h +++ b/tests/include/lilfbtf/test2.h @@ -28,6 +28,7 @@ namespace fb inline void execute_tests() { execute(); + funny(); } } diff --git a/tests/src/tests2.cpp b/tests/src/tests2.cpp index 2aaf9c1..6285b1f 100644 --- a/tests/src/tests2.cpp +++ b/tests/src/tests2.cpp @@ -86,17 +86,54 @@ namespace fb std::array arg_c = {2, 2, 2, 0}; + struct construct_info + { + type_t type; + bool choice; + }; + + std::vector create_info(blt::size_t size) + { + std::vector info; + + construct_info root = {random_type(), false}; + std::stack> stack; + stack.emplace(root, 0); + while (!stack.empty()) + { + auto top = stack.top(); + auto node = top.first; + auto depth = top.second; + info.push_back(node); + stack.pop(); + for (blt::i32 i = 0; i < arg_c[static_cast(info.back().type)]; i++) + { + if (depth >= size) + break; + if (choice()) + stack.emplace(construct_info{random_type(), true}, depth + 1); + else + stack.emplace(construct_info{random_type_sub(), false}, depth + 1); + } + } + + return info; + } + class tree1 { + public: + blt::bump_allocator alloc{sizeof(node_t) * 8192}; private: struct node_t { + blt::bump_allocator& alloc; std::array children{}; double value = 0; blt::i32 argc; type_t type; - explicit node_t(type_t type): argc(arg_c[static_cast(type)]), type(type) + explicit node_t(type_t type, blt::bump_allocator& alloc): alloc(alloc), argc(arg_c[static_cast(type)]), type(type) { if (type == type_t::VALUE) value = random_value(); @@ -113,7 +150,7 @@ namespace fb value = children[0]->value - children[1]->value; return; case type_t::MUL: - value = children[0]->value * children[1]->value; + value = children[0]->value * children[1]->value; return; case type_t::VALUE: return; @@ -143,15 +180,44 @@ namespace fb } return value; } + + ~node_t() + { + for (int i = 0; i < argc; i++) + { + alloc.destroy(children[i]); + alloc.deallocate(children[i]); + } + } }; node_t* root = nullptr; public: - blt::bump_allocator alloc{sizeof(node_t) * 8192}; - void create(blt::size_t size) + void create(const std::vector& info) { - root = alloc.emplace(random_type()); +// root = alloc.emplace(random_type(), alloc); +// std::stack> stack; +// stack.emplace(root, 0); +// while (!stack.empty()) +// { +// auto top = stack.top(); +// auto* node = top.first; +// auto depth = top.second; +// stack.pop(); +// for (blt::i32 i = 0; i < node->argc; i++) +// { +// auto& assignment = node->children[i]; +// if (choice()) +// assignment = alloc.emplace(random_type(), alloc); +// else +// assignment = alloc.emplace(random_type_sub(), alloc); +// if (depth < size) +// stack.emplace(assignment, depth + 1); +// } +// } + root = alloc.emplace(info[0].type, alloc); + blt::size_t index = 1; std::stack> stack; stack.emplace(root, 0); while (!stack.empty()) @@ -163,12 +229,8 @@ namespace fb for (blt::i32 i = 0; i < node->argc; i++) { auto& assignment = node->children[i]; - if (choice()) - assignment = alloc.emplace(random_type()); - else - assignment = alloc.emplace(random_type_sub()); - if (depth < size) - stack.emplace(assignment, depth + 1); + assignment = alloc.emplace(info[index++].type, alloc); + stack.emplace(assignment, depth + 1); } } } @@ -177,12 +239,49 @@ namespace fb { return root->evaluate_tree(); } + + ~tree1() + { + alloc.destroy(root); + alloc.deallocate(root); + } + }; + + struct tree2 + { + private: + struct node_t + { + double value; + blt::i32 argc; + type_t type; + }; + node_t* nodes = nullptr; + blt::size_t size = 0; + public: + tree2() = default; + + void create(const std::vector& info) + { + size = static_cast(std::pow(2, std::log2(info.size()) + 1)); + nodes = new node_t[size]; + for (blt::size_t i = 0; i < info; i++) + { + + } + } + + ~tree2() + { + delete[] nodes; + } }; void funny() { + auto info = create_info(25); tree1 love; - love.create(10); + love.create(info); BLT_TRACE(love.evaluate()); } } \ No newline at end of file