diff options
author | glx <glx@openttd.org> | 2019-04-12 01:28:11 +0200 |
---|---|---|
committer | PeterN <peter@fuzzle.org> | 2019-04-13 12:49:18 +0100 |
commit | 801cbea9cce1e04e6921bb087add8206cffe1fe1 (patch) | |
tree | 9ac04e07320af1b5bd0fa8e822ec571e6bd58303 | |
parent | 5b77102b63f77bfbad02ae3383b87fbef6e60e7d (diff) | |
download | openttd-801cbea9cce1e04e6921bb087add8206cffe1fe1.tar.xz |
Codechange: use std::sort() for all std::vector types
-rw-r--r-- | src/core/smallmap_type.hpp | 6 | ||||
-rw-r--r-- | src/newgrf_engine.cpp | 14 | ||||
-rw-r--r-- | src/timetable_cmd.cpp | 19 | ||||
-rw-r--r-- | src/window.cpp | 8 | ||||
-rw-r--r-- | src/window_gui.h | 2 |
5 files changed, 24 insertions, 25 deletions
diff --git a/src/core/smallmap_type.hpp b/src/core/smallmap_type.hpp index bdb6ed475..7581fc6a7 100644 --- a/src/core/smallmap_type.hpp +++ b/src/core/smallmap_type.hpp @@ -167,12 +167,12 @@ struct SmallMap : std::vector<SmallPair<T, U> > { 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; } }; diff --git a/src/newgrf_engine.cpp b/src/newgrf_engine.cpp index d6f4b0997..3a4318873 100644 --- a/src/newgrf_engine.cpp +++ b/src/newgrf_engine.cpp @@ -1205,19 +1205,19 @@ void AlterVehicleListOrder(EngineID engine, uint target) * @param b right side * @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_b = _engine_mngr.at(*b); + const EngineIDMapping &id_a = _engine_mngr.at(a); + const EngineIDMapping &id_b = _engine_mngr.at(b); /* 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 */ - 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 */ - 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) { ordering.push_back(e->index); } - QSortT(ordering.data(), ordering.size(), EnginePreSort); + std::sort(ordering.begin(), ordering.end(), EnginePreSort); /* Apply Insertion-Sort operations */ for (const ListOrderChange &it : _list_order_changes) { diff --git a/src/timetable_cmd.cpp b/src/timetable_cmd.cpp index 4ab7c746f..2b2de28a9 100644 --- a/src/timetable_cmd.cpp +++ b/src/timetable_cmd.cpp @@ -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 * they would reach the first station. * - * @param ap First Vehicle pointer. - * @param bp Second Vehicle pointer. + * @param a First Vehicle pointer. + * @param b Second Vehicle pointer. * @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 b_order = b->cur_real_order_index; 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. */ int i = (int)b_order - (int)a_order; - if (i != 0) return i; - if (j != 0) return j; + if (i != 0) return i < 0; + if (j != 0) return j < 0; /* 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; - 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. */ - 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(); 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); diff --git a/src/window.cpp b/src/window.cpp index d956b55f9..f6faee3ac 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -154,10 +154,10 @@ void WindowDesc::LoadFromConfig() /** * 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); - return ((*b)->ini_key != nullptr ? 1 : 0) - ((*a)->ini_key != nullptr ? 1 : 0); + if (this->ini_key != nullptr && other->ini_key != nullptr) return strcmp(this->ini_key, other->ini_key) < 0; + return this->ini_key != nullptr; } /** @@ -166,7 +166,7 @@ static int CDECL DescSorter(WindowDesc * const *a, WindowDesc * const *b) void WindowDesc::SaveToConfig() { /* 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(); ini->LoadFromDisk(_windows_file, NO_DIRECTORY); diff --git a/src/window_gui.h b/src/window_gui.h index db42cafcd..b338d345f 100644 --- a/src/window_gui.h +++ b/src/window_gui.h @@ -192,6 +192,8 @@ struct WindowDesc : ZeroedMemoryAllocator { static void LoadFromConfig(); static void SaveToConfig(); + bool operator< (WindowDesc * const &other) const; + private: 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). |