From: Mikko Rasa Date: Fri, 17 Aug 2018 17:26:37 +0000 (+0300) Subject: Add equality comparison for conditions X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=7d7eaf754af04f81bb1e7a8872272ea15e01d0c4;p=poefilter.git Add equality comparison for conditions --- diff --git a/source/choicecondition.h b/source/choicecondition.h index 9792f64..5673d3f 100644 --- a/source/choicecondition.h +++ b/source/choicecondition.h @@ -20,6 +20,7 @@ public: ChoiceCondition(const std::vector &); virtual ChoiceCondition *clone() const; + virtual bool equals(const Condition &) const; virtual bool can_merge(const Condition &, const CompoundCondition &) const; virtual ChoiceCondition *merge(const std::vector &, const CompoundCondition &) const; virtual void add_lines(std::list &) const; @@ -61,6 +62,20 @@ ChoiceCondition *ChoiceCondition::clone() const return new ChoiceCondition(values); } +template +bool ChoiceCondition::equals(const Condition &other) const +{ + const ChoiceCondition *other_choice = dynamic_cast *>(&other); + if(!other_choice || values.size()!=other_choice->values.size()) + return false; + + for(unsigned i=0; ivalues[i]) + return false; + + return true; +} + template bool ChoiceCondition::can_merge(const Condition &other, const CompoundCondition &parent) const { diff --git a/source/condition.cpp b/source/condition.cpp index 6d383ea..e9400c4 100644 --- a/source/condition.cpp +++ b/source/condition.cpp @@ -16,6 +16,18 @@ void CompoundCondition::clone_to(CompoundCondition &other) const other.add((*i)->clone()); } +bool CompoundCondition::sub_equals(const CompoundCondition &other) const +{ + if(other.count()!=conditions.size()) + return false; + + for(unsigned i=0; iequals(other.get(i))) + return false; + + return true; +} + void CompoundCondition::add(Condition *cond) { if(!cond) @@ -171,6 +183,12 @@ AndCondition *AndCondition::clone() const return result; } +bool AndCondition::equals(const Condition &other) const +{ + const AndCondition *other_and = dynamic_cast(&other); + return (other_and ? sub_equals(*other_and) : false); +} + Condition *AndCondition::flatten(Condition *cond1, Condition *cond2) const { if(cond1->can_merge(*cond2, *this)) @@ -255,6 +273,12 @@ OrCondition *OrCondition::clone() const return result; } +bool OrCondition::equals(const Condition &other) const +{ + const OrCondition *other_or = dynamic_cast(&other); + return (other_or ? sub_equals(*other_or) : false); +} + Condition *OrCondition::flatten(Condition *cond1, Condition *cond2) const { if(cond1->can_merge(*cond2, *this)) @@ -321,6 +345,23 @@ LinkedColorsCondition *LinkedColorsCondition::clone() const return new LinkedColorsCondition(colors); } +bool LinkedColorsCondition::equals(const Condition &other) const +{ + const LinkedColorsCondition *other_linked = dynamic_cast(&other); + if(!other_linked) + return false; + + for(unsigned i=0; i<7; ++i) + { + if(colors.colors[i]!=other_linked->colors.colors[i]) + return false; + if(!colors.colors[i]) + break; + } + + return true; +} + void LinkedColorsCondition::add_lines(list &st) const { FilterStatement::add_line(st, format("SocketGroup %s", colors.colors)); diff --git a/source/condition.h b/source/condition.h index af8aa95..9d945c7 100644 --- a/source/condition.h +++ b/source/condition.h @@ -18,6 +18,7 @@ public: virtual ~Condition() { } virtual Condition *clone() const = 0; + virtual bool equals(const Condition &) const = 0; virtual Condition *flatten() const { return clone(); } virtual bool can_merge(const Condition &, const CompoundCondition &) const { return false; } virtual Condition *merge(const std::vector &, const CompoundCondition &) const { return 0; } @@ -39,6 +40,7 @@ public: protected: void clone_to(CompoundCondition &) const; + bool sub_equals(const CompoundCondition &) const; public: void add(Condition *); @@ -64,6 +66,7 @@ class AndCondition: public CompoundCondition { public: virtual AndCondition *clone() const; + virtual bool equals(const Condition &) const; protected: virtual Condition *flatten(Condition *, Condition *) const; virtual Condition *flatten(AndCondition *, Condition *) const; @@ -81,6 +84,7 @@ class OrCondition: public CompoundCondition { public: virtual OrCondition *clone() const; + virtual bool equals(const Condition &) const; protected: virtual Condition *flatten(Condition *, Condition *) const; virtual Condition *flatten(AndCondition *, Condition *) const; @@ -109,6 +113,7 @@ public: LinkedColorsCondition(const Colors &); virtual LinkedColorsCondition *clone() const; + virtual bool equals(const Condition &) const; virtual void add_lines(std::list &) const; }; diff --git a/source/rangecondition.h b/source/rangecondition.h index a72fec2..811a1aa 100644 --- a/source/rangecondition.h +++ b/source/rangecondition.h @@ -20,6 +20,7 @@ public: RangeCondition(Type, Type); virtual RangeCondition *clone() const; + virtual bool equals(const Condition &) const; virtual bool can_merge(const Condition &, const CompoundCondition &) const; virtual RangeCondition *merge(const std::vector &, const CompoundCondition &) const; virtual bool is_viable() const { return min<=max; } @@ -133,6 +134,16 @@ RangeCondition *RangeCondition::clone() const return new RangeCondition(min, max); } +template +bool RangeCondition::equals(const Condition &other) const +{ + const RangeCondition *other_range = dynamic_cast *>(&other); + if(!other_range) + return false; + + return (min==other_range->min && max==other_range->max); +} + template bool RangeCondition::can_merge(const Condition &other, const CompoundCondition &parent) const {