1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-08-13 19:54:17 +02:00

* improved speed of CLodHandler::entries

This commit is contained in:
mateuszb
2011-01-14 16:52:37 +00:00
parent 56ec829049
commit 9e3f8b86bf
3 changed files with 30 additions and 22 deletions

View File

@@ -5,6 +5,7 @@
#include <algorithm> //std::find
#include <string> //std::find
#include <boost/logic/tribool.hpp>
#include <boost/unordered_set.hpp>
using boost::logic::tribool;
#include <boost/cstdint.hpp>
#include <assert.h>
@@ -400,7 +401,12 @@ namespace vstd
return c.find(i)!=c.end();
}
template <typename V, typename Item, typename Item2>
bool contains(const bmap<Item,V> & c, const Item2 &i) //returns true if map c contains item i
bool contains(const bmap<Item,V> & c, const Item2 &i) //returns true if bmap c contains item i
{
return c.find(i)!=c.end();
}
template <typename Item>
bool contains(const boost::unordered_set<Item> & c, const Item &i) //returns true if unordered set c contains item i
{
return c.find(i)!=c.end();
}

View File

@@ -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<Entry>::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<Entry>::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();

View File

@@ -6,6 +6,7 @@
#include <fstream>
#include <set>
#include <map>
#include <boost/unordered_set.hpp>
/*
* 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<comp.name
: type<comp.type;
}
bool operator == (const Entry & comp) const
{
return (type==comp.type || comp.type== FILE_ANY) && name==comp.name;
@@ -78,6 +74,18 @@ struct Entry
Entry(){};
};
template<>
struct boost::hash<Entry> : public std::unary_function<Entry, std::size_t> {
private:
boost::hash<std::string> 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<std::string, LodFileType> 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<Entry> entries;
boost::unordered_set<Entry> entries;
CLodHandler();
~CLodHandler();