Skip to content

Commit

Permalink
use stable_sort everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
ilteroi committed Apr 29, 2023
1 parent 2dedb4d commit 73a7cbb
Show file tree
Hide file tree
Showing 25 changed files with 72 additions and 72 deletions.
2 changes: 1 addition & 1 deletion CvGameCoreDLL_Expansion2/CvAIOperation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ int CvAIOperation::GrabUnitsFromTheReserves(CvPlot* pMusterPlot, CvPlot* pTarget
LogOperationSpecialMessage(strMsg);
}

std::sort(choices.begin(),choices.end());
std::stable_sort(choices.begin(),choices.end());

//rebuild the list
int iCount = 0;
Expand Down
6 changes: 3 additions & 3 deletions CvGameCoreDLL_Expansion2/CvAStar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2822,7 +2822,7 @@ map<CvPlot*,SPath> CvPathFinder::GetMultiplePaths(const CvPlot* pStartPlot, vect
{
bool operator()(const CvPlot* lhs, const CvPlot* rhs) const { return lhs->GetPlotIndex() < rhs->GetPlotIndex(); }
};
std::sort( vDestPlots.begin(), vDestPlots.end(), PrSortByPlotIndex() );
std::stable_sort( vDestPlots.begin(), vDestPlots.end(), PrSortByPlotIndex() );

//there is no destination! the return value will always be false
CvAStar::FindPathWithCurrentConfiguration(pStartPlot->getX(),pStartPlot->getY(), -1, -1, data);
Expand Down Expand Up @@ -3639,7 +3639,7 @@ void ReachablePlots::createIndex()
lookup.reserve(storage.size());
for (size_t i = 0; i < storage.size(); i++)
lookup.push_back( make_pair(storage[i].iPlotIndex,i) );
sort(lookup.begin(), lookup.end(), PairCompareFirst());
std::stable_sort(lookup.begin(), lookup.end(), PairCompareFirst());
}

struct EqualRangeComparison
Expand Down Expand Up @@ -3678,7 +3678,7 @@ void ReachablePlots::insertWithIndex(const SMovePlot& plot)
{
lookup.push_back( make_pair(plot.iPlotIndex,storage.size()) );
storage.push_back(plot);
sort(lookup.begin(), lookup.end(), PairCompareFirst());
std::stable_sort(lookup.begin(), lookup.end(), PairCompareFirst());
}

FDataStream & operator >> (FDataStream & kStream, CvPathNode & node)
Expand Down
4 changes: 2 additions & 2 deletions CvGameCoreDLL_Expansion2/CvAdvisorCounsel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void CvAdvisorCounsel::BuildCounselList(PlayerTypes ePlayer)
CvAssertMsg(bSuccess, "Unable to add counsel to list. Too many strategies running at once");
uiCounselIndex++;

std::sort(&m_aCounsel[0], &m_aCounsel[0] + m_aCounsel.size(), CounselSort);
std::stable_sort(&m_aCounsel[0], &m_aCounsel[0] + m_aCounsel.size(), CounselSort);
return;
}

Expand Down Expand Up @@ -1986,7 +1986,7 @@ void CvAdvisorCounsel::BuildCounselList(PlayerTypes ePlayer)
}

// sort the list
std::sort(&m_aCounsel[0], &m_aCounsel[0] + m_aCounsel.size(), CounselSort);
std::stable_sort(&m_aCounsel[0], &m_aCounsel[0] + m_aCounsel.size(), CounselSort);
}

bool CvAdvisorCounsel::SetCounselEntry(uint uiIndex, AdvisorTypes eAdvisor, const char* strAdvisorCounsel, int iValue)
Expand Down
2 changes: 1 addition & 1 deletion CvGameCoreDLL_Expansion2/CvArmyAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ CvPlot* CvArmyAI::GetCenterOfMass(bool bClampToUnit, float* pfVarX, float* pfVar
return NULL;

//this sorts ascending!
std::sort(vPlots.begin(),vPlots.end());
std::stable_sort(vPlots.begin(),vPlots.end());
return vPlots.front().pPlot;
}
else
Expand Down
2 changes: 1 addition & 1 deletion CvGameCoreDLL_Expansion2/CvBeliefClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3757,7 +3757,7 @@ std::vector<int> CvReligionBeliefs::GetFreePromotions(PlayerTypes ePlayer, const
// sort and remove duplicates
if (rtnVector.size() > 1)
{
std::sort(rtnVector.begin(), rtnVector.end());
std::stable_sort(rtnVector.begin(), rtnVector.end());
rtnVector.erase( std::unique( rtnVector.begin(), rtnVector.end() ), rtnVector.end() );
}

Expand Down
2 changes: 1 addition & 1 deletion CvGameCoreDLL_Expansion2/CvBuilderTaskingAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ BuilderDirective CvBuilderTaskingAI::EvaluateBuilder(CvUnit* pUnit, const map<Cv
}
}

sort(m_aDirectives.begin(), m_aDirectives.end());
std::stable_sort(m_aDirectives.begin(), m_aDirectives.end());
LogDirectives(pUnit);

//nothing found?
Expand Down
4 changes: 2 additions & 2 deletions CvGameCoreDLL_Expansion2/CvCity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29291,7 +29291,7 @@ void CvCity::GetBuyablePlotList(std::vector<int>& aiPlotList, bool bForPurchase,
}

//we want only the best
std::sort(resultList.begin(), resultList.end());
std::stable_sort(resultList.begin(), resultList.end());
if (resultList.size() > (size_t)nChoices)
resultList.erase(resultList.begin() + nChoices, resultList.end());

Expand Down Expand Up @@ -31100,7 +31100,7 @@ CvUnit* CvCity::CreateUnit(UnitTypes eUnitType, UnitAITypes eAIType, UnitCreatio
if (!freeSlotsPerOp.empty())
{
//default sort order is ascending by first pair member
stable_sort(freeSlotsPerOp.begin(), freeSlotsPerOp.end());
std::stable_sort(freeSlotsPerOp.begin(), freeSlotsPerOp.end());
for (size_t i=0; i<freeSlotsPerOp.size(); i++)
if (GET_PLAYER(m_eOwner).getAIOperationByIndex( freeSlotsPerOp[i].second )->RecruitUnit(pUnit))
break;
Expand Down
4 changes: 2 additions & 2 deletions CvGameCoreDLL_Expansion2/CvCityConnections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,9 +580,9 @@ bool CvCityConnections::ShouldConnectToOtherPlayer(PlayerTypes eOtherPlayer)
void CvCityConnections::CheckPlotRouteStateChanges(PlotIndexStore& lastState, PlotIndexStore& newState)
{
//make sure the input is sorted and unique
std::sort(lastState.begin(),lastState.end());
std::stable_sort(lastState.begin(),lastState.end());
lastState.erase( std::unique(lastState.begin(),lastState.end()), lastState.end() );
std::sort(newState.begin(),newState.end());
std::stable_sort(newState.begin(),newState.end());
newState.erase( std::unique(newState.begin(),newState.end()), newState.end() );

PlotIndexStore addedPlots( newState.size() );
Expand Down
2 changes: 1 addition & 1 deletion CvGameCoreDLL_Expansion2/CvCityStrategyAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ void CvCityStrategyAI::PrecalcYieldStats()
}

//this sorts in descending order
sort(deviations.begin(), deviations.end());
std::stable_sort(deviations.begin(), deviations.end());

m_eMostAbundantYield = deviations.front().score > 0 ? deviations.front().option : NO_YIELD;
m_eMostDeficientYield = deviations.back().score < 0 ? deviations.back().option : NO_YIELD;
Expand Down
4 changes: 2 additions & 2 deletions CvGameCoreDLL_Expansion2/CvDealAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4349,7 +4349,7 @@ void CvDealAI::DoAddStrategicResourceToThem(CvDeal* pDeal, PlayerTypes eThem, in
}
}

sort(vOptions.begin(), vOptions.end());
std::stable_sort(vOptions.begin(), vOptions.end());
for (size_t i=0; i<vOptions.size(); i++)
{
ResourceTypes eResource = vOptions[i].option.first;
Expand Down Expand Up @@ -4438,7 +4438,7 @@ void CvDealAI::DoAddStrategicResourceToUs(CvDeal* pDeal, PlayerTypes eThem, int&
}
}

sort(vOptions.begin(), vOptions.end());
std::stable_sort(vOptions.begin(), vOptions.end());
for (size_t i=0; i<vOptions.size(); i++)
{
ResourceTypes eResource = vOptions[i].option.first;
Expand Down
4 changes: 2 additions & 2 deletions CvGameCoreDLL_Expansion2/CvDllDatabaseUtility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ void CvDllDatabaseUtility::orderHotkeyInfo(int** ppiSortedIndex, T* pHotkeyInfos
}

// sort the array
std::sort(viOrderPriority.begin(), viOrderPriority.end(), sortHotkeyPriority);
std::stable_sort(viOrderPriority.begin(), viOrderPriority.end(), sortHotkeyPriority);

// insert new order into the array to return
for(iI=0; iI<iLength; iI++)
Expand All @@ -1067,7 +1067,7 @@ void CvDllDatabaseUtility::orderHotkeyInfo(int** ppiSortedIndex, int* pHotkeyInd
}

// sort the array
std::sort(viOrderPriority.begin(), viOrderPriority.end(), sortHotkeyPriority);
std::stable_sort(viOrderPriority.begin(), viOrderPriority.end(), sortHotkeyPriority);

// insert new order into the array to return
for(iI=0; iI<iLength; iI++)
Expand Down
8 changes: 4 additions & 4 deletions CvGameCoreDLL_Expansion2/CvEconomicAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2115,7 +2115,7 @@ void CvEconomicAI::DoReconState()
//choose the one who is farthest out
if (!eligibleExplorers.empty())
{
std::sort( eligibleExplorers.begin(), eligibleExplorers.end() );
std::stable_sort( eligibleExplorers.begin(), eligibleExplorers.end() );
CvUnit* pNewExplorer = m_pPlayer->getUnit( eligibleExplorers.back().second );
pNewExplorer->AI_setUnitAIType(UNITAI_EXPLORE);
if(GC.getLogging() && GC.getAILogging())
Expand All @@ -2141,7 +2141,7 @@ void CvEconomicAI::DoReconState()
//choose the one who is closest
if (!eligibleExplorers.empty())
{
std::sort(eligibleExplorers.begin(), eligibleExplorers.end());
std::stable_sort(eligibleExplorers.begin(), eligibleExplorers.end());
CvUnit* pExplorer = m_pPlayer->getUnit(eligibleExplorers.front().second);

pExplorer->AI_setUnitAIType(pExplorer->getUnitInfo().GetDefaultUnitAIType());
Expand Down Expand Up @@ -2213,7 +2213,7 @@ void CvEconomicAI::DoReconState()
//choose the one who is farthest out
if (!eligibleExplorers.empty())
{
std::sort( eligibleExplorers.begin(), eligibleExplorers.end() );
std::stable_sort( eligibleExplorers.begin(), eligibleExplorers.end() );
CvUnit* pNewExplorer = m_pPlayer->getUnit( eligibleExplorers.back().second );
pNewExplorer->AI_setUnitAIType(UNITAI_EXPLORE_SEA);
if(GC.getLogging() && GC.getAILogging())
Expand All @@ -2239,7 +2239,7 @@ void CvEconomicAI::DoReconState()
//choose the one who is closest
if (!eligibleExplorers.empty())
{
std::sort(eligibleExplorers.begin(), eligibleExplorers.end());
std::stable_sort(eligibleExplorers.begin(), eligibleExplorers.end());
CvUnit* pExplorer = m_pPlayer->getUnit(eligibleExplorers.front().second);

pExplorer->AI_setUnitAIType(pExplorer->getUnitInfo().GetDefaultUnitAIType());
Expand Down
2 changes: 1 addition & 1 deletion CvGameCoreDLL_Expansion2/CvFractal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ void CvFractal::ridgeBuilder(CvRandom& random, int iNumVoronoiSeeds, int iRidgeF

// use the modified distance between the two closest seeds to determine the ridge height
// ??? are there any other fudge factors I want to add in here???
//std::sort(vDistances.begin(),vDistances.end());
//std::stable_sort(vDistances.begin(),vDistances.end());

int iRidgeHeight = (255 * iClosestSeed) / iNextClosestSeed;

Expand Down
6 changes: 3 additions & 3 deletions CvGameCoreDLL_Expansion2/CvGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11271,7 +11271,7 @@ int CvGame::CalculateMedianNumCities()
if (v.empty())
return 0;

std::sort(v.begin(), v.end());
std::stable_sort(v.begin(), v.end());

return v[v.size()/2];
}
Expand All @@ -11292,7 +11292,7 @@ int CvGame::CalculateMedianNumPlots()
if (v.empty())
return 0;

std::sort(v.begin(), v.end());
std::stable_sort(v.begin(), v.end());

return v[v.size()/2];
}
Expand All @@ -11313,7 +11313,7 @@ int CvGame::CalculateMedianNumWondersConstructed()
if (v.empty())
return 0;

std::sort(v.begin(), v.end());
std::stable_sort(v.begin(), v.end());

return v[v.size()/2];
}
Expand Down
2 changes: 1 addition & 1 deletion CvGameCoreDLL_Expansion2/CvGameCoreUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ T PseudoRandomChoiceByWeight(vector<OptionWithScore<T>>& candidates, const T& de
return candidates[0].option;

size_t maxCandidates = min(candidates.size(), (size_t)maxCandidatesToConsider);
sort(candidates.begin(), candidates.end());
std::stable_sort(candidates.begin(), candidates.end());

int totalWeight = 0;
for (size_t i = 0; i < maxCandidates; i++)
Expand Down
2 changes: 1 addition & 1 deletion CvGameCoreDLL_Expansion2/CvHomelandAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6042,7 +6042,7 @@ vector<SPatrolTarget> HomelandAIHelpers::GetPatrolTargets(PlayerTypes ePlayer, b
}

//default sort is descending!
std::sort( vTargets.begin(), vTargets.end() );
std::stable_sort( vTargets.begin(), vTargets.end() );

vector<SPatrolTarget> vResult;
for (size_t i=0; i<MIN(vTargets.size(),(size_t)nMaxTargets); i++)
Expand Down
6 changes: 3 additions & 3 deletions CvGameCoreDLL_Expansion2/CvMilitaryAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ size_t CvMilitaryAI::UpdateAttackTargets()
if (!vAttackOptions.empty())
{
//some might be present twice but we sort that out below
std::sort(vAttackOptions.begin(), vAttackOptions.end());
std::stable_sort(vAttackOptions.begin(), vAttackOptions.end());

int iBestScore = vAttackOptions.front().score;
for (size_t i = 0; i < vAttackOptions.size(); i++)
Expand Down Expand Up @@ -903,7 +903,7 @@ size_t CvMilitaryAI::UpdateAttackTargets()

if (!vDefenseOptions.empty())
{
std::sort(vDefenseOptions.begin(), vDefenseOptions.end());
std::stable_sort(vDefenseOptions.begin(), vDefenseOptions.end());

int iBestScore = vDefenseOptions.front().score;
for (size_t i = 0; i < vDefenseOptions.size(); i++)
Expand Down Expand Up @@ -2511,7 +2511,7 @@ CvUnit* CvMilitaryAI::FindUselessShip()
{
bool operator()(const CvUnit* lhs, const CvUnit* rhs) const { return lhs->getExperienceTimes100() < rhs->getExperienceTimes100(); }
};
std::sort( candidates.begin(), candidates.end(), PrSortByExperience() );
std::stable_sort( candidates.begin(), candidates.end(), PrSortByExperience() );

//check other areas we can reach before deciding
for (size_t i=0; i<candidates.size(); i++)
Expand Down
16 changes: 8 additions & 8 deletions CvGameCoreDLL_Expansion2/CvPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5290,7 +5290,7 @@ vector<CvCity*> CvPlayer::GetThreatenedCities(bool bCoastalOnly)
result.push_back(pLoopCity);
}

sort(result.begin(), result.end(), SortByThreatLevel());
std::stable_sort(result.begin(), result.end(), SortByThreatLevel());
return result;
}

Expand Down Expand Up @@ -18065,7 +18065,7 @@ int CvPlayer::GetAverageProductionTimes100() const
}
int iNumCities = max((int)viCityProduction.size(), 1);
// Sort production from lowest to highest
sort(viCityProduction.begin(), viCityProduction.end());
std::stable_sort(viCityProduction.begin(), viCityProduction.end());

// Add the X highest city production together
int iCurrentCity = 0;
Expand Down Expand Up @@ -18103,7 +18103,7 @@ int CvPlayer::GetAverageInstantProductionTimes100()
}
int iNumCities = max((int)viCityProduction.size(), 1);
// Sort production from lowest to highest
sort(viCityProduction.begin(), viCityProduction.end());
std::stable_sort(viCityProduction.begin(), viCityProduction.end());

// Add the X highest city production together
int iCurrentCity = 0;
Expand Down Expand Up @@ -23747,7 +23747,7 @@ bool CvPlayer::UpdateCityConnection(const CvPlot * pPlot, bool bActive)

//insert and sort
m_vCityConnectionPlots.push_back(pPlot->GetPlotIndex());
std::sort(m_vCityConnectionPlots.begin(), m_vCityConnectionPlots.end());
std::stable_sort(m_vCityConnectionPlots.begin(), m_vCityConnectionPlots.end());
return true;
}
else
Expand Down Expand Up @@ -49925,10 +49925,10 @@ void CvPlayer::SetBestWonderCities()
for (map<BuildingClassTypes,vector<sct>>::iterator it = allScores.begin(); it != allScores.end(); ++it)
{
ASSERT(!it->second.empty());
sort(it->second.begin(), it->second.end());
std::stable_sort(it->second.begin(), it->second.end());
bestScorePerClass.push_back(make_pair(it->second.back().score, it->first));
}
sort(bestScorePerClass.begin(), bestScorePerClass.end());
std::stable_sort(bestScorePerClass.begin(), bestScorePerClass.end());

//now we starting with the best overall score, find the best city for the type of wonder
for (vector<pair<int, BuildingClassTypes>>::reverse_iterator it = bestScorePerClass.rbegin(); it != bestScorePerClass.rend(); ++it)
Expand Down Expand Up @@ -49969,7 +49969,7 @@ void CvPlayer::SetBestWonderCities()
it2->score -= it2->score / 4; //reduce by 25% for every other world wonder

//need to sort again ...
sort(it->second.begin(), it->second.end());
std::stable_sort(it->second.begin(), it->second.end());
}
}
}
Expand Down Expand Up @@ -52310,7 +52310,7 @@ void CvPlayer::computeFoundValueThreshold()
if (vValues.empty())
return;

std::sort(vValues.begin(), vValues.end());
std::stable_sort(vValues.begin(), vValues.end());

//set our threshold halfway between the worst plot and the median
m_iReferenceFoundValue = (vValues[0] + vValues[vValues.size()/2])/2;
Expand Down
4 changes: 2 additions & 2 deletions CvGameCoreDLL_Expansion2/CvPlayerAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ void CvPlayerAI::AI_considerAnnex()
if (!options.empty())
{
//descending by default
sort(options.begin(), options.end());
std::stable_sort(options.begin(), options.end());

CvCity* pTargetCity = options.front().option;
if (pTargetCity)
Expand Down Expand Up @@ -1926,7 +1926,7 @@ CvPlot* CvPlayerAI::FindBestMerchantTargetPlotForCash(CvUnit* pMerchant)
}
}

sort(vCandidates.begin(), vCandidates.end());
std::stable_sort(vCandidates.begin(), vCandidates.end());

int iFlags = CvUnit::MOVEFLAG_NO_ENEMY_TERRITORY | CvUnit::MOVEFLAG_APPROX_TARGET_RING1 | CvUnit::MOVEFLAG_APPROX_TARGET_NATIVE_DOMAIN;
for (size_t i = 0; i < vCandidates.size(); i++)
Expand Down

0 comments on commit 73a7cbb

Please sign in to comment.