Mercurial > hg > openttd
changeset 13032:dbc1aa7d826b draft
(svn r17530) -Codechange: use QSortT instead of qsort for sorting EngineIDs
author | rubidium <rubidium@openttd.org> |
---|---|
date | Sun, 13 Sep 2009 17:47:07 +0000 |
parents | 2ac3b388fb4c |
children | bc1115a0f463 |
files | src/autoreplace_gui.cpp src/build_vehicle_gui.cpp src/core/sort_func.hpp src/engine_gui.cpp src/engine_gui.h src/sortlist_type.h |
diffstat | 6 files changed, 56 insertions(+), 62 deletions(-) [+] |
line wrap: on
line diff
--- a/src/autoreplace_gui.cpp +++ b/src/autoreplace_gui.cpp @@ -58,11 +58,9 @@ RVW_WIDGET_TRAIN_WAGONREMOVE_TOGGLE, }; -static int CDECL EngineNumberSorter(const void *a, const void *b) +static int CDECL EngineNumberSorter(const EngineID *a, const EngineID *b) { - const EngineID va = *(const EngineID*)a; - const EngineID vb = *(const EngineID*)b; - int r = ListPositionOfEngine(va) - ListPositionOfEngine(vb); + int r = ListPositionOfEngine(*a) - ListPositionOfEngine(*b); return r; }
--- a/src/build_vehicle_gui.cpp +++ b/src/build_vehicle_gui.cpp @@ -114,19 +114,17 @@ static bool _last_sort_order[] = {false, false, false, false}; static byte _last_filter_criteria[] = {0, 0, 0, 0}; -static int CDECL EngineNumberSorter(const void *a, const void *b) +static int CDECL EngineNumberSorter(const EngineID *a, const EngineID *b) { - const EngineID va = *(const EngineID*)a; - const EngineID vb = *(const EngineID*)b; - int r = ListPositionOfEngine(va) - ListPositionOfEngine(vb); + int r = ListPositionOfEngine(*a) - ListPositionOfEngine(*b); return _internal_sort_order ? -r : r; } -static int CDECL EngineIntroDateSorter(const void *a, const void *b) +static int CDECL EngineIntroDateSorter(const EngineID *a, const EngineID *b) { - const int va = Engine::Get(*(const EngineID*)a)->intro_date; - const int vb = Engine::Get(*(const EngineID*)b)->intro_date; + const int va = Engine::Get(*a)->intro_date; + const int vb = Engine::Get(*b)->intro_date; const int r = va - vb; /* Use EngineID to sort instead since we want consistent sorting */ @@ -134,13 +132,13 @@ return _internal_sort_order ? -r : r; } -static int CDECL EngineNameSorter(const void *a, const void *b) +static int 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 = *(const EngineID*)a; - const EngineID vb = *(const EngineID*)b; + const EngineID va = *a; + const EngineID vb = *b; if (va != last_engine[0]) { last_engine[0] = va; @@ -161,10 +159,10 @@ return _internal_sort_order ? -r : r; } -static int CDECL EngineReliabilitySorter(const void *a, const void *b) +static int CDECL EngineReliabilitySorter(const EngineID *a, const EngineID *b) { - const int va = Engine::Get(*(const EngineID*)a)->reliability; - const int vb = Engine::Get(*(const EngineID*)b)->reliability; + const int va = Engine::Get(*a)->reliability; + const int vb = Engine::Get(*b)->reliability; const int r = va - vb; /* Use EngineID to sort instead since we want consistent sorting */ @@ -172,10 +170,10 @@ return _internal_sort_order ? -r : r; } -static int CDECL EngineCostSorter(const void *a, const void *b) +static int CDECL EngineCostSorter(const EngineID *a, const EngineID *b) { - Money va = Engine::Get(*(const EngineID*)a)->GetCost(); - Money vb = Engine::Get(*(const EngineID*)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 */ @@ -183,10 +181,10 @@ return _internal_sort_order ? -r : r; } -static int CDECL EngineSpeedSorter(const void *a, const void *b) +static int CDECL EngineSpeedSorter(const EngineID *a, const EngineID *b) { - int va = Engine::Get(*(const EngineID*)a)->GetDisplayMaxSpeed(); - int vb = Engine::Get(*(const EngineID*)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 */ @@ -194,10 +192,10 @@ return _internal_sort_order ? -r : r; } -static int CDECL EnginePowerSorter(const void *a, const void *b) +static int CDECL EnginePowerSorter(const EngineID *a, const EngineID *b) { - int va = Engine::Get(*(const EngineID*)a)->GetPower(); - int vb = Engine::Get(*(const EngineID*)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 */ @@ -205,10 +203,10 @@ return _internal_sort_order ? -r : r; } -static int CDECL EngineRunningCostSorter(const void *a, const void *b) +static int CDECL EngineRunningCostSorter(const EngineID *a, const EngineID *b) { - Money va = Engine::Get(*(const EngineID*)a)->GetRunningCost(); - Money vb = Engine::Get(*(const EngineID*)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 */ @@ -217,10 +215,10 @@ } /* Train sorting functions */ -static int CDECL TrainEnginePowerVsRunningCostSorter(const void *a, const void *b) +static int CDECL TrainEnginePowerVsRunningCostSorter(const EngineID *a, const EngineID *b) { - const Engine *e_a = Engine::Get(*(const EngineID*)a); - const Engine *e_b = Engine::Get(*(const EngineID*)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, @@ -237,13 +235,13 @@ return _internal_sort_order ? -r : r; } -static int CDECL TrainEngineCapacitySorter(const void *a, const void *b) +static int CDECL TrainEngineCapacitySorter(const EngineID *a, const EngineID *b) { - const RailVehicleInfo *rvi_a = RailVehInfo(*(const EngineID*)a); - const RailVehicleInfo *rvi_b = RailVehInfo(*(const EngineID*)b); + const RailVehicleInfo *rvi_a = RailVehInfo(*a); + const RailVehicleInfo *rvi_b = RailVehInfo(*b); - int va = GetTotalCapacityOfArticulatedParts(*(const EngineID*)a, VEH_TRAIN) * (rvi_a->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1); - int vb = GetTotalCapacityOfArticulatedParts(*(const EngineID*)b, VEH_TRAIN) * (rvi_b->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1); + int va = GetTotalCapacityOfArticulatedParts(*a, VEH_TRAIN) * (rvi_a->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1); + int vb = GetTotalCapacityOfArticulatedParts(*b, VEH_TRAIN) * (rvi_b->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1); int r = va - vb; /* Use EngineID to sort instead since we want consistent sorting */ @@ -251,12 +249,10 @@ return _internal_sort_order ? -r : r; } -static int CDECL TrainEnginesThenWagonsSorter(const void *a, const void *b) +static int CDECL TrainEnginesThenWagonsSorter(const EngineID *a, const EngineID *b) { - EngineID va = *(const EngineID*)a; - EngineID vb = *(const EngineID*)b; - int val_a = (RailVehInfo(va)->railveh_type == RAILVEH_WAGON ? 1 : 0); - int val_b = (RailVehInfo(vb)->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 */ @@ -265,10 +261,10 @@ } /* Road vehicle sorting functions */ -static int CDECL RoadVehEngineCapacitySorter(const void *a, const void *b) +static int CDECL RoadVehEngineCapacitySorter(const EngineID *a, const EngineID *b) { - int va = GetTotalCapacityOfArticulatedParts(*(const EngineID*)a, VEH_ROAD); - int vb = GetTotalCapacityOfArticulatedParts(*(const EngineID*)b, VEH_ROAD); + int va = GetTotalCapacityOfArticulatedParts(*a, VEH_ROAD); + int vb = GetTotalCapacityOfArticulatedParts(*b, VEH_ROAD); int r = va - vb; /* Use EngineID to sort instead since we want consistent sorting */ @@ -277,10 +273,10 @@ } /* Ship vehicle sorting functions */ -static int CDECL ShipEngineCapacitySorter(const void *a, const void *b) +static int CDECL ShipEngineCapacitySorter(const EngineID *a, const EngineID *b) { - const Engine *e_a = Engine::Get(*(const EngineID*)a); - const Engine *e_b = Engine::Get(*(const EngineID*)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(); @@ -292,10 +288,10 @@ } /* Aircraft sorting functions */ -static int CDECL AircraftEngineCargoSorter(const void *a, const void *b) +static int CDECL AircraftEngineCargoSorter(const EngineID *a, const EngineID *b) { - const Engine *e_a = Engine::Get(*(const EngineID*)a); - const Engine *e_b = Engine::Get(*(const EngineID*)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(); @@ -303,8 +299,8 @@ if (r == 0) { /* The planes has the same passenger capacity. Check mail capacity instead */ - va = AircraftVehInfo(*(const EngineID*)a)->mail_capacity; - vb = AircraftVehInfo(*(const EngineID*)b)->mail_capacity; + va = AircraftVehInfo(*a)->mail_capacity; + vb = AircraftVehInfo(*b)->mail_capacity; r = va - vb; if (r == 0) {
--- a/src/core/sort_func.hpp +++ b/src/core/sort_func.hpp @@ -19,7 +19,6 @@ /** * Type safe qsort() * - * @todo replace the normal qsort with this one * @note Use this sort for irregular sorted data. * * @param base Pointer to the first element of the array to be sorted.
--- a/src/engine_gui.cpp +++ b/src/engine_gui.cpp @@ -23,6 +23,7 @@ #include "vehicle_func.h" #include "company_func.h" #include "rail.h" +#include "core/sort_func.hpp" #include "table/strings.h" #include "table/sprites.h" @@ -263,7 +264,7 @@ } } -/** Sort all items using qsort() and given 'CompareItems' function +/** Sort all items using quick sort and given 'CompareItems' function * @param el list to be sorted * @param compare function for evaluation of the quicksort */ @@ -273,7 +274,7 @@ /* 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; - qsort(el->Begin(), size, sizeof(*el->Begin()), compare); // MorphOS doesn't know vector::at(int) ... + QSortT(el->Begin(), size, compare); } /** Sort selected range of items (on indices @ <begin, begin+num_items-1>) @@ -287,6 +288,6 @@ if (num_items < 2) return; assert(begin < el->Length()); assert(begin + num_items <= el->Length()); - qsort(el->Get(begin), num_items, sizeof(*el->Begin()), compare); + QSortT(el->Get(begin), num_items, compare); }
--- a/src/engine_gui.h +++ b/src/engine_gui.h @@ -16,9 +16,9 @@ typedef GUIList<EngineID, CargoID> GUIEngineList; -typedef int CDECL EngList_SortTypeFunction(const void*, const void*); ///< argument type for EngList_Sort() -void EngList_Sort(GUIEngineList *el, EngList_SortTypeFunction compare); ///< qsort of the engine list -void EngList_SortPartial(GUIEngineList *el, EngList_SortTypeFunction compare, uint begin, uint num_items); ///< qsort of specified portion of the engine list +typedef int CDECL EngList_SortTypeFunction(const EngineID*, const EngineID*); ///< argument type for EngList_Sort() +void EngList_Sort(GUIEngineList *el, EngList_SortTypeFunction compare); ///< sort of the engine list +void EngList_SortPartial(GUIEngineList *el, EngList_SortTypeFunction compare, uint begin, uint num_items); ///< sort of specified portion of the engine list StringID GetEngineCategoryName(EngineID engine); StringID GetEngineInfoString(EngineID engine);
--- a/src/sortlist_type.h +++ b/src/sortlist_type.h @@ -25,7 +25,7 @@ VL_DESC = 1 << 0, ///< sort descending or ascending VL_RESORT = 1 << 1, ///< instruct the code to resort the list in the next loop VL_REBUILD = 1 << 2, ///< rebuild the sort list - VL_FIRST_SORT = 1 << 3, ///< sort with qsort first + VL_FIRST_SORT = 1 << 3, ///< sort with quick sort first VL_FILTER = 1 << 4, ///< filter disabled/enabled VL_END = 1 << 5, }; @@ -246,7 +246,7 @@ /** * Sort the list. - * For the first sorting we use qsort since it is + * For the first sorting we use quick sort since it is * faster for irregular sorted data. After that we * use gsort. *