From 397e88e1d7073bd417455926fd0b7193d4067f13 Mon Sep 17 00:00:00 2001 From: oy Date: Sun, 3 Feb 2019 19:58:15 +0100 Subject: [PATCH] use stable_sort for sorting --- src/base/tl/algorithm.h | 3 ++- src/engine/client/client.cpp | 21 +++-------------- src/engine/client/client.h | 2 -- src/engine/serverbrowser.h | 8 +++++++ src/game/client/components/countryflags.h | 2 +- src/game/client/components/menus.h | 6 ++--- src/game/editor/auto_map.cpp | 28 +++-------------------- src/game/editor/auto_map.h | 17 ++++++++++++++ src/game/editor/editor.cpp | 10 +------- src/game/editor/editor.h | 5 ++-- src/game/mapitems.h | 4 ++-- 11 files changed, 43 insertions(+), 63 deletions(-) diff --git a/src/base/tl/algorithm.h b/src/base/tl/algorithm.h index 011609182..3f361ed5e 100644 --- a/src/base/tl/algorithm.h +++ b/src/base/tl/algorithm.h @@ -4,6 +4,7 @@ #define BASE_TL_ALGORITHM_H #include "range.h" +#include /* @@ -110,7 +111,7 @@ void sort_quick(R range) template void sort(R range) { - sort_bubble(range); + std::stable_sort(&range.front(), &range.back()+1); } diff --git a/src/engine/client/client.cpp b/src/engine/client/client.cpp index e8f708c14..f1fca781d 100644 --- a/src/engine/client/client.cpp +++ b/src/engine/client/client.cpp @@ -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 +#include -#include // qsort #include #include @@ -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; } diff --git a/src/engine/client/client.h b/src/engine/client/client.h index 5c3586a27..c6609df52 100644 --- a/src/engine/client/client.h +++ b/src/engine/client/client.h @@ -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); diff --git a/src/engine/serverbrowser.h b/src/engine/serverbrowser.h index 6fdbe1353..9af753231 100644 --- a/src/engine/serverbrowser.h +++ b/src/engine/serverbrowser.h @@ -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; diff --git a/src/game/client/components/countryflags.h b/src/game/client/components/countryflags.h index c11d537fc..07e887e1a 100644 --- a/src/game/client/components/countryflags.h +++ b/src/game/client/components/countryflags.h @@ -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(); diff --git a/src/game/client/components/menus.h b/src/game/client/components/menus.h index 7c11fece8..bb2ed6b86 100644 --- a/src/game/client/components/menus.h +++ b/src/game/client/components/menus.h @@ -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 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; diff --git a/src/game/editor/auto_map.cpp b/src/game/editor/auto_map.cpp index e23ca4baf..c7525cf4a 100644 --- a/src/game/editor/auto_map.cpp +++ b/src/game/editor/auto_map.cpp @@ -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 diff --git a/src/game/editor/auto_map.h b/src/game/editor/auto_map.h index e13cd7510..7a66b55ec 100644 --- a/src/game/editor/auto_map.h +++ b/src/game/editor/auto_map.h @@ -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 diff --git a/src/game/editor/editor.cpp b/src/game/editor/editor.cpp index 6edccbe68..dcced1112 100644 --- a/src/game/editor/editor.cpp +++ b/src/game/editor/editor.cpp @@ -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 lTemp = array(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++) diff --git a/src/game/editor/editor.h b/src/game/editor/editor.h index 5b180c7c3..afabb2e47 100644 --- a/src/game/editor/editor.h +++ b/src/game/editor/editor.h @@ -3,7 +3,7 @@ #ifndef GAME_EDITOR_EDITOR_H #define GAME_EDITOR_EDITOR_H -#include +#include #include #include @@ -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 diff --git a/src/game/mapitems.h b/src/game/mapitems.h index b23a4d477..ba89d34b0 100644 --- a/src/game/mapitems.h +++ b/src/game/mapitems.h @@ -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