(svn r8007) -Codechange: Change _vehicle_position_hash from VehicleID to Vehicle*. This removes the need for look ups by index. Also declare the array static.
authorpeter1138
Tue, 09 Jan 2007 16:27:25 +0000
changeset 5825 d813480f22ac
parent 5824 49420056ec89
child 5826 fe5417aa722f
(svn r8007) -Codechange: Change _vehicle_position_hash from VehicleID to Vehicle*. This removes the need for look ups by index. Also declare the array static.
src/vehicle.c
src/vehicle.h
--- a/src/vehicle.c	Tue Jan 09 15:26:43 2007 +0000
+++ b/src/vehicle.c	Tue Jan 09 16:27:25 2007 +0000
@@ -263,7 +263,7 @@
 	v->left_coord = INVALID_COORD;
 	v->first = NULL;
 	v->next = NULL;
-	v->next_hash = INVALID_VEHICLE;
+	v->next_hash = NULL;
 	v->string_id = 0;
 	v->next_shared = NULL;
 	v->prev_shared = NULL;
@@ -368,7 +368,7 @@
 }
 
 
-VehicleID _vehicle_position_hash[0x1000];
+static Vehicle *_vehicle_position_hash[0x1000];
 
 void *VehicleFromPos(TileIndex tile, void *data, VehicleFromPosProc *proc)
 {
@@ -385,14 +385,13 @@
 
 	for (y = yl;; y = (y + (1 << 6)) & (0x3F << 6)) {
 		for (x = xl;; x = (x + 1) & 0x3F) {
-			VehicleID veh = _vehicle_position_hash[(x + y) & 0xFFFF];
-
-			while (veh != INVALID_VEHICLE) {
-				Vehicle *v = GetVehicle(veh);
+			Vehicle *v = _vehicle_position_hash[(x + y) & 0xFFFF];
+
+			while (v != NULL) {
 				void* a = proc(v, data);
 
 				if (a != NULL) return a;
-				veh = v->next_hash;
+				v = v->next_hash;
 			}
 
 			if (x == xu) break;
@@ -406,10 +405,9 @@
 
 static void UpdateVehiclePosHash(Vehicle* v, int x, int y)
 {
-	VehicleID *old_hash, *new_hash;
+	Vehicle **old_hash, **new_hash;
 	int old_x = v->left_coord;
 	int old_y = v->top_coord;
-	Vehicle *u;
 
 	new_hash = (x == INVALID_COORD) ? NULL : &_vehicle_position_hash[GEN_HASH(x,y)];
 	old_hash = (old_x == INVALID_COORD) ? NULL : &_vehicle_position_hash[GEN_HASH(old_x, old_y)];
@@ -419,11 +417,11 @@
 	/* remove from hash table? */
 	if (old_hash != NULL) {
 		Vehicle *last = NULL;
-		VehicleID idx = *old_hash;
-		while ((u = GetVehicle(idx)) != v) {
-			idx = u->next_hash;
-			assert(idx != INVALID_VEHICLE);
+		Vehicle *u = *old_hash;
+		while (u != v) {
 			last = u;
+			u = u->next_hash;
+			assert(u != NULL);
 		}
 
 		if (last == NULL) {
@@ -436,16 +434,13 @@
 	/* insert into hash table? */
 	if (new_hash != NULL) {
 		v->next_hash = *new_hash;
-		*new_hash = v->index;
+		*new_hash = v;
 	}
 }
 
 void ResetVehiclePosHash(void)
 {
-	uint i;
-	for (i = 0; i < lengthof(_vehicle_position_hash); i++) {
-		_vehicle_position_hash[i] = INVALID_VEHICLE;
-	}
+	memset(_vehicle_position_hash, 0, sizeof(_vehicle_position_hash));
 }
 
 void InitializeVehicles(void)
@@ -574,7 +569,7 @@
 	}
 
 	UpdateVehiclePosHash(v, INVALID_COORD, 0);
-	v->next_hash = INVALID_VEHICLE;
+	v->next_hash = NULL;
 	if (v->orders != NULL) DeleteVehicleOrders(v);
 
 	/* Now remove any artic part. This will trigger an other
@@ -831,11 +826,9 @@
 
 	for (y = yl;; y = (y + (1 << 6)) & (0x3F << 6)) {
 		for (x = xl;; x = (x + 1) & 0x3F) {
-			VehicleID veh = _vehicle_position_hash[(x + y) & 0xFFFF];
-
-			while (veh != INVALID_VEHICLE) {
-				const Vehicle* v = GetVehicle(veh);
-
+			const Vehicle *v = _vehicle_position_hash[(x + y) & 0xFFFF];
+
+			while (v != NULL) {
 				if (!(v->vehstatus & VS_HIDDEN) &&
 						l <= v->right_coord &&
 						t <= v->bottom_coord &&
@@ -843,7 +836,7 @@
 						b >= v->top_coord) {
 					DoDrawVehicle(v);
 				}
-				veh = v->next_hash;
+				v = v->next_hash;
 			}
 
 			if (x == xu) break;
--- a/src/vehicle.h	Tue Jan 09 15:26:43 2007 +0000
+++ b/src/vehicle.h	Tue Jan 09 16:27:25 2007 +0000
@@ -219,7 +219,7 @@
 	int32 top_coord;
 	int32 right_coord;
 	int32 bottom_coord;
-	VehicleID next_hash;
+	Vehicle *next_hash;
 
 	// Related to age and service time
 	Date age;     // Age in days