-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This introduces Conditional expression constructs. Issue #67
- Loading branch information
Showing
7 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright (C) 2017 Olzhas Rakhimov | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
/// @file conditional.cc | ||
/// Implementation of conditional expressions. | ||
|
||
#include "conditional.h" | ||
|
||
#include <algorithm> | ||
|
||
namespace scram { | ||
namespace mef { | ||
|
||
Interval Ite::interval() noexcept { | ||
assert(args().size() == 3); | ||
Interval then_interval = args()[1]->interval(); | ||
Interval else_interval = args()[2]->interval(); | ||
return Interval::closed( | ||
std::min(then_interval.lower(), else_interval.lower()), | ||
std::max(then_interval.upper(), else_interval.upper())); | ||
} | ||
|
||
} // namespace mef | ||
} // namespace scram |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright (C) 2017 Olzhas Rakhimov | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
/// @file conditional.h | ||
/// Conditional (if-then-else, switch-case) expressions. | ||
|
||
#ifndef SCRAM_SRC_EXPRESSION_CONDITIONAL_H_ | ||
#define SCRAM_SRC_EXPRESSION_CONDITIONAL_H_ | ||
|
||
#include "src/expression.h" | ||
|
||
namespace scram { | ||
namespace mef { | ||
|
||
/// If-Then-Else ternary expression. | ||
class Ite : public ExpressionFormula<Ite> { | ||
public: | ||
/// @param[in] condition The Boolean expression to be tested. | ||
/// @param[in] then_arm The expression if the Boolean is true. | ||
/// @param[in] else_arm The expression if the Boolean is false. | ||
Ite(Expression* condition, Expression* then_arm, Expression* else_arm) | ||
: ExpressionFormula<Ite>({condition, then_arm, else_arm}) {} | ||
|
||
Interval interval() noexcept override; | ||
|
||
/// Computes the if-then-else expression with the given evaluator. | ||
template <typename F> | ||
double Compute(F&& eval) noexcept { | ||
assert(args().size() == 3); | ||
return eval(args()[0]) ? eval(args()[1]) : eval(args()[2]); | ||
} | ||
}; | ||
|
||
} // namespace mef | ||
} // namespace scram | ||
|
||
#endif // SCRAM_SRC_EXPRESSION_CONDITIONAL_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters