diff --git a/CMakeLists.txt b/CMakeLists.txt index e09c673..9854000 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(blt-gp VERSION 0.0.6) +project(blt-gp VERSION 0.0.7) 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 56da706..a501c71 100644 --- a/examples/main.cpp +++ b/examples/main.cpp @@ -17,11 +17,266 @@ */ #include #include +#include +#include +#include +#include +#include + +// small scale +enum class op +{ + ADD, + SUB, + MUL, + DIV, + LIT +}; + +std::string to_string(op o) +{ + switch (o) + { + case op::ADD: + return "ADD"; + case op::SUB: + return "SUB"; + case op::MUL: + return "MUL"; + case op::DIV: + return "DIV"; + case op::LIT: + return "LIT"; + } + return ""; +} + +constexpr static long SEED = 41912; + +op generate_op() +{ + static std::mt19937_64 engine(SEED); + static std::uniform_int_distribution dist(0, static_cast(op::LIT) - 1); + return static_cast(dist(engine)); +} + +bool choice() +{ + static std::mt19937_64 engine(SEED); + static std::uniform_int_distribution dist(0, 1); + return dist(engine); +} + +float random_value() +{ + static std::mt19937_64 engine(SEED); + static std::uniform_real_distribution dist(0.0f, 10.0f); + return dist(engine); +} + +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) + { + switch (v) + { + case op::ADD: + case op::SUB: + case op::MUL: + case op::DIV: + process.emplace(2); + std::cout << "("; + break; + case op::LIT: + break; + } + std::cout << to_string(v); + while (!process.empty()) + { + auto top = process.top(); + process.pop(); + if (top == 0) + { + std::cout << ")"; + continue; + } else + { + std::cout << " "; + process.push(top - 1); + break; + } + } + } + while (!process.empty()) + { + auto top = process.top(); + process.pop(); + if (top == 0) + { + std::cout << ") "; + continue; + } else + { + std::cerr << "FUCK YOU\n"; + break; + } + } + std::cout << std::endl; + } + + for (const auto& v : values) + std::cout << v << " "; + std::cout << std::endl; + + { + std::stack process; + blt::size_t index = 0; + for (const auto& v : operations) + { + switch (v) + { + case op::ADD: + case op::SUB: + case op::MUL: + case op::DIV: + process.emplace(2); + std::cout << "("; + std::cout << to_string(v); + break; + case op::LIT: + std::cout << values[index++]; + break; + } + + while (!process.empty()) + { + auto top = process.top(); + process.pop(); + if (top == 0) + { + std::cout << ")"; + continue; + } else + { + std::cout << " "; + process.push(top - 1); + break; + } + } + } + while (!process.empty()) + { + auto top = process.top(); + process.pop(); + if (top == 0) + { + std::cout << ") "; + continue; + } else + { + std::cerr << "FUCK YOU\n"; + break; + } + } + 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(); + operators.pop(); + if (oper == op::LIT) + { + process.push(values.back()); + values.pop_back(); + } else + { + auto v1 = process.top(); + process.pop(); + auto v2 = process.top(); + process.pop(); + std::cout << "processing oper " << to_string(oper) << " with values " << v1 << " " << v2 << std::endl; + switch (oper) + { + case op::ADD: + values.push_back(v1 + v2); + operators.push(op::LIT); + break; + case op::SUB: + values.push_back(v1 - v2); + operators.push(op::LIT); + break; + case op::MUL: + values.push_back(v1 * v2); + operators.push(op::LIT); + break; + case op::DIV: + if (v2 == 0) + v2 = 1; + values.push_back(v1 / v2); + operators.push(op::LIT); + break; + case op::LIT: + break; + } + std::cout << "\tresult: " << values.back() << std::endl; + } + } + + std::cout << process.size() << std::endl; + std::cout << process.top() << std::endl; + +} int main() { + test(); + blt::gp::operation silly([](float f, int i, bool b) -> float { - return static_cast(b); + return static_cast(f); }); float f = 10.5;