CWeaponMgr: Avoid unnecessary repeated std::map lookups

Avoids trivial redundant std::map indexes by doing the lookup once and
then referring to the looked up object instead of doing repeated
lookups.
This commit is contained in:
Lioncash 2020-04-07 13:32:02 -04:00
parent db0091e9cd
commit 8ba2af9b5a
1 changed files with 35 additions and 18 deletions

View File

@ -3,47 +3,64 @@
namespace urde {
void CWeaponMgr::Add(TUniqueId uid, EWeaponType type) {
x0_weapons.insert(std::make_pair(uid, rstl::reserved_vector<s32, 15>()));
x0_weapons[uid].resize(15);
++x0_weapons[uid][u32(type)];
auto iter = x0_weapons.emplace(uid, rstl::reserved_vector<s32, 15>()).first;
iter->second.resize(15);
++iter->second[size_t(type)];
}
void CWeaponMgr::Remove(TUniqueId uid) {
s32 totalActive = 0;
for (u32 i = 0; i < 10; ++i)
totalActive += x0_weapons[uid][i];
const auto& weapon = x0_weapons[uid];
s32 totalActive = 0;
for (size_t i = 0; i < 10; ++i) {
totalActive += weapon[i];
}
if (totalActive != 0) {
return;
}
if (totalActive == 0)
x0_weapons.erase(uid);
}
void CWeaponMgr::IncrCount(TUniqueId uid, EWeaponType type) {
if (GetIndex(uid) < 0)
if (GetIndex(uid) < 0) {
Add(uid, type);
else
} else {
x0_weapons[uid][u32(type)]++;
}
}
void CWeaponMgr::DecrCount(TUniqueId uid, EWeaponType type) {
if (GetIndex(uid) < 0)
if (GetIndex(uid) < 0) {
return;
}
auto& weapon = x0_weapons[uid];
weapon[size_t(type)]--;
if (weapon[size_t(type)] > 0) {
return;
}
x0_weapons[uid][u32(type)]--;
if (x0_weapons[uid][u32(type)] <= 0)
Remove(uid);
}
s32 CWeaponMgr::GetNumActive(TUniqueId uid, EWeaponType type) const {
if (GetIndex(uid) < 0)
if (GetIndex(uid) < 0) {
return 0;
}
return x0_weapons.at(uid)[u32(type)];
return x0_weapons.at(uid)[size_t(type)];
}
s32 CWeaponMgr::GetIndex(TUniqueId uid) const {
if (x0_weapons.find(uid) == x0_weapons.end())
const auto iter = x0_weapons.find(uid);
if (iter == x0_weapons.cend()) {
return -1;
return s32(std::distance(x0_weapons.begin(), x0_weapons.find(uid)));
}
return s32(std::distance(x0_weapons.cbegin(), iter));
}
} // namespace urde