mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-10 10:08:18 +00:00
Optimize uuid lookup
This commit is contained in:
parent
2917681bf5
commit
9e8cbfea5d
|
@ -68,7 +68,7 @@ R find_linear(R range, T value)
|
|||
template<class R, class T>
|
||||
R find_binary(R range, T value)
|
||||
{
|
||||
range = partition_linear(range, value);
|
||||
range = partition_binary(range, value);
|
||||
if(range.empty())
|
||||
return range;
|
||||
if(range.front() == value)
|
||||
|
|
|
@ -46,6 +46,7 @@ public:
|
|||
Returns a sorted range that contains the whole array.
|
||||
*/
|
||||
range all() { return range(parent::list, parent::list + parent::num_elements); }
|
||||
range all() const { return range(parent::list, parent::list + parent::num_elements); }
|
||||
};
|
||||
|
||||
#endif // TL_FILE_SORTED_ARRAY_HPP
|
||||
|
|
|
@ -61,12 +61,12 @@ void ParseUuid(CUuid *pUuid, char *pBuffer)
|
|||
&p[8], &p[9], &p[10], &p[11], &p[12], &p[13], &p[14], &p[15]);
|
||||
}
|
||||
|
||||
bool CUuid::operator==(const CUuid &Other)
|
||||
bool CUuid::operator==(const CUuid &Other) const
|
||||
{
|
||||
return mem_comp(this, &Other, sizeof(*this)) == 0;
|
||||
}
|
||||
|
||||
bool CUuid::operator!=(const CUuid &Other)
|
||||
bool CUuid::operator!=(const CUuid &Other) const
|
||||
{
|
||||
return !(*this == Other);
|
||||
}
|
||||
|
@ -90,6 +90,11 @@ void CUuidManager::RegisterName(int ID, const char *pName)
|
|||
dbg_assert(LookupUuid(Name.m_Uuid) == -1, "duplicate uuid");
|
||||
|
||||
m_aNames.add(Name);
|
||||
|
||||
CNameIndexed NameIndexed;
|
||||
NameIndexed.m_Uuid = Name.m_Uuid;
|
||||
NameIndexed.m_ID = GetIndex(ID);
|
||||
m_aNamesSorted.add(NameIndexed);
|
||||
}
|
||||
|
||||
CUuid CUuidManager::GetUuid(int ID) const
|
||||
|
@ -104,12 +109,10 @@ const char *CUuidManager::GetName(int ID) const
|
|||
|
||||
int CUuidManager::LookupUuid(CUuid Uuid) const
|
||||
{
|
||||
for(int i = 0; i < m_aNames.size(); i++)
|
||||
sorted_array<CNameIndexed>::range it = ::find_binary(m_aNamesSorted.all(), Uuid);
|
||||
if(!it.empty())
|
||||
{
|
||||
if(Uuid == m_aNames[i].m_Uuid)
|
||||
{
|
||||
return GetID(i);
|
||||
}
|
||||
return GetID(it.front().m_ID);
|
||||
}
|
||||
return UUID_UNKNOWN;
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#define ENGINE_SHARED_UUID_MANAGER_H
|
||||
|
||||
#include <base/tl/array.h>
|
||||
#include <base/tl/sorted_array.h>
|
||||
|
||||
enum
|
||||
{
|
||||
|
@ -17,8 +18,9 @@ struct CUuid
|
|||
{
|
||||
unsigned char m_aData[16];
|
||||
|
||||
bool operator==(const CUuid &Other);
|
||||
bool operator!=(const CUuid &Other);
|
||||
bool operator==(const CUuid &Other) const;
|
||||
bool operator!=(const CUuid &Other) const;
|
||||
bool operator<(const CUuid &Other) const { return mem_comp(m_aData, Other.m_aData, sizeof(m_aData)) < 0; }
|
||||
};
|
||||
|
||||
CUuid RandomUuid();
|
||||
|
@ -33,12 +35,23 @@ struct CName
|
|||
const char *m_pName;
|
||||
};
|
||||
|
||||
struct CNameIndexed
|
||||
{
|
||||
CUuid m_Uuid;
|
||||
int m_ID;
|
||||
|
||||
bool operator<(const CNameIndexed &Other) const { return m_Uuid < Other.m_Uuid; }
|
||||
bool operator<(const CUuid &Other) const { return m_Uuid < Other; }
|
||||
bool operator==(const CUuid &Other) const { return m_Uuid == Other; }
|
||||
};
|
||||
|
||||
class CPacker;
|
||||
class CUnpacker;
|
||||
|
||||
class CUuidManager
|
||||
{
|
||||
array<CName> m_aNames;
|
||||
sorted_array<CNameIndexed> m_aNamesSorted;
|
||||
|
||||
public:
|
||||
void RegisterName(int ID, const char *pName);
|
||||
|
|
Loading…
Reference in a new issue