From ba146839129e30f122fe247cdc052166d8edc08a Mon Sep 17 00:00:00 2001 From: tron Date: Mon, 14 Aug 2006 20:25:29 +0000 Subject: (svn r5894) Remove a totally unnecessary indirection in the vehicle sorter code. Less code, less data, simply better --- aircraft_gui.c | 4 ++-- roadveh_gui.c | 4 ++-- ship_gui.c | 4 ++-- train_gui.c | 4 ++-- vehicle_gui.c | 58 ++++++++++++++++++++++++++-------------------------------- window.h | 2 +- 6 files changed, 35 insertions(+), 41 deletions(-) diff --git a/aircraft_gui.c b/aircraft_gui.c index 72b05f018..72784b99e 100644 --- a/aircraft_gui.c +++ b/aircraft_gui.c @@ -1047,7 +1047,7 @@ static void PlayerAircraftWndProc(Window *w, WindowEvent *e) max = min(w->vscroll.pos + w->vscroll.cap, vl->list_length); for (i = w->vscroll.pos; i < max; ++i) { - Vehicle *v = GetVehicle(vl->sort_list[i].index); + const Vehicle* v = vl->sort_list[i]; StringID str; assert(v->type == VEH_Aircraft && v->subtype <= 2); @@ -1101,7 +1101,7 @@ static void PlayerAircraftWndProc(Window *w, WindowEvent *e) if (id_v >= vl->list_length) return; // click out of list bound - v = GetVehicle(vl->sort_list[id_v].index); + v = vl->sort_list[id_v]; assert(v->type == VEH_Aircraft && v->subtype <= 2); diff --git a/roadveh_gui.c b/roadveh_gui.c index 1716c3556..f047cd93a 100644 --- a/roadveh_gui.c +++ b/roadveh_gui.c @@ -986,7 +986,7 @@ static void PlayerRoadVehWndProc(Window *w, WindowEvent *e) max = min(w->vscroll.pos + w->vscroll.cap, vl->list_length); for (i = w->vscroll.pos; i < max; ++i) { - Vehicle *v = GetVehicle(vl->sort_list[i].index); + const Vehicle* v = vl->sort_list[i]; StringID str; assert(v->type == VEH_Road && v->owner == owner); @@ -1037,7 +1037,7 @@ static void PlayerRoadVehWndProc(Window *w, WindowEvent *e) if (id_v >= vl->list_length) return; // click out of list bound - v = GetVehicle(vl->sort_list[id_v].index); + v = vl->sort_list[id_v]; assert(v->type == VEH_Road && v->owner == owner); diff --git a/ship_gui.c b/ship_gui.c index 96e2db62d..e8fd59c71 100644 --- a/ship_gui.c +++ b/ship_gui.c @@ -996,7 +996,7 @@ static void PlayerShipsWndProc(Window *w, WindowEvent *e) max = min(w->vscroll.pos + w->vscroll.cap, vl->list_length); for (i = w->vscroll.pos; i < max; ++i) { - Vehicle *v = GetVehicle(vl->sort_list[i].index); + const Vehicle* v = vl->sort_list[i]; StringID str; assert(v->type == VEH_Ship); @@ -1048,7 +1048,7 @@ static void PlayerShipsWndProc(Window *w, WindowEvent *e) if (id_v >= vl->list_length) return; // click out of list bound - v = GetVehicle(vl->sort_list[id_v].index); + v = vl->sort_list[id_v]; assert(v->type == VEH_Ship); diff --git a/train_gui.c b/train_gui.c index 4b6091702..b732e02a7 100644 --- a/train_gui.c +++ b/train_gui.c @@ -1445,7 +1445,7 @@ static void PlayerTrainsWndProc(Window *w, WindowEvent *e) max = min(w->vscroll.pos + w->vscroll.cap, vl->list_length); for (i = w->vscroll.pos; i < max; ++i) { - Vehicle *v = GetVehicle(vl->sort_list[i].index); + const Vehicle* v = vl->sort_list[i]; StringID str; assert(v->type == VEH_Train && v->owner == owner); @@ -1498,7 +1498,7 @@ static void PlayerTrainsWndProc(Window *w, WindowEvent *e) if (id_v >= vl->list_length) return; // click out of list bound - v = GetVehicle(vl->sort_list[id_v].index); + v = vl->sort_list[id_v]; assert(v->type == VEH_Train && IsFrontEngine(v) && v->owner == owner); diff --git a/vehicle_gui.c b/vehicle_gui.c index 8b24a5653..c231e5f8d 100644 --- a/vehicle_gui.c +++ b/vehicle_gui.c @@ -117,7 +117,7 @@ void ResortVehicleLists(void) void BuildVehicleList(vehiclelist_d* vl, int type, PlayerID owner, StationID station) { - SortStruct* sort_list; + const Vehicle** sort_list; uint subtype = (type != VEH_Aircraft) ? Train_Front : 2; uint n = 0; uint i; @@ -143,9 +143,7 @@ void BuildVehicleList(vehiclelist_d* vl, int type, PlayerID owner, StationID sta FOR_VEHICLE_ORDERS(v, order) { if (order->type == OT_GOTO_STATION && order->station == station) { - sort_list[n].index = v->index; - sort_list[n].owner = v->owner; - ++n; + sort_list[n++] = v; break; } } @@ -158,9 +156,7 @@ void BuildVehicleList(vehiclelist_d* vl, int type, PlayerID owner, StationID sta (type == VEH_Train && IsFrontEngine(v)) || (type != VEH_Train && v->subtype <= subtype) )) { - sort_list[n].index = v->index; - sort_list[n].owner = v->owner; - ++n; + sort_list[n++] = v; } } } @@ -283,7 +279,7 @@ int CDECL GeneralOwnerSorter(const void *a, const void *b) */ static int CDECL VehicleUnsortedSorter(const void *a, const void *b) { - return ((const SortStruct*)a)->index - ((const SortStruct*)b)->index; + return (*(const Vehicle**)a)->index - (*(const Vehicle**)b)->index; } // if the sorting criteria had the same value, sort vehicle by unitnumber @@ -291,8 +287,8 @@ static int CDECL VehicleUnsortedSorter(const void *a, const void *b) static int CDECL VehicleNumberSorter(const void *a, const void *b) { - const Vehicle *va = GetVehicle((*(const SortStruct*)a).index); - const Vehicle *vb = GetVehicle((*(const SortStruct*)b).index); + const Vehicle* va = *(const Vehicle**)a; + const Vehicle* vb = *(const Vehicle**)b; int r = va->unitnumber - vb->unitnumber; return (_internal_sort_order & 1) ? -r : r; @@ -301,10 +297,8 @@ static int CDECL VehicleNumberSorter(const void *a, const void *b) static char _bufcache[64]; // used together with _last_vehicle_idx to hopefully speed up stringsorting static int CDECL VehicleNameSorter(const void *a, const void *b) { - const SortStruct *cmp1 = (const SortStruct*)a; - const SortStruct *cmp2 = (const SortStruct*)b; - const Vehicle *va = GetVehicle(cmp1->index); - const Vehicle *vb = GetVehicle(cmp2->index); + const Vehicle* va = *(const Vehicle**)a; + const Vehicle* vb = *(const Vehicle**)b; char buf1[64] = "\0"; int r; @@ -313,8 +307,8 @@ static int CDECL VehicleNameSorter(const void *a, const void *b) GetString(buf1, STR_JUST_STRING); } - if (cmp2->index != _last_vehicle_idx) { - _last_vehicle_idx = cmp2->index; + if (vb->index != _last_vehicle_idx) { + _last_vehicle_idx = vb->index; _bufcache[0] = '\0'; if (vb->string_id != _internal_name_sorter_id) { SetDParam(0, vb->string_id); @@ -331,8 +325,8 @@ static int CDECL VehicleNameSorter(const void *a, const void *b) static int CDECL VehicleAgeSorter(const void *a, const void *b) { - const Vehicle *va = GetVehicle((*(const SortStruct*)a).index); - const Vehicle *vb = GetVehicle((*(const SortStruct*)b).index); + const Vehicle* va = *(const Vehicle**)a; + const Vehicle* vb = *(const Vehicle**)b; int r = va->age - vb->age; VEHICLEUNITNUMBERSORTER(r, va, vb); @@ -342,8 +336,8 @@ static int CDECL VehicleAgeSorter(const void *a, const void *b) static int CDECL VehicleProfitThisYearSorter(const void *a, const void *b) { - const Vehicle *va = GetVehicle((*(const SortStruct*)a).index); - const Vehicle *vb = GetVehicle((*(const SortStruct*)b).index); + const Vehicle* va = *(const Vehicle**)a; + const Vehicle* vb = *(const Vehicle**)b; int r = va->profit_this_year - vb->profit_this_year; VEHICLEUNITNUMBERSORTER(r, va, vb); @@ -353,8 +347,8 @@ static int CDECL VehicleProfitThisYearSorter(const void *a, const void *b) static int CDECL VehicleProfitLastYearSorter(const void *a, const void *b) { - const Vehicle *va = GetVehicle((*(const SortStruct*)a).index); - const Vehicle *vb = GetVehicle((*(const SortStruct*)b).index); + const Vehicle* va = *(const Vehicle**)a; + const Vehicle* vb = *(const Vehicle**)b; int r = va->profit_last_year - vb->profit_last_year; VEHICLEUNITNUMBERSORTER(r, va, vb); @@ -364,8 +358,8 @@ static int CDECL VehicleProfitLastYearSorter(const void *a, const void *b) static int CDECL VehicleCargoSorter(const void *a, const void *b) { - const Vehicle* va = GetVehicle(((const SortStruct*)a)->index); - const Vehicle* vb = GetVehicle(((const SortStruct*)b)->index); + const Vehicle* va = *(const Vehicle**)a; + const Vehicle* vb = *(const Vehicle**)b; const Vehicle* v; AcceptedCargo cargoa; AcceptedCargo cargob; @@ -389,8 +383,8 @@ static int CDECL VehicleCargoSorter(const void *a, const void *b) static int CDECL VehicleReliabilitySorter(const void *a, const void *b) { - const Vehicle *va = GetVehicle((*(const SortStruct*)a).index); - const Vehicle *vb = GetVehicle((*(const SortStruct*)b).index); + const Vehicle* va = *(const Vehicle**)a; + const Vehicle* vb = *(const Vehicle**)b; int r = va->reliability - vb->reliability; VEHICLEUNITNUMBERSORTER(r, va, vb); @@ -400,8 +394,8 @@ static int CDECL VehicleReliabilitySorter(const void *a, const void *b) static int CDECL VehicleMaxSpeedSorter(const void *a, const void *b) { - const Vehicle *va = GetVehicle((*(const SortStruct*)a).index); - const Vehicle *vb = GetVehicle((*(const SortStruct*)b).index); + const Vehicle* va = *(const Vehicle**)a; + const Vehicle* vb = *(const Vehicle**)b; int max_speed_a = 0xFFFF, max_speed_b = 0xFFFF; int r; const Vehicle *ua = va, *ub = vb; @@ -429,8 +423,8 @@ static int CDECL VehicleMaxSpeedSorter(const void *a, const void *b) static int CDECL VehicleModelSorter(const void *a, const void *b) { - const Vehicle *va = GetVehicle((*(const SortStruct*)a).index); - const Vehicle *vb = GetVehicle((*(const SortStruct*)b).index); + const Vehicle* va = *(const Vehicle**)a; + const Vehicle* vb = *(const Vehicle**)b; int r = va->engine_type - vb->engine_type; VEHICLEUNITNUMBERSORTER(r, va, vb); @@ -440,8 +434,8 @@ static int CDECL VehicleModelSorter(const void *a, const void *b) static int CDECL VehicleValueSorter(const void *a, const void *b) { - const Vehicle *va = GetVehicle((*(const SortStruct*)a).index); - const Vehicle *vb = GetVehicle((*(const SortStruct*)b).index); + const Vehicle* va = *(const Vehicle**)a; + const Vehicle* vb = *(const Vehicle**)b; const Vehicle *u; int valuea = 0, valueb = 0; int r; diff --git a/window.h b/window.h index 353e6bc12..a5700f22e 100644 --- a/window.h +++ b/window.h @@ -431,7 +431,7 @@ typedef enum VehicleListFlags { } VehicleListFlags; typedef struct vehiclelist_d { - SortStruct *sort_list; + const Vehicle** sort_list; uint16 list_length; byte sort_type; VehicleListFlags flags; -- cgit v1.2.3-70-g09d2