Codechange: use std::sort() in EngList_Sort[Partial]()

This commit is contained in:
glx 2019-04-11 22:17:30 +02:00 committed by PeterN
parent 2db88953e7
commit b52561fd38
4 changed files with 123 additions and 131 deletions

View File

@ -32,11 +32,9 @@
void DrawEngineList(VehicleType type, int x, int r, int y, const GUIEngineList *eng_list, uint16 min, uint16 max, EngineID selected_id, bool show_count, GroupID selected_group);
static int CDECL EngineNumberSorter(const EngineID *a, const EngineID *b)
static bool EngineNumberSorter(const EngineID &a, const EngineID &b)
{
int r = Engine::Get(*a)->list_position - Engine::Get(*b)->list_position;
return r;
return Engine::Get(a)->list_position < Engine::Get(b)->list_position;
}
/**

View File

@ -99,57 +99,54 @@ static CargoID _engine_sort_last_cargo_criteria[] = {CF_ANY, CF_ANY, CF_ANY, CF_
/**
* Determines order of engines by engineID
* @param *a first engine to compare
* @param *b second engine to compare
* @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
* @param a first engine to compare
* @param b second engine to compare
* @return for descending order: returns true if a < b. Vice versa for ascending order
*/
static int CDECL EngineNumberSorter(const EngineID *a, const EngineID *b)
static bool EngineNumberSorter(const EngineID &a, const EngineID &b)
{
int r = Engine::Get(*a)->list_position - Engine::Get(*b)->list_position;
int r = Engine::Get(a)->list_position - Engine::Get(b)->list_position;
return _engine_sort_direction ? -r : r;
return _engine_sort_direction ? r > 0 : r < 0;
}
/**
* Determines order of engines by introduction date
* @param *a first engine to compare
* @param *b second engine to compare
* @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
* @param a first engine to compare
* @param b second engine to compare
* @return for descending order: returns true if a < b. Vice versa for ascending order
*/
static int CDECL EngineIntroDateSorter(const EngineID *a, const EngineID *b)
static bool EngineIntroDateSorter(const EngineID &a, const EngineID &b)
{
const int va = Engine::Get(*a)->intro_date;
const int vb = Engine::Get(*b)->intro_date;
const int va = Engine::Get(a)->intro_date;
const int vb = Engine::Get(b)->intro_date;
const int r = va - vb;
/* Use EngineID to sort instead since we want consistent sorting */
if (r == 0) return EngineNumberSorter(a, b);
return _engine_sort_direction ? -r : r;
return _engine_sort_direction ? r > 0 : r < 0;
}
/**
* Determines order of engines by name
* @param *a first engine to compare
* @param *b second engine to compare
* @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
* @param a first engine to compare
* @param b second engine to compare
* @return for descending order: returns true if a < b. Vice versa for ascending order
*/
static int CDECL EngineNameSorter(const EngineID *a, const EngineID *b)
static bool EngineNameSorter(const EngineID &a, const EngineID &b)
{
static EngineID last_engine[2] = { INVALID_ENGINE, INVALID_ENGINE };
static char last_name[2][64] = { "\0", "\0" };
const EngineID va = *a;
const EngineID vb = *b;
if (va != last_engine[0]) {
last_engine[0] = va;
SetDParam(0, va);
if (a != last_engine[0]) {
last_engine[0] = a;
SetDParam(0, a);
GetString(last_name[0], STR_ENGINE_NAME, lastof(last_name[0]));
}
if (vb != last_engine[1]) {
last_engine[1] = vb;
SetDParam(0, vb);
if (b != last_engine[1]) {
last_engine[1] = b;
SetDParam(0, b);
GetString(last_name[1], STR_ENGINE_NAME, lastof(last_name[1]));
}
@ -157,121 +154,121 @@ static int CDECL EngineNameSorter(const EngineID *a, const EngineID *b)
/* Use EngineID to sort instead since we want consistent sorting */
if (r == 0) return EngineNumberSorter(a, b);
return _engine_sort_direction ? -r : r;
return _engine_sort_direction ? r > 0 : r < 0;
}
/**
* Determines order of engines by reliability
* @param *a first engine to compare
* @param *b second engine to compare
* @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
* @param a first engine to compare
* @param b second engine to compare
* @return for descending order: returns true if a < b. Vice versa for ascending order
*/
static int CDECL EngineReliabilitySorter(const EngineID *a, const EngineID *b)
static bool EngineReliabilitySorter(const EngineID &a, const EngineID &b)
{
const int va = Engine::Get(*a)->reliability;
const int vb = Engine::Get(*b)->reliability;
const int va = Engine::Get(a)->reliability;
const int vb = Engine::Get(b)->reliability;
const int r = va - vb;
/* Use EngineID to sort instead since we want consistent sorting */
if (r == 0) return EngineNumberSorter(a, b);
return _engine_sort_direction ? -r : r;
return _engine_sort_direction ? r > 0 : r < 0;
}
/**
* Determines order of engines by purchase cost
* @param *a first engine to compare
* @param *b second engine to compare
* @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
* @param a first engine to compare
* @param b second engine to compare
* @return for descending order: returns true if a < b. Vice versa for ascending order
*/
static int CDECL EngineCostSorter(const EngineID *a, const EngineID *b)
static bool EngineCostSorter(const EngineID &a, const EngineID &b)
{
Money va = Engine::Get(*a)->GetCost();
Money vb = Engine::Get(*b)->GetCost();
Money va = Engine::Get(a)->GetCost();
Money vb = Engine::Get(b)->GetCost();
int r = ClampToI32(va - vb);
/* Use EngineID to sort instead since we want consistent sorting */
if (r == 0) return EngineNumberSorter(a, b);
return _engine_sort_direction ? -r : r;
return _engine_sort_direction ? r > 0 : r < 0;
}
/**
* Determines order of engines by speed
* @param *a first engine to compare
* @param *b second engine to compare
* @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
* @param a first engine to compare
* @param b second engine to compare
* @return for descending order: returns true if a < b. Vice versa for ascending order
*/
static int CDECL EngineSpeedSorter(const EngineID *a, const EngineID *b)
static bool EngineSpeedSorter(const EngineID &a, const EngineID &b)
{
int va = Engine::Get(*a)->GetDisplayMaxSpeed();
int vb = Engine::Get(*b)->GetDisplayMaxSpeed();
int va = Engine::Get(a)->GetDisplayMaxSpeed();
int vb = Engine::Get(b)->GetDisplayMaxSpeed();
int r = va - vb;
/* Use EngineID to sort instead since we want consistent sorting */
if (r == 0) return EngineNumberSorter(a, b);
return _engine_sort_direction ? -r : r;
return _engine_sort_direction ? r > 0 : r < 0;
}
/**
* Determines order of engines by power
* @param *a first engine to compare
* @param *b second engine to compare
* @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
* @param a first engine to compare
* @param b second engine to compare
* @return for descending order: returns true if a < b. Vice versa for ascending order
*/
static int CDECL EnginePowerSorter(const EngineID *a, const EngineID *b)
static bool EnginePowerSorter(const EngineID &a, const EngineID &b)
{
int va = Engine::Get(*a)->GetPower();
int vb = Engine::Get(*b)->GetPower();
int va = Engine::Get(a)->GetPower();
int vb = Engine::Get(b)->GetPower();
int r = va - vb;
/* Use EngineID to sort instead since we want consistent sorting */
if (r == 0) return EngineNumberSorter(a, b);
return _engine_sort_direction ? -r : r;
return _engine_sort_direction ? r > 0 : r < 0;
}
/**
* Determines order of engines by tractive effort
* @param *a first engine to compare
* @param *b second engine to compare
* @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
* @param a first engine to compare
* @param b second engine to compare
* @return for descending order: returns true if a < b. Vice versa for ascending order
*/
static int CDECL EngineTractiveEffortSorter(const EngineID *a, const EngineID *b)
static bool EngineTractiveEffortSorter(const EngineID &a, const EngineID &b)
{
int va = Engine::Get(*a)->GetDisplayMaxTractiveEffort();
int vb = Engine::Get(*b)->GetDisplayMaxTractiveEffort();
int va = Engine::Get(a)->GetDisplayMaxTractiveEffort();
int vb = Engine::Get(b)->GetDisplayMaxTractiveEffort();
int r = va - vb;
/* Use EngineID to sort instead since we want consistent sorting */
if (r == 0) return EngineNumberSorter(a, b);
return _engine_sort_direction ? -r : r;
return _engine_sort_direction ? r > 0 : r < 0;
}
/**
* Determines order of engines by running costs
* @param *a first engine to compare
* @param *b second engine to compare
* @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
* @param a first engine to compare
* @param b second engine to compare
* @return for descending order: returns true if a < b. Vice versa for ascending order
*/
static int CDECL EngineRunningCostSorter(const EngineID *a, const EngineID *b)
static bool EngineRunningCostSorter(const EngineID &a, const EngineID &b)
{
Money va = Engine::Get(*a)->GetRunningCost();
Money vb = Engine::Get(*b)->GetRunningCost();
Money va = Engine::Get(a)->GetRunningCost();
Money vb = Engine::Get(b)->GetRunningCost();
int r = ClampToI32(va - vb);
/* Use EngineID to sort instead since we want consistent sorting */
if (r == 0) return EngineNumberSorter(a, b);
return _engine_sort_direction ? -r : r;
return _engine_sort_direction ? r > 0 : r < 0;
}
/**
* Determines order of engines by running costs
* @param *a first engine to compare
* @param *b second engine to compare
* @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
* @param a first engine to compare
* @param b second engine to compare
* @return for descending order: returns true if a < b. Vice versa for ascending order
*/
static int CDECL EnginePowerVsRunningCostSorter(const EngineID *a, const EngineID *b)
static bool EnginePowerVsRunningCostSorter(const EngineID &a, const EngineID &b)
{
const Engine *e_a = Engine::Get(*a);
const Engine *e_b = Engine::Get(*b);
const Engine *e_a = Engine::Get(a);
const Engine *e_b = Engine::Get(b);
/* Here we are using a few tricks to get the right sort.
* We want power/running cost, but since we usually got higher running cost than power and we store the result in an int,
@ -285,79 +282,79 @@ static int CDECL EnginePowerVsRunningCostSorter(const EngineID *a, const EngineI
/* Use EngineID to sort instead since we want consistent sorting */
if (r == 0) return EngineNumberSorter(a, b);
return _engine_sort_direction ? -r : r;
return _engine_sort_direction ? r > 0 : r < 0;
}
/* Train sorting functions */
/**
* Determines order of train engines by capacity
* @param *a first engine to compare
* @param *b second engine to compare
* @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
* @param a first engine to compare
* @param b second engine to compare
* @return for descending order: returns true if a < b. Vice versa for ascending order
*/
static int CDECL TrainEngineCapacitySorter(const EngineID *a, const EngineID *b)
static bool TrainEngineCapacitySorter(const EngineID &a, const EngineID &b)
{
const RailVehicleInfo *rvi_a = RailVehInfo(*a);
const RailVehicleInfo *rvi_b = RailVehInfo(*b);
const RailVehicleInfo *rvi_a = RailVehInfo(a);
const RailVehicleInfo *rvi_b = RailVehInfo(b);
int va = GetTotalCapacityOfArticulatedParts(*a) * (rvi_a->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1);
int vb = GetTotalCapacityOfArticulatedParts(*b) * (rvi_b->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1);
int va = GetTotalCapacityOfArticulatedParts(a) * (rvi_a->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1);
int vb = GetTotalCapacityOfArticulatedParts(b) * (rvi_b->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1);
int r = va - vb;
/* Use EngineID to sort instead since we want consistent sorting */
if (r == 0) return EngineNumberSorter(a, b);
return _engine_sort_direction ? -r : r;
return _engine_sort_direction ? r > 0 : r < 0;
}
/**
* Determines order of train engines by engine / wagon
* @param *a first engine to compare
* @param *b second engine to compare
* @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
* @param a first engine to compare
* @param b second engine to compare
* @return for descending order: returns true if a < b. Vice versa for ascending order
*/
static int CDECL TrainEnginesThenWagonsSorter(const EngineID *a, const EngineID *b)
static bool TrainEnginesThenWagonsSorter(const EngineID &a, const EngineID &b)
{
int val_a = (RailVehInfo(*a)->railveh_type == RAILVEH_WAGON ? 1 : 0);
int val_b = (RailVehInfo(*b)->railveh_type == RAILVEH_WAGON ? 1 : 0);
int val_a = (RailVehInfo(a)->railveh_type == RAILVEH_WAGON ? 1 : 0);
int val_b = (RailVehInfo(b)->railveh_type == RAILVEH_WAGON ? 1 : 0);
int r = val_a - val_b;
/* Use EngineID to sort instead since we want consistent sorting */
if (r == 0) return EngineNumberSorter(a, b);
return _engine_sort_direction ? -r : r;
return _engine_sort_direction ? r > 0 : r < 0;
}
/* Road vehicle sorting functions */
/**
* Determines order of road vehicles by capacity
* @param *a first engine to compare
* @param *b second engine to compare
* @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
* @param a first engine to compare
* @param b second engine to compare
* @return for descending order: returns true if a < b. Vice versa for ascending order
*/
static int CDECL RoadVehEngineCapacitySorter(const EngineID *a, const EngineID *b)
static bool RoadVehEngineCapacitySorter(const EngineID &a, const EngineID &b)
{
int va = GetTotalCapacityOfArticulatedParts(*a);
int vb = GetTotalCapacityOfArticulatedParts(*b);
int va = GetTotalCapacityOfArticulatedParts(a);
int vb = GetTotalCapacityOfArticulatedParts(b);
int r = va - vb;
/* Use EngineID to sort instead since we want consistent sorting */
if (r == 0) return EngineNumberSorter(a, b);
return _engine_sort_direction ? -r : r;
return _engine_sort_direction ? r > 0 : r < 0;
}
/* Ship vehicle sorting functions */
/**
* Determines order of ships by capacity
* @param *a first engine to compare
* @param *b second engine to compare
* @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
* @param a first engine to compare
* @param b second engine to compare
* @return for descending order: returns true if a < b. Vice versa for ascending order
*/
static int CDECL ShipEngineCapacitySorter(const EngineID *a, const EngineID *b)
static bool ShipEngineCapacitySorter(const EngineID &a, const EngineID &b)
{
const Engine *e_a = Engine::Get(*a);
const Engine *e_b = Engine::Get(*b);
const Engine *e_a = Engine::Get(a);
const Engine *e_b = Engine::Get(b);
int va = e_a->GetDisplayDefaultCapacity();
int vb = e_b->GetDisplayDefaultCapacity();
@ -365,21 +362,21 @@ static int CDECL ShipEngineCapacitySorter(const EngineID *a, const EngineID *b)
/* Use EngineID to sort instead since we want consistent sorting */
if (r == 0) return EngineNumberSorter(a, b);
return _engine_sort_direction ? -r : r;
return _engine_sort_direction ? r > 0 : r < 0;
}
/* Aircraft sorting functions */
/**
* Determines order of aircraft by cargo
* @param *a first engine to compare
* @param *b second engine to compare
* @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal
* @param a first engine to compare
* @param b second engine to compare
* @return for descending order: returns true if a < b. Vice versa for ascending order
*/
static int CDECL AircraftEngineCargoSorter(const EngineID *a, const EngineID *b)
static bool AircraftEngineCargoSorter(const EngineID &a, const EngineID &b)
{
const Engine *e_a = Engine::Get(*a);
const Engine *e_b = Engine::Get(*b);
const Engine *e_a = Engine::Get(a);
const Engine *e_b = Engine::Get(b);
uint16 mail_a, mail_b;
int va = e_a->GetDisplayDefaultCapacity(&mail_a);
@ -395,25 +392,25 @@ static int CDECL AircraftEngineCargoSorter(const EngineID *a, const EngineID *b)
return EngineNumberSorter(a, b);
}
}
return _engine_sort_direction ? -r : r;
return _engine_sort_direction ? r > 0 : r < 0;
}
/**
* Determines order of aircraft by range.
* @param *a first engine to compare.
* @param *b second engine to compare.
* @return for descending order: returns < 0 if a < b and > 0 for a > b. Vice versa for ascending order and 0 for equal.
* @param a first engine to compare
* @param b second engine to compare
* @return for descending order: returns true if a < b. Vice versa for ascending order
*/
static int CDECL AircraftRangeSorter(const EngineID *a, const EngineID *b)
static bool AircraftRangeSorter(const EngineID &a, const EngineID &b)
{
uint16 r_a = Engine::Get(*a)->GetRange();
uint16 r_b = Engine::Get(*b)->GetRange();
uint16 r_a = Engine::Get(a)->GetRange();
uint16 r_b = Engine::Get(b)->GetRange();
int r = r_a - r_b;
/* Use EngineID to sort instead since we want consistent sorting */
if (r == 0) return EngineNumberSorter(a, b);
return _engine_sort_direction ? -r : r;
return _engine_sort_direction ? r > 0 : r < 0;
}
/** Sort functions for the vehicle sort criteria, for each vehicle type. */

View File

@ -325,11 +325,8 @@ void DrawVehicleEngine(int left, int right, int preferred_x, int y, EngineID eng
*/
void EngList_Sort(GUIEngineList *el, EngList_SortTypeFunction compare)
{
size_t size = el->size();
/* out-of-bounds access at the next line for size == 0 (even with operator[] at some systems)
* generally, do not sort if there are less than 2 items */
if (size < 2) return;
QSortT(el->data(), size, compare);
if (el->size() < 2) return;
std::sort(el->begin(), el->end(), compare);
}
/**
@ -344,6 +341,6 @@ void EngList_SortPartial(GUIEngineList *el, EngList_SortTypeFunction compare, ui
if (num_items < 2) return;
assert(begin < el->size());
assert(begin + num_items <= el->size());
QSortT(el->data() + begin, num_items, compare);
std::sort(el->begin() + begin, el->begin() + begin + num_items, compare);
}

View File

@ -19,7 +19,7 @@
typedef GUIList<EngineID, CargoID> GUIEngineList;
typedef int CDECL EngList_SortTypeFunction(const EngineID*, const EngineID*); ///< argument type for #EngList_Sort.
typedef bool EngList_SortTypeFunction(const EngineID&, const EngineID&); ///< argument type for #EngList_Sort.
void EngList_Sort(GUIEngineList *el, EngList_SortTypeFunction compare);
void EngList_SortPartial(GUIEngineList *el, EngList_SortTypeFunction compare, uint begin, uint num_items);