Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
HashVisitor.cpp
Go to the documentation of this file.
2
3#include <boost/functional/hash.hpp>
4
6
7namespace storm {
8namespace expressions {
9
10namespace {
11
12// A distinct seed for every expression type, so that two expressions of different types with equal
13// operator and operands do not collide. Collisions would be harmless for correctness, but avoiding
14// the obvious ones keeps the distribution reasonable.
15std::size_t const TypeIfThenElse = 0xa1;
16std::size_t const TypeBinaryBooleanFunction = 0xb2;
17std::size_t const TypeBinaryNumericalFunction = 0xb3;
18std::size_t const TypeBinaryRelation = 0xb4;
19std::size_t const TypeVariable = 0xc5;
20std::size_t const TypeUnaryBooleanFunction = 0xd6;
21std::size_t const TypeUnaryNumericalFunction = 0xd7;
22std::size_t const TypeBooleanLiteral = 0xe8;
23std::size_t const TypeIntegerLiteral = 0xe9;
24std::size_t const TypeRationalLiteral = 0xea;
25std::size_t const TypePredicate = 0xfb;
26
27template<typename T>
28std::size_t hashValue(T const& value) {
29 return boost::hash<T>{}(value);
30}
31
32} // namespace
33
34std::size_t HashVisitor::hash(storm::expressions::Expression const& expression) {
35 return boost::any_cast<std::size_t>(expression.accept(*this, boost::any{}));
36}
37
38std::size_t HashVisitor::hashExpression(BaseExpression const& expression) {
39 return boost::any_cast<std::size_t>(expression.accept(*this, boost::any{}));
40}
41
42boost::any HashVisitor::visit(IfThenElseExpression const& expression, boost::any const& data) {
43 std::size_t result = TypeIfThenElse;
44 boost::hash_combine(result, hashExpression(*expression.getCondition()));
45 boost::hash_combine(result, hashExpression(*expression.getThenExpression()));
46 boost::hash_combine(result, hashExpression(*expression.getElseExpression()));
47 return result;
48}
49
50boost::any HashVisitor::visit(BinaryBooleanFunctionExpression const& expression, boost::any const& data) {
51 std::size_t result = TypeBinaryBooleanFunction;
52 boost::hash_combine(result, hashValue(expression.getOperatorType()));
53 boost::hash_combine(result, hashExpression(*expression.getFirstOperand()));
54 boost::hash_combine(result, hashExpression(*expression.getSecondOperand()));
55 return result;
56}
57
58boost::any HashVisitor::visit(BinaryNumericalFunctionExpression const& expression, boost::any const& data) {
59 std::size_t result = TypeBinaryNumericalFunction;
60 boost::hash_combine(result, hashValue(expression.getOperatorType()));
61 boost::hash_combine(result, hashExpression(*expression.getFirstOperand()));
62 boost::hash_combine(result, hashExpression(*expression.getSecondOperand()));
63 return result;
64}
65
66boost::any HashVisitor::visit(BinaryRelationExpression const& expression, boost::any const& data) {
67 std::size_t result = TypeBinaryRelation;
68 boost::hash_combine(result, hashValue(expression.getRelationType()));
69 boost::hash_combine(result, hashExpression(*expression.getFirstOperand()));
70 boost::hash_combine(result, hashExpression(*expression.getSecondOperand()));
71 return result;
72}
73
74boost::any HashVisitor::visit(VariableExpression const& expression, boost::any const& data) {
75 std::size_t result = TypeVariable;
76 // The variable's index identifies it within its manager, which is all that the syntactical
77 // equality check uses as well.
78 boost::hash_combine(result, hashValue(expression.getVariable().getIndex()));
79 return result;
80}
81
82boost::any HashVisitor::visit(UnaryBooleanFunctionExpression const& expression, boost::any const& data) {
83 std::size_t result = TypeUnaryBooleanFunction;
84 boost::hash_combine(result, hashValue(expression.getOperatorType()));
85 boost::hash_combine(result, hashExpression(*expression.getOperand()));
86 return result;
87}
88
89boost::any HashVisitor::visit(UnaryNumericalFunctionExpression const& expression, boost::any const& data) {
90 std::size_t result = TypeUnaryNumericalFunction;
91 boost::hash_combine(result, hashValue(expression.getOperatorType()));
92 boost::hash_combine(result, hashExpression(*expression.getOperand()));
93 return result;
94}
95
96boost::any HashVisitor::visit(BooleanLiteralExpression const& expression, boost::any const& data) {
97 std::size_t result = TypeBooleanLiteral;
98 boost::hash_combine(result, hashValue(expression.getValue()));
99 return result;
100}
101
102boost::any HashVisitor::visit(IntegerLiteralExpression const& expression, boost::any const& data) {
103 std::size_t result = TypeIntegerLiteral;
104 boost::hash_combine(result, hashValue(expression.getValue()));
105 return result;
106}
107
108boost::any HashVisitor::visit(RationalLiteralExpression const& expression, boost::any const& data) {
109 std::size_t result = TypeRationalLiteral;
110 boost::hash_combine(result, hashValue(expression.getValue()));
111 return result;
112}
113
114boost::any HashVisitor::visit(PredicateExpression const& expression, boost::any const& data) {
115 std::size_t result = TypePredicate;
116 boost::hash_combine(result, hashValue(expression.getPredicateType()));
117 for (uint_fast64_t i = 0; i < expression.getArity(); ++i) {
118 boost::hash_combine(result, hashExpression(*expression.getOperand(i)));
119 }
120 return result;
121}
122
123} // namespace expressions
124} // namespace storm
The base class of all expression classes.
virtual boost::any accept(ExpressionVisitor &visitor, boost::any const &data) const =0
Accepts the given visitor by calling its visit method.
OperatorType getOperatorType() const
Retrieves the operator associated with the expression.
std::shared_ptr< BaseExpression const > const & getSecondOperand() const
Retrieves the second operand of the expression.
std::shared_ptr< BaseExpression const > const & getFirstOperand() const
Retrieves the first operand of the expression.
OperatorType getOperatorType() const
Retrieves the operator associated with the expression.
RelationType getRelationType() const
Retrieves the relation associated with the expression.
bool getValue() const
Retrieves the value of the boolean literal.
boost::any accept(ExpressionVisitor &visitor, boost::any const &data) const
Accepts the given visitor.
virtual boost::any visit(IfThenElseExpression const &expression, boost::any const &data) override
std::size_t hash(storm::expressions::Expression const &expression)
Computes the hash of the given expression.
std::shared_ptr< BaseExpression const > getElseExpression() const
Retrieves the else expression of the if-then-else expression.
std::shared_ptr< BaseExpression const > getCondition() const
Retrieves the condition expression of the if-then-else expression.
std::shared_ptr< BaseExpression const > getThenExpression() const
Retrieves the then expression of the if-then-else expression.
int_fast64_t getValue() const
Retrieves the value of the integer literal.
The base class of all binary expressions.
virtual std::shared_ptr< BaseExpression const > getOperand(uint_fast64_t operandIndex) const override
Retrieves the given operand from the expression.
PredicateType getPredicateType() const
Retrieves the relation associated with the expression.
virtual uint_fast64_t getArity() const override
Returns the arity of the expression.
storm::RationalNumber getValue() const
Retrieves the value of the double literal.
OperatorType getOperatorType() const
Retrieves the operator associated with this expression.
virtual std::shared_ptr< BaseExpression const > getOperand(uint_fast64_t operandIndex) const override
Retrieves the given operand from the expression.
OperatorType getOperatorType() const
Retrieves the operator associated with this expression.
Variable const & getVariable() const
Retrieves the variable associated with this expression.
uint_fast64_t getIndex() const
Retrieves the index of the variable.
Definition Variable.cpp:38