File size: 19,299 Bytes
2492322 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 | // Copyright (c) 2023, QuantStack and Mamba Contributors
//
// Distributed under the terms of the BSD 3-Clause License.
//
// The full license is in the file LICENSE, distributed with this software.
#ifndef MAMBA_UTIL_FLAT_EXPR_TREE_HPP
#define MAMBA_UTIL_FLAT_EXPR_TREE_HPP
#include <cassert>
#include <functional>
#include <utility>
#include <variant>
#include <vector>
#include "mamba/util/flat_binary_tree.hpp"
namespace mamba::util
{
/**
* A parser for postfix expressions.
*
* The parser creates an expression tree and validate that the expression being pushed
* is a valid postfix expression.
* For example, for the expression ``a + b * c`` on might push ``a b c * +``
* or ``b c * a +``.
*/
template <typename Variable, typename Operator>
class PostfixParser
{
public:
using operator_type = Operator;
using variable_type = Variable;
using tree_type = flat_binary_tree<operator_type, variable_type>;
[[nodiscard]] auto push_variable(const variable_type& var) -> bool;
[[nodiscard]] auto push_variable(variable_type&& var) -> bool;
[[nodiscard]] auto push_operator(const operator_type& op) -> bool;
[[nodiscard]] auto push_operator(operator_type&& op) -> bool;
[[nodiscard]] auto finalize() -> bool;
[[nodiscard]] auto tree() const& -> const tree_type&;
[[nodiscard]] auto tree() && -> tree_type&&;
private:
using idx_type = typename tree_type::idx_type;
using node_idx_stack = std::vector<idx_type>;
/** The expression tree containing the expression being parsed. */
tree_type m_tree = {};
/** Orphan nodes are node without a parent. */
node_idx_stack m_orphans = {};
void orphans_push(idx_type idx);
auto orphans_pop() -> idx_type;
template <typename V>
[[nodiscard]] auto push_variable_impl(V&& var) -> bool;
template <typename O>
[[nodiscard]] auto push_operator_impl(O&& op) -> bool;
};
/**
* A parser for infix expressions.
*
* The parser creates an expression tree and validate that the expression being pushed
* is a valid infix expression.
* For example, the expression ``a + b * c`` can be pushed directly (thanks to the
* operator precedence), or parenthesised as ``a + (b * c)``.
*/
template <typename Variable, typename Operator, typename OperatorCmp = std::less<>>
class InfixParser
{
public:
using operator_type = Operator;
using variable_type = Variable;
using tree_type = flat_binary_tree<operator_type, variable_type>;
using operator_precedence_type = OperatorCmp;
InfixParser(const operator_precedence_type& cmp);
InfixParser(operator_precedence_type&& cmp = {});
[[nodiscard]] auto push_variable(const variable_type& var) -> bool;
[[nodiscard]] auto push_variable(variable_type&& var) -> bool;
[[nodiscard]] auto push_operator(const operator_type& op) -> bool;
[[nodiscard]] auto push_operator(operator_type&& op) -> bool;
[[nodiscard]] auto push_left_parenthesis() -> bool;
[[nodiscard]] auto push_right_parenthesis() -> bool;
[[nodiscard]] auto finalize() -> bool;
[[nodiscard]] auto tree() const& -> const tree_type&;
[[nodiscard]] auto tree() && -> tree_type&&;
private:
using postfix_parser_type = PostfixParser<variable_type, operator_type>;
struct LeftParenthesis
{
};
using operator_or_parenthesis_type = std::variant<operator_type, LeftParenthesis>;
using operator_stack_type = std::vector<operator_or_parenthesis_type>;
postfix_parser_type m_postfix_parser = {};
operator_stack_type m_op_stack = {};
std::size_t m_parenthesis_level = 0;
bool m_expects_op = false;
operator_precedence_type m_op_cmp = {};
template <typename T>
void stack_push(T&& elem);
auto stack_pop() -> operator_or_parenthesis_type;
[[nodiscard]] auto stack_empty() const -> bool;
auto stack_top() const -> const operator_or_parenthesis_type&;
[[nodiscard]] auto stack_top_is_parenthesis() const -> bool;
auto stack_top_is_op_with_greater_precedence_than(const operator_type&) const -> bool;
template <typename V>
[[nodiscard]] auto push_variable_impl(V&& var) -> bool;
template <typename O>
[[nodiscard]] auto push_operator_impl(O&& op) -> bool;
};
enum struct BoolOperator
{
logical_and,
logical_or
};
template <typename Variable>
class flat_bool_expr_tree
{
public:
using self_type = flat_bool_expr_tree<Variable>;
using operator_type = BoolOperator;
using variable_type = Variable;
using tree_type = flat_binary_tree<operator_type, variable_type>;
using size_type = typename tree_type::size_type;
struct LeftParenthesis
{
};
struct RightParenthesis
{
};
flat_bool_expr_tree() = default;
flat_bool_expr_tree(const flat_bool_expr_tree&) = default;
flat_bool_expr_tree(flat_bool_expr_tree&&) = default;
flat_bool_expr_tree(const tree_type& tree);
flat_bool_expr_tree(tree_type&& tree);
auto operator=(const flat_bool_expr_tree&) -> flat_bool_expr_tree& = default;
auto operator=(flat_bool_expr_tree&&) -> flat_bool_expr_tree& = default;
[[nodiscard]] auto size() const -> size_type;
[[nodiscard]] auto empty() const -> bool;
void clear();
void reserve(size_type size);
template <typename UnaryFunc = std::identity>
[[nodiscard]] auto evaluate(UnaryFunc&& var_evaluator = {}, bool empty_val = true) const
-> bool;
template <typename UnaryFunc>
void infix_for_each(UnaryFunc&& func) const;
// TODO(C++20): replace by the `= default` implementation of `operator==`
[[nodiscard]] auto operator==(const self_type& other) const -> bool
{
return m_tree == other.m_tree;
}
[[nodiscard]] auto operator!=(const self_type& other) const -> bool
{
return !(*this == other);
}
private:
using idx_type = typename tree_type::idx_type;
template <typename UnaryFunc>
auto evaluate_impl(UnaryFunc& var_evaluator, idx_type idx) const -> bool;
tree_type m_tree = {};
};
template <typename V>
constexpr auto operator==(
typename flat_bool_expr_tree<V>::LeftParenthesis,
typename flat_bool_expr_tree<V>::LeftParenthesis
) -> bool
{
return true;
}
template <typename V>
constexpr auto operator!=(
typename flat_bool_expr_tree<V>::LeftParenthesis,
typename flat_bool_expr_tree<V>::LeftParenthesis
) -> bool
{
return false;
}
template <typename V>
constexpr auto operator==(
typename flat_bool_expr_tree<V>::RightParenthesis,
typename flat_bool_expr_tree<V>::RightParenthesis
) -> bool
{
return true;
}
template <typename V>
constexpr auto operator!=(
typename flat_bool_expr_tree<V>::RightParenthesis,
typename flat_bool_expr_tree<V>::RightParenthesis
) -> bool
{
return false;
}
/*************************************
* Implementation of PostfixParser *
*************************************/
template <typename V, typename O>
void PostfixParser<V, O>::orphans_push(idx_type idx)
{
return m_orphans.push_back(idx);
}
template <typename V, typename O>
auto PostfixParser<V, O>::orphans_pop() -> idx_type
{
assert(!m_orphans.empty());
auto out = m_orphans.back();
m_orphans.pop_back();
return out;
}
template <typename V, typename O>
template <typename Var>
auto PostfixParser<V, O>::push_variable_impl(Var&& var) -> bool
{
orphans_push(m_tree.add_leaf(std::forward<Var>(var)));
return true; // Always valid
}
template <typename V, typename O>
auto PostfixParser<V, O>::push_variable(const variable_type& var) -> bool
{
return push_variable_impl(var);
}
template <typename V, typename O>
auto PostfixParser<V, O>::push_variable(variable_type&& var) -> bool
{
return push_variable_impl(std::move(var));
}
template <typename V, typename O>
template <typename Op>
auto PostfixParser<V, O>::push_operator_impl(Op&& op) -> bool
{
if (m_orphans.size() < 2)
{
return false;
}
const auto right = orphans_pop();
const auto left = orphans_pop();
orphans_push(m_tree.add_branch(std::forward<Op>(op), left, right));
return true;
}
template <typename V, typename O>
auto PostfixParser<V, O>::push_operator(const operator_type& op) -> bool
{
return push_operator_impl(op);
}
template <typename V, typename O>
auto PostfixParser<V, O>::push_operator(operator_type&& op) -> bool
{
return push_operator_impl(std::move(op));
}
template <typename V, typename O>
auto PostfixParser<V, O>::finalize() -> bool
{
if (((m_orphans.size() == 1) && !m_tree.empty()) || (m_orphans.empty() && m_tree.empty()))
{
return true;
}
return false; // Incomplete expression
}
template <typename V, typename O>
auto PostfixParser<V, O>::tree() const& -> const tree_type&
{
return m_tree;
}
template <typename V, typename O>
auto PostfixParser<V, O>::tree() && -> tree_type&&
{
return std::move(m_tree);
}
/***********************************
* Implementation of InfixParser *
***********************************/
template <typename V, typename O, typename C>
InfixParser<V, O, C>::InfixParser(const operator_precedence_type& cmp)
: m_op_cmp(cmp)
{
}
template <typename V, typename O, typename C>
InfixParser<V, O, C>::InfixParser(operator_precedence_type&& cmp)
: m_op_cmp(std::move(cmp))
{
}
template <typename V, typename O, typename C>
template <typename T>
void InfixParser<V, O, C>::stack_push(T&& elem)
{
m_op_stack.push_back(std::forward<T>(elem));
}
template <typename V, typename O, typename C>
auto InfixParser<V, O, C>::stack_pop() -> operator_or_parenthesis_type
{
assert(!stack_empty());
auto top = stack_top();
m_op_stack.pop_back();
return top;
}
template <typename V, typename O, typename C>
auto InfixParser<V, O, C>::stack_empty() const -> bool
{
return m_op_stack.empty();
}
template <typename V, typename O, typename C>
auto InfixParser<V, O, C>::stack_top() const -> const operator_or_parenthesis_type&
{
assert(!stack_empty());
return m_op_stack.back();
}
template <typename V, typename O, typename C>
auto InfixParser<V, O, C>::stack_top_is_parenthesis() const -> bool
{
return (!stack_empty()) && std::holds_alternative<LeftParenthesis>(stack_top());
}
template <typename V, typename O, typename C>
auto InfixParser<V, O, C>::stack_top_is_op_with_greater_precedence_than(const operator_type& op
) const -> bool
{
if (stack_empty())
{
return false;
}
if (const auto* const op_ptr = std::get_if<operator_type>(&stack_top()))
{
return m_op_cmp(op, *op_ptr);
}
return false;
}
template <typename V, typename O, typename C>
template <typename Var>
auto InfixParser<V, O, C>::push_variable_impl(Var&& var) -> bool
{
// Input check
if (m_expects_op)
{
return false; // Unexpected variable
}
m_expects_op = true;
// Parsing
return m_postfix_parser.push_variable(std::forward<Var>(var));
}
template <typename V, typename O, typename C>
auto InfixParser<V, O, C>::push_variable(const variable_type& var) -> bool
{
return push_variable_impl(var);
}
template <typename V, typename O, typename C>
auto InfixParser<V, O, C>::push_variable(variable_type&& var) -> bool
{
return push_variable_impl(std::move(var));
}
template <typename V, typename O, typename C>
template <typename Op>
auto InfixParser<V, O, C>::push_operator_impl(Op&& op) -> bool
{
// Input check
if (!m_expects_op)
{
return false;
}
m_expects_op = false;
// Parsing
while (stack_top_is_op_with_greater_precedence_than(op))
{
bool pushed = m_postfix_parser.push_operator(std::get<operator_type>(stack_pop()));
if (!pushed)
{
return false;
}
}
stack_push(std::forward<Op>(op));
return true;
}
template <typename V, typename O, typename C>
auto InfixParser<V, O, C>::push_operator(const operator_type& op) -> bool
{
return push_operator_impl(op);
}
template <typename V, typename O, typename C>
auto InfixParser<V, O, C>::push_operator(operator_type&& op) -> bool
{
return push_operator_impl(std::move(op));
}
template <typename V, typename O, typename C>
auto InfixParser<V, O, C>::push_left_parenthesis() -> bool
{
// Input check
if (m_expects_op)
{
return false; // Unexpected left parenthesis
}
++m_parenthesis_level;
// Parsing
stack_push(LeftParenthesis{});
return true;
}
template <typename V, typename O, typename C>
auto InfixParser<V, O, C>::push_right_parenthesis() -> bool
{
// Input check
if (!m_expects_op || (m_parenthesis_level == 0))
{
return false; // Unexpected right parenthesis
}
--m_parenthesis_level;
// Parsing
while (!stack_top_is_parenthesis())
{
assert(!stack_empty());
bool pushed = m_postfix_parser.push_operator(std::get<operator_type>(stack_pop()));
if (!pushed)
{
return false;
}
}
assert(stack_top_is_parenthesis());
stack_pop();
return true;
}
template <typename V, typename O, typename C>
auto InfixParser<V, O, C>::finalize() -> bool
{
// Empty expression case
if (m_postfix_parser.tree().empty() && stack_empty())
{
return true;
}
// Input check
if (!m_expects_op || (m_parenthesis_level != 0))
{
return false; // Invalid expression
}
// Parsing
while (!stack_empty())
{
assert(!stack_top_is_parenthesis());
bool pushed = m_postfix_parser.push_operator(std::get<operator_type>(stack_pop()));
if (!pushed)
{
return false;
}
}
return m_postfix_parser.finalize();
}
template <typename V, typename O, typename C>
auto InfixParser<V, O, C>::tree() const& -> const tree_type&
{
return m_postfix_parser.tree();
}
template <typename V, typename O, typename C>
auto InfixParser<V, O, C>::tree() && -> tree_type&&
{
return std::move(m_postfix_parser).tree();
}
/*******************************************
* Implementation of flat_bool_expr_tree *
*******************************************/
template <typename V>
flat_bool_expr_tree<V>::flat_bool_expr_tree(const tree_type& tree)
: m_tree(tree)
{
}
template <typename V>
flat_bool_expr_tree<V>::flat_bool_expr_tree(tree_type&& tree)
: m_tree(std::move(tree))
{
}
template <typename V>
auto flat_bool_expr_tree<V>::size() const -> size_type
{
return m_tree.size();
}
template <typename V>
auto flat_bool_expr_tree<V>::empty() const -> bool
{
return m_tree.empty();
}
template <typename V>
void flat_bool_expr_tree<V>::clear()
{
return m_tree.clear();
}
template <typename V>
void flat_bool_expr_tree<V>::reserve(size_type size)
{
return m_tree.reserve(size);
}
template <typename V>
template <typename UnaryFunc>
auto flat_bool_expr_tree<V>::evaluate(UnaryFunc&& var_evaluator, bool empty_val) const -> bool
{
if (m_tree.empty())
{
return empty_val;
}
return evaluate_impl(var_evaluator, m_tree.root());
}
template <typename V>
template <typename UnaryFunc>
auto flat_bool_expr_tree<V>::evaluate_impl(UnaryFunc& var_eval, idx_type idx) const -> bool
{
// We do a tree evaluation rather than a stack-based postfix evaluation to
// avoid evaluation sub trees thanks to operator && and || short circuiting.
assert(idx < m_tree.size());
if (m_tree.is_leaf(idx))
{
return var_eval(m_tree.leaf(idx));
}
if ((m_tree.branch(idx) == BoolOperator::logical_and))
{
return evaluate_impl(var_eval, m_tree.left(idx))
&& evaluate_impl(var_eval, m_tree.right(idx));
}
else // BoolOperator::logical_or
{
return evaluate_impl(var_eval, m_tree.left(idx))
|| evaluate_impl(var_eval, m_tree.right(idx));
}
}
template <typename V>
template <typename UnaryFunc>
void flat_bool_expr_tree<V>::infix_for_each(UnaryFunc&& func) const
{
struct TreeVisitor
{
using idx_type = typename tree_type::idx_type;
void on_leaf(const tree_type& tree, idx_type idx)
{
m_func(tree.leaf(idx));
}
void on_branch_left_before(const tree_type& tree, idx_type, idx_type left_idx)
{
if (!tree.is_leaf(left_idx))
{
m_func(LeftParenthesis{});
}
}
void
on_branch_infix(const tree_type& tree, idx_type branch_idx, idx_type left_idx, idx_type right_idx)
{
if (!tree.is_leaf(left_idx))
{
m_func(RightParenthesis{});
}
m_func(tree.branch(branch_idx));
if (!tree.is_leaf(right_idx))
{
m_func(LeftParenthesis{});
}
}
void on_branch_right_after(const tree_type& tree, idx_type, idx_type right_idx)
{
if (!tree.is_leaf(right_idx))
{
m_func(RightParenthesis{});
}
}
UnaryFunc m_func;
} tree_visitor{ std::forward<UnaryFunc>(func) };
m_tree.dfs_raw(tree_visitor, m_tree.root());
}
}
#endif
|