Author: cjhopman
Date: Thu Apr  2 07:03:07 2009
New Revision: 34388

URL: http://svn.gna.org/viewcvs/wesnoth?rev=34388&view=rev
Log:
- convert image::locator to do a hash-based lookup

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

Modified: trunk/src/image.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/image.cpp?rev=34388&r1=34387&r2=34388&view=diff
==============================================================================
--- trunk/src/image.cpp (original)
+++ trunk/src/image.cpp Thu Apr  2 07:03:07 2009
@@ -31,6 +31,8 @@
 
 #include "SDL_image.h"
 
+#include <boost/functional/hash.hpp>
+
 
 #define ERR_DP LOG_STREAM(err, display)
 #define INFO_DP LOG_STREAM(info, display)
@@ -38,9 +40,7 @@
 
 namespace {
 
-typedef std::map<image::locator::value, int> locator_finder_t;
-typedef std::pair<image::locator::value, int> locator_finder_pair;
-locator_finder_t locator_finder;
+image::locator::locator_finder_t locator_finder;
 
 /** Definition of all image maps */
 image::image_cache 
images_,hexed_images_,scaled_to_hex_images_,scaled_to_zoom_,unmasked_images_;
@@ -103,13 +103,12 @@
 
 void locator::init_index()
 {
-       locator_finder_t::iterator i = locator_finder.find(val_);
-
-       if(i == locator_finder.end()) {
+       std::map<value, int>& finder = locator_finder[hash_value(val_)];
+       std::map<value, int>::iterator i = finder.find(val_);
+
+       if(i == finder.end()) {
                index_ = last_index_++;
-               locator_finder.insert(locator_finder_pair(val_, index_));
-
-
+               finder.insert(std::make_pair(val_, index_));
        } else {
                index_ = i->second;
        }
@@ -290,6 +289,25 @@
        } else {
                return false;
        }
+}
+
+size_t hash_value(const locator::value& val) {
+       using boost::hash_value;
+       using boost::hash_combine;
+
+       size_t hash = hash_value(val.type_);
+       if (val.type_ == locator::FILE || val.type_ == locator::SUB_FILE) {
+               hash_combine(hash, val.filename_);
+       }
+       if (val.type_ == locator::SUB_FILE) {
+               hash_combine(hash, val.loc_.x);
+               hash_combine(hash, val.loc_.y);
+               hash_combine(hash, val.center_x_);
+               hash_combine(hash, val.center_y_);
+               hash_combine(hash, val.modifications_);
+       }
+
+       return hash;
 }
 
 surface locator::load_image_file() const

Modified: trunk/src/image.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/image.hpp?rev=34388&r1=34387&r2=34388&view=diff
==============================================================================
--- trunk/src/image.hpp (original)
+++ trunk/src/image.hpp Thu Apr  2 07:03:07 2009
@@ -20,6 +20,8 @@
 
 #include "SDL.h"
 #include "cassert"
+
+#include <map>
 #include <string>
 #include <vector>
 #include <list>
@@ -97,14 +99,15 @@
        //a generic image locator. Abstracts the location of an image.
        class locator
        {
+       public:         
+               enum type { NONE, FILE, SUB_FILE };
+
+
        private:
                // Called by each constructor after actual construction to
                // initialize the index_ field
                void init_index();
                void parse_arguments();
-       public:
-               enum type { NONE, FILE, SUB_FILE };
-
                struct value {
                        value();
                        value(const value &a);
@@ -125,6 +128,18 @@
                        int center_x_;
                        int center_y_;
                };
+               
+               friend size_t hash_value(const value&);
+
+       public:
+
+               /**
+                * @TODO replace this with std::unordered_map<value, int> or 
boost::unordered_map<value, int>
+                *       boost::unordered_map can almost just be dropped in as 
boost::hash<T>(T val) will return hash_value(val), but it 
+                *       requires boost 1.35 (preferably 1.36 or later)
+                *
+                **/
+               typedef std::map<size_t, std::map<value, int> > 
locator_finder_t;
 
                // Constructing locators is somewhat slow, accessing image
                // through locators is fast. The idea is that calling functions
@@ -193,6 +208,8 @@
                value val_;
        };
 
+       size_t hash_value(const locator::value&);
+
 
        typedef cache_type<surface> image_cache;
        typedef cache_type<locator> locator_cache;


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

Reply via email to