From d8c9a71e18de21784d091505762579e609f257c6 Mon Sep 17 00:00:00 2001 From: Learath2 Date: Fri, 9 Oct 2020 22:49:52 +0200 Subject: [PATCH] Remove usages of qsort --- src/engine/client/client.cpp | 25 +++++++++++-------------- src/engine/client/client.h | 2 +- src/game/editor/editor.cpp | 30 ++++++++++++------------------ 3 files changed, 24 insertions(+), 33 deletions(-) diff --git a/src/engine/client/client.cpp b/src/engine/client/client.cpp index 5f2b2254e..d0490b6a1 100644 --- a/src/engine/client/client.cpp +++ b/src/engine/client/client.cpp @@ -6,7 +6,6 @@ #include #include -#include // qsort #include #include @@ -1239,27 +1238,25 @@ const char *CClient::LoadMapSearch(const char *pMapName, SHA256_DIGEST *pWantedS return pError; } -int CClient::PlayerScoreNameComp(const void *a, const void *b) +bool CClient::PlayerScoreNameLess(const CServerInfo::CClient &p0, const CServerInfo::CClient &p1) { - CServerInfo::CClient *p0 = (CServerInfo::CClient *)a; - CServerInfo::CClient *p1 = (CServerInfo::CClient *)b; - if(p0->m_Player && !p1->m_Player) - return -1; - if(!p0->m_Player && p1->m_Player) - return 1; + if(p0.m_Player && !p1.m_Player) + return true; + if(!p0.m_Player && p1.m_Player) + return false; - int Score0 = p0->m_Score; - int Score1 = p1->m_Score; + int Score0 = p0.m_Score; + int Score1 = p1.m_Score; if(Score0 == -9999) Score0 = INT_MIN; if(Score1 == -9999) Score1 = INT_MIN; if(Score0 > Score1) - return -1; + return true; if(Score0 < Score1) - return 1; - return str_comp_nocase(p0->m_aName, p1->m_aName); + return false; + return str_comp_nocase(p0.m_aName, p1.m_aName) < 0; } void CClient::ProcessConnlessPacket(CNetChunk *pPacket) @@ -1520,7 +1517,7 @@ void CClient::ProcessServerInfo(int RawType, NETADDR *pFrom, const void *pData, if(!Up.Error() || IgnoreError) { - qsort(Info.m_aClients, Info.m_NumReceivedClients, sizeof(*Info.m_aClients), PlayerScoreNameComp); + std::sort(Info.m_aClients, Info.m_aClients + Info.m_NumReceivedClients, PlayerScoreNameLess); if(!DuplicatedPacket && (!pEntry || !pEntry->m_GotInfo || SavedType >= pEntry->m_Info.m_Type)) { diff --git a/src/engine/client/client.h b/src/engine/client/client.h index 6f91d0684..f2649bfa6 100644 --- a/src/engine/client/client.h +++ b/src/engine/client/client.h @@ -352,7 +352,7 @@ public: const char *LoadMap(const char *pName, const char *pFilename, SHA256_DIGEST *pWantedSha256, unsigned WantedCrc); const char *LoadMapSearch(const char *pMapName, SHA256_DIGEST *pWantedSha256, int WantedCrc); - static int PlayerScoreNameComp(const void *a, const void *b); + static bool PlayerScoreNameLess(const CServerInfo::CClient &p0, const CServerInfo::CClient &p1); void ProcessConnlessPacket(CNetChunk *pPacket); void ProcessServerInfo(int Type, NETADDR *pFrom, const void *pData, int DataSize); diff --git a/src/game/editor/editor.cpp b/src/game/editor/editor.cpp index bc8f671a2..2ad519a35 100644 --- a/src/game/editor/editor.cpp +++ b/src/game/editor/editor.cpp @@ -3908,43 +3908,37 @@ void CEditor::SelectGameLayer() } } -static int CompareImageName(const void *pObject1, const void *pObject2) +static bool ImageNameLess(const CEditorImage *const &a, const CEditorImage *const &b) { - CEditorImage *pImage1 = *(CEditorImage **)pObject1; - CEditorImage *pImage2 = *(CEditorImage **)pObject2; - - return str_comp(pImage1->m_aName, pImage2->m_aName); + return str_comp(a->m_aName, b->m_aName) < 0; } static int *gs_pSortedIndex = 0; static void ModifySortedIndex(int *pIndex) { - if(*pIndex > -1) + if(*pIndex >= 0) *pIndex = gs_pSortedIndex[*pIndex]; } void CEditor::SortImages() { - bool Sorted = true; - for(int i = 1; i < m_Map.m_lImages.size(); i++) - if(str_comp(m_Map.m_lImages[i]->m_aName, m_Map.m_lImages[i - 1]->m_aName) < 0) - { - Sorted = false; - break; - } - - if(!Sorted) + if(!std::is_sorted(m_Map.m_lImages.base_ptr(), m_Map.m_lImages.base_ptr() + m_Map.m_lImages.size(), ImageNameLess)) { array lTemp = m_Map.m_lImages; gs_pSortedIndex = new int[lTemp.size()]; - qsort(m_Map.m_lImages.base_ptr(), m_Map.m_lImages.size(), sizeof(CEditorImage *), CompareImageName); - + std::sort(m_Map.m_lImages.base_ptr(), m_Map.m_lImages.base_ptr() + m_Map.m_lImages.size(), ImageNameLess); for(int OldIndex = 0; OldIndex < lTemp.size(); OldIndex++) + { for(int NewIndex = 0; NewIndex < m_Map.m_lImages.size(); NewIndex++) + { if(lTemp[OldIndex] == m_Map.m_lImages[NewIndex]) + { gs_pSortedIndex[OldIndex] = NewIndex; - + break; + } + } + } m_Map.ModifyImageIndex(ModifySortedIndex); delete[] gs_pSortedIndex;