Explorar o código

replacing vector+sort+binary_search with std::unordered_map

dmuratshin %!s(int64=10) %!d(string=hai) anos
pai
achega
7364d1d5f1

BIN=BIN
examples/Demo/data/images/batterfly.png


+ 1 - 1
examples/Demo/src/TestDrag.h

@@ -39,7 +39,7 @@ public:
             float angle = scalar::randFloat(0, (float)MATH_PI * 2);
             float angle = scalar::randFloat(0, (float)MATH_PI * 2);
             sprite->setRotation(angle);
             sprite->setRotation(angle);
             sprite->addTween(Actor::TweenRotation(MATH_PI * 2 + angle), 30000, -1);
             sprite->addTween(Actor::TweenRotation(MATH_PI * 2 + angle), 30000, -1);
-            sprite->setScale(scalar::randFloat(1.0f, 1.5f));
+            sprite->setScale(scalar::randFloat(0.8f, 1.2f));
             sprite->setAnchor(0.5f, 0.5f);
             sprite->setAnchor(0.5f, 0.5f);
 
 
             sprite->addEventListener(TouchEvent::TOUCH_DOWN, CLOSURE(this, &DragTest::onMouseDown));
             sprite->addEventListener(TouchEvent::TOUCH_DOWN, CLOSURE(this, &DragTest::onMouseDown));

+ 5 - 6
examples/Demo/src/TestManageRes.h

@@ -19,9 +19,12 @@ public:
 #endif
 #endif
 
 
 
 
-        for (int i = 0; i < resources.getCount(); ++i)
+        Resources::resources items;
+        resources.collect(items);
+
+        for (size_t i = 0; i < items.size(); ++i)
         {
         {
-            ResAnim* ra = dynamic_cast<ResAnim*>(resources.get(i));
+            ResAnim* ra = dynamic_cast<ResAnim*>(items[i].get());
             if (!ra)
             if (!ra)
                 continue;
                 continue;
             if (ra->getName().find("_") != string::npos)
             if (ra->getName().find("_") != string::npos)
@@ -38,10 +41,6 @@ public:
                 sprite->setPosition(0, 0);
                 sprite->setPosition(0, 0);
                 sprite->setPriority(-1);
                 sprite->setPriority(-1);
             }
             }
-            else
-            {
-                //sprite->addEventHandler(new DragHandler);
-            }
         }
         }
 
 
         spTextField text = new TextField;
         spTextField text = new TextField;

+ 10 - 23
oxygine/src/res/Resources.cpp

@@ -299,46 +299,35 @@ namespace oxygine
             }
             }
         }
         }
 
 
-        sort();
         FS_LOG("xml loaded");
         FS_LOG("xml loaded");
     }
     }
 
 
+    void Resources::collect(resources&)
+    {
+    }
+
     void Resources::add(Resource* r)
     void Resources::add(Resource* r)
     {
     {
         OX_ASSERT(r);
         OX_ASSERT(r);
         if (!r)
         if (!r)
             return;
             return;
-        /*
-        OX_ASSERT(_resources[r->getName()] == 0);
-
-        _resources[r->getName()] = r;
-        */
 
 
         //todo insert to correct place
         //todo insert to correct place
         r->setName(lower(r->getName()));
         r->setName(lower(r->getName()));
-        _fastAccessResources.push_back(r);
-
-        //if (own)
-        //  _owned.push_back(r);
-        //OX_ASSERT(0);
+        _fastAccessResources[r->getName()] = r;
     }
     }
 
 
 
 
-    void Resources::print()
+    void Resources::print() const
     {
     {
         log::message("resources:\n");
         log::message("resources:\n");
-        for (resources::iterator i = _fastAccessResources.begin(); i != _fastAccessResources.end(); ++i)
+        for (resourcesMap::const_iterator i = _fastAccessResources.cbegin(); i != _fastAccessResources.cend(); ++i)
         {
         {
-            spResource res = *i;
+            spResource res = i->second;
             log::message("%s\n", res->getName().c_str());
             log::message("%s\n", res->getName().c_str());
         }
         }
     }
     }
 
 
-    void Resources::sort()
-    {
-        std::sort(_fastAccessResources.begin(), _fastAccessResources.end(), ObjectBasePredicate());
-    }
-
     Resources::resources& Resources::_getResources()
     Resources::resources& Resources::_getResources()
     {
     {
         return _resources;
         return _resources;
@@ -348,13 +337,11 @@ namespace oxygine
     {
     {
         std::string id = lower(id_);
         std::string id = lower(id_);
 
 
-        resources::const_iterator it = std::lower_bound(_fastAccessResources.begin(), _fastAccessResources.end(),
-                                       id.c_str(), ObjectBasePredicate());
+        resourcesMap::const_iterator it = _fastAccessResources.find(id);
 
 
         if (it != _fastAccessResources.end())
         if (it != _fastAccessResources.end())
         {
         {
-            if ((*it)->getName() == id)
-                return (*it).get();
+            return it->second.get();
         }
         }
 
 
         handleErrorPolicy(ep, "can't find resource: '%s' in '%s'", id.c_str(), _name.c_str());
         handleErrorPolicy(ep, "can't find resource: '%s' in '%s'", id.c_str(), _name.c_str());

+ 9 - 6
oxygine/src/res/Resources.h

@@ -7,6 +7,7 @@
 #include "closure/closure.h"
 #include "closure/closure.h"
 #include "core/Object.h"
 #include "core/Object.h"
 #include "Resource.h"
 #include "Resource.h"
+#include <unordered_map>
 
 
 namespace pugi
 namespace pugi
 {
 {
@@ -71,8 +72,8 @@ namespace oxygine
         Resource* get(const std::string& id, error_policy ep = ep_show_error) const;
         Resource* get(const std::string& id, error_policy ep = ep_show_error) const;
 
 
         /** returns resource by index */
         /** returns resource by index */
-        Resource* get(int index) const {return _fastAccessResources[index].get();}
-        int       getCount() const {return (int)_fastAccessResources.size();}
+        //Resource* get(int index) const {return _fastAccessResources.at(index).get();}
+        //int       getCount() const {return (int)_fastAccessResources.size();}
 
 
         Resource* operator[](const std::string& id) { return get(id); }
         Resource* operator[](const std::string& id) { return get(id); }
 
 
@@ -91,10 +92,11 @@ namespace oxygine
         template<class T>
         template<class T>
         T* getT(const std::string& id, error_policy ep = ep_show_error) const { return safeCast<T*>(get(id, ep)); }
         T* getT(const std::string& id, error_policy ep = ep_show_error) const { return safeCast<T*>(get(id, ep)); }
 
 
-        /**sorting manually added resources*/
-        void sort();
         /**debug function. prints all loaded resources*/
         /**debug function. prints all loaded resources*/
-        void print();
+        void print() const;
+
+        /**collects all resources into vector*/
+        void collect(resources&);
 
 
 
 
         resources& _getResources();
         resources& _getResources();
@@ -129,7 +131,8 @@ namespace oxygine
 
 
 
 
         resources _resources;
         resources _resources;
-        resources _fastAccessResources;
+        typedef std::unordered_map<std::string, spResource> resourcesMap;
+        resourcesMap _fastAccessResources;
 
 
 
 
         typedef std::vector< registeredResource > registeredResources;
         typedef std::vector< registeredResource > registeredResources;