From: Mikko Rasa Date: Fri, 17 Aug 2018 16:56:59 +0000 (+0300) Subject: Tighten some parameters and return values X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=746329e61ac9f26962af740b0b11dee716803521;p=poefilter.git Tighten some parameters and return values --- diff --git a/source/choicecondition.h b/source/choicecondition.h index c2f7f0e..9792f64 100644 --- a/source/choicecondition.h +++ b/source/choicecondition.h @@ -21,7 +21,7 @@ public: virtual ChoiceCondition *clone() const; virtual bool can_merge(const Condition &, const CompoundCondition &) const; - virtual ChoiceCondition *merge(const std::vector &, const CompoundCondition &) const; + virtual ChoiceCondition *merge(const std::vector &, const CompoundCondition &) const; virtual void add_lines(std::list &) const; }; @@ -68,14 +68,14 @@ bool ChoiceCondition::can_merge(const Condition &other, const CompoundConditi } template -ChoiceCondition *ChoiceCondition::merge(const std::vector &conditions, const CompoundCondition &parent) const +ChoiceCondition *ChoiceCondition::merge(const std::vector &conditions, const CompoundCondition &parent) const { if(dynamic_cast(&parent) && !conditions.empty()) { ChoiceCondition *result = new ChoiceCondition(std::vector()); - for(std::vector::const_iterator i=conditions.begin(); i!=conditions.end(); ++i) + for(std::vector::const_iterator i=conditions.begin(); i!=conditions.end(); ++i) { - const std::vector &v = static_cast *>(*i)->values; + const std::vector &v = static_cast *>(*i)->values; for(typename std::vector::const_iterator j=v.begin(); j!=v.end(); ++j) if(Msp::find(result->values, *j)==result->values.end()) result->values.push_back(*j); diff --git a/source/condition.cpp b/source/condition.cpp index 4d00b6c..1ff316c 100644 --- a/source/condition.cpp +++ b/source/condition.cpp @@ -18,14 +18,16 @@ void CompoundCondition::clone_to(CompoundCondition &other) const void CompoundCondition::add(Condition *cond) { + if(!cond) + throw invalid_argument("CompoundCondition::add"); conditions.push_back(cond); } -const Condition *CompoundCondition::get(unsigned i) const +const Condition &CompoundCondition::get(unsigned i) const { if(i>=conditions.size()) throw out_of_range("CompoundCondition::get"); - return conditions[i]; + return *conditions[i]; } Condition *CompoundCondition::flatten() const @@ -40,7 +42,10 @@ Condition *CompoundCondition::flatten() const merge = conditions.front()->can_merge(**i, *this); if(merge) - return conditions.front()->merge(conditions, *this); + { + vector merge_conds(conditions.begin(), conditions.end()); + return conditions.front()->merge(merge_conds, *this); + } else { Condition *result = 0; @@ -97,10 +102,9 @@ Condition *CompoundCondition::dispatch_flatten(Condition *cond1, Condition *cond Condition *CompoundCondition::merge_two(Condition *cond1, Condition *cond2, bool del) const { - vector parts; - parts.reserve(2); - parts.push_back(cond1); - parts.push_back(cond2); + vector parts(2); + parts[0] = cond1; + parts[1] = cond2; Condition *result = cond1->merge(parts, *this); if(del) @@ -193,7 +197,7 @@ Condition *AndCondition::flatten(OrCondition *cond1, Condition *cond2) const OrCondition *result = new OrCondition; unsigned count = cond1->count(); for(unsigned i=0; iget(i)->clone(), (i+1clone() : cond2))) + if(Condition *sub = dispatch_flatten(cond1->get(i).clone(), (i+1clone() : cond2))) result->add(sub); delete cond1; @@ -216,7 +220,7 @@ Condition *AndCondition::flatten(OrCondition *cond1, OrCondition *cond2) const OrCondition *result = new OrCondition; for(unsigned i=0; iget(i)->clone(), cond2->get(j)->clone())) + if(Condition *sub = dispatch_flatten(cond1->get(i).clone(), cond2->get(j).clone())) result->add(sub); delete cond1; diff --git a/source/condition.h b/source/condition.h index 163c9c8..bbabf22 100644 --- a/source/condition.h +++ b/source/condition.h @@ -20,7 +20,7 @@ public: virtual Condition *clone() const = 0; virtual Condition *flatten() const { return clone(); } virtual bool can_merge(const Condition &, const CompoundCondition &) const { return false; } - virtual Condition *merge(const std::vector &, const CompoundCondition &) const { return 0; } + virtual Condition *merge(const std::vector &, const CompoundCondition &) const { return 0; } virtual bool is_viable() const { return true; } virtual void add_lines(std::list &) const = 0; }; @@ -43,7 +43,7 @@ protected: public: void add(Condition *); unsigned count() const { return conditions.size(); } - const Condition *get(unsigned) const; + const Condition &get(unsigned) const; virtual Condition *flatten() const; protected: diff --git a/source/rangecondition.h b/source/rangecondition.h index ad54e0a..a72fec2 100644 --- a/source/rangecondition.h +++ b/source/rangecondition.h @@ -21,7 +21,7 @@ public: virtual RangeCondition *clone() const; virtual bool can_merge(const Condition &, const CompoundCondition &) const; - virtual RangeCondition *merge(const std::vector &, const CompoundCondition &) const; + virtual RangeCondition *merge(const std::vector &, const CompoundCondition &) const; virtual bool is_viable() const { return min<=max; } virtual void add_lines(std::list &) const; }; @@ -149,7 +149,7 @@ bool RangeCondition::can_merge(const Condition &other, const CompoundConditio } template -RangeCondition *RangeCondition::merge(const std::vector &conditions, const CompoundCondition &parent) const +RangeCondition *RangeCondition::merge(const std::vector &conditions, const CompoundCondition &parent) const { if(conditions.empty()) return 0; @@ -160,9 +160,9 @@ RangeCondition *RangeCondition::merge(const std::vector &cond 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) + for(std::vector::const_iterator i=conditions.begin(); i!=conditions.end(); ++i) { - const RangeCondition *c = static_cast *>(*i); + const RangeCondition *c = static_cast *>(*i); if(intersect) { result->min = std::max(result->min, c->min);