Condition *sub = (*i)->flatten();
if(!result)
result = sub;
- else
+ else if(sub)
result = dispatch_flatten(result, sub);
}
- return result;
+ if(result && result->is_viable())
+ return result;
+
+ delete result;
+ return 0;
}
}
parts.push_back(cond1);
parts.push_back(cond2);
Condition *result = cond1->merge(parts, *this);
+
if(del)
{
delete cond1;
delete cond2;
+
+ if(!result->is_viable())
+ {
+ delete result;
+ return 0;
+ }
}
+
return result;
}
Condition *CompoundCondition::add_merged_to(Condition *cond, CompoundCondition *target, bool del) const
{
+ bool merged = false;
for(vector<Condition *>::iterator i=target->conditions.begin(); i!=target->conditions.end(); ++i)
if((*i)->can_merge(*cond, *target))
{
if(del)
delete cond;
*i = m;
- return target;
+ merged = true;
+ break;
}
- target->add(del ? cond : cond->clone());
+ if(!merged)
+ target->add(del ? cond : cond->clone());
+
+ if(del && !target->is_viable())
+ {
+ delete target;
+ return 0;
+ }
+
return target;
}
add_merged_to(*i, target, false);
delete cond;
- return target;
+
+ if(target->is_viable())
+ return target;
+
+ delete target;
+ return 0;
}
OrCondition *result = new OrCondition;
unsigned count = cond1->count();
for(unsigned i=0; i<count; ++i)
- result->add(dispatch_flatten(cond1->get(i)->clone(), (i+1<count ? cond2->clone() : cond2)));
+ if(Condition *sub = dispatch_flatten(cond1->get(i)->clone(), (i+1<count ? cond2->clone() : cond2)))
+ result->add(sub);
delete cond1;
- return result;
+
+ if(result->is_viable())
+ return result;
+
+ delete result;
+ return 0;
}
Condition *AndCondition::flatten(OrCondition *cond1, AndCondition *cond2) const
OrCondition *result = new OrCondition;
for(unsigned i=0; i<count1; ++i)
for(unsigned j=0; j<count2; ++j)
- result->add(dispatch_flatten(cond1->get(i)->clone(), cond2->get(j)->clone()));
+ if(Condition *sub = dispatch_flatten(cond1->get(i)->clone(), cond2->get(j)->clone()))
+ result->add(sub);
+
delete cond1;
delete cond2;
- return result;
+
+ if(result->is_viable())
+ return result;
+
+ delete result;
+ return 0;
+}
+
+bool AndCondition::is_viable() const
+{
+ for(vector<Condition *>::const_iterator i=conditions.begin(); i!=conditions.end(); ++i)
+ if(!(*i)->is_viable())
+ return false;
+ return !conditions.empty();
}
void AndCondition::add_lines(list<FilterStatement> &st) const
return merge_contents_to(cond2, cond1);
}
+bool OrCondition::is_viable() const
+{
+ for(vector<Condition *>::const_iterator i=conditions.begin(); i!=conditions.end(); ++i)
+ if((*i)->is_viable())
+ return true;
+ return false;
+}
+
void OrCondition::add_lines(list<FilterStatement> &st) const
{
list<FilterStatement> result;
virtual Condition *flatten() const { return clone(); }
virtual bool can_merge(const Condition &, const CompoundCondition &) const { return false; }
virtual Condition *merge(const std::vector<Condition *> &, const CompoundCondition &) const { return 0; }
+ virtual bool is_viable() const { return true; }
virtual void add_lines(std::list<FilterStatement> &) const = 0;
};
virtual Condition *flatten(OrCondition *, AndCondition *) const;
virtual Condition *flatten(OrCondition *, OrCondition *) const;
public:
+ virtual bool is_viable() const;
virtual void add_lines(std::list<FilterStatement> &) const;
};
virtual Condition *flatten(OrCondition *, AndCondition *) const;
virtual Condition *flatten(OrCondition *, OrCondition *) const;
public:
+ virtual bool is_viable() const;
virtual void add_lines(std::list<FilterStatement> &) const;
};