Overwrite existing records in IndexedStore (Fixes #2182)

moveref
scrawl 10 years ago
parent f771b95a15
commit 83dcf9ce4b

@ -633,8 +633,8 @@ namespace MWGui
MWWorld::Store<ESM::Skill>::iterator it = skills.begin(); MWWorld::Store<ESM::Skill>::iterator it = skills.begin();
for (; it != skills.end(); ++it) for (; it != skills.end(); ++it)
{ {
if (it->mData.mSpecialization == specId) if (it->second.mData.mSpecialization == specId)
specText += std::string("\n#{") + ESM::Skill::sSkillNameIds[it->mIndex] + "}"; specText += std::string("\n#{") + ESM::Skill::sSkillNameIds[it->first] + "}";
} }
widget->setUserString("Caption_CenteredCaptionText", specText); widget->setUserString("Caption_CenteredCaptionText", specText);
widget->setUserString("ToolTipLayout", "TextWithCenteredCaptionToolTip"); widget->setUserString("ToolTipLayout", "TextWithCenteredCaptionToolTip");

@ -143,9 +143,9 @@ namespace MWMechanics
MWWorld::Store<ESM::Skill>::iterator iter = skills.begin(); MWWorld::Store<ESM::Skill>::iterator iter = skills.begin();
for (; iter != skills.end(); ++iter) for (; iter != skills.end(); ++iter)
{ {
if (iter->mData.mSpecialization==class_->mData.mSpecialization) if (iter->second.mData.mSpecialization==class_->mData.mSpecialization)
{ {
int index = iter->mIndex; int index = iter->first;
if (index>=0 && index<27) if (index>=0 && index<27)
{ {

@ -1015,24 +1015,15 @@ namespace MWWorld
template <class T> template <class T>
class IndexedStore class IndexedStore
{ {
struct Compare
{
bool operator()(const T &x, const T &y) const {
return x.mIndex < y.mIndex;
}
};
protected: protected:
std::vector<T> mStatic; typedef typename std::map<int, T> Static;
Static mStatic;
public: public:
typedef typename std::vector<T>::const_iterator iterator; typedef typename std::map<int, T>::const_iterator iterator;
IndexedStore() {} IndexedStore() {}
IndexedStore(unsigned int size) {
mStatic.reserve(size);
}
iterator begin() const { iterator begin() const {
return mStatic.begin(); return mStatic.begin();
} }
@ -1041,10 +1032,14 @@ namespace MWWorld
return mStatic.end(); return mStatic.end();
} }
/// \todo refine loading order
void load(ESM::ESMReader &esm) { void load(ESM::ESMReader &esm) {
mStatic.push_back(T()); T record;
mStatic.back().load(esm); record.load(esm);
// Try to overwrite existing record
std::pair<typename Static::iterator, bool> found = mStatic.insert(std::make_pair(record.mIndex, record));
if (found.second)
found.first->second = record;
} }
int getSize() const { int getSize() const {
@ -1052,40 +1047,13 @@ namespace MWWorld
} }
void setUp() { void setUp() {
/// \note This method sorts indexed values for further
/// searches. Every loaded item is present in storage, but
/// latest loaded shadows any previous while searching.
/// If memory cost will be too high, it is possible to remove
/// unused values.
Compare cmp;
std::stable_sort(mStatic.begin(), mStatic.end(), cmp);
typename std::vector<T>::iterator first, next;
next = first = mStatic.begin();
while (first != mStatic.end() && ++next != mStatic.end()) {
while (next != mStatic.end() && !cmp(*first, *next)) {
++next;
}
if (first != --next) {
std::swap(*first, *next);
}
first = ++next;
}
} }
const T *search(int index) const { const T *search(int index) const {
T item; typename Static::const_iterator it = mStatic.find(index);
item.mIndex = index; if (it != mStatic.end())
return &(it->second);
iterator it = return NULL;
std::lower_bound(mStatic.begin(), mStatic.end(), item, Compare());
if (it != mStatic.end() && it->mIndex == index) {
return &(*it);
}
return 0;
} }
const T *find(int index) const { const T *find(int index) const {
@ -1103,18 +1071,12 @@ namespace MWWorld
struct Store<ESM::Skill> : public IndexedStore<ESM::Skill> struct Store<ESM::Skill> : public IndexedStore<ESM::Skill>
{ {
Store() {} Store() {}
Store(unsigned int size)
: IndexedStore<ESM::Skill>(size)
{}
}; };
template <> template <>
struct Store<ESM::MagicEffect> : public IndexedStore<ESM::MagicEffect> struct Store<ESM::MagicEffect> : public IndexedStore<ESM::MagicEffect>
{ {
Store() {} Store() {}
Store(unsigned int size)
: IndexedStore<ESM::MagicEffect>(size)
{}
}; };
template <> template <>

Loading…
Cancel
Save