From 0cb9452fe3f8cfd3beec53a84bc8df9306fbd44f Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 17 Aug 2018 18:16:26 +0300 Subject: [PATCH] Merge overlapping or adjacent ranges in or conditions --- source/rangecondition.h | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/source/rangecondition.h b/source/rangecondition.h index d4d11fa..ad54e0a 100644 --- a/source/rangecondition.h +++ b/source/rangecondition.h @@ -136,25 +136,45 @@ RangeCondition *RangeCondition::clone() const template bool RangeCondition::can_merge(const Condition &other, const CompoundCondition &parent) const { - return dynamic_cast *>(&other) && dynamic_cast(&parent); + const RangeCondition *other_range = dynamic_cast *>(&other); + if(!other_range) + return false; + + if(dynamic_cast(&parent)) + return true; + else if(dynamic_cast(&parent)) + return min<=other_range->max+1 && max+1>=other_range->min; + else + return false; } template RangeCondition *RangeCondition::merge(const std::vector &conditions, const CompoundCondition &parent) const { - if(dynamic_cast(&parent) && !conditions.empty()) + if(conditions.empty()) + return 0; + + bool intersect = dynamic_cast(&parent); + RangeCondition *result; + if(intersect) + result = new RangeCondition(Traits::get_min(), Traits::get_max()); + else + result = new RangeCondition(Traits::get_max(), Traits::get_min()); + for(std::vector::const_iterator i=conditions.begin(); i!=conditions.end(); ++i) { - RangeCondition *result = new RangeCondition(Traits::get_min(), Traits::get_max()); - for(std::vector::const_iterator i=conditions.begin(); i!=conditions.end(); ++i) + const RangeCondition *c = static_cast *>(*i); + if(intersect) { - const RangeCondition *c = static_cast *>(*i); result->min = std::max(result->min, c->min); result->max = std::min(result->max, c->max); } - return result; + else + { + result->min = std::min(result->min, c->min); + result->max = std::max(result->max, c->max); + } } - else - return 0; + return result; } template -- 2.43.0