std::string_view can be used in map and unordered map

This avoids some unecessary dynamic allocations.
Also applies some review advice.
7220-lua-add-a-general-purpose-lexical-parser
florent.teppe 2 years ago
parent 63e44eddc9
commit a4137e941c

@ -36,7 +36,6 @@ namespace MWMechanics
, mX(x) , mX(x)
, mY(y) , mY(y)
, mZ(z) , mZ(z)
, mCellId("")
, mActive(false) , mActive(false)
, mFollowIndex(mFollowIndexCounter++) , mFollowIndex(mFollowIndexCounter++)
{ {
@ -67,7 +66,6 @@ namespace MWMechanics
, mX(0) , mX(0)
, mY(0) , mY(0)
, mZ(0) , mZ(0)
, mCellId("")
, mActive(false) , mActive(false)
, mFollowIndex(mFollowIndexCounter++) , mFollowIndex(mFollowIndexCounter++)
{ {
@ -170,7 +168,7 @@ namespace MWMechanics
{ {
if (actor.getCell()->isExterior()) // Outside? if (actor.getCell()->isExterior()) // Outside?
{ {
if (mCellId == "") // No cell to travel to if (mCellId.empty()) // No cell to travel to
{ {
mRemainingDuration = mDuration; mRemainingDuration = mDuration;
return true; return true;

@ -515,14 +515,13 @@ namespace MWWorld
} }
const ESM::Cell* Store<ESM::Cell>::search(std::string_view name) const const ESM::Cell* Store<ESM::Cell>::search(std::string_view name) const
{ {
const std::string nameString = std::string(name); DynamicInt::const_iterator it = mInt.find(name);
DynamicInt::const_iterator it = mInt.find(nameString);
if (it != mInt.end()) if (it != mInt.end())
{ {
return &(it->second); return &(it->second);
} }
DynamicInt::const_iterator dit = mDynamicInt.find(nameString); DynamicInt::const_iterator dit = mDynamicInt.find(name);
if (dit != mDynamicInt.end()) if (dit != mDynamicInt.end())
{ {
return &dit->second; return &dit->second;
@ -815,8 +814,7 @@ namespace MWWorld
} }
bool Store<ESM::Cell>::erase(std::string_view name) bool Store<ESM::Cell>::erase(std::string_view name)
{ {
const std::string nameString = std::string(name); auto it = mDynamicInt.find(name);
auto it = mDynamicInt.find(nameString);
if (it == mDynamicInt.end()) if (it == mDynamicInt.end())
{ {

@ -332,7 +332,8 @@ namespace MWWorld
} }
}; };
typedef std::unordered_map<std::string, ESM::Cell, Misc::StringUtils::CiHash> DynamicInt; typedef std::unordered_map<std::string, ESM::Cell, Misc::StringUtils::CiHash, Misc::StringUtils::CiEqual>
DynamicInt;
typedef std::map<std::pair<int, int>, ESM::Cell, DynamicExtCmp> DynamicExt; typedef std::map<std::pair<int, int>, ESM::Cell, DynamicExtCmp> DynamicExt;

@ -1399,7 +1399,7 @@ namespace MWWorld
esmPos.pos[0] = traced.x(); esmPos.pos[0] = traced.x();
esmPos.pos[1] = traced.y(); esmPos.pos[1] = traced.y();
esmPos.pos[2] = traced.z(); esmPos.pos[2] = traced.z();
std::string_view cell = ""; std::string_view cell;
if (!actor.getCell()->isExterior()) if (!actor.getCell()->isExterior())
cell = actor.getCell()->getCell()->mName; cell = actor.getCell()->getCell()->mName;
MWWorld::ActionTeleport(cell, esmPos, false).execute(actor); MWWorld::ActionTeleport(cell, esmPos, false).execute(actor);

@ -64,7 +64,7 @@ MWWorld::CellStore* MWWorld::WorldModel::getCellStore(const ESM::Cell* cell)
{ {
if (cell->mData.mFlags & ESM::Cell::Interior) if (cell->mData.mFlags & ESM::Cell::Interior)
{ {
auto result = mInteriors.find(std::string(cell->mName)); auto result = mInteriors.find(cell->mName);
if (result == mInteriors.end()) if (result == mInteriors.end())
result = mInteriors.emplace(cell->mName, CellStore(cell, mStore, mReaders)).first; result = mInteriors.emplace(cell->mName, CellStore(cell, mStore, mReaders)).first;
@ -198,7 +198,7 @@ MWWorld::CellStore* MWWorld::WorldModel::getExterior(int x, int y)
MWWorld::CellStore* MWWorld::WorldModel::getInterior(std::string_view name) MWWorld::CellStore* MWWorld::WorldModel::getInterior(std::string_view name)
{ {
auto result = mInteriors.find(std::string(name)); auto result = mInteriors.find(name);
if (result == mInteriors.end()) if (result == mInteriors.end())
{ {

@ -175,7 +175,7 @@ namespace
.mEnd = TilePosition(1, 1), .mEnd = TilePosition(1, 1),
}; };
manager.setRange(range, nullptr); manager.setRange(range, nullptr);
manager.setWorldspace(mWorldspace, nullptr); manager.setWorldspace("worldspace", nullptr);
const btBoxShape boxShape(btVector3(20, 20, 100)); const btBoxShape boxShape(btVector3(20, 20, 100));
const btTransform transform( const btTransform transform(

@ -94,6 +94,8 @@ namespace Misc::StringUtils
struct CiComp struct CiComp
{ {
using is_transparent = void;
bool operator()(std::string_view left, std::string_view right) const { return ciLess(left, right); } bool operator()(std::string_view left, std::string_view right) const { return ciLess(left, right); }
}; };

Loading…
Cancel
Save