diff --git a/CMakeLists.txt b/CMakeLists.txt index 49b8680..a7b1de8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(blt-gp VERSION 0.0.21) +project(blt-gp VERSION 0.0.22) option(ENABLE_ADDRSAN "Enable the address sanitizer" OFF) option(ENABLE_UBSAN "Enable the ub sanitizer" OFF) diff --git a/examples/main.cpp b/examples/main.cpp index e62ae4e..4e2959b 100644 --- a/examples/main.cpp +++ b/examples/main.cpp @@ -78,39 +78,39 @@ void test() { std::vector operations; std::vector values; - + std::stack tree_generator; tree_generator.push(generate_op()); - + while (!tree_generator.empty()) { auto opn = tree_generator.top(); tree_generator.pop(); - + operations.push_back(opn); if (opn == op::LIT) { values.push_back(random_value()); continue; } - + // child 1 if (choice()) tree_generator.push(generate_op()); else tree_generator.push(op::LIT); - + // child 2 if (choice()) tree_generator.push(generate_op()); else tree_generator.push(op::LIT); } - + for (const auto& v : operations) std::cout << to_string(v) << " "; std::cout << std::endl; - + { std::stack process; for (const auto& v : operations) @@ -160,11 +160,11 @@ void test() } std::cout << std::endl; } - + for (const auto& v : values) std::cout << v << " "; std::cout << std::endl; - + { std::stack process; blt::size_t index = 0; @@ -184,7 +184,7 @@ void test() std::cout << values[index++]; break; } - + while (!process.empty()) { auto top = process.top(); @@ -217,13 +217,13 @@ void test() } std::cout << std::endl; } - + std::stack process; std::stack operators; - + for (const auto& v : operations) operators.push(v); - + while (!operators.empty()) { auto oper = operators.top(); @@ -265,10 +265,10 @@ void test() std::cout << "\tresult: " << values.back() << std::endl; } } - + std::cout << process.size() << std::endl; std::cout << process.top() << std::endl; - + } // //struct silly @@ -293,15 +293,21 @@ void test() // unsigned char data[9582]; //}; -blt::gp::stack_allocator alloc; +blt::gp::type_system type_system; +blt::gp::gp_program program(type_system); + +blt::gp::operation_t add([](float a, float b) { return a + b; }); +blt::gp::operation_t sub([](float a, float b) { return a - b; }); +blt::gp::operation_t mul([](float a, float b) { return a * b; }); +blt::gp::operation_t pro_div([](float a, float b) { return b == 0 ? 0.0f : a / b; }); +blt::gp::operation_t lit([]() {return 0.0f;}); -float nyah(float f, int i, bool b) -{ - return f + static_cast(i * b); -} int main() { + type_system.register_type(); + type_system.register_type(); + // constexpr blt::size_t MAX_ALIGNMENT = 8; // test(); // std::cout << alignof(silly) << " " << sizeof(silly) << std::endl; @@ -364,18 +370,18 @@ int main() // alloc.push(550.3f); // alloc.push(20.1230345); // alloc.push(std::string("SillyString")); - alloc.push(33.22f); - alloc.push(120); - alloc.push(true); - - blt::gp::operation_t silly_op(nyah); - blt::gp::operation_t silly_op_2([](float f, float g) { - return f + g; - }); - - std::cout << silly_op(alloc) << std::endl; - - std::cout << "Is empty? " << alloc.empty() << std::endl; +// alloc.push(33.22f); +// alloc.push(120); +// alloc.push(true); +// +// blt::gp::operation_t silly_op(nyah); +// blt::gp::operation_t silly_op_2([](float f, float g) { +// return f + g; +// }); + +// std::cout << silly_op(alloc) << std::endl; +// +// std::cout << "Is empty? " << alloc.empty() << std::endl; // std::cout << std::endl; // diff --git a/include/blt/gp/program.h b/include/blt/gp/program.h index ae3a3ec..3cf3a11 100644 --- a/include/blt/gp/program.h +++ b/include/blt/gp/program.h @@ -19,28 +19,58 @@ #ifndef BLT_GP_PROGRAM_H #define BLT_GP_PROGRAM_H -#include + #include -#include #include -#include -#include -#include -#include #include #include #include #include #include +#include +#include +#include +#include +#include +#include +#include +#include namespace blt::gp { + template + struct integer_type + { + T id; + + integer_type() = default; + + integer_type(T id): id(id) // NOLINT + {} + + inline operator T() const // NOLINT + { + return id; + } + }; + + struct operator_id : integer_type + { + using integer_type::integer_type; + }; + + struct type_id : integer_type + { + using integer_type::integer_type; + }; class type { public: + type() = default; + template - static type make_type(blt::size_t id) + static type make_type(type_id id) { return type(sizeof(T), id, blt::type_string()); } @@ -50,7 +80,7 @@ namespace blt::gp return size_; } - [[nodiscard]] blt::size_t id() const + [[nodiscard]] type_id id() const { return id_; } @@ -61,12 +91,19 @@ namespace blt::gp } private: - type(size_t size, size_t id, std::string_view name): size_(size), id_(id), name_(name) + type(size_t size, type_id id, std::string_view name): size_(size), id_(id), name_(name) {} - blt::size_t size_; - blt::size_t id_; - std::string name_; + blt::size_t size_{}; + type_id id_{}; + std::string name_{}; + }; + + class allowed_types_t + { + public: + + private: }; class type_system @@ -77,7 +114,7 @@ namespace blt::gp template inline type register_type() { - types[blt::type_string_raw()](type::make_type(types.size())); + types.insert({blt::type_string_raw(), type::make_type(types.size())}); return types[blt::type_string_raw()]; } @@ -167,27 +204,24 @@ namespace blt::gp template void add_operator(const operation_t& op) { - if (op.get_argc() == 0) - terminals.push_back(operators.size()); - else - non_terminals.push_back(operators.size()); + auto return_type_id = system.get_type().id(); + auto& operator_list = op.get_argc() == 0 ? terminals : non_terminals; + operator_list[return_type_id].push_back(operators.size()); + auto operator_index = operators.size(); + (argument_types[operator_index].push_back(system.get_type()), ...); operators.push_back(op.make_callable()); - - std::vector types; - (types.push_back(system.get_type()), ...); - arg_types.push_back(std::move(types)); - - return_types.push_back(system.get_type()); } private: type_system system; + blt::gp::stack_allocator alloc; + // indexed from return TYPE ID, returns index of operator + blt::expanding_buffer> terminals; + blt::expanding_buffer> non_terminals; + // indexed from OPERATOR NUMBER + blt::expanding_buffer> argument_types; std::vector> operators; - std::vector terminals; - std::vector non_terminals; - std::vector return_types; - std::vector> arg_types; }; } diff --git a/lib/blt b/lib/blt index 2266d64..9ad9619 160000 --- a/lib/blt +++ b/lib/blt @@ -1 +1 @@ -Subproject commit 2266d64f042bc97b54e7e50dcaac730bcc3ab4c3 +Subproject commit 9ad96191ff91ec7efa6aa142e377201fe81b4c40 diff --git a/src/gp.cpp b/src/gp.cpp index 8c72729..2a53a88 100644 --- a/src/gp.cpp +++ b/src/gp.cpp @@ -14,4 +14,4 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - */ + */ \ No newline at end of file