From 9795a54ad0ee21958267c7727459f6fd248caa20 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Thu, 7 Mar 2024 15:46:12 -0500 Subject: [PATCH] funny virtual variant --- CMakeLists.txt | 2 +- tests/include/lilfbtf/test2.h | 61 +++++++++- tests/src/main.cpp | 2 +- tests/src/tests2.cpp | 60 +--------- tests/src/tests3.cpp | 216 +++++++++++++++++++++++++++++++++- 5 files changed, 271 insertions(+), 70 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2ce7be2..3a5cfa4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(lilfbtf5 VERSION 0.1.2) +project(lilfbtf5 VERSION 0.1.3) option(ENABLE_ADDRSAN "Enable the address sanitizer" OFF) option(ENABLE_UBSAN "Enable the ub sanitizer" OFF) diff --git a/tests/include/lilfbtf/test2.h b/tests/include/lilfbtf/test2.h index 6ab2f96..17916c3 100644 --- a/tests/include/lilfbtf/test2.h +++ b/tests/include/lilfbtf/test2.h @@ -19,17 +19,66 @@ #ifndef LILFBTF5_TEST2_H #define LILFBTF5_TEST2_H +#include +#include + namespace fb { + void test2(); - void execute(); + static constexpr blt::u64 SEED = 691; - void funny(); - - inline void execute_tests() + struct random_engine { - execute(); - funny(); + private: + std::mt19937_64 engine{SEED}; + public: + random_engine() = default; + + void reset(blt::u64 seed = SEED) + { + engine = std::mt19937_64{seed}; + } + + auto& get() + { + return engine; + } + }; + + inline random_engine engine; + + enum class type_t + { + ADD, SUB, MUL, DIV, VALUE, END + }; + + type_t random_type() + { + static std::random_device dev; + static std::uniform_int_distribution dist(0, static_cast(type_t::END) - 1); + return static_cast(dist(engine.get())); + } + + type_t random_type_sub() + { + static std::random_device dev; + static std::uniform_int_distribution dist(0, static_cast(type_t::END) - 2); + return static_cast(dist(engine.get())); + } + + double random_value() + { + static std::random_device dev; + static std::uniform_real_distribution dist(-2.0, 2.0); + return dist(engine.get()); + } + + bool choice() + { + static std::random_device dev; + static std::uniform_int_distribution dist(0, 1); + return dist(engine.get()); } } diff --git a/tests/src/main.cpp b/tests/src/main.cpp index c9d1c3c..6ef3c5d 100644 --- a/tests/src/main.cpp +++ b/tests/src/main.cpp @@ -46,7 +46,7 @@ int main(int argc, const char** argv) if (args.contains("--tests")) { - //fb::execute_tests(); +// fb::test2(); fb::test3(); } diff --git a/tests/src/tests2.cpp b/tests/src/tests2.cpp index 830ad17..7879a8f 100644 --- a/tests/src/tests2.cpp +++ b/tests/src/tests2.cpp @@ -43,59 +43,6 @@ namespace fb public: }; - enum class type_t - { - ADD, SUB, MUL, DIV, VALUE, END - }; - - static constexpr blt::u64 SEED = 691; - - struct random_engine - { - private: - std::mt19937_64 engine{SEED}; - public: - random_engine() = default; - - void reset(blt::u64 seed = SEED) - { - engine = std::mt19937_64{seed}; - } - - auto& get() - { - return engine; - } - } engine; - - type_t random_type() - { - static std::random_device dev; - static std::uniform_int_distribution dist(0, static_cast(type_t::END) - 1); - return static_cast(dist(engine.get())); - } - - type_t random_type_sub() - { - static std::random_device dev; - static std::uniform_int_distribution dist(0, static_cast(type_t::END) - 2); - return static_cast(dist(engine.get())); - } - - double random_value() - { - static std::random_device dev; - static std::uniform_real_distribution dist(-2.0, 2.0); - return dist(engine.get()); - } - - bool choice() - { - static std::random_device dev; - static std::uniform_int_distribution dist(0, 1); - return dist(engine.get()); - } - std::array(type_t::END)> arg_c = {2, 2, 2, 2, 0}; template @@ -249,7 +196,7 @@ namespace fb BLT_END_INTERVAL("Tree Evaluation", blt::type_string() + ": Single Class Tree"); } - void funny() + void test2() { bump>(); bump>(); @@ -259,9 +206,4 @@ namespace fb BLT_PRINT_PROFILE("Tree Evaluation"); BLT_PRINT_PROFILE("Tree Destruction"); } - - void execute() - { - - } } \ No newline at end of file diff --git a/tests/src/tests3.cpp b/tests/src/tests3.cpp index 4e21403..c0aa710 100644 --- a/tests/src/tests3.cpp +++ b/tests/src/tests3.cpp @@ -19,20 +19,230 @@ #include #include "blt/std/utility.h" #include "blt/std/logging.h" +#include "blt/std/allocator.h" +#include +#include +#include namespace fb { - class base + using TYPE = double; + + class base_t { private: blt::size_t argc_ = 0; + protected: + TYPE value = 0; public: - explicit base(blt::size_t argc): argc_(argc) + explicit base_t(blt::size_t argc): argc_(argc) {} [[nodiscard]] inline blt::size_t argc() const + { return argc_; } + + [[nodiscard]] inline TYPE getValue() const { - return argc_; + return value; + } + + inline virtual void call(blt::span args) = 0; + + virtual ~base_t() = default; + }; + + class add_t : public base_t + { + public: + add_t(): base_t(2) + {} + + inline void call(blt::span args) final + { + value = args[0] + args[1]; + } + }; + + class sub_t : public base_t + { + public: + sub_t(): base_t(2) + {} + + inline void call(blt::span args) final + { + value = args[0] - args[1]; + } + }; + + class mul_t : public base_t + { + public: + mul_t(): base_t(2) + {} + + inline void call(blt::span args) final + { + value = args[0] * args[1]; + } + }; + + class div_t : public base_t + { + public: + div_t(): base_t(2) + {} + + inline void call(blt::span args) final + { + if (args[1] == 0) + value = 0; + else + value = args[0] / args[1]; + } + }; + + class value_t : public base_t + { + private: + TYPE value = 0; + public: + value_t(): base_t(0) + { + value = random_value(); + } + + inline void call(blt::span) final + {} + }; + + blt::bump_allocator alloc; + + base_t* create_node_type(type_t i) + { + switch (i) + { + case type_t::ADD: + return alloc.emplace(); + case type_t::SUB: + return alloc.emplace(); + case type_t::MUL: + return alloc.emplace(); + case type_t::DIV: + return alloc.emplace(); + case type_t::VALUE: + return alloc.emplace(); + default: + BLT_ERROR("Hey maybe something weird is going on here"); + return nullptr; + } + } + + class tree2 + { + private: + struct node_t + { + base_t* type; + std::array children{nullptr}; + + explicit node_t(type_t type): type(create_node_type(type)) + {} + + void evaluate() + { + TYPE v1 = children[0]->type->getValue(); + TYPE v2 = children[1]->type->getValue(); + TYPE d[2]{v1, v2}; + type->call(blt::span{d}); + } + + double evaluate_tree() + { + std::stack nodes; + std::stack node_stack; + + nodes.push(this); + + while (!nodes.empty()) + { + auto* top = nodes.top(); + node_stack.push(top); + nodes.pop(); + for (blt::size_t i = 0; i < top->type->argc(); i++) + nodes.push(top->children[i]); + } + + while (!node_stack.empty()) + { + node_stack.top()->evaluate(); + node_stack.pop(); + } + return value; + } + + ~node_t() + { + alloc.destroy(type); + alloc.deallocate(type); + for (blt::size_t i = 0; i < type->argc(); i++) + { + alloc.destroy(children[i]); + alloc.deallocate(children[i]); + } + } + }; + + node_t* root = nullptr; + public: + + void create(blt::u64 size) + { + root = alloc.template emplace(random_type()); + std::stack> stack; + stack.emplace(root, 0); + while (!stack.empty()) + { + auto top = stack.top(); + auto* node = top.first; + auto depth = top.second; + //BLT_WARN("gen type %ld with argc: %ld", node->type, node->argc); + stack.pop(); + //BLT_TRACE0_STREAM << "Size: " << stack.size() << "\n"; + for (blt::i32 i = 0; i < node->argc; i++) + { + if (depth >= size) + { + node->children[i] = alloc.template emplace(type_t::VALUE); + //BLT_INFO("Skipping due to size, value %lf", node->children[i]->value); + continue; + } + if (choice()) + node->children[i] = alloc.template emplace(random_type()); + else + node->children[i] = alloc.template emplace(random_type_sub()); + //BLT_INFO("child %p to %p has type generated %ld with argc %d, value %lf", node->children[i], node, + // static_cast(node->children[i]->type), node->children[i]->argc, node->children[i]->value); + if (depth < size) + stack.emplace(node->children[i], depth + 1); + } + //BLT_TRACE0_STREAM << "Size: " << stack.size() << "\n"; + } +// BLT_INFO("We have %ld adds, %ld subs, %ld mul, %ld div, %ld val, == %ld", t1_add, t1_sub, t1_mul, t1_div, t1_val, +// t1_add + t1_sub + t1_mul + t1_val + t1_div); + } + + double evaluate() + { + return root->evaluate_tree(); + } + + ~tree2() + { + BLT_START_INTERVAL("Tree Destruction", blt::type_string() + ": Single Class Tree"); + alloc.destroy(root); + alloc.deallocate(root); + BLT_END_INTERVAL("Tree Destruction", blt::type_string() + ": Single Class Tree"); } };