diff --git a/global.h b/global.h index 0ec278612..a09ba3ea3 100644 --- a/global.h +++ b/global.h @@ -5,6 +5,7 @@ #include //std::find #include //std::find #include +#include using boost::logic::tribool; #include #include @@ -400,7 +401,12 @@ namespace vstd return c.find(i)!=c.end(); } template - bool contains(const bmap & c, const Item2 &i) //returns true if map c contains item i + bool contains(const bmap & c, const Item2 &i) //returns true if bmap c contains item i + { + return c.find(i)!=c.end(); + } + template + bool contains(const boost::unordered_set & c, const Item &i) //returns true if unordered set c contains item i { return c.find(i)!=c.end(); } diff --git a/lib/CLodHandler.cpp b/lib/CLodHandler.cpp index 832ca7d8d..e4a9ee25c 100644 --- a/lib/CLodHandler.cpp +++ b/lib/CLodHandler.cpp @@ -60,16 +60,6 @@ std::string readString(const unsigned char * bufor, int &i) return ret; } -Entry CLodHandler::getEntry(const std::string name, LodFileType type) -{ - Entry ret; - std::set::iterator it = entries.find(Entry(name, type)); - - if (it!=entries.end()) - ret = *it; - return ret; -} - unsigned char * CLodHandler::giveFile(const std::string defName, LodFileType type, int * length) { std::string fname = defName; @@ -77,13 +67,18 @@ unsigned char * CLodHandler::giveFile(const std::string defName, LodFileType typ int dotPos = fname.find_last_of('.'); if ( dotPos != -1 ) fname.erase(dotPos); - - Entry ourEntry = getEntry(fname, type); - if(!vstd::contains(entries, ourEntry)) //nothing's been found + + int count = entries.size(); + + boost::unordered_set::const_iterator en_it = entries.find(Entry(fname, type)); + + if(en_it == entries.end()) //nothing's been found { tlog1 << "Cannot find file: " << fname << std::endl; return NULL; } + Entry ourEntry = *en_it; + if(length) *length = ourEntry.realSize; mutex->lock(); diff --git a/lib/CLodHandler.h b/lib/CLodHandler.h index 8d76008d3..9014e3621 100644 --- a/lib/CLodHandler.h +++ b/lib/CLodHandler.h @@ -6,6 +6,7 @@ #include #include #include +#include /* * CLodhandler.h, part of VCMI engine @@ -52,6 +53,7 @@ enum LodFileType{ FILE_OTHER }; + struct Entry { // Info extracted from LOD file @@ -62,12 +64,6 @@ struct Entry size; //and with LodFileType type;// file type determined by extension - bool operator<(const Entry & comp) const - { - return type==comp.type ? name +struct boost::hash : public std::unary_function { +private: + boost::hash stringHasher; +public: + std::size_t operator()(Entry const& en) const + { + //do NOT improve this hash function as we need same-name hash collisions for find to work properly + return stringHasher(en.name); + } +}; + class DLL_EXPORT CLodHandler { std::map extMap;// to convert extensions to file type @@ -88,12 +96,11 @@ class DLL_EXPORT CLodHandler std::string myDir; //load files from this dir instead of .lod file void initEntry(Entry &e, const std::string name); - Entry getEntry(const std::string name, LodFileType); int infs2(unsigned char * in, int size, int realSize, unsigned char*& out, int wBits=15); //zlib fast handler public: - std::set entries; + boost::unordered_set entries; CLodHandler(); ~CLodHandler();