use stable_sort for sorting

This commit is contained in:
oy 2019-02-03 19:58:15 +01:00
parent 6deb18e8b0
commit 397e88e1d7
11 changed files with 43 additions and 63 deletions

View file

@ -4,6 +4,7 @@
#define BASE_TL_ALGORITHM_H
#include "range.h"
#include <algorithm>
/*
@ -110,7 +111,7 @@ void sort_quick(R range)
template<class R>
void sort(R range)
{
sort_bubble(range);
std::stable_sort(&range.front(), &range.back()+1);
}

View file

@ -1,8 +1,8 @@
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com. */
#include <new>
#include <algorithm>
#include <stdlib.h> // qsort
#include <stdarg.h>
#include <base/math.h>
@ -822,21 +822,6 @@ const char *CClient::LoadMapSearch(const char *pMapName, int WantedCrc)
return pError;
}
int CClient::PlayerScoreComp(const void *a, const void *b)
{
CServerInfo::CClient *p0 = (CServerInfo::CClient *)a;
CServerInfo::CClient *p1 = (CServerInfo::CClient *)b;
if(!(p0->m_PlayerType&CServerInfo::CClient::PLAYERFLAG_SPEC) && (p1->m_PlayerType&CServerInfo::CClient::PLAYERFLAG_SPEC))
return -1;
if((p0->m_PlayerType&CServerInfo::CClient::PLAYERFLAG_SPEC) && !(p1->m_PlayerType&CServerInfo::CClient::PLAYERFLAG_SPEC))
return 1;
if(p0->m_Score == p1->m_Score)
return 0;
if(p0->m_Score < p1->m_Score)
return 1;
return -1;
}
int CClient::UnpackServerInfo(CUnpacker *pUnpacker, CServerInfo *pInfo, int *pToken)
{
if(pToken)
@ -1009,7 +994,7 @@ void CClient::ProcessConnlessPacket(CNetChunk *pPacket)
int Token;
if(!UnpackServerInfo(&Up, &Info, &Token) && !Up.Error())
{
qsort(Info.m_aClients, Info.m_NumClients, sizeof(*Info.m_aClients), PlayerScoreComp);
std::stable_sort(Info.m_aClients, Info.m_aClients + Info.m_NumClients);
m_ServerBrowser.Set(pPacket->m_Address, CServerBrowser::SET_TOKEN, Token, &Info);
}
}
@ -1148,7 +1133,7 @@ void CClient::ProcessServerPacket(CNetChunk *pPacket)
net_addr_str(&pPacket->m_Address, Info.m_aAddress, sizeof(Info.m_aAddress), true);
if(!UnpackServerInfo(&Unpacker, &Info, 0) && !Unpacker.Error())
{
qsort(Info.m_aClients, Info.m_NumClients, sizeof(*Info.m_aClients), PlayerScoreComp);
std::stable_sort(Info.m_aClients, Info.m_aClients + Info.m_NumClients);
mem_copy(&m_CurrentServerInfo, &Info, sizeof(m_CurrentServerInfo));
m_CurrentServerInfo.m_NetAddr = m_ServerAddress;
}

View file

@ -255,8 +255,6 @@ public:
const char *LoadMap(const char *pName, const char *pFilename, unsigned WantedCrc);
const char *LoadMapSearch(const char *pMapName, int WantedCrc);
static int PlayerScoreComp(const void *a, const void *b);
int UnpackServerInfo(CUnpacker *pUnpacker, CServerInfo *pInfo, int *pToken);
void ProcessConnlessPacket(CNetChunk *pPacket);
void ProcessServerPacket(CNetChunk *pPacket);

View file

@ -33,6 +33,14 @@ public:
PLAYERFLAG_BOT=2,
PLAYERFLAG_MASK=3,
};
bool operator<(const CClient &Other) const
{
if(!(m_PlayerType&CServerInfo::CClient::PLAYERFLAG_SPEC) && ((Other.m_PlayerType&CServerInfo::CClient::PLAYERFLAG_SPEC) ||
!(Other.m_PlayerType&CServerInfo::CClient::PLAYERFLAG_SPEC) && m_Score > Other.m_Score))
return true;
return false;
}
};
//int m_SortedIndex;

View file

@ -16,7 +16,7 @@ public:
bool m_Blocked;
IGraphics::CTextureHandle m_Texture;
bool operator<(const CCountryFlag &Other) { return str_comp(m_aCountryCodeString, Other.m_aCountryCodeString) < 0; }
bool operator<(const CCountryFlag &Other) const { return str_comp(m_aCountryCodeString, Other.m_aCountryCodeString) < 0; }
};
void OnInit();

View file

@ -322,7 +322,7 @@ private:
bool m_HasDay;
bool m_HasNight;
IGraphics::CTextureHandle m_IconTexture;
bool operator<(const CTheme &Other) { return m_Name < Other.m_Name; }
bool operator<(const CTheme &Other) const { return m_Name < Other.m_Name; }
};
sorted_array<CTheme> m_lThemes;
@ -414,7 +414,7 @@ private:
bool m_Valid;
CDemoHeader m_Info;
bool operator<(const CDemoItem &Other) { return !str_comp(m_aFilename, "..") ? true : !str_comp(Other.m_aFilename, "..") ? false :
bool operator<(const CDemoItem &Other) const { return !str_comp(m_aFilename, "..") ? true : !str_comp(Other.m_aFilename, "..") ? false :
m_IsDir && !Other.m_IsDir ? true : !m_IsDir && Other.m_IsDir ? false :
str_comp_filenames(m_aFilename, Other.m_aFilename) < 0; }
};
@ -447,7 +447,7 @@ private:
m_pServerInfo = 0;
}
bool operator<(const CFriendItem &Other)
bool operator<(const CFriendItem &Other) const
{
if(m_aName[0] && !Other.m_aName[0])
return true;

View file

@ -186,30 +186,6 @@ void CTilesetMapper::Proceed(CLayerTiles *pLayer, int ConfigID)
m_pEditor->m_Map.m_Modified = true;
}
int CompareRules(const void *a, const void *b)
{
CDoodadsMapper::CRule *ra = (CDoodadsMapper::CRule*)a;
CDoodadsMapper::CRule *rb = (CDoodadsMapper::CRule*)b;
if((ra->m_Location == CDoodadsMapper::CRule::FLOOR && rb->m_Location == CDoodadsMapper::CRule::FLOOR)
|| (ra->m_Location == CDoodadsMapper::CRule::CEILING && rb->m_Location == CDoodadsMapper::CRule::CEILING))
{
if(ra->m_Size.x < rb->m_Size.x)
return +1;
if(rb->m_Size.x < ra->m_Size.x)
return -1;
}
else if(ra->m_Location == CDoodadsMapper::CRule::WALLS && rb->m_Location == CDoodadsMapper::CRule::WALLS)
{
if(ra->m_Size.y < rb->m_Size.y)
return +1;
if(rb->m_Size.y < ra->m_Size.y)
return -1;
}
return 0;
}
void CDoodadsMapper::Load(const json_value &rElement)
{
for(unsigned i = 0; i < rElement.u.array.length; ++i)
@ -307,7 +283,9 @@ void CDoodadsMapper::Load(const json_value &rElement)
// sort
for(int i = 0; i < m_aRuleSets.size(); i++)
qsort(m_aRuleSets[i].m_aRules.base_ptr(), m_aRuleSets[i].m_aRules.size(), sizeof(m_aRuleSets[i].m_aRules[0]), CompareRules);
{
std::stable_sort(&m_aRuleSets[i].m_aRules[0], &m_aRuleSets[i].m_aRules[m_aRuleSets[i].m_aRules.size()]);
}
}
const char* CDoodadsMapper::GetRuleSetName(int Index) const

View file

@ -118,6 +118,23 @@ public:
CEILING,
WALLS
};
bool operator<(const CRule &Other) const
{
if((m_Location == CDoodadsMapper::CRule::FLOOR && Other.m_Location == CDoodadsMapper::CRule::FLOOR)
|| (m_Location == CDoodadsMapper::CRule::CEILING && Other.m_Location == CDoodadsMapper::CRule::CEILING))
{
if(m_Size.x < Other.m_Size.x)
return true;
}
else if(m_Location == CDoodadsMapper::CRule::WALLS && Other.m_Location == CDoodadsMapper::CRule::WALLS)
{
if(m_Size.y < Other.m_Size.y)
return true;
}
return false;
}
};
struct CRuleSet

View file

@ -2824,14 +2824,6 @@ int CEditor::PopupImage(CEditor *pEditor, CUIRect View)
return 0;
}
static int CompareImageName(const void *pObject1, const void *pObject2)
{
CEditorImage *pImage1 = *(CEditorImage**)pObject1;
CEditorImage *pImage2 = *(CEditorImage**)pObject2;
return str_comp(pImage1->m_aName, pImage2->m_aName);
}
static int *gs_pSortedIndex = 0;
static void ModifySortedIndex(int *pIndex)
{
@ -2854,7 +2846,7 @@ void CEditor::SortImages()
array<CEditorImage*> lTemp = array<CEditorImage*>(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::stable_sort(&m_Map.m_lImages[0], &m_Map.m_lImages[m_Map.m_lImages.size()]);
for(int OldIndex = 0; OldIndex < lTemp.size(); OldIndex++)
for(int NewIndex = 0; NewIndex < m_Map.m_lImages.size(); NewIndex++)

View file

@ -3,7 +3,7 @@
#ifndef GAME_EDITOR_EDITOR_H
#define GAME_EDITOR_EDITOR_H
#include <math.h>
#include <algorithm>
#include <base/math.h>
#include <base/system.h>
@ -67,7 +67,7 @@ public:
void Resort()
{
sort(m_lPoints.all());
std::stable_sort(&m_lPoints[0], &m_lPoints[m_lPoints.size()]);
FindTopBottom(0xf);
}
@ -286,6 +286,7 @@ public:
char m_aName[128];
unsigned char m_aTileFlags[256];
class IAutoMapper *m_pAutoMapper;
bool operator<(const CEditorImage &Other) const { return str_comp(m_aName, Other.m_aName); }
};
class CEditorMap

View file

@ -198,7 +198,7 @@ struct CEnvPoint_v1
int m_Curvetype;
int m_aValues[4]; // 1-4 depending on envelope (22.10 fixed point)
bool operator<(const CEnvPoint_v1 &Other) { return m_Time < Other.m_Time; }
bool operator<(const CEnvPoint_v1 &Other) const { return m_Time < Other.m_Time; }
} ;
struct CEnvPoint : public CEnvPoint_v1
@ -210,7 +210,7 @@ struct CEnvPoint : public CEnvPoint_v1
int m_aOutTangentdx[4];
int m_aOutTangentdy[4];
bool operator<(const CEnvPoint& other) { return m_Time < other.m_Time; }
bool operator<(const CEnvPoint& other) const { return m_Time < other.m_Time; }
};
struct CMapItemEnvelope_v1