Author: sapient
Date: Wed Jul  9 05:28:19 2008
New Revision: 27868

URL: http://svn.gna.org/viewcvs/wesnoth?rev=27868&view=rev
Log:
new utility function in_ranges<typename>()

Modified:
    trunk/src/game_events.cpp
    trunk/src/terrain_filter.cpp
    trunk/src/unit.cpp
    trunk/src/util.hpp

Modified: trunk/src/game_events.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/game_events.cpp?rev=27868&r1=27867&r2=27868&view=diff
==============================================================================
--- trunk/src/game_events.cpp (original)
+++ trunk/src/game_events.cpp Wed Jul  9 05:28:19 2008
@@ -226,32 +226,22 @@
                const vconfig::child_list& have_unit = 
cond.get_children("have_unit");
                backwards_compat = backwards_compat && have_unit.empty();
                for(vconfig::child_list::const_iterator u = have_unit.begin(); 
u != have_unit.end(); ++u) {
-
                        if(units == NULL)
                                return false;
-
                        std::vector<std::pair<int,int> > counts = 
(*u).has_attribute("count")
                                ? utils::parse_ranges((*u)["count"]) : 
default_counts;
-                       std::vector<std::pair<int,int> >::const_iterator count, 
count_end = counts.end();
-
                        int match_count = 0;
-                       bool count_matches = false;
                        unit_map::const_iterator itor;
-                       for(itor = units->begin(); itor != units->end() && 
!count_matches; ++itor) {
+                       for(itor = units->begin(); itor != units->end(); 
++itor) {
                                if(itor->second.hitpoints() > 0 && 
game_events::unit_matches_filter(itor, *u)) {
                                        ++match_count;
                                        if(counts == default_counts) {
                                                // by default a single match is 
enough, so avoid extra work
-                                               count_matches = true;
+                                               break;
                                        }
                                }
                        }
-                       for (count = counts.begin(); count != count_end && 
!count_matches; ++count) {
-                               if(count->first <= match_count && match_count 
<= count->second) {
-                                       count_matches = true;
-                               }
-                       }
-                       if(!count_matches) {
+                       if(!in_ranges(match_count, counts)) {
                                return false;
                        }
                }
@@ -267,14 +257,7 @@
 
                        std::vector<std::pair<int,int> > counts = 
(*v).has_attribute("count")
                                ? utils::parse_ranges((*v)["count"]) : 
default_counts;
-                       std::vector<std::pair<int,int> >::const_iterator count, 
count_end = counts.end();
-                       bool count_matches = false;
-                       for (count = counts.begin(); count != count_end && 
!count_matches; ++count) {
-                               if(count->first <= res.size() && res.size() <= 
count->second) {
-                                       count_matches = true;
-                               }
-                       }
-                       if(!count_matches) {
+                       if(!in_ranges<int>(res.size(), counts)) {
                                return false;
                        }
                }

Modified: trunk/src/terrain_filter.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/terrain_filter.cpp?rev=27868&r1=27867&r2=27868&view=diff
==============================================================================
--- trunk/src/terrain_filter.cpp (original)
+++ trunk/src/terrain_filter.cpp Wed Jul  9 05:28:19 2008
@@ -179,14 +179,7 @@
                        static std::vector<std::pair<int,int> > default_counts 
= utils::parse_ranges("1-6");
                        std::vector<std::pair<int,int> > counts = 
(*i).has_attribute("count")
                                ? utils::parse_ranges((*i)["count"]) : 
default_counts;
-                       std::vector<std::pair<int,int> >::const_iterator count, 
count_end = counts.end();
-                       bool count_matches = false;
-                       for (count = counts.begin(); count != count_end && 
!count_matches; ++count) {
-                               if(count->first <= match_count && match_count 
<= count->second) {
-                                       count_matches = true;
-                               }
-                       }
-                       if(!count_matches) {
+                       if(!in_ranges(match_count, counts)) {
                                return false;
                        }
                }

Modified: trunk/src/unit.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/unit.cpp?rev=27868&r1=27867&r2=27868&view=diff
==============================================================================
--- trunk/src/unit.cpp (original)
+++ trunk/src/unit.cpp Wed Jul  9 05:28:19 2008
@@ -1084,14 +1084,7 @@
                        static std::vector<std::pair<int,int> > default_counts 
= utils::parse_ranges("1-6");
                        std::vector<std::pair<int,int> > counts = 
(*i).has_attribute("count")
                                ? utils::parse_ranges((*i)["count"]) : 
default_counts;
-                       std::vector<std::pair<int,int> >::const_iterator count, 
count_end = counts.end();
-                       bool count_matches = false;
-                       for (count = counts.begin(); count != count_end && 
!count_matches; ++count) {
-                               if(count->first <= match_count && match_count 
<= count->second) {
-                                       count_matches = true;
-                               }
-                       }
-                       if(!count_matches) {
+                       if(!in_ranges(match_count, counts)) {
                                return false;
                        }
                }

Modified: trunk/src/util.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/util.hpp?rev=27868&r1=27867&r2=27868&view=diff
==============================================================================
--- trunk/src/util.hpp (original)
+++ trunk/src/util.hpp Wed Jul  9 05:28:19 2008
@@ -22,6 +22,7 @@
 
 #include "global.hpp"
 #include <cmath>
+#include <vector>
 #include <map>
 #include <sstream>
 
@@ -137,6 +138,18 @@
        }
 }
 
+template<typename Cmp>
+bool in_ranges(Cmp c, std::vector<std::pair<Cmp, Cmp> >&ranges) {
+       typename std::vector<std::pair<Cmp,Cmp> >::const_iterator range,
+               range_end = ranges.end();
+       for (range = ranges.begin(); range != range_end; ++range) {
+               if(range->first <= c && c <= range->second) {
+                       return true;
+               }
+       }
+       return false;
+}
+
 inline bool chars_equal_insensitive(char a, char b) { return tolower(a) == 
tolower(b); }
 inline bool chars_less_insensitive(char a, char b) { return tolower(a) < 
tolower(b); }
 


_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits

Reply via email to