Codechange: use std::sort() for all std::vector types

This commit is contained in:
glx 2019-04-12 01:28:11 +02:00 committed by PeterN
parent 5b77102b63
commit 801cbea9cc
5 changed files with 24 additions and 25 deletions

View File

@ -167,12 +167,12 @@ struct SmallMap : std::vector<SmallPair<T, U> > {
inline void SortByKey() inline void SortByKey()
{ {
QSortT(std::vector<Pair>::data(), std::vector<Pair>::size(), KeySorter); std::sort(std::vector<Pair>::begin(), std::vector<Pair>::end());
} }
static int CDECL KeySorter(const Pair *a, const Pair *b) bool operator< (const Pair &other) const
{ {
return a->first - b->first; return (*this).first < other.first;
} }
}; };

View File

@ -1205,19 +1205,19 @@ void AlterVehicleListOrder(EngineID engine, uint target)
* @param b right side * @param b right side
* @return comparison result * @return comparison result
*/ */
static int CDECL EnginePreSort(const EngineID *a, const EngineID *b) static bool EnginePreSort(const EngineID &a, const EngineID &b)
{ {
const EngineIDMapping &id_a = _engine_mngr.at(*a); const EngineIDMapping &id_a = _engine_mngr.at(a);
const EngineIDMapping &id_b = _engine_mngr.at(*b); const EngineIDMapping &id_b = _engine_mngr.at(b);
/* 1. Sort by engine type */ /* 1. Sort by engine type */
if (id_a.type != id_b.type) return (int)id_a.type - (int)id_b.type; if (id_a.type != id_b.type) return (int)id_a.type < (int)id_b.type;
/* 2. Sort by scope-GRFID */ /* 2. Sort by scope-GRFID */
if (id_a.grfid != id_b.grfid) return id_a.grfid < id_b.grfid ? -1 : 1; if (id_a.grfid != id_b.grfid) return id_a.grfid < id_b.grfid;
/* 3. Sort by local ID */ /* 3. Sort by local ID */
return (int)id_a.internal_id - (int)id_b.internal_id; return (int)id_a.internal_id < (int)id_b.internal_id;
} }
/** /**
@ -1231,7 +1231,7 @@ void CommitVehicleListOrderChanges()
FOR_ALL_ENGINES(e) { FOR_ALL_ENGINES(e) {
ordering.push_back(e->index); ordering.push_back(e->index);
} }
QSortT(ordering.data(), ordering.size(), EnginePreSort); std::sort(ordering.begin(), ordering.end(), EnginePreSort);
/* Apply Insertion-Sort operations */ /* Apply Insertion-Sort operations */
for (const ListOrderChange &it : _list_order_changes) { for (const ListOrderChange &it : _list_order_changes) {

View File

@ -216,15 +216,12 @@ CommandCost CmdSetVehicleOnTime(TileIndex tile, DoCommandFlag flags, uint32 p1,
* Order vehicles based on their timetable. The vehicles will be sorted in order * Order vehicles based on their timetable. The vehicles will be sorted in order
* they would reach the first station. * they would reach the first station.
* *
* @param ap First Vehicle pointer. * @param a First Vehicle pointer.
* @param bp Second Vehicle pointer. * @param b Second Vehicle pointer.
* @return Comparison value. * @return Comparison value.
*/ */
static int CDECL VehicleTimetableSorter(Vehicle * const *ap, Vehicle * const *bp) static bool VehicleTimetableSorter(Vehicle * const &a, Vehicle * const &b)
{ {
const Vehicle *a = *ap;
const Vehicle *b = *bp;
VehicleOrderID a_order = a->cur_real_order_index; VehicleOrderID a_order = a->cur_real_order_index;
VehicleOrderID b_order = b->cur_real_order_index; VehicleOrderID b_order = b->cur_real_order_index;
int j = (int)b_order - (int)a_order; int j = (int)b_order - (int)a_order;
@ -242,15 +239,15 @@ static int CDECL VehicleTimetableSorter(Vehicle * const *ap, Vehicle * const *bp
/* First check the order index that accounted for loading, then just the raw one. */ /* First check the order index that accounted for loading, then just the raw one. */
int i = (int)b_order - (int)a_order; int i = (int)b_order - (int)a_order;
if (i != 0) return i; if (i != 0) return i < 0;
if (j != 0) return j; if (j != 0) return j < 0;
/* Look at the time we spent in this order; the higher, the closer to its destination. */ /* Look at the time we spent in this order; the higher, the closer to its destination. */
i = b->current_order_time - a->current_order_time; i = b->current_order_time - a->current_order_time;
if (i != 0) return i; if (i != 0) return i < 0;
/* If all else is equal, use some unique index to sort it the same way. */ /* If all else is equal, use some unique index to sort it the same way. */
return b->unitnumber - a->unitnumber; return b->unitnumber < a->unitnumber;
} }
/** /**
@ -295,7 +292,7 @@ CommandCost CmdSetTimetableStart(TileIndex tile, DoCommandFlag flags, uint32 p1,
int num_vehs = (uint)vehs.size(); int num_vehs = (uint)vehs.size();
if (num_vehs >= 2) { if (num_vehs >= 2) {
QSortT(vehs.data(), vehs.size(), &VehicleTimetableSorter); std::sort(vehs.begin(), vehs.end(), &VehicleTimetableSorter);
} }
int idx = vehs.begin() - std::find(vehs.begin(), vehs.end(), v); int idx = vehs.begin() - std::find(vehs.begin(), vehs.end(), v);

View File

@ -154,10 +154,10 @@ void WindowDesc::LoadFromConfig()
/** /**
* Sort WindowDesc by ini_key. * Sort WindowDesc by ini_key.
*/ */
static int CDECL DescSorter(WindowDesc * const *a, WindowDesc * const *b) bool WindowDesc::operator< (WindowDesc * const &other) const
{ {
if ((*a)->ini_key != nullptr && (*b)->ini_key != nullptr) return strcmp((*a)->ini_key, (*b)->ini_key); if (this->ini_key != nullptr && other->ini_key != nullptr) return strcmp(this->ini_key, other->ini_key) < 0;
return ((*b)->ini_key != nullptr ? 1 : 0) - ((*a)->ini_key != nullptr ? 1 : 0); return this->ini_key != nullptr;
} }
/** /**
@ -166,7 +166,7 @@ static int CDECL DescSorter(WindowDesc * const *a, WindowDesc * const *b)
void WindowDesc::SaveToConfig() void WindowDesc::SaveToConfig()
{ {
/* Sort the stuff to get a nice ini file on first write */ /* Sort the stuff to get a nice ini file on first write */
QSortT(_window_descs->data(), _window_descs->size(), DescSorter); std::sort(_window_descs->begin(), _window_descs->end());
IniFile *ini = new IniFile(); IniFile *ini = new IniFile();
ini->LoadFromDisk(_windows_file, NO_DIRECTORY); ini->LoadFromDisk(_windows_file, NO_DIRECTORY);

View File

@ -192,6 +192,8 @@ struct WindowDesc : ZeroedMemoryAllocator {
static void LoadFromConfig(); static void LoadFromConfig();
static void SaveToConfig(); static void SaveToConfig();
bool operator< (WindowDesc * const &other) const;
private: private:
int16 default_width_trad; ///< Preferred initial width of the window (pixels at 1x zoom). int16 default_width_trad; ///< Preferred initial width of the window (pixels at 1x zoom).
int16 default_height_trad; ///< Preferred initial height of the window (pixels at 1x zoom). int16 default_height_trad; ///< Preferred initial height of the window (pixels at 1x zoom).