Refactored normalization because of recent changes to the pointer usage.

This commit is contained in:
2016-09-04 19:29:05 +02:00
parent 7aa20a5820
commit 6aaf7c039d
30 changed files with 75 additions and 124 deletions

View File

@@ -107,12 +107,8 @@ class Expression
virtual Type expressionType() const = 0;
// Normalizes the expression dependent on its type, recursing through all child expressions
//
// Result:
// * a new expression pointer to replace this one if required; this object is then empty
// * nullptr otherwise; the object may or may not have changed
virtual ExpressionPointer normalize() = 0;
virtual ExpressionPointer normalized();
ExpressionPointer negated();
private:
friend void intrusive_ptr_add_ref(Expression *expression);

View File

@@ -37,7 +37,7 @@ class At: public ExpressionCRTP<At>
void setArgument(ExpressionPointer argument);
ExpressionPointer argument() const;
ExpressionPointer normalize() override;
ExpressionPointer normalized() override;
protected:
size_t m_timePoint;

View File

@@ -31,7 +31,7 @@ class Binary: public ExpressionCRTP<Derived>
void setArgument(size_t i, ExpressionPointer argument);
const std::array<ExpressionPointer, 2> &arguments() const;
ExpressionPointer normalize() override;
ExpressionPointer normalized() override;
protected:
std::array<ExpressionPointer, 2> m_arguments;
@@ -72,7 +72,7 @@ boost::intrusive_ptr<Derived> Binary<Derived>::parse(Context &context,
template<class Derived>
void Binary<Derived>::setArgument(size_t i, ExpressionPointer expression)
{
BOOST_ASSERT_MSG(i <= 2, "Index out of range");
BOOST_ASSERT_MSG(i <= m_arguments.size(), "Index out of range");
m_arguments[i] = expression;
}
@@ -88,22 +88,16 @@ const std::array<ExpressionPointer, 2> &Binary<Derived>::arguments() const
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline ExpressionPointer Binary<Derived>::normalize()
inline ExpressionPointer Binary<Derived>::normalized()
{
for (size_t i = 0; i < m_arguments.size(); i++)
{
BOOST_ASSERT(m_arguments[i]);
auto normalizedArgument = m_arguments[i]->normalize();
// Replace argument if changed by normalization
if (!normalizedArgument)
continue;
m_arguments[i] = std::move(normalizedArgument);
m_arguments[i] = m_arguments[i]->normalized();
}
return nullptr;
return this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@@ -35,8 +35,6 @@ class Constant: public ExpressionCRTP<Constant>
const std::string &name() const;
PrimitiveTypePointer type() const;
ExpressionPointer normalize() override;
private:
static ConstantPointer parseDeclaration(Context &context);
static void parseTypedDeclaration(Context &context, Domain &domain, Constants &constants);

View File

@@ -23,7 +23,7 @@ class Dummy: public ExpressionCRTP<Dummy>
bool isNormalized() const;
ExpressionPointer normalize() override;
ExpressionPointer normalized() override;
private:
bool m_isNormalized = false;

View File

@@ -22,9 +22,6 @@ class Either: public NAry<Either>
static const Expression::Type ExpressionType = Expression::Type::Either;
static const std::string Identifier;
public:
ExpressionPointer normalize() override;
};
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@@ -24,7 +24,7 @@ class Imply: public Binary<Imply>
static const std::string Identifier;
public:
ExpressionPointer normalize() override;
ExpressionPointer normalized() override;
};
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@@ -28,10 +28,11 @@ class NAry: public ExpressionCRTP<Derived>
ExpressionContext &expressionContext, ExpressionParser parseExpression);
public:
void setArgument(size_t i, ExpressionPointer argument);
void addArgument(ExpressionPointer argument);
const Expressions &arguments() const;
ExpressionPointer normalize() override;
ExpressionPointer normalized() override;
protected:
Expressions m_arguments;
@@ -78,6 +79,16 @@ boost::intrusive_ptr<Derived> NAry<Derived>::parse(Context &context,
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
void NAry<Derived>::setArgument(size_t i, ExpressionPointer expression)
{
BOOST_ASSERT_MSG(i <= m_arguments.size(), "Index out of range");
m_arguments[i] = expression;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
void NAry<Derived>::addArgument(ExpressionPointer argument)
{
@@ -98,22 +109,16 @@ const Expressions &NAry<Derived>::arguments() const
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline ExpressionPointer NAry<Derived>::normalize()
inline ExpressionPointer NAry<Derived>::normalized()
{
for (size_t i = 0; i < m_arguments.size(); i++)
{
BOOST_ASSERT(m_arguments[i]);
auto normalizedArgument = m_arguments[i]->normalize();
// Replace argument if changed by normalization
if (!normalizedArgument)
continue;
m_arguments[i] = std::move(normalizedArgument);
m_arguments[i] = m_arguments[i]->normalized();
}
return nullptr;
return this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@@ -32,7 +32,7 @@ class Not: public ExpressionCRTP<Not>
void setArgument(ExpressionPointer argument);
ExpressionPointer argument() const;
ExpressionPointer normalize() override;
ExpressionPointer normalized() override;
protected:
ExpressionPointer m_argument;

View File

@@ -30,8 +30,6 @@ class Predicate: public ExpressionCRTP<Predicate>
bool isDeclared() const;
ExpressionPointer normalize() override;
private:
Predicate();

View File

@@ -29,7 +29,7 @@ class PredicateDeclaration: public ExpressionCRTP<PredicateDeclaration>
bool isDeclared() const;
ExpressionPointer normalize() override;
void normalizeParameterNames();
private:
PredicateDeclaration();

View File

@@ -35,8 +35,6 @@ class PrimitiveType: public ExpressionCRTP<PrimitiveType>
const std::string &name() const;
const PrimitiveTypes &parentTypes() const;
ExpressionPointer normalize() override;
private:
void setDirty(bool isDirty = true);
bool isDirty() const;

View File

@@ -27,8 +27,6 @@ class Unsupported: public ExpressionCRTP<Unsupported>
public:
const std::string &type() const;
ExpressionPointer normalize() override;
private:
std::string m_type;
};

View File

@@ -37,8 +37,6 @@ class Variable: public ExpressionCRTP<Variable>
void setDirty(bool isDirty = true);
bool isDirty() const;
ExpressionPointer normalize() override;
private:
static void parseDeclaration(Context &context, Variables &parameters);