+Condition *CompoundCondition::merge_two(Condition *cond1, Condition *cond2, const CompoundCondition &parent, bool del)
+{
+ vector<const Condition *> parts(2);
+ parts[0] = cond1;
+ parts[1] = cond2;
+ Condition *result = cond1->merge(parts, parent);
+
+ 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)
+{
+ bool merged = false;
+ for(vector<Condition *>::iterator i=target->conditions.begin(); i!=target->conditions.end(); ++i)
+ if((*i)->can_merge(*cond, *target))
+ {
+ Condition *m = merge_two(cond, *i, *target, false);
+ delete *i;
+ if(del)
+ delete cond;
+ *i = m;
+ merged = true;
+ break;
+ }
+
+ if(!merged)
+ target->add(del ? cond : cond->clone());
+
+ if(del && !target->is_viable())
+ {
+ delete target;
+ return 0;
+ }
+
+ return target;
+}
+
+Condition *CompoundCondition::merge_contents_to(CompoundCondition *cond, CompoundCondition *target)
+{
+ for(vector<Condition *>::iterator i=cond->conditions.begin(); i!=cond->conditions.end(); ++i)
+ add_merged_to(*i, target, false);
+
+ delete cond;
+
+ if(target->is_viable())
+ return target;
+
+ delete target;
+ return 0;
+}
+