From: Mikko Rasa Date: Wed, 15 Aug 2018 21:31:28 +0000 (+0300) Subject: Allow range conditions to be merged within and conditions X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=3e3549847cd81bde3fd3ccb4da076af87fe7f0f2;p=poefilter.git Allow range conditions to be merged within and conditions --- diff --git a/source/rangecondition.h b/source/rangecondition.h index 0d93994..8355135 100644 --- a/source/rangecondition.h +++ b/source/rangecondition.h @@ -20,6 +20,8 @@ public: RangeCondition(Type, Type); virtual RangeCondition *clone() const; + virtual bool can_merge(const Condition &, const CompoundCondition &) const; + virtual RangeCondition *merge(const std::vector &, const CompoundCondition &) const; virtual void add_lines(std::list &) const; }; @@ -130,6 +132,30 @@ RangeCondition *RangeCondition::clone() const return new RangeCondition(min, max); } +template +bool RangeCondition::can_merge(const Condition &other, const CompoundCondition &parent) const +{ + return dynamic_cast *>(&other) && dynamic_cast(&parent); +} + +template +RangeCondition *RangeCondition::merge(const std::vector &conditions, const CompoundCondition &parent) const +{ + if(dynamic_cast(&parent) && !conditions.empty()) + { + 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); + result->min = std::max(result->min, c->min); + result->max = std::min(result->max, c->max); + } + return result; + } + else + return 0; +} + template void RangeCondition::add_lines(std::list &st) const {