]> git.tdb.fi Git - poefilter.git/commitdiff
Allow range conditions to be merged within and conditions
authorMikko Rasa <tdb@tdb.fi>
Wed, 15 Aug 2018 21:31:28 +0000 (00:31 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 15 Aug 2018 21:55:44 +0000 (00:55 +0300)
source/rangecondition.h

index 0d939948aa117554cf9155a03f836513196dc1ca..8355135fc2341c7f580e91f4b29ce3fbf422c4ea 100644 (file)
@@ -20,6 +20,8 @@ public:
        RangeCondition(Type, Type);
 
        virtual RangeCondition<T> *clone() const;
+       virtual bool can_merge(const Condition &, const CompoundCondition &) const;
+       virtual RangeCondition<T> *merge(const std::vector<Condition *> &, const CompoundCondition &) const;
        virtual void add_lines(std::list<FilterStatement> &) const;
 };
 
@@ -130,6 +132,30 @@ RangeCondition<T> *RangeCondition<T>::clone() const
        return new RangeCondition<T>(min, max);
 }
 
+template<typename T>
+bool RangeCondition<T>::can_merge(const Condition &other, const CompoundCondition &parent) const
+{
+       return dynamic_cast<const RangeCondition<T> *>(&other) && dynamic_cast<const AndCondition *>(&parent);
+}
+
+template<typename T>
+RangeCondition<T> *RangeCondition<T>::merge(const std::vector<Condition *> &conditions, const CompoundCondition &parent) const
+{
+       if(dynamic_cast<const AndCondition *>(&parent) && !conditions.empty())
+       {
+               RangeCondition<T> *result = new RangeCondition<T>(Traits::get_min(), Traits::get_max());
+               for(std::vector<Condition *>::const_iterator i=conditions.begin(); i!=conditions.end(); ++i)
+               {
+                       const RangeCondition<T> *c = static_cast<RangeCondition<T> *>(*i);
+                       result->min = std::max(result->min, c->min);
+                       result->max = std::min(result->max, c->max);
+               }
+               return result;
+       }
+       else
+               return 0;
+}
+
 template<typename T>
 void RangeCondition<T>::add_lines(std::list<FilterStatement> &st) const
 {