Fixed: Use map for faster search for example (implemented a TODO of GUIGUI)

This commit is contained in:
kervala 2016-01-13 20:04:01 +01:00
parent 29f56159b1
commit d55b781374
3 changed files with 27 additions and 31 deletions

View file

@ -94,70 +94,59 @@ void CVisualSlotManager::init()
f.close(); f.close();
// Display elements read. // Display elements read.
// for(uint i=0; i<_VisualSlot.size(); ++i) for(uint i=0, len = _VisualSlot.size(); i<len; ++i)
// { {
_VisualSlot[i].updateMaps();
// for(uint j=0; j<_VisualSlot[i].Element.size(); ++j) // for(uint j=0; j<_VisualSlot[i].Element.size(); ++j)
// nlinfo("Visu: %d Num: %d Id: %d sheet: %s", i, _VisualSlot[i].Element[j].Index, _VisualSlot[i].Element[j].SheetId.asInt(), _VisualSlot[i].Element[j].SheetId.toString().c_str()); // nlinfo("Visu: %d Num: %d Id: %d sheet: %s", i, _VisualSlot[i].Element[j].Index, _VisualSlot[i].Element[j].SheetId.asInt(), _VisualSlot[i].Element[j].SheetId.toString().c_str());
// } }
} }
else else
nlwarning("VSMngr:load: cannot open the file '%s'.", filename.c_str()); nlwarning("VSMngr:load: cannot open the file '%s'.", filename.c_str());
}// init // }// init //
void CVisualSlotManager::TElementList::updateMaps()
{
SheetIdToIndexMap.clear();
for(uint i=0, len = Element.size(); i<len; ++i)
{
const TElement &e = Element[i];
SheetIdToIndexMap[e.SheetId] = e.Index;
}
}
//----------------------------------------------- //-----------------------------------------------
// rightItem2Index : // rightItem2Index :
// Return the visual slot index from a sheet Id for items in right hand. // Return the visual slot index from a sheet Id for items in right hand.
// \todo GUIGUI : Use map for faster search for example
//----------------------------------------------- //-----------------------------------------------
uint32 CVisualSlotManager::rightItem2Index(const NLMISC::CSheetId &id) uint32 CVisualSlotManager::rightItem2Index(const NLMISC::CSheetId &id)
{ {
if(SLOTTYPE::RIGHT_HAND_SLOT < _VisualSlot.size()) return sheet2Index(id, SLOTTYPE::RIGHT_HAND_SLOT);
{
for(uint i=0; i<_VisualSlot[SLOTTYPE::RIGHT_HAND_SLOT].Element.size(); ++i)
if(_VisualSlot[SLOTTYPE::RIGHT_HAND_SLOT].Element[i].SheetId == id)
return _VisualSlot[SLOTTYPE::RIGHT_HAND_SLOT].Element[i].Index;
}
else
nlwarning("VSMngr:rightItem2Index: Bad slot -> you probably need to rebuild the tab.");
// No Item
return 0;
}// rightItem2Index // }// rightItem2Index //
//----------------------------------------------- //-----------------------------------------------
// leftItem2Index : // leftItem2Index :
// Return the visual slot index from a sheet Id for items in left hand. // Return the visual slot index from a sheet Id for items in left hand.
// \todo GUIGUI : Use map for faster search for example
//----------------------------------------------- //-----------------------------------------------
uint32 CVisualSlotManager::leftItem2Index(const NLMISC::CSheetId &id) uint32 CVisualSlotManager::leftItem2Index(const NLMISC::CSheetId &id)
{ {
if(SLOTTYPE::LEFT_HAND_SLOT < _VisualSlot.size()) return sheet2Index(id, SLOTTYPE::LEFT_HAND_SLOT);
{
for(uint i=0; i<_VisualSlot[SLOTTYPE::LEFT_HAND_SLOT].Element.size(); ++i)
if(_VisualSlot[SLOTTYPE::LEFT_HAND_SLOT].Element[i].SheetId == id)
return _VisualSlot[SLOTTYPE::LEFT_HAND_SLOT].Element[i].Index;
}
else
nlwarning("VSMngr:leftItem2Index: Bad slot -> you probably need to rebuild the tab.");
// No Item
return 0;
}// leftItem2Index // }// leftItem2Index //
//----------------------------------------------- //-----------------------------------------------
// sheet2Index : // sheet2Index :
// Return the visual index from a sheet Id and the visual slot. // Return the visual index from a sheet Id and the visual slot.
// \todo GUIGUI : Use map for faster search for example
//----------------------------------------------- //-----------------------------------------------
uint32 CVisualSlotManager::sheet2Index(const NLMISC::CSheetId &id, SLOTTYPE::EVisualSlot slot) uint32 CVisualSlotManager::sheet2Index(const NLMISC::CSheetId &id, SLOTTYPE::EVisualSlot slot)
{ {
if((uint)slot < _VisualSlot.size()) if((uint)slot < _VisualSlot.size())
{ {
for(uint i=0; i<_VisualSlot[slot].Element.size(); ++i) const TElementList &el = _VisualSlot[slot];
if(_VisualSlot[slot].Element[i].SheetId == id) TElementList::SheetIdToIndexMapType::const_iterator it = el.SheetIdToIndexMap.find(id);
return _VisualSlot[slot].Element[i].Index; if (it != el.SheetIdToIndexMap.end()) return it->second;
} }
else else
nlwarning("VSMngr:sheet2Index: Bad slot '%d' -> you probably need to rebuild the tab.", (sint)slot); nlwarning("VSMngr:sheet2Index: Bad slot '%d' -> you probably need to rebuild the tab.", (sint)slot);

View file

@ -71,11 +71,17 @@ public:
// elements list for a visual slot. // elements list for a visual slot.
std::vector<TElement> Element; std::vector<TElement> Element;
// std::map to increase access speed
typedef std::map<NLMISC::CSheetId, uint32> SheetIdToIndexMapType;
SheetIdToIndexMapType SheetIdToIndexMap;
/// Load/Save the values using the serial system /// Load/Save the values using the serial system
void serial(class NLMISC::IStream &s) throw(NLMISC::EStream) void serial(class NLMISC::IStream &s) throw(NLMISC::EStream)
{ {
s.serialCont(Element); s.serialCont(Element);
} }
void updateMaps();
} TElementList; } TElementList;
typedef std::vector <TElementList> TVisualSlot; typedef std::vector <TElementList> TVisualSlot;

View file

@ -1472,6 +1472,7 @@ void SetupDirectories()
data.readFromFile( "raw_material_generation.cfg" ); data.readFromFile( "raw_material_generation.cfg" );
// beurk :s Use CConfigFile instead
LEVEL_DESIGN_PATH = data.splitFrom( "LevelDesignPath = \"").splitTo( "\"" ); LEVEL_DESIGN_PATH = data.splitFrom( "LevelDesignPath = \"").splitTo( "\"" );
TRANSLATION_PATH = data.splitFrom( "TranslationPath = \"" ).splitTo( "\"" ); TRANSLATION_PATH = data.splitFrom( "TranslationPath = \"" ).splitTo( "\"" );