ChoiceCondition(const std::vector<Type> &);
virtual ChoiceCondition<T> *clone() const;
+ virtual bool equals(const Condition &) const;
virtual bool can_merge(const Condition &, const CompoundCondition &) const;
virtual ChoiceCondition<T> *merge(const std::vector<const Condition *> &, const CompoundCondition &) const;
virtual void add_lines(std::list<FilterStatement> &) const;
return new ChoiceCondition<T>(values);
}
+template<typename T>
+bool ChoiceCondition<T>::equals(const Condition &other) const
+{
+ const ChoiceCondition<T> *other_choice = dynamic_cast<const ChoiceCondition<T> *>(&other);
+ if(!other_choice || values.size()!=other_choice->values.size())
+ return false;
+
+ for(unsigned i=0; i<values.size(); ++i)
+ if(values[i]!=other_choice->values[i])
+ return false;
+
+ return true;
+}
+
template<typename T>
bool ChoiceCondition<T>::can_merge(const Condition &other, const CompoundCondition &parent) const
{
other.add((*i)->clone());
}
+bool CompoundCondition::sub_equals(const CompoundCondition &other) const
+{
+ if(other.count()!=conditions.size())
+ return false;
+
+ for(unsigned i=0; i<conditions.size(); ++i)
+ if(!conditions[i]->equals(other.get(i)))
+ return false;
+
+ return true;
+}
+
void CompoundCondition::add(Condition *cond)
{
if(!cond)
return result;
}
+bool AndCondition::equals(const Condition &other) const
+{
+ const AndCondition *other_and = dynamic_cast<const AndCondition *>(&other);
+ return (other_and ? sub_equals(*other_and) : false);
+}
+
Condition *AndCondition::flatten(Condition *cond1, Condition *cond2) const
{
if(cond1->can_merge(*cond2, *this))
return result;
}
+bool OrCondition::equals(const Condition &other) const
+{
+ const OrCondition *other_or = dynamic_cast<const OrCondition *>(&other);
+ return (other_or ? sub_equals(*other_or) : false);
+}
+
Condition *OrCondition::flatten(Condition *cond1, Condition *cond2) const
{
if(cond1->can_merge(*cond2, *this))
return new LinkedColorsCondition(colors);
}
+bool LinkedColorsCondition::equals(const Condition &other) const
+{
+ const LinkedColorsCondition *other_linked = dynamic_cast<const LinkedColorsCondition *>(&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<FilterStatement> &st) const
{
FilterStatement::add_line(st, format("SocketGroup %s", colors.colors));
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 Condition *> &, const CompoundCondition &) const { return 0; }
protected:
void clone_to(CompoundCondition &) const;
+ bool sub_equals(const CompoundCondition &) const;
public:
void add(Condition *);
{
public:
virtual AndCondition *clone() const;
+ virtual bool equals(const Condition &) const;
protected:
virtual Condition *flatten(Condition *, Condition *) const;
virtual Condition *flatten(AndCondition *, Condition *) const;
{
public:
virtual OrCondition *clone() const;
+ virtual bool equals(const Condition &) const;
protected:
virtual Condition *flatten(Condition *, Condition *) const;
virtual Condition *flatten(AndCondition *, Condition *) const;
LinkedColorsCondition(const Colors &);
virtual LinkedColorsCondition *clone() const;
+ virtual bool equals(const Condition &) const;
virtual void add_lines(std::list<FilterStatement> &) const;
};
RangeCondition(Type, Type);
virtual RangeCondition<T> *clone() const;
+ virtual bool equals(const Condition &) const;
virtual bool can_merge(const Condition &, const CompoundCondition &) const;
virtual RangeCondition<T> *merge(const std::vector<const Condition *> &, const CompoundCondition &) const;
virtual bool is_viable() const { return min<=max; }
return new RangeCondition<T>(min, max);
}
+template<typename T>
+bool RangeCondition<T>::equals(const Condition &other) const
+{
+ const RangeCondition<T> *other_range = dynamic_cast<const RangeCondition<T> *>(&other);
+ if(!other_range)
+ return false;
+
+ return (min==other_range->min && max==other_range->max);
+}
+
template<typename T>
bool RangeCondition<T>::can_merge(const Condition &other, const CompoundCondition &parent) const
{