CAiFuncMap: Avoid redundant map lookups in getters

We can just use the result of the find operation instead of rehashing
into the map to retrieve the entry.

This also allows us to make the getter const qualified.
This commit is contained in:
Lioncash 2019-09-08 17:55:18 -04:00
parent 1cfe83d8d1
commit 2f06fcba0a
2 changed files with 16 additions and 8 deletions

View File

@ -134,15 +134,23 @@ CAiFuncMap::CAiFuncMap() {
CAi::CreateFuncLookup(this); CAi::CreateFuncLookup(this);
} }
CAiStateFunc CAiFuncMap::GetStateFunc(const char* func) { CAiStateFunc CAiFuncMap::GetStateFunc(const char* func) const {
if (x0_stateFuncs.find(func) == x0_stateFuncs.end()) const auto iter = x0_stateFuncs.find(func);
if (iter == x0_stateFuncs.cend()) {
return nullptr; return nullptr;
return x0_stateFuncs[func];
} }
CAiTriggerFunc CAiFuncMap::GetTriggerFunc(const char* func) { return iter->second;
if (x10_triggerFuncs.find(func) == x10_triggerFuncs.end()) }
CAiTriggerFunc CAiFuncMap::GetTriggerFunc(const char* func) const {
const auto iter = x10_triggerFuncs.find(func);
if (iter == x10_triggerFuncs.cend()) {
return nullptr; return nullptr;
return x10_triggerFuncs[func]; }
return iter->second;
} }
} // namespace urde } // namespace urde

View File

@ -18,7 +18,7 @@ class CAiFuncMap {
public: public:
CAiFuncMap(); CAiFuncMap();
CAiStateFunc GetStateFunc(const char*); CAiStateFunc GetStateFunc(const char*) const;
CAiTriggerFunc GetTriggerFunc(const char*); CAiTriggerFunc GetTriggerFunc(const char*) const;
}; };
} // namespace urde } // namespace urde