Use multimap to speed up custom map marker code

sceneinput
scrawl 10 years ago
parent 91d71d0fcd
commit 41bed4c7d9

@ -92,31 +92,41 @@ namespace MWGui
void CustomMarkerCollection::addMarker(const ESM::CustomMarker &marker, bool triggerEvent)
{
mMarkers.push_back(marker);
mMarkers.insert(std::make_pair(marker.mCell, marker));
if (triggerEvent)
eventMarkersChanged();
}
void CustomMarkerCollection::deleteMarker(const ESM::CustomMarker &marker)
{
std::vector<ESM::CustomMarker>::iterator it = std::find(mMarkers.begin(), mMarkers.end(), marker);
if (it != mMarkers.end())
mMarkers.erase(it);
else
throw std::runtime_error("can't find marker to delete");
std::pair<ContainerType::iterator, ContainerType::iterator> range = mMarkers.equal_range(marker.mCell);
for (ContainerType::iterator it = range.first; it != range.second; ++it)
{
if (it->second == marker)
{
mMarkers.erase(it);
eventMarkersChanged();
return;
}
}
throw std::runtime_error("can't find marker to delete");
}
void CustomMarkerCollection::updateMarker(const ESM::CustomMarker &marker, const std::string &newNote)
{
std::vector<ESM::CustomMarker>::iterator it = std::find(mMarkers.begin(), mMarkers.end(), marker);
if (it != mMarkers.end())
it->mNote = newNote;
else
throw std::runtime_error("can't find marker to update");
std::pair<ContainerType::iterator, ContainerType::iterator> range = mMarkers.equal_range(marker.mCell);
for (ContainerType::iterator it = range.first; it != range.second; ++it)
{
if (it->second == marker)
{
it->second.mNote = newNote;
eventMarkersChanged();
return;
}
}
throw std::runtime_error("can't find marker to update");
}
void CustomMarkerCollection::clear()
@ -125,16 +135,21 @@ namespace MWGui
eventMarkersChanged();
}
std::vector<ESM::CustomMarker>::const_iterator CustomMarkerCollection::begin() const
CustomMarkerCollection::ContainerType::const_iterator CustomMarkerCollection::begin() const
{
return mMarkers.begin();
}
std::vector<ESM::CustomMarker>::const_iterator CustomMarkerCollection::end() const
CustomMarkerCollection::ContainerType::const_iterator CustomMarkerCollection::end() const
{
return mMarkers.end();
}
CustomMarkerCollection::RangeType CustomMarkerCollection::getMarkers(const ESM::CellId &cellId) const
{
return mMarkers.equal_range(cellId);
}
size_t CustomMarkerCollection::size() const
{
return mMarkers.size();
@ -299,24 +314,20 @@ namespace MWGui
MyGUI::Gui::getInstance().destroyWidget(*it);
mCustomMarkerWidgets.clear();
for (std::vector<ESM::CustomMarker>::const_iterator it = mCustomMarkers.begin(); it != mCustomMarkers.end(); ++it)
for (int dX = -1; dX <= 1; ++dX)
{
const ESM::CustomMarker& marker = *it;
if (marker.mCell.mPaged != !mInterior)
continue;
if (mInterior)
for (int dY =-1; dY <= 1; ++dY)
{
if (marker.mCell.mWorldspace != mPrefix)
continue;
}
else
ESM::CellId cellId;
cellId.mPaged = !mInterior;
cellId.mWorldspace = (mInterior ? mPrefix : "sys::default");
cellId.mIndex.mX = mCurX+dX;
cellId.mIndex.mY = mCurY+dY;
CustomMarkerCollection::RangeType markers = mCustomMarkers.getMarkers(cellId);
for (CustomMarkerCollection::ContainerType::const_iterator it = markers.first; it != markers.second; ++it)
{
if (std::abs(marker.mCell.mIndex.mX - mCurX) > 1)
continue;
if (std::abs(marker.mCell.mIndex.mY - mCurY) > 1)
continue;
}
const ESM::CustomMarker& marker = it->second;
MarkerUserData markerPos (mLocalMapRender);
MyGUI::IntPoint widgetPos = getMarkerPosition(marker.mWorldX, marker.mWorldY, markerPos);
@ -337,6 +348,9 @@ namespace MWGui
customMarkerCreated(markerWidget);
mCustomMarkerWidgets.push_back(markerWidget);
}
}
}
redraw();
}
@ -411,11 +425,9 @@ namespace MWGui
MWBase::World::DoorMarker marker = *it;
std::vector<std::string> destNotes;
for (std::vector<ESM::CustomMarker>::const_iterator it = mCustomMarkers.begin(); it != mCustomMarkers.end(); ++it)
{
if (it->mCell == marker.dest)
destNotes.push_back(it->mNote);
}
CustomMarkerCollection::RangeType markers = mCustomMarkers.getMarkers(marker.dest);
for (CustomMarkerCollection::ContainerType::const_iterator it = markers.first; it != markers.second; ++it)
destNotes.push_back(it->second.mNote);
MarkerUserData data (mLocalMapRender);
data.notes = destNotes;

@ -42,14 +42,20 @@ namespace MWGui
size_t size() const;
std::vector<ESM::CustomMarker>::const_iterator begin() const;
std::vector<ESM::CustomMarker>::const_iterator end() const;
typedef std::multimap<ESM::CellId, ESM::CustomMarker> ContainerType;
typedef std::pair<ContainerType::const_iterator, ContainerType::const_iterator> RangeType;
ContainerType::const_iterator begin() const;
ContainerType::const_iterator end() const;
RangeType getMarkers(const ESM::CellId& cellId) const;
typedef MyGUI::delegates::CMultiDelegate0 EventHandle_Void;
EventHandle_Void eventMarkersChanged;
private:
std::vector<ESM::CustomMarker> mMarkers;
ContainerType mMarkers;
};
class LocalMapBase

@ -1672,10 +1672,10 @@ namespace MWGui
writer.endRecord(ESM::REC_ASPL);
}
for (std::vector<ESM::CustomMarker>::const_iterator it = mCustomMarkers.begin(); it != mCustomMarkers.end(); ++it)
for (CustomMarkerCollection::ContainerType::const_iterator it = mCustomMarkers.begin(); it != mCustomMarkers.end(); ++it)
{
writer.startRecord(ESM::REC_MARK);
(*it).save(writer);
it->second.save(writer);
writer.endRecord(ESM::REC_MARK);
}
}

@ -35,3 +35,26 @@ bool ESM::operator!= (const CellId& left, const CellId& right)
{
return !(left==right);
}
bool ESM::operator < (const CellId& left, const CellId& right)
{
if (left.mPaged < right.mPaged)
return true;
if (left.mPaged > right.mPaged)
return false;
if (left.mPaged)
{
if (left.mIndex.mX < right.mIndex.mX)
return true;
if (left.mIndex.mX > right.mIndex.mX)
return false;
if (left.mIndex.mY < right.mIndex.mY)
return true;
if (left.mIndex.mY > right.mIndex.mY)
return false;
}
return left.mWorldspace < right.mWorldspace;
}

@ -26,6 +26,7 @@ namespace ESM
bool operator== (const CellId& left, const CellId& right);
bool operator!= (const CellId& left, const CellId& right);
bool operator< (const CellId& left, const CellId& right);
}
#endif

Loading…
Cancel
Save