3046: Remove usages of qsort r=def- a=Learath2

Caught my eye while doing the integrity PR.

Co-authored-by: Learath2 <learath2@gmail.com>
This commit is contained in:
bors[bot] 2020-10-10 08:38:12 +00:00 committed by GitHub
commit 9d39e1da6d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 33 deletions

View file

@ -6,7 +6,6 @@
#include <new> #include <new>
#include <stdarg.h> #include <stdarg.h>
#include <stdlib.h> // qsort
#include <tuple> #include <tuple>
#include <base/hash_ctxt.h> #include <base/hash_ctxt.h>
@ -1239,27 +1238,25 @@ const char *CClient::LoadMapSearch(const char *pMapName, SHA256_DIGEST *pWantedS
return pError; 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; if(p0.m_Player && !p1.m_Player)
CServerInfo::CClient *p1 = (CServerInfo::CClient *)b; return true;
if(p0->m_Player && !p1->m_Player) if(!p0.m_Player && p1.m_Player)
return -1; return false;
if(!p0->m_Player && p1->m_Player)
return 1;
int Score0 = p0->m_Score; int Score0 = p0.m_Score;
int Score1 = p1->m_Score; int Score1 = p1.m_Score;
if(Score0 == -9999) if(Score0 == -9999)
Score0 = INT_MIN; Score0 = INT_MIN;
if(Score1 == -9999) if(Score1 == -9999)
Score1 = INT_MIN; Score1 = INT_MIN;
if(Score0 > Score1) if(Score0 > Score1)
return -1; return true;
if(Score0 < Score1) if(Score0 < Score1)
return 1; return false;
return str_comp_nocase(p0->m_aName, p1->m_aName); return str_comp_nocase(p0.m_aName, p1.m_aName) < 0;
} }
void CClient::ProcessConnlessPacket(CNetChunk *pPacket) void CClient::ProcessConnlessPacket(CNetChunk *pPacket)
@ -1520,7 +1517,7 @@ void CClient::ProcessServerInfo(int RawType, NETADDR *pFrom, const void *pData,
if(!Up.Error() || IgnoreError) 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)) if(!DuplicatedPacket && (!pEntry || !pEntry->m_GotInfo || SavedType >= pEntry->m_Info.m_Type))
{ {

View file

@ -352,7 +352,7 @@ public:
const char *LoadMap(const char *pName, const char *pFilename, SHA256_DIGEST *pWantedSha256, unsigned WantedCrc); 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); 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 ProcessConnlessPacket(CNetChunk *pPacket);
void ProcessServerInfo(int Type, NETADDR *pFrom, const void *pData, int DataSize); void ProcessServerInfo(int Type, NETADDR *pFrom, const void *pData, int DataSize);

View file

@ -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; return str_comp(a->m_aName, b->m_aName) < 0;
CEditorImage *pImage2 = *(CEditorImage **)pObject2;
return str_comp(pImage1->m_aName, pImage2->m_aName);
} }
static int *gs_pSortedIndex = 0; static int *gs_pSortedIndex = 0;
static void ModifySortedIndex(int *pIndex) static void ModifySortedIndex(int *pIndex)
{ {
if(*pIndex > -1) if(*pIndex >= 0)
*pIndex = gs_pSortedIndex[*pIndex]; *pIndex = gs_pSortedIndex[*pIndex];
} }
void CEditor::SortImages() void CEditor::SortImages()
{ {
bool Sorted = true; if(!std::is_sorted(m_Map.m_lImages.base_ptr(), m_Map.m_lImages.base_ptr() + m_Map.m_lImages.size(), ImageNameLess))
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)
{ {
array<CEditorImage *> lTemp = m_Map.m_lImages; array<CEditorImage *> lTemp = m_Map.m_lImages;
gs_pSortedIndex = new int[lTemp.size()]; 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 OldIndex = 0; OldIndex < lTemp.size(); OldIndex++)
{
for(int NewIndex = 0; NewIndex < m_Map.m_lImages.size(); NewIndex++) for(int NewIndex = 0; NewIndex < m_Map.m_lImages.size(); NewIndex++)
{
if(lTemp[OldIndex] == m_Map.m_lImages[NewIndex]) if(lTemp[OldIndex] == m_Map.m_lImages[NewIndex])
{
gs_pSortedIndex[OldIndex] = NewIndex; gs_pSortedIndex[OldIndex] = NewIndex;
break;
}
}
}
m_Map.ModifyImageIndex(ModifySortedIndex); m_Map.ModifyImageIndex(ModifySortedIndex);
delete[] gs_pSortedIndex; delete[] gs_pSortedIndex;