changeset 4266:4130063a9674 draft

(svn r5894) Remove a totally unnecessary indirection in the vehicle sorter code. Less code, less data, simply better
author tron <tron@openttd.org>
date Mon, 14 Aug 2006 20:25:29 +0000
parents b4509994875b
children b41879c80a10
files aircraft_gui.c roadveh_gui.c ship_gui.c train_gui.c vehicle_gui.c window.h
diffstat 6 files changed, 35 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/aircraft_gui.c
+++ b/aircraft_gui.c
@@ -1047,7 +1047,7 @@
 
 		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 @@
 
 			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);
 
--- a/roadveh_gui.c
+++ b/roadveh_gui.c
@@ -986,7 +986,7 @@
 
 		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 @@
 
 			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);
 
--- a/ship_gui.c
+++ b/ship_gui.c
@@ -996,7 +996,7 @@
 
 		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 @@
 
 			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);
 
--- a/train_gui.c
+++ b/train_gui.c
@@ -1445,7 +1445,7 @@
 
 		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 @@
 
 			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);
 
--- a/vehicle_gui.c
+++ b/vehicle_gui.c
@@ -117,7 +117,7 @@
 
 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 @@
 
 				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 @@
 						(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 @@
 */
 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 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 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 @@
 		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 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 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 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 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 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 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 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 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;
--- a/window.h
+++ b/window.h
@@ -431,7 +431,7 @@
 } VehicleListFlags;
 
 typedef struct vehiclelist_d {
-	SortStruct *sort_list;
+	const Vehicle** sort_list;
 	uint16 list_length;
 	byte sort_type;
 	VehicleListFlags flags;