2738: Don't search the sorted array linearly r=heinrich5991 a=Learath2

Small optimization but it could start mattering with #2733

Co-authored-by: Learath <learath2@gmail.com>
This commit is contained in:
bors[bot] 2020-09-10 22:30:46 +00:00 committed by GitHub
commit f6e1ed5f19
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 12 deletions

View file

@ -196,7 +196,7 @@ const CSkins::CSkin *CSkins::Get(int Index)
return &m_aSkins[Index % m_aSkins.size()];
}
int CSkins::Find(const char *pName) const
int CSkins::Find(const char *pName)
{
const char *pSkinPrefix = m_EventSkinPrefix[0] ? m_EventSkinPrefix : g_Config.m_ClSkinPrefix;
if(g_Config.m_ClVanillaSkinsOnly && !IsVanillaSkin(pName))
@ -217,14 +217,11 @@ int CSkins::Find(const char *pName) const
return FindImpl(pName);
}
int CSkins::FindImpl(const char *pName) const
int CSkins::FindImpl(const char *pName)
{
for(int i = 0; i < m_aSkins.size(); i++)
{
if(str_comp(m_aSkins[i].m_aName, pName) == 0)
{
return i;
}
}
return -1;
auto r = ::find_binary(m_aSkins.all(), pName);
if(r.empty())
return -1;
return &r.front() - m_aSkins.base_ptr();
}

View file

@ -20,19 +20,22 @@ public:
ColorRGBA m_BloodColor;
bool operator<(const CSkin &Other) { return str_comp(m_aName, Other.m_aName) < 0; }
bool operator<(const char *pOther) { return str_comp(m_aName, pOther) < 0; }
bool operator==(const char *pOther) { return !str_comp(m_aName, pOther); }
};
void OnInit();
int Num();
const CSkin *Get(int Index);
int Find(const char *pName) const;
int Find(const char *pName);
private:
sorted_array<CSkin> m_aSkins;
char m_EventSkinPrefix[100];
int FindImpl(const char *pName) const;
int FindImpl(const char *pName);
static int SkinScan(const char *pName, int IsDir, int DirType, void *pUser);
};
#endif