diff --git a/src/base/tl/algorithm.h b/src/base/tl/algorithm.h index 74d649fdf..b2fe29ea3 100644 --- a/src/base/tl/algorithm.h +++ b/src/base/tl/algorithm.h @@ -68,7 +68,7 @@ R find_linear(R range, T value) template 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) diff --git a/src/base/tl/sorted_array.h b/src/base/tl/sorted_array.h index f4c1dd95e..87b9b59bb 100644 --- a/src/base/tl/sorted_array.h +++ b/src/base/tl/sorted_array.h @@ -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 diff --git a/src/engine/shared/uuid_manager.cpp b/src/engine/shared/uuid_manager.cpp index 34f3c2166..4ff714ee7 100644 --- a/src/engine/shared/uuid_manager.cpp +++ b/src/engine/shared/uuid_manager.cpp @@ -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::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; } diff --git a/src/engine/shared/uuid_manager.h b/src/engine/shared/uuid_manager.h index 3d7560f82..97e153540 100644 --- a/src/engine/shared/uuid_manager.h +++ b/src/engine/shared/uuid_manager.h @@ -2,6 +2,7 @@ #define ENGINE_SHARED_UUID_MANAGER_H #include +#include 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 m_aNames; + sorted_array m_aNamesSorted; public: void RegisterName(int ID, const char *pName);