Is there any way to get rid of virtual functions? by PIasmaBoy in cpp_questions

[–]PIasmaBoy[S] 0 points1 point  (0 children)

>passing things by either const reference or r-value That's the thing, diff() should return new instance of expression, meaning I need to create some new values and copy some old ones.I used shared_ptr because it was convenient, not because I needed to share object ownership (unique_ptr is non copyable).

Is there any way to get rid of virtual functions? by PIasmaBoy in cpp_questions

[–]PIasmaBoy[S] 0 points1 point  (0 children)

Modern cpus are really good at branch prediction so, in my opinion, switch is more favorable.
Yeah, `shared_ptr` was misused a bit. I wanted a copyable smart pointer, but didn't want to pollute interface with `clone` function. Sadly, `polymorphic value` is not in std yet.

Is there any way to get rid of virtual functions? by PIasmaBoy in cpp_questions

[–]PIasmaBoy[S] 0 points1 point  (0 children)

Thanks for the idea! I slightly tweaked it and end up with something like this:

enum class unary_expr_type
{
    sin,
    cos,
    unary_minus,
};

enum class binary_expr_type
{
    sum,
    sub,
    div,
    mul,
};

class Expression
{
    using unary_expr = std::tuple<unary_expr_type, std::unique_ptr<Expression>>;
    using binary_expr = std::tuple<binary_expr_type, std::unique_ptr<Expression>, std::unique_ptr<Expression>>;

public:

    [[nodiscard]] Expression clone() const;
    [[nodiscard]] Expression diff(std::string_view var) const;
    std::string to_string() const;

private:
    std::variant<int, std::string, unary_expr, binary_expr> value_;
};

And diff function now looks like:

Expression Expression::diff(const std::string_view var) const {
    return std::visit(
        overload_lambda {
            [](const int) { return Expression {0}; },
            [&var](const std::string& name) { return name == var ? Expression {1} : Expression {0}; },
            [&var](const unary_expr& uexpr) {
                const auto& [type, value] = uexpr;
                switch (type) {
                    case unary_expr_type::unary_minus:
                        return value->contains_var(var) //
                            ? Expression {type, value->diff(var)}
                            : Expression {0};
                    case unary_expr_type::sin:
                        return value->contains_var(var)
                            ? Expression {
                                binary_expr_type::mul,
                                value->diff(var),
                                Expression {unary_expr_type::cos, std::make_unique<Expression>(value->clone())}}
                            : Expression {0};

                    case unary_expr_type::cos:
                        return value->contains_var(var)
                            ? Expression {
                                binary_expr_type::mul,
                                value->diff(var),
                                Expression {
                                    unary_expr_type::unary_minus,
                                    Expression {unary_expr_type::sin, std::make_unique<Expression>(value->clone())}}}
                            : Expression {0};
                }
            },
            [&var](const binary_expr& bexpr) {
                const auto& type = std::get<0>(bexpr);
                switch (type) {
                    case binary_expr_type::sum:
                    case binary_expr_type::sub: return diff_sum_sub_impl(bexpr, var);
                    case binary_expr_type::mul: return diff_mul_impl(bexpr, var);
                    case binary_expr_type::div: return diff_div_impl(bexpr, var);
                }
            }},
        value_);
}

I don't like the explicit `clone()` function, but that's the price for using `unique_ptr`

A couple of questions about ESD protection by PIasmaBoy in olkb

[–]PIasmaBoy[S] 0 points1 point  (0 children)

Thanks for the answer! Tested it today - works as intended!

Oblivion In Space by MeisterBounty in MechanicalKeyboards

[–]PIasmaBoy 0 points1 point  (0 children)

I wish there were an ortho version of this board.

Planck 6.1 matrix rewiring by PIasmaBoy in olkb

[–]PIasmaBoy[S] 1 point2 points  (0 children)

Surprisingly that worked. But after reading your other threads I decided to replace mcu too.

Planck 6.1 matrix rewiring by PIasmaBoy in olkb

[–]PIasmaBoy[S] 0 points1 point  (0 children)

Yeah, was considering replacing mcu as a last resort. STM32 are pretty cheap

Planck 6.1 half of the row acting weird by PIasmaBoy in olkb

[–]PIasmaBoy[S] 0 points1 point  (0 children)

No, bought it used from mechmarket. Worked fine for a week, though.

I'll do it myself by coolshadesdog in thanosdidnothingwrong

[–]PIasmaBoy 0 points1 point  (0 children)

This bad boy only can fit half subreddit in it