diff options
author | rubidium <rubidium@openttd.org> | 2009-09-13 17:47:07 +0000 |
---|---|---|
committer | rubidium <rubidium@openttd.org> | 2009-09-13 17:47:07 +0000 |
commit | 17313b6e3f875055c148e3543eea9027e554ca70 (patch) | |
tree | ee1bbc7447553ce6f33025b58f091867bf099452 | |
parent | e4809052c33f1c090b70ec32953ca7592ec7735e (diff) | |
download | openttd-17313b6e3f875055c148e3543eea9027e554ca70.tar.xz |
(svn r17530) -Codechange: use QSortT instead of qsort for sorting EngineIDs
-rw-r--r-- | src/autoreplace_gui.cpp | 6 | ||||
-rw-r--r-- | src/build_vehicle_gui.cpp | 94 | ||||
-rw-r--r-- | src/core/sort_func.hpp | 1 | ||||
-rw-r--r-- | src/engine_gui.cpp | 7 | ||||
-rw-r--r-- | src/engine_gui.h | 6 | ||||
-rw-r--r-- | src/sortlist_type.h | 4 |
6 files changed, 56 insertions, 62 deletions
diff --git a/src/autoreplace_gui.cpp b/src/autoreplace_gui.cpp index 49c00f4fe..3d00bfa3b 100644 --- a/src/autoreplace_gui.cpp +++ b/src/autoreplace_gui.cpp @@ -58,11 +58,9 @@ enum ReplaceVehicleWindowWidgets { 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; } diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp index cec4c9a63..3c0c6e27c 100644 --- a/src/build_vehicle_gui.cpp +++ b/src/build_vehicle_gui.cpp @@ -114,19 +114,17 @@ static byte _last_sort_criteria[] = {0, 0, 0, 0}; 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 @@ static int CDECL EngineIntroDateSorter(const void *a, const void *b) 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 @@ static int CDECL EngineNameSorter(const void *a, const void *b) 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 @@ static int CDECL EngineReliabilitySorter(const void *a, const void *b) 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 @@ static int CDECL EngineCostSorter(const void *a, const void *b) 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 @@ static int CDECL EngineSpeedSorter(const void *a, const void *b) 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 @@ static int CDECL EnginePowerSorter(const void *a, const void *b) 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 @@ static int CDECL EngineRunningCostSorter(const void *a, const void *b) } /* 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 @@ static int CDECL TrainEnginePowerVsRunningCostSorter(const void *a, const void * 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 @@ static int CDECL TrainEngineCapacitySorter(const void *a, const void *b) 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 @@ static int CDECL TrainEnginesThenWagonsSorter(const void *a, const void *b) } /* 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 @@ static int CDECL RoadVehEngineCapacitySorter(const void *a, const void *b) } /* 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 @@ static int CDECL ShipEngineCapacitySorter(const void *a, const void *b) } /* 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 @@ static int CDECL AircraftEngineCargoSorter(const void *a, const void *b) 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) { diff --git a/src/core/sort_func.hpp b/src/core/sort_func.hpp index 3580d9e10..d3bb7202a 100644 --- 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. diff --git a/src/engine_gui.cpp b/src/engine_gui.cpp index c6f3d02e9..a70967688 100644 --- 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 @@ void DrawVehicleEngine(int x, int y, EngineID engine, SpriteID pal) } } -/** 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 @@ 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; - 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 @@ void EngList_SortPartial(GUIEngineList *el, EngList_SortTypeFunction compare, ui 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); } diff --git a/src/engine_gui.h b/src/engine_gui.h index b69f8d348..eb752f48a 100644 --- 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); diff --git a/src/sortlist_type.h b/src/sortlist_type.h index a59f8e59a..1c777d96f 100644 --- a/src/sortlist_type.h +++ b/src/sortlist_type.h @@ -25,7 +25,7 @@ enum SortListFlags { 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 @@ public: /** * 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. * |