mail[Wesnoth-commits] r40047 - in /trunk/src: unit_types.cpp unit_types.hpp


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by alinktomine on December 03, 2009 - 03:30:
Author: alink
Date: Thu Dec  3 03:30:46 2009
New Revision: 40047

URL: http://svn.gna.org/viewcvs/wesnoth?rev=40047&view=rev
Log:
Optimize find_unit_type() by stopping to search the same things 3 times

Modified:
    trunk/src/unit_types.cpp
    trunk/src/unit_types.hpp

Modified: trunk/src/unit_types.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/unit_types.cpp?rev=40047&r1=40046&r2=40047&view=diff
==============================================================================
--- trunk/src/unit_types.cpp (original)
+++ trunk/src/unit_types.cpp Thu Dec  3 03:30:46 2009
@@ -1250,7 +1250,7 @@
         return types_.end();
 
        DBG_CF << "trying to find " << key  << " in unit_type list 
(unit_type_data.unit_types)\n";
-    const unit_type_map::const_iterator itor = types_.find(key);
+    const unit_type_map::iterator itor = types_.find(key);
 
     //This might happen if units of another era are requested (for example 
for savegames)
     if (itor == types_.end()){
@@ -1262,9 +1262,10 @@
     }
 
     //check if the unit_type is constructed and build it if necessary
-    build_unit_type(key, status);
-
-    return types_.find(key);
+    build_unit_type(itor, status);
+
+    //return unit_type_data::unit_type_map::const_iterator();
+    return itor;
 }
 
 const config& unit_type_data::unit_type_map_wrapper::find_config(const 
std::string& key) const
@@ -1295,23 +1296,21 @@
 {
     assert(unit_cfg_ != NULL);
 
-    for (unit_type_map::const_iterator u = types_.begin(); u != 
types_.end(); ++u){
-        build_unit_type(u->first, status);
+    for (unit_type_map::iterator u = types_.begin(); u != types_.end(); ++u){
+        build_unit_type(u, status);
     }
     for (unit_type_map::iterator u = types_.begin(); u != types_.end(); ++u){
         add_advancement(u->second);
     }
 }
 
-unit_type& unit_type_data::unit_type_map_wrapper::build_unit_type(const 
std::string& key, unit_type::BUILD_STATUS status) const
-{
-    unit_type_map::iterator ut = types_.find(key);
-
+unit_type& unit_type_data::unit_type_map_wrapper::build_unit_type(const 
unit_type_map::iterator& ut, unit_type::BUILD_STATUS status) const
+{
     DBG_UT << "Building unit type " << ut->first << ", level " << status << 
"\n";
 
     switch (status){
         case unit_type::CREATED: {
-                       const config& unit_cfg = find_config(key);
+                       const config& unit_cfg = find_config(ut->first);
             ut->second.set_config(unit_cfg);
                        ut->second.build_created(unit_cfg, movement_types_, 
races_, unit_cfg_->child_range("trait"));
             break;
@@ -1319,7 +1318,7 @@
         case unit_type::HELP_INDEX: {
             //build the stuff that is needed to feed the help index
             if ( (ut->second.build_status() == unit_type::NOT_BUILT) || 
(ut->second.build_status() == unit_type::CREATED) ) {
-                               const config& unit_cfg = find_config(key);
+                               const config& unit_cfg = 
find_config(ut->first);
                                ut->second.build_help_index(unit_cfg, 
movement_types_, races_, unit_cfg_->child_range("trait"));
                        }
             break;
@@ -1330,7 +1329,7 @@
                 (ut->second.build_status() == unit_type::CREATED) ||
                 (ut->second.build_status() == unit_type::HELP_INDEX) )
             {
-                               const config& unit_cfg = find_config(key);
+                               const config& unit_cfg = 
find_config(ut->first);
                                ut->second.build_full(unit_cfg, 
movement_types_, races_, unit_cfg_->child_range("trait"));
             }
             break;

Modified: trunk/src/unit_types.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/unit_types.hpp?rev=40047&r1=40046&r2=40047&view=diff
==============================================================================
--- trunk/src/unit_types.hpp (original)
+++ trunk/src/unit_types.hpp Thu Dec  3 03:30:46 2009
@@ -375,7 +375,7 @@
                        std::pair<unit_type_map::iterator, bool> insert(const 
std::pair<std::string,unit_type>& utype) { return types_.insert(utype); }
                        void clear();
 
-            unit_type& build_unit_type(const std::string& key, 
unit_type::BUILD_STATUS status) const;
+            unit_type& build_unit_type(const unit_type_map::iterator& ut, 
unit_type::BUILD_STATUS status) const;
             void add_advancefrom(const config& unit_cfg) const;
             void add_advancement(unit_type& to_unit) const;
 




Related Messages


Powered by MHonArc, Updated Thu Dec 03 11:20:07 2009