Optimized dialogue merging with a lookup map

deque
scrawl 11 years ago
parent 1677fcf324
commit 0aa1042fd7

@ -47,37 +47,46 @@ void Dialogue::save(ESMWriter &esm) const
void Dialogue::addInfo(const ESM::DialInfo& info, bool merge)
{
// TODO: use std::move
if (!merge || mInfo.empty() || info.mNext.empty())
{
mInfo.push_back(info);
mLookup[info.mId] = mInfo.insert(mInfo.end(), info);
return;
}
if (info.mPrev.empty())
{
mInfo.push_front(info);
mLookup[info.mId] = mInfo.insert(mInfo.begin(), info);
return;
}
int i=0;
for (ESM::Dialogue::InfoContainer::iterator it = mInfo.begin(); it != mInfo.end(); ++it)
ESM::Dialogue::InfoContainer::iterator it = mInfo.end();
std::map<std::string, ESM::Dialogue::InfoContainer::iterator>::iterator lookup;
lookup = mLookup.find(info.mPrev);
if (lookup != mLookup.end())
{
it = lookup->second;
mLookup[info.mId] = mInfo.insert(++it, info);
return;
}
lookup = mLookup.find(info.mNext);
if (lookup != mLookup.end())
{
if (info.mPrev == it->mId)
{
mInfo.insert(++it, info);
return;
}
if (info.mNext == it->mId)
{
mInfo.insert(it, info);
return;
}
if (info.mId == it->mId)
{
*it = info;
return;
}
++i;
it = lookup->second;
mLookup[info.mId] = mInfo.insert(it, info);
return;
}
lookup = mLookup.find(info.mId);
if (lookup != mLookup.end())
{
it = lookup->second;
*it = info;
return;
}
std::cerr << "Failed to insert info " << info.mId << std::endl;
}

@ -3,6 +3,7 @@
#include <string>
#include <list>
#include <map>
#include "loadinfo.hpp"
@ -36,8 +37,13 @@ struct Dialogue
typedef std::list<DialInfo> InfoContainer;
typedef std::map<std::string, InfoContainer::iterator> LookupMap;
InfoContainer mInfo;
// This is only used during the loading phase to speed up DialInfo merging.
LookupMap mLookup;
void load(ESMReader &esm);
void save(ESMWriter &esm) const;

Loading…
Cancel
Save