Skip to content
Snippets Groups Projects
Commit 2b838d4b authored by Adam Wardynski's avatar Adam Wardynski
Browse files

Refactoring the distinction of Equals for Position.

The Equals<T> now uses helper class EqualityComparer<T> and distinction is in the helper class specialisation.
parent 1677fd4c
Branches
No related tags found
No related merge requests found
...@@ -9,57 +9,50 @@ ...@@ -9,57 +9,50 @@
namespace Wccl { namespace Wccl {
/** /**
* Predicate that checks for equality of values * Helper template class to factor out equality comparison
* (namely for special treatment of Position). By default,
* T::equals(const T& other) is used.
*/ */
template <class T> template <class T>
class Equals : public Predicate { class EqualityComparer
{
public: public:
typedef boost::shared_ptr<Function<T> > ArgFunctionPtr; static boost::shared_ptr<const Value> apply(
const T& arg1,
Equals(const ArgFunctionPtr& arg1_expr, const ArgFunctionPtr& arg2_expr) const T& arg2,
: arg1_expr_(arg1_expr), arg2_expr_(arg2_expr) const FunExecContext& context)
{ {
BOOST_ASSERT(arg1_expr_); return Predicate::evaluate(arg1.equals(arg2), context);
BOOST_ASSERT(arg2_expr_);
}
virtual std::string to_string(const Corpus2::Tagset& tagset) const {
return BinaryFunctionFormatter::to_string(tagset, *this, *arg1_expr_, *arg2_expr_);
}
virtual std::string to_raw_string() const {
return BinaryFunctionFormatter::to_raw_string(*this, *arg1_expr_, *arg2_expr_);
} }
};
virtual const std::string raw_operator_name() const {
return "equals";
}
protected:
const ArgFunctionPtr arg1_expr_;
const ArgFunctionPtr arg2_expr_;
/** /**
* Take values of arguments from expressions and return True if they are equal, * Helper template class to factor out equality comparison.
* False otherwise. * This is specialization for Position that uses
* Position::equals(const Position& other, const SentenceContext& context)
*/ */
virtual BaseRetValPtr apply_internal(const FunExecContext& context) const { template <>
const boost::shared_ptr<const T>& arg1 = this->arg1_expr_->apply(context); class EqualityComparer<Position>
const boost::shared_ptr<const T>& arg2 = this->arg2_expr_->apply(context); {
if(arg1->equals(*arg2)) { public:
return Predicate::True(context); static boost::shared_ptr<const Value> apply(
} const Position& arg1,
return Predicate::False(context); const Position& arg2,
const FunExecContext& context)
{
return Predicate::evaluate(
arg1.equals(arg2, context.sentence_context()),
context);
} }
}; };
/** /**
* Predicate that checks for equality of Positions, given sentence context * Predicate that checks for equality of values
*/ */
template <> template <class T>
class Equals<Position> : public Predicate { class Equals : public Predicate {
public: public:
typedef boost::shared_ptr<Function<Position> > ArgFunctionPtr; typedef boost::shared_ptr<Function<T> > ArgFunctionPtr;
Equals(const ArgFunctionPtr& arg1_expr, const ArgFunctionPtr& arg2_expr) Equals(const ArgFunctionPtr& arg1_expr, const ArgFunctionPtr& arg2_expr)
: arg1_expr_(arg1_expr), arg2_expr_(arg2_expr) : arg1_expr_(arg1_expr), arg2_expr_(arg2_expr)
...@@ -84,19 +77,15 @@ protected: ...@@ -84,19 +77,15 @@ protected:
const ArgFunctionPtr arg1_expr_; const ArgFunctionPtr arg1_expr_;
const ArgFunctionPtr arg2_expr_; const ArgFunctionPtr arg2_expr_;
typedef FunctionBase::BaseRetValPtr BaseRetValPtr;
/** /**
* Take values of arguments from expressions and return True if Positions * Take values of arguments from expressions and return True if they are equal,
* are equal, in given context, False otherwise. * False otherwise.
*/ */
virtual BaseRetValPtr apply_internal(const FunExecContext& context) const { virtual BaseRetValPtr apply_internal(const FunExecContext& context) const {
const boost::shared_ptr<const Position>& arg1 = this->arg1_expr_->apply(context); return EqualityComparer<T>::apply(
const boost::shared_ptr<const Position>& arg2 = this->arg2_expr_->apply(context); *(this->arg1_expr_->apply(context)),
if(arg1->equals(*arg2, context.sentence_context())) { *(this->arg2_expr_->apply(context)),
return Predicate::True(context); context);
}
return Predicate::False(context);
} }
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment