// I'm certain this function could be cleaned up, but it will require some // serious brain storming, so it is a work in progress.... // // The idea is I want to generate all possible permutations of RPN stacks given // a set of 4 input integers. void perms(std::set<int> possible, std::set<int>& all, std::stack<rpn_item> e, std::vector<rpn_item> const& ops) { // Once the stack is finished evaluate and add to the set if (e.size() == 7) { auto test = eval_rpn_stack(e); if (test > 0 && test != BAD_DIVIDE) all.insert(test); return; } // The very bottom of the stack MUST be an operator if (e.size() == 0) { for (auto i : ops) { auto tmp = e; tmp.push(i); perms(possible, all, tmp, ops); } } // the TOP 2 items in the stack MUST be integers if (e.size() >= 5) { for (auto i : possible) { auto tmp = possible; auto tmp_e = e; tmp.erase(i); tmp_e.push(i); perms(tmp, all, tmp_e, ops); } } // This is where things get messy... // I enumerate all possible stack configurations // There is no visible pattern that I can see and easily program so for now // this will remain a mess for (auto n1 = possible.begin(); n1 != possible.end(); ++n1) { for (auto n2 = std::next(n1); n2 != possible.end(); ++n2) { auto tmp_p = possible; tmp_p.erase(*n1); tmp_p.erase(*n2); for (auto op1 = ops.begin(); op1 != ops.end(); ++op1) { for (auto op2 = ops.begin(); op2 != ops.end(); ++op2) { // op op n n auto n = push_helper(e, {*op1, *op2, *n1, *n2}); perms(tmp_p, all, n, ops); // op n op n n = push_helper(e, {*op1, *n1, *op2, *n2}); perms(tmp_p, all, n, ops); // n op n op n = push_helper(e, {*n1, *op1, *n2, *op2}); perms(tmp_p, all, n, ops); // op n n op n = push_helper(e, {*op1, *n1, *n2, *op2}); perms(tmp_p, all, n, ops); // n op op n n = push_helper(e, {*n1, *op1, *op2, *n2}); perms(tmp_p, all, n, ops); } } } } }
void push(THandlers&... handlers) { osmium::item_type type = osmium::item_type::undefined; while (osmium::memory::Buffer buffer = read()) { type = push_helper(buffer, type, handlers...); delete[] buffer.data(); } push_helper(type, handlers...); }
void push_helper(osmium::item_type type, THead& handler, TTail&... more) { handler(type); push_helper(type, more...); }
osmium::item_type push_helper(osmium::memory::Buffer& buffer, osmium::item_type type, THead& handler, TTail&... more) { handler(buffer, type); return push_helper(buffer, type, more...); }