Loading
#openttdcoop - Paste
Archives
Trending
Docs
Login
ABAP
ActionScript
ActionScript 3
Ada
AIMMS3
ALGOL 68
Apache configuration
AppleScript
Apt sources
ARM ASSEMBLER
ASM
ASP
asymptote
Autoconf
Autohotkey
AutoIt
AviSynth
awk
BASCOM AVR
Bash
Basic4GL
BibTeX
BlitzBasic
bnf
Boo
Brainfuck
C
C#
C (LoadRunner)
C (Mac)
C (WinAPI)
C++
C++ (Qt)
C++ (WinAPI)
CAD DCL
CAD Lisp
CFDG
ChaiScript
Chapel
CIL
Clojure
CMake
COBOL
CoffeeScript
ColdFusion
CSS
Cuesheet
D
Dart
DCL
DCPU-16 Assembly
DCS
Delphi
Diff
DIV
DOS
dot
E
ECMAScript
Eiffel
eMail (mbox)
EPC
Erlang
Euphoria
EZT
F#
Falcon
FO (abas-ERP)
Formula One
Fortran
FreeBasic
FreeSWITCH
GADV 4CS
GAMBAS
GDB
genero
Genie
glSlang
GML
GNU/Octave
GNU Gettext
GNU make
Gnuplot
Go
Groovy
GwBasic
Haskell
Haxe
HicEst
HQ9+
HTML
HTML5
Icon
INI
Inno
INTERCAL
Io
ISPF Panel
J
Java
Java(TM) 2 Platform Standard Edition 5.0
Javascript
JCL
jQuery
KiXtart
KLone C
KLone C++
LaTeX
LDIF
Liberty BASIC
Lisp
LLVM Intermediate Representation
Locomotive Basic
Logtalk
LOLcode
Lotus Notes @Formulas
LotusScript
LScript
LSL2
Lua
MagikSF
MapBasic
Matlab M
Microchip Assembler
Microsoft Registry
mIRC Scripting
MMIX
Modula-2
Modula-3
MOS 6502 (6510) ACME Cross Assembler format
MOS 6502 (6510) Kick Assembler format
MOS 6502 (6510) TASM/64TASS 1.46 Assembler format
Motorola 68000 - HiSoft Devpac ST 2 Assembler format
Motorola 68000 Assembler
MXML
MySQL
Nagios
NetRexx
newlisp
nginx
Nimrod
NML NewGRF Meta Language
NSIS
Oberon-2
Objeck Programming Language
Objective-C
OCaml
OCaml (brief)
ooRexx
OpenBSD Packet Filter
OpenOffice.org Basic
Oracle 8 SQL
Oracle 11 SQL
Oxygene
OZ
ParaSail
PARI/GP
Pascal
PCRE
per
Perl
Perl 6
PHP
PHP (brief)
PIC16
Pike
Pixel Bender 1.0
PL/I
PL/SQL
PostgreSQL
PostScript
POVRAY
PowerBuilder
PowerShell
ProFTPd configuration
Progress
Prolog
PROPERTIES
ProvideX
Puppet
PureBasic
Python
Python for S60
q/kdb+
QBasic/QuickBASIC
QML
R / S+
Racket
Rails
RBScript
REBOL
rexx
robots.txt
RPM Specification File
Ruby
Rust
SAS
Scala
Scheme
SciLab
SCL
sdlBasic
Smalltalk
Smarty
SPARK
SPARQL
SQL
Squirrel Script
Squirrel Script with OpenTTD AI/GS
StandardML
StoneScript
SystemVerilog
T-SQL
TCL
Tera Term Macro
Text
thinBasic
TypoScript
Unicon (Unified Extended Dialect of Icon)
Uno Idl
Unreal Script
UPC
Urbi
Vala
vb.net
VBScript
Vedit macro language
Verilog
VHDL
Vim Script
Visual Basic
Visual Fox Pro
Visual Prolog
Whitespace
Whois (RPSL format)
Winbatch
X++
XBasic
XML
Xorg configuration
YAML
ZiLOG Z80 Assembler
ZXBasic
diff --git a/source.list b/source.list index df35cdd26..888578b4b 100644 --- a/source.list +++ b/source.list @@ -442,7 +442,6 @@ core/smallmap_type.hpp core/smallmatrix_type.hpp core/smallstack_type.hpp core/smallvec_type.hpp -core/sort_func.hpp core/string_compare_type.hpp # GUI Source Code diff --git a/src/autoreplace_gui.cpp b/src/autoreplace_gui.cpp index 1074d1dd7..7342c1864 100644 --- a/src/autoreplace_gui.cpp +++ b/src/autoreplace_gui.cpp @@ -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 CDECL 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; } /** diff --git a/src/bridge_gui.cpp b/src/bridge_gui.cpp index 797ead1f5..dfbe5d435 100644 --- a/src/bridge_gui.cpp +++ b/src/bridge_gui.cpp @@ -93,21 +93,21 @@ private: Scrollbar *vscroll; /** Sort the bridges by their index */ - static int CDECL BridgeIndexSorter(const BuildBridgeData *a, const BuildBridgeData *b) + static bool CDECL BridgeIndexSorter(const BuildBridgeData &a, const BuildBridgeData &b) { - return a->index - b->index; + return a.index < b.index; } /** Sort the bridges by their price */ - static int CDECL BridgePriceSorter(const BuildBridgeData *a, const BuildBridgeData *b) + static bool CDECL BridgePriceSorter(const BuildBridgeData &a, const BuildBridgeData &b) { - return a->cost - b->cost; + return a.cost < b.cost; } /** Sort the bridges by their maximum speed */ - static int CDECL BridgeSpeedSorter(const BuildBridgeData *a, const BuildBridgeData *b) + static bool CDECL BridgeSpeedSorter(const BuildBridgeData &a, const BuildBridgeData &b) { - return a->spec->speed - b->spec->speed; + return a.spec->speed < b.spec->speed; } void BuildBridge(uint8 i) diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp index 655a18d7f..ab71593d0 100644 --- a/src/build_vehicle_gui.cpp +++ b/src/build_vehicle_gui.cpp @@ -99,15 +99,14 @@ 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 + * @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 */ -static int CDECL EngineNumberSorter(const EngineID *a, const EngineID *b) +static bool CDECL EngineNumberSorter(const EngineID &a, const EngineID &b) { - int r = Engine::Get(*a)->list_position - Engine::Get(*b)->list_position; - - return _engine_sort_direction ? -r : r; + bool r = Engine::Get(a)->list_position < Engine::Get(b)->list_position; + return _engine_sort_direction != r; } /** @@ -116,15 +115,14 @@ static int CDECL EngineNumberSorter(const EngineID *a, const EngineID *b) * @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 */ -static int CDECL EngineIntroDateSorter(const EngineID *a, const EngineID *b) +static bool CDECL 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 r = va - vb; + const int va = Engine::Get(a)->intro_date; + const int vb = Engine::Get(b)->intro_date; /* Use EngineID to sort instead since we want consistent sorting */ - if (r == 0) return EngineNumberSorter(a, b); - return _engine_sort_direction ? -r : r; + if (va == vb) return EngineNumberSorter(a, b); + return _engine_sort_direction != (va < vb); } /** @@ -133,13 +131,13 @@ static int CDECL EngineIntroDateSorter(const EngineID *a, const EngineID *b) * @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 */ -static int CDECL EngineNameSorter(const EngineID *a, const EngineID *b) +static bool CDECL 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; + const EngineID va = a; + const EngineID vb = b; if (va != last_engine[0]) { last_engine[0] = va; @@ -157,7 +155,7 @@ 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); } /** @@ -166,15 +164,14 @@ static int CDECL EngineNameSorter(const EngineID *a, const EngineID *b) * @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 */ -static int CDECL EngineReliabilitySorter(const EngineID *a, const EngineID *b) +static bool CDECL EngineReliabilitySorter(const EngineID &a, const EngineID &b) { - const int va = Engine::Get(*a)->reliability; - const int vb = Engine::Get(*b)->reliability; - const int r = va - vb; + const int va = Engine::Get(a)->reliability; + const int vb = Engine::Get(b)->reliability; /* Use EngineID to sort instead since we want consistent sorting */ - if (r == 0) return EngineNumberSorter(a, b); - return _engine_sort_direction ? -r : r; + if (va == vb) return EngineNumberSorter(a, b); + return _engine_sort_direction != (va < vb); } /** @@ -183,15 +180,15 @@ static int CDECL EngineReliabilitySorter(const EngineID *a, const EngineID *b) * @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 */ -static int CDECL EngineCostSorter(const EngineID *a, const EngineID *b) +static bool CDECL 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); } /** @@ -200,15 +197,15 @@ static int CDECL EngineCostSorter(const EngineID *a, const EngineID *b) * @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 */ -static int CDECL EngineSpeedSorter(const EngineID *a, const EngineID *b) +static bool CDECL 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); } /** @@ -217,15 +214,15 @@ static int CDECL EngineSpeedSorter(const EngineID *a, const EngineID *b) * @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 */ -static int CDECL EnginePowerSorter(const EngineID *a, const EngineID *b) +static bool CDECL 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); } /** @@ -234,15 +231,15 @@ static int CDECL EnginePowerSorter(const EngineID *a, const EngineID *b) * @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 */ -static int CDECL EngineTractiveEffortSorter(const EngineID *a, const EngineID *b) +static bool CDECL 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); } /** @@ -251,15 +248,15 @@ static int CDECL EngineTractiveEffortSorter(const EngineID *a, const EngineID *b * @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 */ -static int CDECL EngineRunningCostSorter(const EngineID *a, const EngineID *b) +static bool CDECL 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); } /** @@ -268,10 +265,10 @@ static int CDECL EngineRunningCostSorter(const EngineID *a, const EngineID *b) * @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 */ -static int CDECL EnginePowerVsRunningCostSorter(const EngineID *a, const EngineID *b) +static bool CDECL 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,7 +282,7 @@ 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); } /* Train sorting functions */ @@ -296,18 +293,18 @@ static int CDECL EnginePowerVsRunningCostSorter(const EngineID *a, const EngineI * @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 */ -static int CDECL TrainEngineCapacitySorter(const EngineID *a, const EngineID *b) +static bool CDECL 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); } /** @@ -316,15 +313,15 @@ static int CDECL TrainEngineCapacitySorter(const EngineID *a, const EngineID *b) * @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 */ -static int CDECL TrainEnginesThenWagonsSorter(const EngineID *a, const EngineID *b) +static bool CDECL 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); } /* Road vehicle sorting functions */ @@ -335,15 +332,15 @@ static int CDECL TrainEnginesThenWagonsSorter(const EngineID *a, const EngineID * @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 */ -static int CDECL RoadVehEngineCapacitySorter(const EngineID *a, const EngineID *b) +static bool CDECL 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); } /* Ship vehicle sorting functions */ @@ -354,10 +351,10 @@ static int CDECL RoadVehEngineCapacitySorter(const EngineID *a, const EngineID * * @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 */ -static int CDECL ShipEngineCapacitySorter(const EngineID *a, const EngineID *b) +static bool CDECL 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,7 +362,7 @@ 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); } /* Aircraft sorting functions */ @@ -376,10 +373,10 @@ static int CDECL ShipEngineCapacitySorter(const EngineID *a, const EngineID *b) * @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 */ -static int CDECL AircraftEngineCargoSorter(const EngineID *a, const EngineID *b) +static bool CDECL 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,7 +392,7 @@ 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); } /** @@ -404,16 +401,16 @@ static int CDECL AircraftEngineCargoSorter(const EngineID *a, const EngineID *b) * @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. */ -static int CDECL AircraftRangeSorter(const EngineID *a, const EngineID *b) +static bool CDECL 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); } /** Sort functions for the vehicle sort criteria, for each vehicle type. */ diff --git a/src/cargotype.cpp b/src/cargotype.cpp index 863c58561..ce7622798 100644 --- a/src/cargotype.cpp +++ b/src/cargotype.cpp @@ -10,11 +10,13 @@ /** @file cargotype.cpp Implementation of cargoes. */ #include "stdafx.h" + +#include <algorithm> + #include "cargotype.h" #include "newgrf_cargo.h" #include "string_func.h" #include "strings_func.h" -#include "core/sort_func.hpp" #include "table/sprites.h" #include "table/strings.h" @@ -138,35 +140,28 @@ uint8 _sorted_standard_cargo_specs_size; ///< Number of standard cargo s /** Sort cargo specifications by their name. */ -static int CDECL CargoSpecNameSorter(const CargoSpec * const *a, const CargoSpec * const *b) +static bool CDECL CargoSpecNameSorter(const CargoSpec * const &a, const CargoSpec * const &b) { static char a_name[64]; static char b_name[64]; - GetString(a_name, (*a)->name, lastof(a_name)); - GetString(b_name, (*b)->name, lastof(b_name)); + GetString(a_name, a->name, lastof(a_name)); + GetString(b_name, b->name, lastof(b_name)); int res = strnatcmp(a_name, b_name); // Sort by name (natural sorting). /* If the names are equal, sort by cargo bitnum. */ - return (res != 0) ? res : ((*a)->bitnum - (*b)->bitnum); + return (res != 0) ? res < 0 : (a->bitnum < b->bitnum); } /** Sort cargo specifications by their cargo class. */ -static int CDECL CargoSpecClassSorter(const CargoSpec * const *a, const CargoSpec * const *b) +static bool CDECL CargoSpecClassSorter(const CargoSpec * const &a, const CargoSpec * const &b) { uint16 mail_a, mail_b; int va = e_a->GetDisplayDefaultCapacity(&mail_a); @@ -395,7 +392,7 @@ 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); } /** @@ -404,16 +401,16 @@ static int CDECL AircraftEngineCargoSorter(const EngineID *a, const EngineID *b) * @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. */ -static int CDECL AircraftRangeSorter(const EngineID *a, const EngineID *b) +static bool CDECL 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); } /** Sort functions for the vehicle sort criteria, for each vehicle type. */ diff --git a/src/cargotype.cpp b/src/cargotype.cpp index 863c58561..ce7622798 100644 --- a/src/cargotype.cpp +++ b/src/cargotype.cpp @@ -10,11 +10,13 @@ /** @file cargotype.cpp Implementation of cargoes. */ #include "stdafx.h" + +#include <algorithm> + #include "cargotype.h" #include "newgrf_cargo.h" #include "string_func.h" #include "strings_func.h" -#include "core/sort_func.hpp" #include "table/sprites.h" #include "table/strings.h" @@ -138,35 +140,28 @@ uint8 _sorted_standard_cargo_specs_size; ///< Number of standard cargo s /** Sort cargo specifications by their name. */ -static int CDECL CargoSpecNameSorter(const CargoSpec * const *a, const CargoSpec * const *b) +static bool CDECL CargoSpecNameSorter(const CargoSpec * const &a, const CargoSpec * const &b) { static char a_name[64]; static char b_name[64]; - GetString(a_name, (*a)->name, lastof(a_name)); - GetString(b_name, (*b)->name, lastof(b_name)); + GetString(a_name, a->name, lastof(a_name)); + GetString(b_name, b->name, lastof(b_name)); int res = strnatcmp(a_name, b_name); // Sort by name (natural sorting). /* If the names are equal, sort by cargo bitnum. */ - return (res != 0) ? res : ((*a)->bitnum - (*b)->bitnum); + return (res != 0) ? res < 0 : (a->bitnum < b->bitnum); } /** Sort cargo specifications by their cargo class. */ -static int CDECL CargoSpecClassSorter(const CargoSpec * const *a, const CargoSpec * const *b) +static bool CDECL CargoSpecClassSorter(const CargoSpec * const &a, const CargoSpec * const &b) { - int res = ((*b)->classes & CC_PASSENGERS) - ((*a)->classes & CC_PASSENGERS); - if (res == 0) { - res = ((*b)->classes & CC_MAIL) - ((*a)->classes & CC_MAIL); - if (res == 0) { - res = ((*a)->classes & CC_SPECIAL) - ((*b)->classes & CC_SPECIAL); - if (res == 0) { - return CargoSpecNameSorter(a, b); - } - } - } - - return res; + int r = (b->classes & CC_PASSENGERS) - (a->classes & CC_PASSENGERS); + if (r == 0) r = (b->classes & CC_MAIL) - (a->classes & CC_MAIL); + if (r == 0) r = (a->classes & CC_SPECIAL) - (b->classes & CC_SPECIAL); + if (r == 0) return CargoSpecNameSorter(a, b); + return r < 0; } /** Initialize the list of sorted cargo specifications. */ @@ -181,7 +176,7 @@ void InitializeSortedCargoSpecs() } /* Sort cargo specifications by cargo class and name. */ - QSortT(_sorted_cargo_specs, _sorted_cargo_specs_size, &CargoSpecClassSorter); + std::sort(_sorted_cargo_specs, _sorted_cargo_specs + _sorted_cargo_specs_size, &CargoSpecClassSorter); _standard_cargo_mask = 0; diff --git a/src/core/smallmap_type.hpp b/src/core/smallmap_type.hpp index dda0fc2a1..49b5f3b34 100644 --- a/src/core/smallmap_type.hpp +++ b/src/core/smallmap_type.hpp @@ -12,8 +12,9 @@ #ifndef SMALLMAP_TYPE_HPP #define SMALLMAP_TYPE_HPP +#include <algorithm> + #include "smallvec_type.hpp" -#include "sort_func.hpp" /** * Simple pair of data. Both types have to be POD ("Plain Old Data")! @@ -146,12 +147,12 @@ struct SmallMap : SmallVector<SmallPair<T, U>, S> { inline void SortByKey() { - QSortT(this->Begin(), this->items, KeySorter); + std::sort(this->Begin(), this->End(), KeySorter); } - static int CDECL KeySorter(const Pair *a, const Pair *b) + static bool CDECL KeySorter(const Pair &a, const Pair &b) { - return a->first - b->first; + return a->first < b->first; } }; diff --git a/src/core/sort_func.hpp b/src/core/sort_func.hpp deleted file mode 100644 index 470a0ccf4..000000000 --- a/src/core/sort_func.hpp +++ /dev/null @@ -1,89 +0,0 @@ -/* $Id$ */ - -/* - * This file is part of OpenTTD. - * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. - * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>. - */ - -/** @file sort_func.hpp Functions related to sorting operations. */ - -#ifndef SORT_FUNC_HPP -#define SORT_FUNC_HPP - -#include "mem_func.hpp" - -/** - * Type safe qsort() - * - * @note Use this sort for irregular sorted data. - * - * @param base Pointer to the first element of the array to be sorted. - * @param num Number of elements in the array pointed by base. - * @param comparator Function that compares two elements. - * @param desc Sort descending. - */ -template <typename T> -static inline void QSortT(T *base, uint num, int (CDECL *comparator)(const T*, const T*), bool desc = false) -{ - if (num < 2) return; - - qsort(base, num, sizeof(T), (int (CDECL *)(const void *, const void *))comparator); - - if (desc) MemReverseT(base, num); -} - -/** - * Type safe Gnome Sort. - * - * This is a slightly modified Gnome search. The basic - * Gnome search tries to sort already sorted list parts. - * The modification skips these. - * - * @note Use this sort for presorted / regular sorted data. - * - * @param base Pointer to the first element of the array to be sorted. - * @param num Number of elements in the array pointed by base. - * @param comparator Function that compares two elements. - * @param desc Sort descending. - */ -template <typename T> -static inline void GSortT(T *base, uint num, int (CDECL *comparator)(const T*, const T*), bool desc = false) -{ - if (num < 2) return; - - assert(base != NULL); - assert(comparator != NULL); - - T *a = base; - T *b = base + 1; - uint offset = 0; - - while (num > 1) { - const int diff = comparator(a, b); - if ((!desc && diff <= 0) || (desc && diff >= 0)) { - if (offset != 0) { - /* Jump back to the last direction switch point */ - a += offset; - b += offset; - offset = 0; - continue; - } - - a++; - b++; - num--; - } else { - Swap(*a, *b); - - if (a == base) continue; - - a--; - * @param num Number of elements in the array pointed by base. - * @param comparator Function that compares two elements. - * @param desc Sort descending. - */ -template <typename T> -static inline void QSortT(T *base, uint num, int (CDECL *comparator)(const T*, const T*), bool desc = false) -{ - if (num < 2) return; - - qsort(base, num, sizeof(T), (int (CDECL *)(const void *, const void *))comparator); - - if (desc) MemReverseT(base, num); -} - -/** - * Type safe Gnome Sort. - * - * This is a slightly modified Gnome search. The basic - * Gnome search tries to sort already sorted list parts. - * The modification skips these. - * - * @note Use this sort for presorted / regular sorted data. - * - * @param base Pointer to the first element of the array to be sorted. - * @param num Number of elements in the array pointed by base. - * @param comparator Function that compares two elements. - * @param desc Sort descending. - */ -template <typename T> -static inline void GSortT(T *base, uint num, int (CDECL *comparator)(const T*, const T*), bool desc = false) -{ - if (num < 2) return; - - assert(base != NULL); - assert(comparator != NULL); - - T *a = base; - T *b = base + 1; - uint offset = 0; - - while (num > 1) { - const int diff = comparator(a, b); - if ((!desc && diff <= 0) || (desc && diff >= 0)) { - if (offset != 0) { - /* Jump back to the last direction switch point */ - a += offset; - b += offset; - offset = 0; - continue; - } - - a++; - b++; - num--; - } else { - Swap(*a, *b); - - if (a == base) continue; - - a--; - b--; - offset++; - } - } -} - -#endif /* SORT_FUNC_HPP */ diff --git a/src/engine_gui.cpp b/src/engine_gui.cpp index 070ad6727..e82fd7e4e 100644 --- a/src/engine_gui.cpp +++ b/src/engine_gui.cpp @@ -329,7 +329,7 @@ void EngList_Sort(GUIEngineList *el, EngList_SortTypeFunction compare) /* 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->Begin(), size, compare); + std::sort(el->Begin(), el->End(), compare); } /** @@ -344,6 +344,6 @@ void EngList_SortPartial(GUIEngineList *el, EngList_SortTypeFunction compare, ui if (num_items < 2) return; assert(begin < el->Length()); assert(begin + num_items <= el->Length()); - QSortT(el->Get(begin), num_items, compare); + std::sort(el->Get(begin), el->Get(begin + num_items), compare); } diff --git a/src/engine_gui.h b/src/engine_gui.h index fc0b7ad7d..ac2ec718f 100644 --- a/src/engine_gui.h +++ b/src/engine_gui.h @@ -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 CDECL 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); diff --git a/src/fios.cpp b/src/fios.cpp index 8ed01152f..117667f90 100644 --- a/src/fios.cpp +++ b/src/fios.cpp @@ -49,25 +49,19 @@ extern void GetOldSaveGameName(const char *file, char *title, const char *last); * @param db A pointer to the second FiosItem to compare. * @return -1, 0 or 1, depending on how the two items should be sorted. */ -int CDECL CompareFiosItems(const FiosItem *da, const FiosItem *db) +bool CDECL CompareFiosItems(const FiosItem &da, const FiosItem &db) { - int r = 0; - - if ((_savegame_sort_order & SORT_BY_NAME) == 0 && da->mtime != db->mtime) { - r = da->mtime < db->mtime ? -1 : 1; + bool r; + if ((_savegame_sort_order & SORT_BY_NAME) == 0 && da.mtime != db.mtime) { + r = da.mtime < db.mtime; } else { - r = strcasecmp(da->title, db->title); + r = strcasecmp(da.title, db.title) < 0; } - if (_savegame_sort_order & SORT_DESCENDING) r = -r; + if (_savegame_sort_order & SORT_DESCENDING) r = !r; return r; } -FileList::~FileList() -{ - this->Clear(); -} - /** * Construct a file list with the given kind of files, for the stated purpose. * @param abstract_filetype Kind of files to collect. @@ -398,7 +392,7 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c { SortingBits order = _savegame_sort_order; _savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING; - QSortT(file_list.files.Begin(), file_list.files.Length(), CompareFiosItems); + std::sort(file_list.Begin(), file_list.End(), CompareFiosItems); _savegame_sort_order = order; } @@ -413,7 +407,7 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c scanner.Scan(NULL, subdir, true, true); } - QSortT(file_list.Get(sort_start), file_list.Length() - sort_start, CompareFiosItems); + std::sort(file_list.Get(sort_start), file_list.End(), CompareFiosItems); /* Show drives */ FiosGetDrives(file_list); diff --git a/src/fios.h b/src/fios.h index 5e17e8ee1..40f4852fe 100644 --- a/src/fios.h +++ b/src/fios.h @@ -103,94 +103,10 @@ struct FiosItem { }; /** List of file information. */ -class FileList { +class FileList : public SmallVector<FiosItem, 32> { public: - ~FileList(); - - /** - * Construct a new entry in the file list. - * @return Pointer to the new items to be initialized. - */ - inline FiosItem *Append() - { - return this->files.Append(); - } - - /** - * Get the number of files in the list. - * @return The number of files stored in the list. - */ - inline uint Length() const - { - return this->files.Length(); - } - - /** - * Get a pointer to the first file information. - * @return Address of the first file information. - */ - inline const FiosItem *Begin() const - { - return this->files.Begin(); - } - - /** - * Get a pointer behind the last file information. - * @return Address behind the last file information. - */ - inline const FiosItem *End() const - { - return this->files.End(); - } - - /** - * Get a pointer to the indicated file information. File information must exist. - * @return Address of the indicated existing file information. - */ - inline const FiosItem *Get(uint index) const - { - return this->files.Get(index); - } - - /** - * Get a pointer to the indicated file information. File information must exist. - * @return Address of the indicated existing file information. - */ - inline FiosItem *Get(uint index) - { - return this->files.Get(index); - } - - inline const FiosItem &operator[](uint index) const - { - return this->files[index]; - } - - /** /* Show drives */ FiosGetDrives(file_list); diff --git a/src/fios.h b/src/fios.h index 5e17e8ee1..40f4852fe 100644 --- a/src/fios.h +++ b/src/fios.h @@ -103,94 +103,10 @@ struct FiosItem { }; /** List of file information. */ -class FileList { +class FileList : public SmallVector<FiosItem, 32> { public: - ~FileList(); - - /** - * Construct a new entry in the file list. - * @return Pointer to the new items to be initialized. - */ - inline FiosItem *Append() - { - return this->files.Append(); - } - - /** - * Get the number of files in the list. - * @return The number of files stored in the list. - */ - inline uint Length() const - { - return this->files.Length(); - } - - /** - * Get a pointer to the first file information. - * @return Address of the first file information. - */ - inline const FiosItem *Begin() const - { - return this->files.Begin(); - } - - /** - * Get a pointer behind the last file information. - * @return Address behind the last file information. - */ - inline const FiosItem *End() const - { - return this->files.End(); - } - - /** - * Get a pointer to the indicated file information. File information must exist. - * @return Address of the indicated existing file information. - */ - inline const FiosItem *Get(uint index) const - { - return this->files.Get(index); - } - - /** - * Get a pointer to the indicated file information. File information must exist. - * @return Address of the indicated existing file information. - */ - inline FiosItem *Get(uint index) - { - return this->files.Get(index); - } - - inline const FiosItem &operator[](uint index) const - { - return this->files[index]; - } - - /** /* Show drives */ FiosGetDrives(file_list); diff --git a/src/fios.h b/src/fios.h index 5e17e8ee1..40f4852fe 100644 --- a/src/fios.h +++ b/src/fios.h @@ -103,94 +103,10 @@ struct FiosItem { }; /** List of file information. */ -class FileList { +class FileList : public SmallVector<FiosItem, 32> { public: - ~FileList(); - - /** - * Construct a new entry in the file list. - * @return Pointer to the new items to be initialized. - */ - inline FiosItem *Append() - { - return this->files.Append(); - } - - /** - * Get the number of files in the list. - * @return The number of files stored in the list. - */ - inline uint Length() const - { - return this->files.Length(); - } - - /** - * Get a pointer to the first file information. - * @return Address of the first file information. - */ - inline const FiosItem *Begin() const - { - return this->files.Begin(); - } - - /** - * Get a pointer behind the last file information. - * @return Address behind the last file information. - */ - inline const FiosItem *End() const - { - return this->files.End(); - } - - /** - * Get a pointer to the indicated file information. File information must exist. - * @return Address of the indicated existing file information. - */ - inline const FiosItem *Get(uint index) const - { - return this->files.Get(index); - } - - /** - * Get a pointer to the indicated file information. File information must exist. - * @return Address of the indicated existing file information. - */ - inline FiosItem *Get(uint index) - { - return this->files.Get(index); - } - - inline const FiosItem &operator[](uint index) const - { - return this->files[index]; - } - - /** - * Get a reference to the indicated file information. File information must exist. - * @return The requested file information. - */ - inline FiosItem &operator[](uint index) - { - return this->files[index]; - } - - /** Remove all items from the list. */ - inline void Clear() - { - this->files.Clear(); - } - - /** Compact the list down to the smallest block size boundary. */ - inline void Compact() - { - this->files.Compact(); - } - void BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperation fop); const FiosItem *FindItem(const char *file); - - SmallVector<FiosItem, 32> files; ///< The list of files. }; enum SortingBits { @@ -219,6 +135,6 @@ void FiosMakeSavegameName(char *buf, const char *name, const char *last); FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const char *file, const char *ext, char *title, const char *last); -int CDECL CompareFiosItems(const FiosItem *a, const FiosItem *b); +bool CDECL CompareFiosItems(const FiosItem &a, const FiosItem &b); #endif /* FIOS_H */ diff --git a/src/fios_gui.cpp b/src/fios_gui.cpp index e6cd9625c..64eac2a50 100644 --- a/src/fios_gui.cpp +++ b/src/fios_gui.cpp @@ -221,8 +221,7 @@ static void SortSaveGameList(FileList &file_list) } } - uint s_amount = file_list.Length() - sort_start - sort_end; - QSortT(file_list.Get(sort_start), s_amount, CompareFiosItems); + std::sort(file_list.Get(sort_start), file_list.Get(sort_end), CompareFiosItems); } struct SaveLoadWindow : public Window { diff --git a/src/gfx.cpp b/src/gfx.cpp index 719505157..efe6e03eb 100644 --- a/src/gfx.cpp +++ b/src/gfx.cpp @@ -1699,14 +1699,12 @@ bool ToggleFullScreen(bool fs) return result; } -static int CDECL compare_res(const Dimension *pa, const Dimension *pb) +static bool CDECL compare_res(const Dimension &pa, const Dimension &pb) { - int x = pa->width - pb->width; - if (x != 0) return x; - return pa->height - pb->height; + return pa.width < pb.width || pa.height < pb.height; } void SortResolutions(int count) { - QSortT(_resolutions, count, &compare_res); + std::sort(_resolutions, _resolutions + count, &compare_res); } diff --git a/src/graph_gui.cpp b/src/graph_gui.cpp index c12c6ace4..16fc1107a 100644 --- a/src/graph_gui.cpp +++ b/src/graph_gui.cpp @@ -1151,9 +1151,9 @@ private: } /** Sort the company league by performance history */ - static int CDECL PerformanceSorter(const Company * const *c1, const Company * const *c2) + static bool CDECL PerformanceSorter(const Company * const &c1, const Company * const &c2) { - return (*c2)->old_economy[0].performance_history - (*c1)->old_economy[0].performance_history; + return c2->old_economy[0].performance_history < c1->old_economy[0].performance_history; } public: diff --git a/src/group_gui.cpp b/src/group_gui.cpp index d3e1eafbb..713b11cdb 100644 --- a/src/group_gui.cpp +++ b/src/group_gui.cpp @@ -134,26 +134,26 @@ private: } /** Sort the groups by their name */ - static int CDECL GroupNameSorter(const Group * const *a, const Group * const *b) + static bool CDECL GroupNameSorter(const Group * const &a, const Group * const &b) { static const Group *last_group[2] = { NULL, NULL }; static char last_name[2][64] = { "", "" }; - if (*a != last_group[0]) { - last_group[0] = *a; - SetDParam(0, (*a)->index); + if (a != last_group[0]) { + last_group[0] = a; + SetDParam(0, a->index); GetString(last_name[0], STR_GROUP_NAME, lastof(last_name[0])); } - if (*b != last_group[1]) { - last_group[1] = *b; - SetDParam(0, (*b)->index); + if (b != last_group[1]) { + last_group[1] = b; + SetDParam(0, b->index); GetString(last_name[1], STR_GROUP_NAME, lastof(last_name[1])); } int r = strnatcmp(last_name[0], last_name[1]); // Sort by name (natural sorting). - if (r == 0) return (*a)->index - (*b)->index; - return r; + if (r == 0) return a->index < b->index; + return r < 0; } /** diff --git a/src/highscore.cpp b/src/highscore.cpp index 86e4f5ae8..57bb2e9f3 100644 --- a/src/highscore.cpp +++ b/src/highscore.cpp @@ -10,6 +10,9 @@ /** @file highscore.cpp Definition of functions used for highscore handling */ #include "stdafx.h" + +#include <algorithm> + #include "highscore.h" #include "company_base.h" #include "company_func.h" @@ -17,7 +20,6 @@ #include "string_func.h" #include "strings_func.h" #include "table/strings.h" -#include "core/sort_func.hpp" #include "debug.h" #include "safeguards.h" @@ -79,9 +81,9 @@ int8 SaveHighScoreValue(const Company *c) } /** Sort all companies given their performance */ -static int CDECL HighScoreSorter(const Company * const *a, const Company * const *b) +static bool CDECL HighScoreSorter(const Company * const &a, const Company * const &b) { - return (*b)->old_economy[0].performance_history - (*a)->old_economy[0].performance_history; + return b->old_economy[0].performance_history < a->old_economy[0].performance_history; } /** @@ -98,7 +100,7 @@ int8 SaveHighScoreValueNetwork() /* Sort all active companies with the highest score first */ FOR_ALL_COMPANIES(c) cl[count++] = c; - QSortT(cl, count, &HighScoreSorter); + std::sort(cl, cl + count, &HighScoreSorter); { uint i; diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp index b9077b9d7..9e152649b 100644 --- a/src/industry_gui.cpp +++ b/src/industry_gui.cpp @@ -160,20 +160,20 @@ static inline void GetAllCargoSuffixes(uint cb_offset, CargoSuffixType cst, cons IndustryType _sorted_industry_types[NUM_INDUSTRYTYPES]; ///< Industry types sorted by name. /** Sort industry types by their name. */ -static int CDECL IndustryTypeNameSorter(const IndustryType *a, const IndustryType *b) +static bool CDECL IndustryTypeNameSorter(const IndustryType &a, const IndustryType &b) { static char industry_name[2][64]; - const IndustrySpec *indsp1 = GetIndustrySpec(*a); + const IndustrySpec *indsp1 = GetIndustrySpec(a); GetString(industry_name[0], indsp1->name, lastof(industry_name[0])); - const IndustrySpec *indsp2 = GetIndustrySpec(*b); + const IndustrySpec *indsp2 = GetIndustrySpec(b); GetString(industry_name[1], indsp2->name, lastof(industry_name[1])); int r = strnatcmp(industry_name[0], industry_name[1]); // Sort by name (natural sorting). /* If the names are equal, sort by industry type. */ - return (r != 0) ? r : (*a - *b); + return (r != 0) ? r < 0 : (a < b); } /** @@ -187,7 +187,8 @@ void SortIndustryTypes() } /* Sort industry types by name. */ - QSortT(_sorted_industry_types, NUM_INDUSTRYTYPES, &IndustryTypeNameSorter); + + std::sort(std::begin(_sorted_industry_types), std::end(_sorted_industry_types), IndustryTypeNameSorter); } /** @@ -1201,52 +1202,49 @@ protected: } /** Sort industries by name */ - static int CDECL IndustryNameSorter(const Industry * const *a, const Industry * const *b) + static bool CDECL IndustryNameSorter(const Industry * const &a, const Industry * const &b) { static char buf_cache[96]; static char buf[96]; - SetDParam(0, (*a)->index); + SetDParam(0, a->index); GetString(buf, STR_INDUSTRY_NAME, lastof(buf)); - if (*b != last_industry) { - last_industry = *b; - SetDParam(0, (*b)->index); + if (b != last_industry) { + last_industry = b; + SetDParam(0, b->index); GetString(buf_cache, STR_INDUSTRY_NAME, lastof(buf_cache)); } - return strnatcmp(buf, buf_cache); // Sort by name (natural sorting). + return strnatcmp(buf, buf_cache) < 0; // Sort by name (natural sorting). } /** Sort industries by type and name */ - static int CDECL IndustryTypeSorter(const Industry * const *a, const Industry * const *b) + static bool CDECL IndustryTypeSorter(const Industry * const &a, const Industry * const &b) { int it_a = 0; - while (it_a != NUM_INDUSTRYTYPES && (*a)->type != _sorted_industry_types[it_a]) it_a++; + while (it_a != NUM_INDUSTRYTYPES && a->type != _sorted_industry_types[it_a]) it_a++; int it_b = 0; - while (it_b != NUM_INDUSTRYTYPES && (*b)->type != _sorted_industry_types[it_b]) it_b++; - int r = it_a - it_b; - return (r == 0) ? IndustryNameSorter(a, b) : r; + while (it_b != NUM_INDUSTRYTYPES && b->type != _sorted_industry_types[it_b]) it_b++; + return (it_a == it_b) ? IndustryNameSorter(a, b) : it_a < it_b; } /** Sort industries by production and name */ - static int CDECL IndustryProductionSorter(const Industry * const *a, const Industry * const *b) + static bool CDECL IndustryProductionSorter(const Industry * const &a, const Industry * const &b) { uint prod_a = 0, prod_b = 0; - for (uint i = 0; i < lengthof((*a)->produced_cargo); i++) { - if ((*a)->produced_cargo[i] != CT_INVALID) prod_a += (*a)->last_month_production[i]; - if ((*b)->produced_cargo[i] != CT_INVALID) prod_b += (*b)->last_month_production[i]; + for (uint i = 0; i < lengthof(a->produced_cargo); i++) { + if (a->produced_cargo[i] != CT_INVALID) prod_a += a->last_month_production[i]; + if (b->produced_cargo[i] != CT_INVALID) prod_b += b->last_month_production[i]; } - int r = prod_a - prod_b; - - return (r == 0) ? IndustryTypeSorter(a, b) : r; + return (prod_a == prod_b) ? IndustryTypeSorter(a, b) : prod_a < prod_b; } /** Sort industries by transported cargo and name */ - static int CDECL IndustryTransportedCargoSorter(const Industry * const *a, const Industry * const *b) + static bool CDECL IndustryTransportedCargoSorter(const Industry * const &a, const Industry * const &b) { - int r = GetCargoTransportedSortValue(*a) - GetCargoTransportedSortValue(*b); - return (r == 0) ? IndustryNameSorter(a, b) : r; + int r = GetCargoTransportedSortValue(a) - GetCargoTransportedSortValue(b); + return (r == 0) ? IndustryNameSorter(a, b) : r < 0; } /** diff --git a/src/network/network_content_gui.cpp b/src/network/network_content_gui.cpp index 1227d43db..a152d447c 100644 --- a/src/network/network_content_gui.cpp +++ b/src/network/network_content_gui.cpp @@ -407,28 +407,29 @@ class NetworkContentListWindow : public Window, ContentCallback { } /** Sort content by name. */ - static int CDECL NameSorter(const ContentInfo * const *a, const ContentInfo * const *b) + static bool CDECL NameSorter(const ContentInfo * const &a, const ContentInfo * const &b) { - return strnatcmp((*a)->name, (*b)->name, true); // Sort by name (natural sorting). + return strnatcmp(a->name, b->name, true) < 0; // Sort by name (natural sorting). } /** Sort content by type. */ - static int CDECL TypeSorter(const ContentInfo * const *a, const ContentInfo * const *b) + static bool CDECL TypeSorter(const ContentInfo * const &a, const ContentInfo * const &b) { int r = 0; - if ((*a)->type != (*b)->type) { - r = strnatcmp(content_type_strs[(*a)->type], content_type_strs[(*b)->type]); + if (a->type != b->type) { + r = strnatcmp(content_type_strs[a->type], content_type_strs[b->type]); } - if (r == 0) r = NameSorter(a, b); - return r; + if (r == 0) return NameSorter(a, b); + return r < 0; } /** Sort content by state. */ - static int CDECL StateSorter(const ContentInfo * const *a, const ContentInfo * const *b) + static bool CDECL StateSorter(const ContentInfo * const &a, const ContentInfo * const &b) { - int r = (*a)->state - (*b)->state; - if (r == 0) r = TypeSorter(a, b); - return r; + if (a->state != b->state) { + return a->state < b->state; + } + return TypeSorter(a, b); } /** Sort the content list */ diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp index 520c4f60a..ac8d7d950 100644 --- a/src/network/network_gui.cpp +++ b/src/network/network_gui.cpp @@ -74,7 +74,7 @@ void SortNetworkLanguages() } /* Sort the strings (we don't move 'any' and the 'invalid' one) */ - QSortT(_language_dropdown + 1, NETLANG_COUNT - 1, &StringIDSorter); + std::sort(_language_dropdown + 1, _language_dropdown + NETLANG_COUNT - 1, &StringIDSorter); } /** @@ -276,10 +276,10 @@ protected: } /** Sort servers by name. */ - static int CDECL NGameNameSorter(NetworkGameList * const *a, NetworkGameList * const *b) + static bool CDECL NGameNameSorter(NetworkGameList * const &a, NetworkGameList * const &b) { - int r = strnatcmp((*a)->info.server_name, (*b)->info.server_name, true); // Sort by name (natural sorting). - return r == 0 ? (*a)->address.CompareTo((*b)->address) : r; + int r = strnatcmp(a->info.server_name, b->info.server_name, true); // Sort by name (natural sorting). + return r == 0 ? a->address.CompareTo(b->address) < 0 : r < 0; } /** @@ -287,60 +287,60 @@ protected: * server. If the two servers have the same amount, the one with the * higher maximum is preferred. */ - static int CDECL NGameClientSorter(NetworkGameList * const *a, NetworkGameList * const *b) + static bool CDECL NGameClientSorter(NetworkGameList * const &a, NetworkGameList * const &b) { /* Reverse as per default we are interested in most-clients first */ - int r = (*a)->info.clients_on - (*b)->info.clients_on; + int r = a->info.clients_on - b->info.clients_on; - if (r == 0) r = (*a)->info.clients_max - (*b)->info.clients_max; - if (r == 0) r = NGameNameSorter(a, b); + if (r == 0) r = a->info.clients_max - b->info.clients_max; + if (r == 0) return NGameNameSorter(a, b); - return r; + return r < 0; } /** Sort servers by map size */ - static int CDECL NGameMapSizeSorter(NetworkGameList * const *a, NetworkGameList * const *b) + static bool CDECL NGameMapSizeSorter(NetworkGameList * const &a, NetworkGameList * const &b) { /* Sort by the area of the map. */ - int r = ((*a)->info.map_height) * ((*a)->info.map_width) - ((*b)->info.map_height) * ((*b)->info.map_width); + int r = (a->info.map_height) * (a->info.map_width) - (b->info.map_height) * (b->info.map_width); - if (r == 0) r = (*a)->info.map_width - (*b)->info.map_width; - return (r != 0) ? r : NGameClientSorter(a, b); + if (r == 0) r = a->info.map_width - b->info.map_width; + return (r != 0) ? r < 0 : NGameClientSorter(a, b); } /** Sort servers by current date */ - static int CDECL NGameDateSorter(NetworkGameList * const *a, NetworkGameList * const *b) + static bool CDECL NGameDateSorter(NetworkGameList * const &a, NetworkGameList * const &b) { - int r = (*a)->info.game_date - (*b)->info.game_date; - return (r != 0) ? r : NGameClientSorter(a, b); + int r = a->info.game_date - b->info.game_date; + return (r != 0) ? r < 0 : NGameClientSorter(a, b); } /** Sort servers by the number of days the game is running */ - static int CDECL NGameYearsSorter(NetworkGameList * const *a, NetworkGameList * const *b) + static bool CDECL NGameYearsSorter(NetworkGameList * const &a, NetworkGameList * const &b) { - int r = (*a)->info.game_date - (*a)->info.start_date - (*b)->info.game_date + (*b)->info.start_date; - return (r != 0) ? r : NGameDateSorter(a, b); + int r = a->info.game_date - a->info.start_date - b->info.game_date + b->info.start_date; + return (r != 0) ? r < 0 : NGameDateSorter(a, b); } /** * Sort servers by joinability. If both servers are the * same, prefer the non-passworded server first. */ - static int CDECL NGameAllowedSorter(NetworkGameList * const *a, NetworkGameList * const *b) + static bool CDECL NGameAllowedSorter(NetworkGameList * const &a, NetworkGameList * const &b) { /* The servers we do not know anything about (the ones that did not reply) should be at the bottom) */ - int r = StrEmpty((*a)->info.server_revision) - StrEmpty((*b)->info.server_revision); + int r = StrEmpty(a->info.server_revision) - StrEmpty(b->info.server_revision); /* Reverse default as we are interested in version-compatible clients first */ - if (r == 0) r = (*b)->info.version_compatible - (*a)->info.version_compatible; + if (r == 0) r = b->info.version_compatible - a->info.version_compatible; /* The version-compatible ones are then sorted with NewGRF compatible first, incompatible last */ - if (r == 0) r = (*b)->info.compatible - (*a)->info.compatible; + if (r == 0) r = b->info.compatible - a->info.compatible; /* Passworded servers should be below unpassworded servers */ - if (r == 0) r = (*a)->info.use_password - (*b)->info.use_password; + if (r == 0) r = a->info.use_password - b->info.use_password; /* Finally sort on the number of clients of the server */ - if (r == 0) r = -NGameClientSorter(a, b); + if (r == 0) return !NGameClientSorter(a, b); - return r; + return r < 0; } /** Sort the server list */ diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp index 593851e28..beffb2ed8 100644 --- a/src/newgrf_config.cpp +++ b/src/newgrf_config.cpp @@ -10,6 +10,9 @@ /** @file newgrf_config.cpp Finding NewGRFs and configuring them. */ #include "stdafx.h" + +#include <algorithm> + #include "debug.h" #include "3rdparty/md5/md5.h" #include "newgrf.h" @@ -711,12 +714,9 @@ bool GRFFileScanner::AddFile(const char *filename, size_t basepath_length, const * @param p2 the second GRFConfig * * @return the same strcmp would return for the name of the NewGRF. */ -static int CDECL GRFSorter(GRFConfig * const *p1, GRFConfig * const *p2) +static bool CDECL GRFSorter(const GRFConfig * const p1, const GRFConfig * const p2) { - if (r == 0) r = (*a)->info.map_width - (*b)->info.map_width; - return (r != 0) ? r : NGameClientSorter(a, b); + if (r == 0) r = a->info.map_width - b->info.map_width; + return (r != 0) ? r < 0 : NGameClientSorter(a, b); } /** Sort servers by current date */ - static int CDECL NGameDateSorter(NetworkGameList * const *a, NetworkGameList * const *b) + static bool CDECL NGameDateSorter(NetworkGameList * const &a, NetworkGameList * const &b) { - int r = (*a)->info.game_date - (*b)->info.game_date; - return (r != 0) ? r : NGameClientSorter(a, b); + int r = a->info.game_date - b->info.game_date; + return (r != 0) ? r < 0 : NGameClientSorter(a, b); } /** Sort servers by the number of days the game is running */ - static int CDECL NGameYearsSorter(NetworkGameList * const *a, NetworkGameList * const *b) + static bool CDECL NGameYearsSorter(NetworkGameList * const &a, NetworkGameList * const &b) { - int r = (*a)->info.game_date - (*a)->info.start_date - (*b)->info.game_date + (*b)->info.start_date; - return (r != 0) ? r : NGameDateSorter(a, b); + int r = a->info.game_date - a->info.start_date - b->info.game_date + b->info.start_date; + return (r != 0) ? r < 0 : NGameDateSorter(a, b); } /** * Sort servers by joinability. If both servers are the * same, prefer the non-passworded server first. */ - static int CDECL NGameAllowedSorter(NetworkGameList * const *a, NetworkGameList * const *b) + static bool CDECL NGameAllowedSorter(NetworkGameList * const &a, NetworkGameList * const &b) { /* The servers we do not know anything about (the ones that did not reply) should be at the bottom) */ - int r = StrEmpty((*a)->info.server_revision) - StrEmpty((*b)->info.server_revision); + int r = StrEmpty(a->info.server_revision) - StrEmpty(b->info.server_revision); /* Reverse default as we are interested in version-compatible clients first */ - if (r == 0) r = (*b)->info.version_compatible - (*a)->info.version_compatible; + if (r == 0) r = b->info.version_compatible - a->info.version_compatible; /* The version-compatible ones are then sorted with NewGRF compatible first, incompatible last */ - if (r == 0) r = (*b)->info.compatible - (*a)->info.compatible; + if (r == 0) r = b->info.compatible - a->info.compatible; /* Passworded servers should be below unpassworded servers */ - if (r == 0) r = (*a)->info.use_password - (*b)->info.use_password; + if (r == 0) r = a->info.use_password - b->info.use_password; /* Finally sort on the number of clients of the server */ - if (r == 0) r = -NGameClientSorter(a, b); + if (r == 0) return !NGameClientSorter(a, b); - return r; + return r < 0; } /** Sort the server list */ diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp index 593851e28..beffb2ed8 100644 --- a/src/newgrf_config.cpp +++ b/src/newgrf_config.cpp @@ -10,6 +10,9 @@ /** @file newgrf_config.cpp Finding NewGRFs and configuring them. */ #include "stdafx.h" + +#include <algorithm> + #include "debug.h" #include "3rdparty/md5/md5.h" #include "newgrf.h" @@ -711,12 +714,9 @@ bool GRFFileScanner::AddFile(const char *filename, size_t basepath_length, const * @param p2 the second GRFConfig * * @return the same strcmp would return for the name of the NewGRF. */ -static int CDECL GRFSorter(GRFConfig * const *p1, GRFConfig * const *p2) +static bool CDECL GRFSorter(const GRFConfig * const p1, const GRFConfig * const p2) { - const GRFConfig *c1 = *p1; - const GRFConfig *c2 = *p2; - - return strnatcmp(c1->GetName(), c2->GetName()); + return strnatcmp(p1->GetName(), p2->GetName()) < 0; } /** @@ -747,7 +747,7 @@ void DoScanNewGRFFiles(void *callback) /* Number of files is not necessarily right */ num = i; - QSortT(to_sort, num, &GRFSorter); + std::sort(to_sort, to_sort + num, GRFSorter); for (i = 1; i < num; i++) { to_sort[i - 1]->next = to_sort[i]; diff --git a/src/newgrf_engine.cpp b/src/newgrf_engine.cpp index 8dd8d545a..e3e821f75 100644 --- a/src/newgrf_engine.cpp +++ b/src/newgrf_engine.cpp @@ -10,6 +10,9 @@ /** @file newgrf_engine.cpp NewGRF handling of engines. */ #include "stdafx.h" + +#include <algorithm> + #include "debug.h" #include "train.h" #include "roadveh.h" @@ -1232,19 +1235,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 CDECL EnginePreSort(const EngineID a, const EngineID b) { - const EngineIDMapping *id_a = _engine_mngr.Get(*a); - const EngineIDMapping *id_b = _engine_mngr.Get(*b); + const EngineIDMapping *id_a = _engine_mngr.Get(a); + const EngineIDMapping *id_b = _engine_mngr.Get(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 id_a->type < 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 id_a->internal_id < id_b->internal_id; } /** @@ -1258,7 +1261,7 @@ void CommitVehicleListOrderChanges() FOR_ALL_ENGINES(e) { *ordering.Append() = e->index; } - QSortT(ordering.Begin(), ordering.Length(), EnginePreSort); + std::sort(ordering.Begin(), ordering.End(), EnginePreSort); /* Apply Insertion-Sort operations */ const ListOrderChange *end = _list_order_changes.End(); diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp index c3007ac42..ff93986ed 100644 --- a/src/newgrf_gui.cpp +++ b/src/newgrf_gui.cpp @@ -1434,15 +1434,12 @@ struct NewGRFWindow : public Window, NewGRFScanCallback { private: /** Sort grfs by name. */ - static int CDECL NameSorter(const GRFConfig * const *a, const GRFConfig * const *b) + static bool CDECL NameSorter(const GRFConfig * const &a, const GRFConfig * const &b) { - int i = strnatcmp((*a)->GetName(), (*b)->GetName(), true); // Sort by name (natural sorting). - if (i != 0) return i; + int i = strnatcmp(a->GetName(), b->GetName(), true); // Sort by name (natural sorting). + if (i == 0) i = a->version - b->version; - i = (*a)->version - (*b)->version; - if (i != 0) return i; - - return memcmp((*a)->ident.md5sum, (*b)->ident.md5sum, lengthof((*b)->ident.md5sum)); + return i == 0 ? memcmp(a->ident.md5sum, b->ident.md5sum, lengthof(b->ident.md5sum)) < 0 : i < 0; } /** Filter grfs by tags/name */ diff --git a/src/rail_cmd.cpp b/src/rail_cmd.cpp index 5582666b1..f01adda78 100644 --- a/src/rail_cmd.cpp +++ b/src/rail_cmd.cpp @@ -127,9 +127,9 @@ void ResolveRailTypeGUISprites(RailtypeInfo *rti) * @param second The railtype to compare. * @return True iff the first should be sorted before the second. */ -static int CDECL CompareRailTypes(const RailType *first, const RailType *second) +static bool CDECL CompareRailTypes(const RailType &first, const RailType &second) { - return GetRailTypeInfo(*first)->sorting_order - GetRailTypeInfo(*second)->sorting_order; + return GetRailTypeInfo(first)->sorting_order < GetRailTypeInfo(second)->sorting_order; } /** @@ -148,7 +148,7 @@ void InitRailTypes() _sorted_railtypes[_sorted_railtypes_size++] = rt; } } - QSortT(_sorted_railtypes, _sorted_railtypes_size, CompareRailTypes); + std::sort(_sorted_railtypes, _sorted_railtypes + _sorted_railtypes_size, CompareRailTypes); } /** diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp index 6cde709b0..4465a5ae7 100644 --- a/src/settings_gui.cpp +++ b/src/settings_gui.cpp @@ -203,7 +203,7 @@ struct GameOptionsWindow : Window { if (i == CURRENCY_CUSTOM) continue; *list->Append() = new DropDownListStringItem(*items, i, HasBit(disabled, i)); } - QSortT(list->Begin(), list->Length(), DropDownListStringItem::NatSortFunc); + std::sort(list->Begin(), list->End(), DropDownListStringItem::NatSortFunc); /* Append custom currency at the end */ *list->Append() = new DropDownListItem(-1, false); // separator line @@ -241,7 +241,7 @@ struct GameOptionsWindow : Window { int result = _nb_orig_names + i; *list->Append() = new DropDownListStringItem(_grf_names[i], result, enabled_item != result && enabled_item >= 0); } - QSortT(list->Begin(), list->Length(), DropDownListStringItem::NatSortFunc); + std::sort(list->Begin(), list->End(), DropDownListStringItem::NatSortFunc); int newgrf_size = list->Length(); /* Insert newgrf_names at the top of the list */ @@ -254,7 +254,7 @@ struct GameOptionsWindow : Window { for (int i = 0; i < _nb_orig_names; i++) { *list->Append() = new DropDownListStringItem(STR_GAME_OPTIONS_TOWN_NAME_ORIGINAL_ENGLISH + i, i, enabled_item != i && enabled_item >= 0); } - QSortT(list->Begin() + newgrf_size, list->Length() - newgrf_size, DropDownListStringItem::NatSortFunc); + std::sort(list->Begin() + newgrf_size, list->End(), DropDownListStringItem::NatSortFunc); break; } @@ -274,7 +274,7 @@ struct GameOptionsWindow : Window { if (&_languages[i] == _current_language) *selected_index = i; *list->Append() = new DropDownListStringItem(SPECSTR_LANGUAGE_START + i, i, false); } - QSortT(list->Begin(), list->Length(), DropDownListStringItem::NatSortFunc); + std::sort(list->Begin(), list->End(), DropDownListStringItem::NatSortFunc); break; } diff --git a/src/signs_gui.cpp b/src/signs_gui.cpp index 020ee1b6e..d7b396624 100644 --- a/src/signs_gui.cpp +++ b/src/signs_gui.cpp @@ -72,23 +72,22 @@ struct SignList { } /** Sort signs by their name */ - static int CDECL SignNameSorter(const Sign * const *a, const Sign * const *b) + static bool CDECL SignNameSorter(const Sign * const &a, const Sign * const &b) { static char buf_cache[64]; char buf[64]; - SetDParam(0, (*a)->index); + SetDParam(0, a->index); GetString(buf, STR_SIGN_NAME, lastof(buf)); - if (*b != last_sign) { - last_sign = *b; - SetDParam(0, (*b)->index); + if (b != last_sign) { + last_sign = b; + SetDParam(0, b->index); GetString(buf_cache, STR_SIGN_NAME, lastof(buf_cache)); } int r = strnatcmp(buf, buf_cache); // Sort by name (natural sorting). - - return r != 0 ? r : ((*a)->index - (*b)->index); + return r != 0 ? r < 0 : (a->index < b->index); } void SortSignsList() diff --git a/src/sortlist_type.h b/src/sortlist_type.h index 1a30c3b1a..68924f446 100644 --- a/src/sortlist_type.h +++ b/src/sortlist_type.h @@ -12,9 +12,11 @@ #ifndef SORTLIST_TYPE_H #define SORTLIST_TYPE_H +#include <algorithm> +#include <functional> + #include "core/enum_type.hpp" #include "core/bitmath_func.hpp" -#include "core/sort_func.hpp" #include "core/smallvec_type.hpp" #include "date_type.h" @@ -41,6 +43,19 @@ struct Filtering { byte criteria; ///< Filtering criteria }; +template <typename F, typename T> +struct BinNegator { + F *f; + BinNegator(F *f) : f(f) {} + + bool operator()(const T &a, const T &b) { return !f(a, b); }; + + static BinNegator<F, T> Make(F *f) + { + return BinNegator<F, T>(f); + } +}; + /** * List template of 'things' \p T to sort in a GUI. * @tparam T Type of data stored in the list to represent each item. @@ -49,8 +64,8 @@ struct Filtering { template <typename T, typename F = const char*> class GUIList : public SmallVector<T, 32> { public: - typedef int CDECL SortFunction(const T*, const T*); ///< Signature of sort function. - typedef bool CDECL FilterFunction(const T*, F); ///< Signature of filter function. + typedef bool CDECL SortFunction(const T&, const T&); ///< Signature of sort function. + typedef bool CDECL FilterFunction(const T*, F); ///< Signature of filter function. protected: SortFunction * const *sort_func_list; ///< the sort criteria functions @@ -265,16 +280,12 @@ public: /* Do not sort when the list is not sortable */ if (!this->IsSortable()) return false; - const bool desc = (this->flags & VL_DESC) != 0; - if (this->flags & VL_FIRST_SORT) { CLRBITS(this->flags, VL_FIRST_SORT); - - QSortT(this->data, this->items, compare, desc); - return true; } - GSortT(this->data, this->items, compare, desc); + const bool desc = (this->flags & VL_DESC) != 0; + std::sort(this->Begin(), this->End(), desc ? BinNegator<SortFunction, T>::Make(compare) : compare); return true; } diff --git a/src/station_gui.cpp b/src/station_gui.cpp index 7399fe006..5df763ee4 100644 --- a/src/station_gui.cpp +++ b/src/station_gui.cpp @@ -209,85 +209,80 @@ protected: } /** Sort stations by their name */ - static int CDECL StationNameSorter(const Station * const *a, const Station * const *b) + static bool CDECL StationNameSorter(const Station * const &a, const Station * const &b) { static char buf_cache[64]; char buf[64]; - SetDParam(0, (*a)->index); + SetDParam(0, a->index); GetString(buf, STR_STATION_NAME, lastof(buf)); - if (*b != last_station) { - last_station = *b; - SetDParam(0, (*b)->index); + if (b != last_station) { + last_station = b; + SetDParam(0, b->index); GetString(buf_cache, STR_STATION_NAME, lastof(buf_cache)); } int r = strnatcmp(buf, buf_cache); // Sort by name (natural sorting). - if (r == 0) return (*a)->index - (*b)->index; - return r; + return r == 0 ? a->index < b->index : r < 0; } /** Sort stations by their type */ - static int CDECL StationTypeSorter(const Station * const *a, const Station * const *b) + static bool CDECL StationTypeSorter(const Station * const &a, const Station * const &b) { - return (*a)->facilities - (*b)->facilities; + return a->facilities - b->facilities; } /** Sort stations by their waiting cargo */ - static int CDECL StationWaitingTotalSorter(const Station * const *a, const Station * const *b) + static bool CDECL StationWaitingTotalSorter(const Station * const &a, const Station * const &b) { int diff = 0; CargoID j; FOR_EACH_SET_CARGO_ID(j, cargo_filter) { - diff += (*a)->goods[j].cargo.TotalCount() - (*b)->goods[j].cargo.TotalCount(); + diff += a->goods[j].cargo.TotalCount() - b->goods[j].cargo.TotalCount(); } - - return diff; + return diff < 0; } /** Sort stations by their available waiting cargo */ - static int CDECL StationWaitingAvailableSorter(const Station * const *a, const Station * const *b) + static bool CDECL StationWaitingAvailableSorter(const Station * const &a, const Station * const &b) { int diff = 0; CargoID j; FOR_EACH_SET_CARGO_ID(j, cargo_filter) { - diff += (*a)->goods[j].cargo.AvailableCount() - (*b)->goods[j].cargo.AvailableCount(); + diff += a->goods[j].cargo.AvailableCount() - b->goods[j].cargo.AvailableCount(); } - - return diff; + return diff < 0; } /** Sort stations by their rating */ - static int CDECL StationRatingMaxSorter(const Station * const *a, const Station * const *b) + static bool CDECL StationRatingMaxSorter(const Station * const &a, const Station * const &b) { byte maxr1 = 0; byte maxr2 = 0; CargoID j; FOR_EACH_SET_CARGO_ID(j, cargo_filter) { - if ((*a)->goods[j].HasRating()) maxr1 = max(maxr1, (*a)->goods[j].rating); - if ((*b)->goods[j].HasRating()) maxr2 = max(maxr2, (*b)->goods[j].rating); + if (a->goods[j].HasRating()) maxr1 = max(maxr1, a->goods[j].rating); + if (b->goods[j].HasRating()) maxr2 = max(maxr2, b->goods[j].rating); } - - return maxr1 - maxr2; + return maxr1 < maxr2; } /** Sort stations by their rating */ - static int CDECL StationRatingMinSorter(const Station * const *a, const Station * const *b) + static bool CDECL StationRatingMinSorter(const Station * const &a, const Station * const &b) { byte minr1 = 255; byte minr2 = 255; for (CargoID j = 0; j < NUM_CARGO; j++) { if (!HasBit(cargo_filter, j)) continue; - if ((*a)->goods[j].HasRating()) minr1 = min(minr1, (*a)->goods[j].rating); - if ((*b)->goods[j].HasRating()) minr2 = min(minr2, (*b)->goods[j].rating); + if (a->goods[j].HasRating()) minr1 = min(minr1, a->goods[j].rating); + if (b->goods[j].HasRating()) minr2 = min(minr2, b->goods[j].rating); } - - return -(minr1 - minr2); + return !(minr1 < minr2); } /** Sort the stations list */ diff --git a/src/story_gui.cpp b/src/story_gui.cpp index 003843310..2ee00225c 100644 --- a/src/story_gui.cpp +++ b/src/story_gui.cpp @@ -69,9 +69,9 @@ protected: } /** Sort story pages by order value. */ - static int CDECL PageOrderSorter(const StoryPage * const *a, const StoryPage * const *b) + static bool CDECL PageOrderSorter(const StoryPage * const &a, const StoryPage * const &b) { - return (*a)->sort_value - (*b)->sort_value; + return a->sort_value < b->sort_value; } /** (Re)Build story page element list. */ @@ -98,9 +98,9 @@ protected: } /** Sort story page elements by order value. */ - static int CDECL PageElementOrderSorter(const StoryPageElement * const *a, const StoryPageElement * const *b) + static bool CDECL PageElementOrderSorter(const StoryPageElement * const &a, const StoryPageElement * const &b) { - return (*a)->sort_value - (*b)->sort_value; + return a->sort_value < b->sort_value; } /* diff --git a/src/strings.cpp b/src/strings.cpp index 1c539d934..476a9095d 100644 --- a/src/strings.cpp +++ b/src/strings.cpp @@ -1857,14 +1857,14 @@ const char *GetCurrentLocale(const char *param) const char *GetCurrentLocale(const char *param); #endif /* !(defined(WIN32) || defined(__APPLE__)) */ -int CDECL StringIDSorter(const StringID *a, const StringID *b) +bool CDECL StringIDSorter(const StringID &a, const StringID &b) { char stra[512]; char strb[512]; - GetString(stra, *a, lastof(stra)); - GetString(strb, *b, lastof(strb)); + GetString(stra, a, lastof(stra)); + GetString(strb, b, lastof(strb)); - return strnatcmp(stra, strb); + return strnatcmp(stra, strb) < 0; } /** diff --git a/src/strings_func.h b/src/strings_func.h index 0da711bc4..4d17a5462 100644 --- a/src/strings_func.h +++ b/src/strings_func.h @@ -238,7 +238,7 @@ extern TextDirection _current_text_dir; ///< Text direction of the currently sel void InitializeLanguagePacks(); const char *GetCurrentLanguageIsoCode(); -int CDECL StringIDSorter(const StringID *a, const StringID *b); +bool CDECL StringIDSorter(const StringID &a, const StringID &b); /** * A searcher for missing glyphs. diff --git a/src/timetable_cmd.cpp b/src/timetable_cmd.cpp index 29986c353..274d4a68e 100644 --- a/src/timetable_cmd.cpp +++ b/src/timetable_cmd.cpp @@ -16,7 +16,6 @@ #include "window_func.h" #include "vehicle_base.h" #include "cmd_helper.h" -#include "core/sort_func.hpp" #include "table/strings.h" @@ -220,11 +219,8 @@ CommandCost CmdSetVehicleOnTime(TileIndex tile, DoCommandFlag flags, uint32 p1, * @param bp Second Vehicle pointer. * @return Comparison value. */ -static int CDECL VehicleTimetableSorter(Vehicle * const *ap, Vehicle * const *bp) +static bool CDECL 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 +238,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 +291,7 @@ CommandCost CmdSetTimetableStart(TileIndex tile, DoCommandFlag flags, uint32 p1, int num_vehs = vehs.Length(); if (num_vehs >= 2) { - QSortT(vehs.Begin(), vehs.Length(), &VehicleTimetableSorter); + std::sort(vehs.Begin(), vehs.End(), &VehicleTimetableSorter); } int base = vehs.FindIndex(v); diff --git a/src/town_gui.cpp b/src/town_gui.cpp index 142966b69..e7c82be53 100644 --- a/src/town_gui.cpp +++ b/src/town_gui.cpp @@ -666,11 +666,11 @@ private: } /** Sort by town name */ - static int CDECL TownNameSorter(const Town * const *a, const Town * const *b) + static bool CDECL TownNameSorter(const Town * const &a, const Town * const &b) { static char buf_cache[64]; - const Town *ta = *a; - const Town *tb = *b; + const Town *ta = a; + const Town *tb = b; char buf[64]; SetDParam(0, ta->index); @@ -685,35 +685,35 @@ private: GetString(buf_cache, STR_TOWN_NAME, lastof(buf_cache)); } - return strnatcmp(buf, buf_cache); // Sort by name (natural sorting). + return strnatcmp(buf, buf_cache) < 0; // Sort by name (natural sorting). } /** Sort by population (default descending, as big towns are of the most interest). */ - static int CDECL TownPopulationSorter(const Town * const *a, const Town * const *b) + static bool CDECL TownPopulationSorter(const Town * const &a, const Town * const &b) { - uint32 a_population = (*a)->cache.population; - uint32 b_population = (*b)->cache.population; + uint32 a_population = a->cache.population; + uint32 b_population = b->cache.population; if (a_population == b_population) return TownDirectoryWindow::TownNameSorter(a, b); - return (a_population < b_population) ? -1 : 1; + return a_population < b_population; } /** Sort by town rating */ - static int CDECL TownRatingSorter(const Town * const *a, const Town * const *b) + static bool CDECL TownRatingSorter(const Town * const &a, const Town * const &b) { - int before = TownDirectoryWindow::last_sorting.order ? 1 : -1; // Value to get 'a' before 'b'. + bool before = !TownDirectoryWindow::last_sorting.order; // Value to get 'a' before 'b'. /* Towns without rating are always after towns with rating. */ - if (HasBit((*a)->have_ratings, _local_company)) { - if (HasBit((*b)->have_ratings, _local_company)) { - int16 a_rating = (*a)->ratings[_local_company]; - int16 b_rating = (*b)->ratings[_local_company]; + if (HasBit(a->have_ratings, _local_company)) { + if (HasBit(b->have_ratings, _local_company)) { + int16 a_rating = a->ratings[_local_company]; + int16 b_rating = b->ratings[_local_company]; if (a_rating == b_rating) return TownDirectoryWindow::TownNameSorter(a, b); - return (a_rating < b_rating) ? -1 : 1; + return a_rating < b_rating; } return before; } - if (HasBit((*b)->have_ratings, _local_company)) return -before; - return -before * TownDirectoryWindow::TownNameSorter(a, b); // Sort unrated towns always on ascending town name. + if (HasBit(b->have_ratings, _local_company)) return !before; + return !before && TownDirectoryWindow::TownNameSorter(a, b); // Sort unrated towns always on ascending town name. } public: diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index b9c24ddd0..9d2fe7b54 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -194,7 +194,7 @@ void BaseVehicleListWindow::SortVehicleList() void DepotSortList(VehicleList *list) { if (list->Length() < 2) return; - QSortT(list->Begin(), list->Length(), &VehicleNumberSorter); + std::sort(list->Begin(), list->End(), &VehicleNumberSorter); } /** draw the vehicle profit button in the vehicle list window. */ @@ -1085,62 +1085,61 @@ StringID GetCargoSubtypeText(const Vehicle *v) } /** Sort vehicles by their number */ -static int CDECL VehicleNumberSorter(const Vehicle * const *a, const Vehicle * const *b) +static bool CDECL VehicleNumberSorter(const Vehicle * const &a, const Vehicle * const &b) { - return (*a)->unitnumber - (*b)->unitnumber; + return a->unitnumber < b->unitnumber; } /** Sort vehicles by their name */ -static int CDECL VehicleNameSorter(const Vehicle * const *a, const Vehicle * const *b) +static bool CDECL VehicleNameSorter(const Vehicle * const &a, const Vehicle * const &b) { static char last_name[2][64]; - if (*a != _last_vehicle[0]) { - _last_vehicle[0] = *a; - SetDParam(0, (*a)->index); + if (a != _last_vehicle[0]) { + _last_vehicle[0] = a; + SetDParam(0, a->index); GetString(last_name[0], STR_VEHICLE_NAME, lastof(last_name[0])); } - if (*b != _last_vehicle[1]) { - _last_vehicle[1] = *b; - SetDParam(0, (*b)->index); + if (b != _last_vehicle[1]) { + _last_vehicle[1] = b; + SetDParam(0, b->index); GetString(last_name[1], STR_VEHICLE_NAME, lastof(last_name[1])); } int r = strnatcmp(last_name[0], last_name[1]); // Sort by name (natural sorting). - return (r != 0) ? r : VehicleNumberSorter(a, b); + return (r != 0) ? r < 0 : VehicleNumberSorter(a, b); } /** Sort vehicles by their age */ -static int CDECL VehicleAgeSorter(const Vehicle * const *a, const Vehicle * const *b) +static bool CDECL VehicleAgeSorter(const Vehicle * const &a, const Vehicle * const &b) { - int r = (*a)->age - (*b)->age; - return (r != 0) ? r : VehicleNumberSorter(a, b); + return a->age != b->age ? a->age < b->age : VehicleNumberSorter(a, b); } /** Sort vehicles by this year profit */ -static int CDECL VehicleProfitThisYearSorter(const Vehicle * const *a, const Vehicle * const *b) +static bool CDECL VehicleProfitThisYearSorter(const Vehicle * const &a, const Vehicle * const &b) { - int r = ClampToI32((*a)->GetDisplayProfitThisYear() - (*b)->GetDisplayProfitThisYear()); - return (r != 0) ? r : VehicleNumberSorter(a, b); + int r = ClampToI32(a->GetDisplayProfitThisYear() - b->GetDisplayProfitThisYear()); + return (r != 0) ? r < 0 : VehicleNumberSorter(a, b); } /** Sort vehicles by last year profit */ -static int CDECL VehicleProfitLastYearSorter(const Vehicle * const *a, const Vehicle * const *b) +static bool CDECL VehicleProfitLastYearSorter(const Vehicle * const &a, const Vehicle * const &b) { - int r = ClampToI32((*a)->GetDisplayProfitLastYear() - (*b)->GetDisplayProfitLastYear()); - return (r != 0) ? r : VehicleNumberSorter(a, b); + int r = ClampToI32(a->GetDisplayProfitLastYear() - b->GetDisplayProfitLastYear()); + return (r != 0) ? r < 0 : VehicleNumberSorter(a, b); } /** Sort vehicles by their cargo */ -static int CDECL VehicleCargoSorter(const Vehicle * const *a, const Vehicle * const *b) +static bool CDECL VehicleCargoSorter(const Vehicle * const &a, const Vehicle * const &b) { const Vehicle *v; CargoArray diff; /* Append the cargo of the connected waggons */ - for (v = *a; v != NULL; v = v->Next()) diff[v->cargo_type] += v->cargo_cap; - for (v = *b; v != NULL; v = v->Next()) diff[v->cargo_type] -= v->cargo_cap; + for (v = a; v != NULL; v = v->Next()) diff[v->cargo_type] += v->cargo_cap; + for (v = b; v != NULL; v = v->Next()) diff[v->cargo_type] -= v->cargo_cap; int r = 0; for (CargoID i = 0; i < NUM_CARGO; i++) { @@ -1148,62 +1147,62 @@ static int CDECL VehicleCargoSorter(const Vehicle * const *a, const Vehicle * co if (r != 0) break; } - return (r != 0) ? r : VehicleNumberSorter(a, b); + return (r != 0) ? r < 0 : VehicleNumberSorter(a, b); } /** Sort vehicles by their reliability */ -static int CDECL VehicleReliabilitySorter(const Vehicle * const *a, const Vehicle * const *b) +static bool CDECL VehicleReliabilitySorter(const Vehicle * const &a, const Vehicle * const &b) { - int r = (*a)->reliability - (*b)->reliability; - return (r != 0) ? r : VehicleNumberSorter(a, b); + int r = a->reliability - b->reliability; + return (r != 0) ? r < 0 : VehicleNumberSorter(a, b); } /** Sort vehicles by their max speed */ -static int CDECL VehicleMaxSpeedSorter(const Vehicle * const *a, const Vehicle * const *b) +static bool CDECL VehicleMaxSpeedSorter(const Vehicle * const &a, const Vehicle * const &b) { - int r = (*a)->vcache.cached_max_speed - (*b)->vcache.cached_max_speed; - return (r != 0) ? r : VehicleNumberSorter(a, b); + int r = a->vcache.cached_max_speed - b->vcache.cached_max_speed; + return (r != 0) ? r < 0 : VehicleNumberSorter(a, b); } /** Sort vehicles by model */ -static int CDECL VehicleModelSorter(const Vehicle * const *a, const Vehicle * const *b) +static bool CDECL VehicleModelSorter(const Vehicle * const &a, const Vehicle * const &b) { - int r = (*a)->engine_type - (*b)->engine_type; - return (r != 0) ? r : VehicleNumberSorter(a, b); + int r = a->engine_type - b->engine_type; + return (r != 0) ? r < 0 : VehicleNumberSorter(a, b); } /** Sort vehicles by their value */ -static int CDECL VehicleValueSorter(const Vehicle * const *a, const Vehicle * const *b) +static bool CDECL VehicleValueSorter(const Vehicle * const &a, const Vehicle * const &b) { const Vehicle *u; Money diff = 0; - for (u = *a; u != NULL; u = u->Next()) diff += u->value; - for (u = *b; u != NULL; u = u->Next()) diff -= u->value; + for (u = a; u != NULL; u = u->Next()) diff += u->value; + for (u = b; u != NULL; u = u->Next()) diff -= u->value; int r = ClampToI32(diff); - return (r != 0) ? r : VehicleNumberSorter(a, b); + return (r != 0) ? r < 0 : VehicleNumberSorter(a, b); } /** Sort vehicles by their length */ -static int CDECL VehicleLengthSorter(const Vehicle * const *a, const Vehicle * const *b) +static bool CDECL VehicleLengthSorter(const Vehicle * const &a, const Vehicle * const &b) { - int r = (*a)->GetGroundVehicleCache()->cached_total_length - (*b)->GetGroundVehicleCache()->cached_total_length; - return (r != 0) ? r : VehicleNumberSorter(a, b); + int r = a->GetGroundVehicleCache()->cached_total_length - b->GetGroundVehicleCache()->cached_total_length; + return (r != 0) ? r < 0 : VehicleNumberSorter(a, b); } /** Sort vehicles by the time they can still live */ -static int CDECL VehicleTimeToLiveSorter(const Vehicle * const *a, const Vehicle * const *b) +static bool CDECL VehicleTimeToLiveSorter(const Vehicle * const &a, const Vehicle * const &b) { - int r = ClampToI32(((*a)->max_age - (*a)->age) - ((*b)->max_age - (*b)->age)); - return (r != 0) ? r : VehicleNumberSorter(a, b); + int r = ClampToI32((a->max_age - a->age) - (b->max_age - b->age)); + return (r != 0) ? r < 0 : VehicleNumberSorter(a, b); } /** Sort vehicles by the timetable delay */ -static int CDECL VehicleTimetableDelaySorter(const Vehicle * const *a, const Vehicle * const *b) +static bool CDECL VehicleTimetableDelaySorter(const Vehicle * const &a, const Vehicle * const &b) { - int r = (*a)->lateness_counter - (*b)->lateness_counter; - return (r != 0) ? r : VehicleNumberSorter(a, b); + int r = a->lateness_counter - b->lateness_counter; + return (r != 0) ? r < 0 : VehicleNumberSorter(a, b); } void InitializeGUI() diff --git a/src/video/cocoa/cocoa_v.mm b/src/video/cocoa/cocoa_v.mm index 4df7cb003..4618a45b2 100644 --- a/src/video/cocoa/cocoa_v.mm +++ b/src/video/cocoa/cocoa_v.mm @@ -36,6 +36,7 @@ #include "../../window_func.h" #include "../../window_gui.h" +#import <sort> #import <sys/param.h> /* for MAXPATHLEN */ /** @@ -278,7 +279,7 @@ uint QZ_ListModes(OTTD_Point *modes, uint max_modes, CGDirectDisplayID display_i } /* Sort list smallest to largest */ - QSortT(modes, count, &ModeSorter); + std::sort(modes, modes + count, &ModeSorter); return count; } diff --git a/src/widgets/dropdown.cpp b/src/widgets/dropdown.cpp index d4c229cb1..7f5fce130 100644 --- a/src/widgets/dropdown.cpp +++ b/src/widgets/dropdown.cpp @@ -50,12 +50,12 @@ void DropDownListStringItem::Draw(int left, int right, int top, int bottom, bool * @return true if \a first precedes \a second. * @warning All items in the list need to be derivates of DropDownListStringItem. */ -/* static */ int DropDownListStringItem::NatSortFunc(const DropDownListItem * const *first, const DropDownListItem * const * second) +/* static */ bool DropDownListStringItem::NatSortFunc(const DropDownListItem * const &first, const DropDownListItem * const &second) { char buffer1[512], buffer2[512]; - GetString(buffer1, static_cast<const DropDownListStringItem*>(*first)->String(), lastof(buffer1)); - GetString(buffer2, static_cast<const DropDownListStringItem*>(*second)->String(), lastof(buffer2)); - return strnatcmp(buffer1, buffer2); + GetString(buffer1, static_cast<const DropDownListStringItem*>(first)->String(), lastof(buffer1)); + GetString(buffer2, static_cast<const DropDownListStringItem*>(second)->String(), lastof(buffer2)); + return strnatcmp(buffer1, buffer2) < 0; } StringID DropDownListParamStringItem::String() const diff --git a/src/widgets/dropdown_type.h b/src/widgets/dropdown_type.h index b65d4557b..e87ea1e85 100644 --- a/src/widgets/dropdown_type.h +++ b/src/widgets/dropdown_type.h @@ -50,7 +50,7 @@ public: virtual void Draw(int left, int right, int top, int bottom, bool sel, int bg_colour) const; virtual StringID String() const { return this->string; } - static int CDECL NatSortFunc(const DropDownListItem * const *first, const DropDownListItem * const *second); + static bool CDECL NatSortFunc(const DropDownListItem * const &first, const DropDownListItem * const &second); }; /** diff --git a/src/window.cpp b/src/window.cpp index 8378f60f7..b901abc77 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -150,10 +150,10 @@ void WindowDesc::LoadFromConfig() /** * Sort WindowDesc by ini_key. */ -static int CDECL DescSorter(WindowDesc * const *a, WindowDesc * const *b) +static bool CDECL DescSorter(WindowDesc * const &a, WindowDesc * const &b) { - if ((*a)->ini_key != NULL && (*b)->ini_key != NULL) return strcmp((*a)->ini_key, (*b)->ini_key); - return ((*b)->ini_key != NULL ? 1 : 0) - ((*a)->ini_key != NULL ? 1 : 0); + if (a->ini_key != NULL && b->ini_key != NULL) return strcmp(a->ini_key, b->ini_key) < 0; + return a->ini_key != NULL; } /** @@ -162,7 +162,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->Begin(), _window_descs->Length(), DescSorter); + std::sort(_window_descs->Begin(), _window_descs->End(), &DescSorter); IniFile *ini = new IniFile(); ini->LoadFromDisk(_windows_file, NO_DIRECTORY);
Mark as private
for 30 minutes
for 6 hours
for 1 day
for 1 week
for 1 month
for 1 year
forever