changeset 19376:091acbb3fc45 draft

(svn r24277) -Codechange: Store cargo and railtype translation tables in a SmallVector.
author frosch <frosch@openttd.org>
date Fri, 25 May 2012 20:57:36 +0000
parents c890215c5f1f
children eb2138edac21
files src/newgrf.cpp src/newgrf.h src/newgrf_cargo.cpp src/newgrf_railtype.cpp
diffstat 4 files changed, 26 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/src/newgrf.cpp
+++ b/src/newgrf.cpp
@@ -953,7 +953,7 @@
 			case 0x05: { // Track type
 				uint8 tracktype = buf->ReadByte();
 
-				if (tracktype < _cur.grffile->railtype_max) {
+				if (tracktype < _cur.grffile->railtype_list.Length()) {
 					_gted[e->index].railtypelabel = _cur.grffile->railtype_list[tracktype];
 					break;
 				}
@@ -1089,7 +1089,7 @@
 					break;
 				}
 
-				if (_cur.grffile->railtype_max == 0) {
+				if (_cur.grffile->railtype_list.Length() == 0) {
 					/* Use traction type to select between normal and electrified
 					 * rail only when no translation list is in place. */
 					if (_gted[e->index].railtypelabel == RAILTYPE_RAIL_LABEL     && engclass >= EC_ELECTRIC) _gted[e->index].railtypelabel = RAILTYPE_ELECTRIC_LABEL;
@@ -2449,9 +2449,8 @@
 						return CIR_INVALID_ID;
 					}
 
-					free(_cur.grffile->cargo_list);
-					_cur.grffile->cargo_max = numinfo;
-					_cur.grffile->cargo_list = MallocT<CargoLabel>(numinfo);
+					_cur.grffile->cargo_list.Clear();
+					_cur.grffile->cargo_list.Append(numinfo);
 				}
 
 				CargoLabel cl = buf->ReadDWord();
@@ -2578,9 +2577,8 @@
 						return CIR_INVALID_ID;
 					}
 
-					free(_cur.grffile->railtype_list);
-					_cur.grffile->railtype_max = numinfo;
-					_cur.grffile->railtype_list = MallocT<RailTypeLabel>(numinfo);
+					_cur.grffile->railtype_list.Clear();
+					_cur.grffile->railtype_list.Append(numinfo);
 				}
 
 				RailTypeLabel rtl = buf->ReadDWord();
@@ -2679,9 +2677,8 @@
 						return CIR_INVALID_ID;
 					}
 
-					free(_cur.grffile->cargo_list);
-					_cur.grffile->cargo_max = numinfo;
-					_cur.grffile->cargo_list = MallocT<CargoLabel>(numinfo);
+					_cur.grffile->cargo_list.Clear();
+					_cur.grffile->cargo_list.Append(numinfo);
 				}
 
 				CargoLabel cl = buf->ReadDWord();
@@ -2719,9 +2716,8 @@
 						return CIR_INVALID_ID;
 					}
 
-					free(_cur.grffile->railtype_list);
-					_cur.grffile->railtype_max = numinfo;
-					_cur.grffile->railtype_list = MallocT<RailTypeLabel>(numinfo);
+					_cur.grffile->railtype_list.Clear();
+					_cur.grffile->railtype_list.Append(numinfo);
 				}
 
 				RailTypeLabel rtl = buf->ReadDWord();
@@ -4725,7 +4721,7 @@
 	if (feature == GSF_STATIONS && ctype == 0xFE) return CT_DEFAULT_NA;
 	if (ctype == 0xFF) return CT_PURCHASE;
 
-	if (_cur.grffile->cargo_max == 0) {
+	if (_cur.grffile->cargo_list.Length() == 0) {
 		/* No cargo table, so use bitnum values */
 		if (ctype >= 32) {
 			grfmsg(1, "TranslateCargo: Cargo bitnum %d out of range (max 31), skipping.", ctype);
@@ -4745,8 +4741,8 @@
 	}
 
 	/* Check if the cargo type is out of bounds of the cargo translation table */
-	if (ctype >= _cur.grffile->cargo_max) {
-		grfmsg(1, "TranslateCargo: Cargo type %d out of range (max %d), skipping.", ctype, _cur.grffile->cargo_max - 1);
+	if (ctype >= _cur.grffile->cargo_list.Length()) {
+		grfmsg(1, "TranslateCargo: Cargo type %d out of range (max %d), skipping.", ctype, _cur.grffile->cargo_list.Length() - 1);
 		return CT_INVALID;
 	}
 
@@ -8036,17 +8032,13 @@
 		const CargoSpec *cs = CargoSpec::Get(c);
 		if (!cs->IsValid()) continue;
 
-		if (_cur.grffile->cargo_max == 0) {
+		if (_cur.grffile->cargo_list.Length() == 0) {
 			/* Default translation table, so just a straight mapping to bitnum */
 			_cur.grffile->cargo_map[c] = cs->bitnum;
 		} else {
 			/* Check the translation table for this cargo's label */
-			for (uint i = 0; i < _cur.grffile->cargo_max; i++) {
-				if (cs->label == _cur.grffile->cargo_list[i]) {
-					_cur.grffile->cargo_map[c] = i;
-					break;
-				}
-			}
+			int index = _cur.grffile->cargo_list.FindIndex(cs->label);
+			if (index >= 0) _cur.grffile->cargo_map[c] = index;
 		}
 	}
 }
@@ -8107,8 +8099,6 @@
 GRFFile::~GRFFile()
 {
 	free(this->filename);
-	free(this->cargo_list);
-	free(this->railtype_list);
 	delete[] this->language_map;
 }
 
@@ -8219,7 +8209,7 @@
 			{
 				const GRFFile *file = _gted[engine].defaultcargo_grf;
 				if (file == NULL) file = e->GetGRF();
-				if (file != NULL && file->grf_version >= 8 && file->cargo_max != 0) {
+				if (file != NULL && file->grf_version >= 8 && file->cargo_list.Length() != 0) {
 					cargo_map_for_first_refittable = file->cargo_map;
 				}
 			}
--- a/src/newgrf.h
+++ b/src/newgrf.h
@@ -17,6 +17,7 @@
 #include "fileio_type.h"
 #include "core/bitmath_func.hpp"
 #include "core/alloc_type.hpp"
+#include "core/smallvec_type.hpp"
 
 /**
  * List of different canal 'features'.
@@ -121,12 +122,10 @@
 
 	GRFLabel *label; ///< Pointer to the first label. This is a linked list, not an array.
 
-	uint8 cargo_max;
-	CargoLabel *cargo_list;
-	uint8 cargo_map[NUM_CARGO];
+	SmallVector<CargoLabel, 4> cargo_list;          ///< Cargo translation table (local ID -> label)
+	uint8 cargo_map[NUM_CARGO];                     ///< Inverse cargo translation table (CargoID -> local ID)
 
-	uint8 railtype_max;
-	RailTypeLabel *railtype_list;
+	SmallVector<RailTypeLabel, 4> railtype_list;    ///< Railtype translation table
 	RailType railtype_map[RAILTYPE_END];
 
 	CanalProperties canal_local_properties[CF_END]; ///< Canal properties as set by this NewGRF
--- a/src/newgrf_cargo.cpp
+++ b/src/newgrf_cargo.cpp
@@ -116,10 +116,10 @@
 
 	/* Other cases use (possibly translated) cargobits */
 
-	if (grffile->cargo_max > 0) {
+	if (grffile->cargo_list.Length() > 0) {
 		/* ...and the cargo is in bounds, then get the cargo ID for
 		 * the label */
-		if (cargo < grffile->cargo_max) return GetCargoIDByLabel(grffile->cargo_list[cargo]);
+		if (cargo < grffile->cargo_list.Length()) return GetCargoIDByLabel(grffile->cargo_list[cargo]);
 	} else {
 		/* Else the cargo value is a 'climate independent' 'bitnum' */
 		return GetCargoIDByBitnum(cargo);
--- a/src/newgrf_railtype.cpp
+++ b/src/newgrf_railtype.cpp
@@ -130,13 +130,12 @@
 uint8 GetReverseRailTypeTranslation(RailType railtype, const GRFFile *grffile)
 {
 	/* No rail type table present, return rail type as-is */
-	if (grffile == NULL || grffile->railtype_max == 0) return railtype;
+	if (grffile == NULL || grffile->railtype_list.Length() == 0) return railtype;
 
 	/* Look for a matching rail type label in the table */
 	RailTypeLabel label = GetRailTypeInfo(railtype)->label;
-	for (uint i = 0; i < grffile->railtype_max; i++) {
-		if (label == grffile->railtype_list[i]) return i;
-	}
+	int index = grffile->railtype_list.FindIndex(label);
+	if (index >= 0) return index;
 
 	/* If not found, return as invalid */
 	return 0xFF;