(svn r13266) -Codechange: Use SmallVector in GUIList
authorpeter1138
Mon, 26 May 2008 16:23:23 +0000
changeset 10716 2406d1520245
parent 10714 87db0ce130b3
child 10717 996d586844d9
(svn r13266) -Codechange: Use SmallVector in GUIList
src/bridge_gui.cpp
src/group_gui.cpp
src/industry_gui.cpp
src/network/network_gui.cpp
src/sortlist_type.h
src/station_gui.cpp
src/vehicle.cpp
src/vehicle_func.h
src/vehicle_gui.cpp
--- a/src/bridge_gui.cpp	Mon May 26 13:50:00 2008 +0000
+++ b/src/bridge_gui.cpp	Mon May 26 16:23:23 2008 +0000
@@ -120,7 +120,7 @@
 
 	void BuildBridge(uint8 i)
 	{
-		DoCommandP(this->end_tile, this->start_tile, this->type | this->bridges->sort_list[i].index,
+		DoCommandP(this->end_tile, this->start_tile, this->type | this->bridges->Get(i)->index,
 				CcBuildBridge, CMD_BUILD_BRIDGE | CMD_MSG(STR_5015_CAN_T_BUILD_BRIDGE_HERE));
 	}
 
@@ -130,7 +130,7 @@
 		/* Skip sorting if resort bit is not set */
 		if (!(bridges->flags & VL_RESORT)) return;
 
-		qsort(this->bridges->sort_list, this->bridges->list_length, sizeof(this->bridges->sort_list[0]), _bridge_sorter[_bridge_sorting.criteria]);
+		qsort(this->bridges->Begin(), this->bridges->Length(), sizeof(this->bridges->Begin()), _bridge_sorter[_bridge_sorting.criteria]);
 
 		/* Display the current sort variant */
 		this->widget[BBSW_DROPDOWN_CRITERIA].data = _bridge_sort_listing[this->bridges->sort_type];
@@ -155,7 +155,7 @@
 		this->widget[BBSW_CAPTION].data = (GB(this->type, 15, 2) == TRANSPORT_ROAD) ? STR_1803_SELECT_ROAD_BRIDGE : STR_100D_SELECT_RAIL_BRIDGE;
 
 		this->resize.step_height = 22;
-		this->vscroll.count = bl->list_length;
+		this->vscroll.count = bl->Length();
 
 		if (this->last_size <= 4) {
 			this->vscroll.cap = 4;
@@ -171,7 +171,6 @@
 
 	~BuildBridgeWindow()
 	{
-		free(this->bridges->sort_list);
 		delete bridges;
 	}
 
@@ -183,10 +182,10 @@
 
 		uint y = this->widget[BBSW_BRIDGE_LIST].top + 2;
 
-		for (int i = this->vscroll.pos; (i < (this->vscroll.cap + this->vscroll.pos)) && (i < this->bridges->list_length); i++) {
-			const BridgeSpec *b = this->bridges->sort_list[i].spec;
+		for (int i = this->vscroll.pos; (i < (this->vscroll.cap + this->vscroll.pos)) && (i < (int)this->bridges->Length()); i++) {
+			const BridgeSpec *b = this->bridges->Get(i)->spec;
 
-			SetDParam(2, this->bridges->sort_list[i].cost);
+			SetDParam(2, this->bridges->Get(i)->cost);
 			SetDParam(1, b->speed * 10 / 16);
 			SetDParam(0, b->material);
 
@@ -200,7 +199,7 @@
 	virtual EventState OnKeyPress(uint16 key, uint16 keycode)
 	{
 		const uint8 i = keycode - '1';
-		if (i < 9 && i < this->bridges->list_length) {
+		if (i < 9 && i < this->bridges->Length()) {
 			/* Build the requested bridge */
 			this->BuildBridge(i);
 			delete this;
@@ -217,7 +216,7 @@
 				uint i = ((int)pt.y - this->widget[BBSW_BRIDGE_LIST].top) / this->resize.step_height;
 				if (i < this->vscroll.cap) {
 					i += this->vscroll.pos;
-					if (i < this->bridges->list_length) {
+					if (i < this->bridges->Length()) {
 						this->BuildBridge(i);
 						delete this;
 					}
@@ -254,7 +253,7 @@
 	{
 		this->vscroll.cap += delta.y / (int)this->resize.step_height;
 		this->widget[BBSW_BRIDGE_LIST].data = (this->vscroll.cap << 8) + 1;
-		SetVScrollCount(this, this->bridges->list_length);
+		SetVScrollCount(this, this->bridges->Length());
 
 		this->last_size = this->vscroll.cap;
 	}
@@ -286,35 +285,6 @@
 };
 
 /**
- * Add a buildable bridge to the list.
- *  If the list is empty a new one is created.
- *
- * @param bl The list which we want to manage
- * @param item The item to add
- * @return The pointer to the list
- */
-static GUIBridgeList *PushBridgeList(GUIBridgeList *bl, BuildBridgeData item)
-{
-	if (bl == NULL) {
-		/* Create the list if needed */
-		bl = new GUIBridgeList();
-		bl->flags |= VL_RESORT;
-		if (_bridge_sorting.order) bl->flags |= VL_DESC;
-		bl->list_length = 1;
-		bl->sort_type = _bridge_sorting.criteria;
-	} else {
-		/* Resize the list */
-		bl->list_length++;
-	}
-
-	bl->sort_list = ReallocT(bl->sort_list, bl->list_length);
-
-	bl->sort_list[bl->list_length - 1] = item;
-
-	return bl;
-}
-
-/**
  * Prepare the data for the build a bridge window.
  *  If we can't build a bridge under the given conditions
  *  show an error message.
@@ -350,24 +320,26 @@
 		/* total length of bridge */
 		const uint tot_bridgedata_len = CalcBridgeLenCostFactor(bridge_len + 2);
 
+		bl = new GUIBridgeList();
+
 		/* loop for all bridgetypes */
 		for (BridgeType brd_type = 0; brd_type != MAX_BRIDGES; brd_type++) {
 			if (CheckBridge_Stuff(brd_type, bridge_len)) {
 				/* bridge is accepted, add to list */
-				BuildBridgeData item;
-				item.index = brd_type;
-				item.spec = GetBridgeSpec(brd_type);
+				BuildBridgeData *item = bl->Append();
+				item->index = brd_type;
+				item->spec = GetBridgeSpec(brd_type);
 				/* Add to terraforming & bulldozing costs the cost of the
 				 * bridge itself (not computed with DC_QUERY_COST) */
-				item.cost = ret.GetCost() + (((int64)tot_bridgedata_len * _price.build_bridge * item.spec->price) >> 8);
-				bl = PushBridgeList(bl, item);
+				item->cost = ret.GetCost() + (((int64)tot_bridgedata_len * _price.build_bridge * item->spec->price) >> 8);
 			}
 		}
 	}
 
-	if (bl != NULL) {
+	if (bl != NULL && bl->Length() != 0) {
 		new BuildBridgeWindow(&_build_bridge_desc, start, end, type, bl);
 	} else {
+		if (bl != NULL) delete bl;
 		ShowErrorMessage(errmsg, STR_5015_CAN_T_BUILD_BRIDGE_HERE, TileX(end) * TILE_SIZE, TileY(end) * TILE_SIZE);
 	}
 }
--- a/src/group_gui.cpp	Mon May 26 13:50:00 2008 +0000
+++ b/src/group_gui.cpp	Mon May 26 16:23:23 2008 +0000
@@ -32,22 +32,16 @@
 
 static void BuildGroupList(GUIGroupList *gl, PlayerID owner, VehicleType vehicle_type)
 {
-	uint n = 0;
-
 	if (!(gl->flags & VL_REBUILD)) return;
 
-	const Group **list = MallocT<const Group*>(GetGroupArraySize());
+	gl->Clear();
 
 	const Group *g;
 	FOR_ALL_GROUPS(g) {
-		if (g->owner == owner && g->vehicle_type == vehicle_type) list[n++] = g;
+		if (g->owner == owner && g->vehicle_type != vehicle_type) *gl->Append() = g;
 	}
 
-	gl->sort_list = ReallocT(gl->sort_list, n);
-	gl->list_length = n;
-
-	for (uint i = 0; i < n; ++i) gl->sort_list[i] = list[i];
-	free(list);
+	gl->Compact();
 
 	gl->flags &= ~VL_REBUILD;
 	gl->flags |= VL_RESORT;
@@ -87,7 +81,7 @@
 {
 	if (!(gl->flags & VL_RESORT)) return;
 
-	qsort((void*)gl->sort_list, gl->list_length, sizeof(gl->sort_list[0]), GroupNameSorter);
+	qsort((void*)gl->Begin(), gl->Length(), sizeof(gl->Begin()), GroupNameSorter);
 
 	gl->resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS;
 	gl->flags &= ~VL_RESORT;
@@ -223,12 +217,12 @@
 			case VEH_AIRCRAFT: this->sorting = &_sorting.aircraft; break;
 		}
 
-		this->vehicles.sort_list = NULL;
+		this->vehicles.Clear();
 		this->vehicles.sort_type = this->sorting->criteria;
 		this->vehicles.flags = VL_REBUILD | (this->sorting->order ? VL_DESC : VL_NONE);
 		this->vehicles.resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS; // Set up resort timer
 
-		this->groups.sort_list = NULL;
+		this->groups.Clear();
 		this->groups.flags = VL_REBUILD | VL_NONE;
 		this->groups.resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS; // Set up resort timer
 
@@ -280,8 +274,6 @@
 
 	~VehicleGroupWindow()
 	{
-		free((void*)this->vehicles.sort_list);
-		free((void*)this->groups.sort_list);
 	}
 
 	virtual void OnInvalidateData(int data)
@@ -313,17 +305,17 @@
 		BuildGroupList(&this->groups, owner, this->vehicle_type);
 		SortGroupList(&this->groups);
 
-		SetVScrollCount(this, this->groups.list_length);
-		SetVScroll2Count(this, this->vehicles.list_length);
+		SetVScrollCount(this, this->groups.Length());
+		SetVScroll2Count(this, this->vehicles.Length());
 
 		/* The drop down menu is out, *but* it may not be used, retract it. */
-		if (this->vehicles.list_length == 0 && this->IsWidgetLowered(GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN)) {
+		if (this->vehicles.Length() == 0 && this->IsWidgetLowered(GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN)) {
 			this->RaiseWidget(GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN);
 			HideDropDownMenu(this);
 		}
 
 		/* Disable all lists management button when the list is empty */
-		this->SetWidgetsDisabledState(this->vehicles.list_length == 0 || _local_player != owner,
+		this->SetWidgetsDisabledState(this->vehicles.Length() == 0 || _local_player != owner,
 				GRP_WIDGET_STOP_ALL,
 				GRP_WIDGET_START_ALL,
 				GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN,
@@ -352,7 +344,7 @@
 				We list all vehicles or ungrouped vehicles */
 		if (IsDefaultGroupID(this->group_sel) || IsAllGroupID(this->group_sel)) {
 			SetDParam(0, owner);
-			SetDParam(1, this->vehicles.list_length);
+			SetDParam(1, this->vehicles.Length());
 
 			switch (this->vehicle_type) {
 				case VEH_TRAIN:
@@ -434,9 +426,9 @@
 
 		DrawString(10, y1, str_no_group_veh, IsDefaultGroupID(this->group_sel) ? TC_WHITE : TC_BLACK);
 
-		max = min(this->vscroll.pos + this->vscroll.cap, this->groups.list_length);
+		max = min(this->vscroll.pos + this->vscroll.cap, this->groups.Length());
 		for (i = this->vscroll.pos ; i < max ; ++i) {
-			const Group *g = this->groups.sort_list[i];
+			const Group *g = this->groups[i];
 
 			assert(g->owner == owner);
 
@@ -454,9 +446,9 @@
 		this->DrawSortButtonState(GRP_WIDGET_SORT_BY_ORDER, this->vehicles.flags & VL_DESC ? SBS_DOWN : SBS_UP);
 
 		/* Draw Matrix Vehicle according to the vehicle list built before */
-		max = min(this->vscroll2.pos + this->vscroll2.cap, this->vehicles.list_length);
+		max = min(this->vscroll2.pos + this->vscroll2.cap, this->vehicles.Length());
 		for (i = this->vscroll2.pos ; i < max ; ++i) {
-			const Vehicle* v = this->vehicles.sort_list[i];
+			const Vehicle* v = this->vehicles[i];
 
 			assert(v->type == this->vehicle_type && v->owner == owner);
 
@@ -521,9 +513,9 @@
 
 				id_g += this->vscroll.pos;
 
-				if (id_g >= this->groups.list_length) return;
+				if (id_g >= this->groups.Length()) return;
 
-				this->group_sel = this->groups.sort_list[id_g]->index;;
+				this->group_sel = this->groups[id_g]->index;;
 
 				this->vehicles.flags |= VL_REBUILD;
 				this->SetDirty();
@@ -538,9 +530,9 @@
 
 				id_v += this->vscroll2.pos;
 
-				if (id_v >= this->vehicles.list_length) return; // click out of list bound
+				if (id_v >= this->vehicles.Length()) return; // click out of list bound
 
-				v = this->vehicles.sort_list[id_v];
+				v = this->vehicles[id_v];
 
 				this->vehicle_sel = v->index;
 
@@ -628,9 +620,9 @@
 
 				id_g += this->vscroll.pos;
 
-				if (id_g >= this->groups.list_length) return;
+				if (id_g >= this->groups.Length()) return;
 
-				DoCommandP(0, this->groups.sort_list[id_g]->index, vindex, NULL, CMD_ADD_VEHICLE_GROUP | CMD_MSG(STR_GROUP_CAN_T_ADD_VEHICLE));
+				DoCommandP(0, this->groups[id_g]->index, vindex, NULL, CMD_ADD_VEHICLE_GROUP | CMD_MSG(STR_GROUP_CAN_T_ADD_VEHICLE));
 
 				break;
 			}
@@ -648,9 +640,9 @@
 
 				id_v += this->vscroll2.pos;
 
-				if (id_v >= this->vehicles.list_length) return; // click out of list bound
+				if (id_v >= this->vehicles.Length()) return; // click out of list bound
 
-				v = this->vehicles.sort_list[id_v];
+				v = this->vehicles[id_v];
 
 				if (vindex == v->index) {
 					ShowVehicleViewWindow(v);
@@ -693,7 +685,7 @@
 				break;
 
 			case GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN:
-				assert(this->vehicles.list_length != 0);
+				assert(this->vehicles.Length() != 0);
 
 				switch (index) {
 					case GALF_REPLACE: // Replace window
--- a/src/industry_gui.cpp	Mon May 26 13:50:00 2008 +0000
+++ b/src/industry_gui.cpp	Mon May 26 16:23:23 2008 +0000
@@ -812,27 +812,21 @@
  */
 static void BuildIndustriesList(GUIIndustryList *sl)
 {
-	uint n = 0;
-	const Industry *i;
-
 	if (!(sl->flags & VL_REBUILD)) return;
 
-	/* Create array for sorting */
-	const Industry **industry_sort = MallocT<const Industry*>(GetMaxIndustryIndex() + 1);
+	sl->Clear();
 
 	DEBUG(misc, 3, "Building industry list");
 
-	FOR_ALL_INDUSTRIES(i) industry_sort[n++] = i;
+	const Industry *i;
+	FOR_ALL_INDUSTRIES(i) {
+		*sl->Append() = i;
+	}
 
-	free((void*)sl->sort_list);
-	sl->sort_list = MallocT<const Industry*>(n);
-	sl->list_length = n;
-
-	for (uint i = 0; i < n; ++i) sl->sort_list[i] = industry_sort[i];
+	sl->Compact();
 
 	sl->flags &= ~VL_REBUILD;
 	sl->flags |= VL_RESORT;
-	free((void*)industry_sort);
 }
 
 
@@ -847,7 +841,7 @@
 
 	_internal_sort_order = (sl->sort_type << 1) | (sl->flags & VL_DESC);
 	_last_industry = NULL; // used for "cache" in namesorting
-	qsort((void*)sl->sort_list, sl->list_length, sizeof(sl->sort_list[0]), &GeneralIndustrySorter);
+	qsort((void*)sl->Begin(), sl->Length(), sizeof(sl->Begin()), &GeneralIndustrySorter);
 
 	sl->flags &= ~VL_RESORT;
 }
@@ -865,7 +859,6 @@
 		this->resize.step_height = 10;
 		this->FindWindowPlacementAndResize(desc);
 
-		this->sort_list = NULL;
 		this->flags = VL_REBUILD;
 		this->sort_type = industry_sort.criteria;
 		if (industry_sort.order) this->flags |= VL_DESC;
@@ -876,16 +869,16 @@
 		BuildIndustriesList(this);
 		SortIndustriesList(this);
 
-		SetVScrollCount(this, this->list_length);
+		SetVScrollCount(this, this->Length());
 
 		this->DrawWidgets();
 		this->DrawSortButtonState(IDW_SORTBYNAME + this->sort_type, this->flags & VL_DESC ? SBS_DOWN : SBS_UP);
 
-		int max = min(this->vscroll.pos + this->vscroll.cap, this->list_length);
+		int max = min(this->vscroll.pos + this->vscroll.cap, this->Length());
 		int y = 28; // start of the list-widget
 
 		for (int n = this->vscroll.pos; n < max; ++n) {
-			const Industry* i = this->sort_list[n];
+			const Industry* i = *this->Get(n);
 			const IndustrySpec *indsp = GetIndustrySpec(i->type);
 			byte p = 0;
 
@@ -940,11 +933,11 @@
 
 				if (!IsInsideMM(y, 0, this->vscroll.cap)) return;
 				p = y + this->vscroll.pos;
-				if (p < this->list_length) {
+				if (p < this->Length()) {
 					if (_ctrl_pressed) {
-						ShowExtraViewPortWindow(this->sort_list[p]->xy);
+						ShowExtraViewPortWindow((*this->Get(p))->xy);
 					} else {
-						ScrollMainWindowToTile(this->sort_list[p]->xy);
+						ScrollMainWindowToTile((*this->Get(p))->xy);
 					}
 				}
 			} break;
--- a/src/network/network_gui.cpp	Mon May 26 13:50:00 2008 +0000
+++ b/src/network/network_gui.cpp	Mon May 26 16:23:23 2008 +0000
@@ -190,7 +190,6 @@
 		this->field = NGWW_PLAYER;
 		this->server = NULL;
 
-		this->servers.sort_list = NULL;
 		this->servers.flags = VL_REBUILD | (_ng_sorting.order ? VL_DESC : VL_NONE);
 		this->servers.sort_type = _ng_sorting.criteria;
 
@@ -199,7 +198,6 @@
 
 	~NetworkGameWindow()
 	{
-		free(this->servers.sort_list);
 	}
 
 	/**
@@ -208,23 +206,17 @@
 	 */
 	void BuildNetworkGameList()
 	{
-		NetworkGameList *ngl_temp;
-		uint n = 0;
-
 		if (!(this->servers.flags & VL_REBUILD)) return;
 
-		/* Count the number of games in the list */
-		for (ngl_temp = _network_game_list; ngl_temp != NULL; ngl_temp = ngl_temp->next) n++;
-		if (n == 0) return;
-
 		/* Create temporary array of games to use for listing */
-		this->servers.sort_list = ReallocT(this->servers.sort_list, n);
-		this->servers.list_length = n;
+		this->servers.Clear();
 
-		for (n = 0, ngl_temp = _network_game_list; ngl_temp != NULL; ngl_temp = ngl_temp->next) {
-			this->servers.sort_list[n++] = ngl_temp;
+		for (NetworkGameList *ngl = _network_game_list; ngl != NULL; ngl = ngl->next) {
+			*this->servers.Append() = ngl;
 		}
 
+		this->servers.Compact();
+
 		/* Force resort */
 		this->servers.flags &= ~VL_REBUILD;
 		this->servers.flags |= VL_RESORT;
@@ -242,17 +234,17 @@
 		uint i;
 
 		if (!(this->servers.flags & VL_RESORT)) return;
-		if (this->servers.list_length == 0) return;
+		if (this->servers.Length() == 0) return;
 
 		_internal_sort_order = !!(this->servers.flags & VL_DESC);
-		qsort(this->servers.sort_list, this->servers.list_length, sizeof(this->servers.sort_list[0]), ngame_sorter[this->servers.sort_type]);
+		qsort(this->servers.Begin(), this->servers.Length(), sizeof(this->servers.Begin()), ngame_sorter[this->servers.sort_type]);
 
 		/* After sorting ngl->sort_list contains the sorted items. Put these back
 		 * into the original list. Basically nothing has changed, we are only
 		 * shuffling the ->next pointers */
-		_network_game_list = this->servers.sort_list[0];
-		for (item = _network_game_list, i = 1; i != this->servers.list_length; i++) {
-			item->next = this->servers.sort_list[i];
+		_network_game_list = this->servers[0];
+		for (item = _network_game_list, i = 1; i != this->servers.Length(); i++) {
+			item->next = this->servers[i];
 			item = item->next;
 		}
 		item->next = NULL;
@@ -300,7 +292,7 @@
 
 		if (this->servers.flags & VL_REBUILD) {
 			this->BuildNetworkGameList();
-			SetVScrollCount(this, this->servers.list_length);
+			SetVScrollCount(this, this->servers.Length());
 		}
 		if (this->servers.flags & VL_RESORT) this->SortNetworkGameList();
 
@@ -582,7 +574,7 @@
 
 		this->widget[NGWW_MATRIX].data = (this->vscroll.cap << 8) + 1;
 
-		SetVScrollCount(this, this->servers.list_length);
+		SetVScrollCount(this, this->servers.Length());
 
 		int widget_width = this->widget[NGWW_FIND].right - this->widget[NGWW_FIND].left;
 		int space = (this->width - 4 * widget_width - 25) / 3;
--- a/src/sortlist_type.h	Mon May 26 13:50:00 2008 +0000
+++ b/src/sortlist_type.h	Mon May 26 16:23:23 2008 +0000
@@ -5,6 +5,8 @@
 #ifndef SORTLIST_TYPE_H
 #define SORTLIST_TYPE_H
 
+#include "misc/smallvec.h"
+
 enum SortListFlags {
 	VL_NONE    = 0,      ///< no sort
 	VL_DESC    = 1 << 0, ///< sort descending or ascending
@@ -20,10 +22,8 @@
 };
 
 template <typename T>
-struct GUIList {
-	T* sort_list;        ///< The items to sort.
+struct GUIList : public SmallVector<T, 32> {
 	SortListFlags flags; ///< used to control sorting/resorting/etc.
-	uint16 list_length;  ///< length of the list being sorted
 	uint16 resort_timer; ///< resort list after a given amount of ticks if set
 	byte sort_type;      ///< what criteria to sort on
 };
--- a/src/station_gui.cpp	Mon May 26 13:50:00 2008 +0000
+++ b/src/station_gui.cpp	Mon May 26 16:23:23 2008 +0000
@@ -178,16 +178,13 @@
  */
 static void BuildStationsList(GUIStationList *sl, PlayerID owner, byte facilities, uint32 cargo_filter, bool include_empty)
 {
-	uint n = 0;
-	const Station *st;
-
 	if (!(sl->flags & VL_REBUILD)) return;
 
-	/* Create array for sorting */
-	const Station **station_sort = MallocT<const Station*>(GetMaxStationIndex() + 1);
+	sl->Clear();
 
 	DEBUG(misc, 3, "Building station list for player %d", owner);
 
+	const Station *st;
 	FOR_ALL_STATIONS(st) {
 		if (st->owner == owner || (st->owner == OWNER_NONE && !st->IsBuoy() && HasStationInUse(st->index, owner))) {
 			if (facilities & st->facilities) { //only stations with selected facilities
@@ -196,28 +193,23 @@
 					if (!st->goods[j].cargo.Empty()) {
 						num_waiting_cargo++; //count number of waiting cargo
 						if (HasBit(cargo_filter, j)) {
-							station_sort[n++] = st;
+							*sl->Append() = st;
 							break;
 						}
 					}
 				}
 				/* stations without waiting cargo */
 				if (num_waiting_cargo == 0 && include_empty) {
-					station_sort[n++] = st;
+					*sl->Append() = st;
 				}
 			}
 		}
 	}
 
-	free((void*)sl->sort_list);
-	sl->sort_list = MallocT<const Station*>(n);
-	sl->list_length = n;
-
-	for (uint i = 0; i < n; ++i) sl->sort_list[i] = station_sort[i];
+	sl->Compact();
 
 	sl->flags &= ~VL_REBUILD;
 	sl->flags |= VL_RESORT;
-	free((void*)station_sort);
 }
 
 
@@ -239,7 +231,7 @@
 
 	_internal_sort_order = sl->flags & VL_DESC;
 	_last_station = NULL; // used for "cache" in namesorting
-	qsort((void*)sl->sort_list, sl->list_length, sizeof(sl->sort_list[0]), _station_sorter[sl->sort_type]);
+	qsort((void*)sl->Begin(), sl->Length(), sizeof(sl->Begin()), _station_sorter[sl->sort_type]);
 
 	sl->resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS;
 	sl->flags &= ~VL_RESORT;
@@ -311,7 +303,6 @@
 		this->SetWidgetLoweredState(SLW_CARGOALL, _cargo_filter == _cargo_mask && include_empty);
 		this->SetWidgetLoweredState(SLW_NOCARGOWAITING, include_empty);
 
-		this->sort_list = NULL;
 		this->flags = VL_REBUILD;
 		this->sort_type = station_sort.criteria;
 		if (station_sort.order) this->flags |= VL_DESC;
@@ -329,7 +320,7 @@
 		BuildStationsList(this, owner, facilities, _cargo_filter, include_empty);
 		SortStationsList(this);
 
-		SetVScrollCount(this, this->list_length);
+		SetVScrollCount(this, this->Length());
 
 		/* draw widgets, with player's name in the caption */
 		SetDParam(0, owner);
@@ -375,11 +366,11 @@
 			return;
 		}
 
-		int max = min(this->vscroll.pos + this->vscroll.cap, this->list_length);
+		int max = min(this->vscroll.pos + this->vscroll.cap, this->Length());
 		y = 40; // start of the list-widget
 
 		for (int i = this->vscroll.pos; i < max; ++i) { // do until max number of stations of owner
-			const Station *st = this->sort_list[i];
+			const Station *st = *this->Get(i);
 			int x;
 
 			assert(st->xy != 0);
@@ -413,9 +404,9 @@
 
 				id_v += this->vscroll.pos;
 
-				if (id_v >= this->list_length) return; // click out of list bound
+				if (id_v >= this->Length()) return; // click out of list bound
 
-				const Station *st = this->sort_list[id_v];
+				const Station *st = *this->Get(id_v);
 				/* do not check HasStationInUse - it is slow and may be invalid */
 				assert(st->owner == (PlayerID)this->window_number || (st->owner == OWNER_NONE && !st->IsBuoy()));
 
--- a/src/vehicle.cpp	Mon May 26 13:50:00 2008 +0000
+++ b/src/vehicle.cpp	Mon May 26 16:23:23 2008 +0000
@@ -1335,35 +1335,6 @@
 }
 
 /**
- * @param sort_list list to store the list in. Either NULL or the length length_of_array tells
- * @param length_of_array informs the length allocated for sort_list. This is not the same as the number of vehicles in the list. Needs to be 0 when sort_list is NULL
- * @param type type of vehicle
- * @param owner PlayerID of owner to generate a list for
- * @param index This parameter has different meanings depending on window_type
- *    <ul>
- *      <li>VLW_STATION_LIST:  index of station to generate a list for</li>
- *      <li>VLW_SHARED_ORDERS: index of order to generate a list for<li>
- *      <li>VLW_STANDARD: not used<li>
- *      <li>VLW_DEPOT_LIST: TileIndex of the depot/hangar to make the list for</li>
- *      <li>VLW_GROUP_LIST: index of group to generate a list for</li>
- *    </ul>
- * @param window_type tells what kind of window the list is for. Use the VLW flags in vehicle_gui.h
- * @return the number of vehicles added to the list
- */
-uint GenerateVehicleSortList(const Vehicle ***sort_list, uint16 *length_of_array, VehicleType type, PlayerID owner, uint32 index, uint16 window_type)
-{
-	VehicleList list;
-	GenerateVehicleSortList(&list, type, owner, index, window_type);
-
-	if (list.Length() > 0) {
-		*sort_list = ReallocT(*sort_list, list.Length());
-		memcpy(*sort_list, list.Begin(), sizeof(list.Begin()) * list.Length());
-	}
-
-	return list.Length();
-}
-
-/**
  * Generate a list of vehicles based on window type.
  * @param list        Pointer to list to add vehicles to
  * @param type        Type of vehicle
--- a/src/vehicle_func.h	Mon May 26 13:50:00 2008 +0000
+++ b/src/vehicle_func.h	Mon May 26 16:23:23 2008 +0000
@@ -68,8 +68,6 @@
 void TrainPowerChanged(Vehicle *v);
 Money GetTrainRunningCost(const Vehicle *v);
 
-/* Old style list kept for migration */
-uint GenerateVehicleSortList(const Vehicle*** sort_list, uint16 *length_of_array, VehicleType type, PlayerID owner, uint32 index, uint16 window_type);
 void GenerateVehicleSortList(VehicleList *list, VehicleType type, PlayerID owner, uint32 index, uint16 window_type);
 void BuildDepotVehicleList(VehicleType type, TileIndex tile, VehicleList *engine_list, VehicleList *wagon_list);
 CommandCost SendAllVehiclesToDepot(VehicleType type, uint32 flags, bool service, PlayerID owner, uint16 vlw_flag, uint32 id);
--- a/src/vehicle_gui.cpp	Mon May 26 13:50:00 2008 +0000
+++ b/src/vehicle_gui.cpp	Mon May 26 16:23:23 2008 +0000
@@ -92,7 +92,7 @@
 
 	DEBUG(misc, 3, "Building vehicle list for player %d at station %d", owner, index);
 
-	vl->vehicles.list_length = GenerateVehicleSortList(&vl->vehicles.sort_list, &vl->vehicles.list_length, vl->vehicle_type, owner, index, window_type);
+	GenerateVehicleSortList(&vl->vehicles, vl->vehicle_type, owner, index, window_type);
 
 	vl->vehicles.flags &= ~VL_REBUILD;
 	vl->vehicles.flags |= VL_RESORT;
@@ -110,7 +110,7 @@
 	_last_vehicle[0] = _last_vehicle[1] = NULL;
 
 	_internal_sort_order = (vl->vehicles.flags & VL_DESC) != 0;
-	qsort((void*)vl->vehicles.sort_list, vl->vehicles.list_length, sizeof(vl->vehicles.sort_list[0]),
+	qsort((void*)vl->vehicles.Begin(), vl->vehicles.Length(), sizeof(vl->vehicles.Begin()),
 		_vehicle_sorter[vl->vehicles.sort_type]);
 
 	vl->vehicles.resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS;
@@ -806,8 +806,7 @@
 		PlayerID player = (PlayerID)GB(this->window_number, 0, 8);
 
 		this->vehicle_type = (VehicleType)GB(this->window_number, 11, 5);
-		this->vehicles.list_length = 0;
-		this->vehicles.sort_list = NULL;
+		this->vehicles.Clear();
 		this->caption_color = player;
 
 		/* Hide the widgets that we will not use in this window
@@ -925,7 +924,6 @@
 
 	~VehicleListWindow()
 	{
-		free((void*)this->vehicles.sort_list);
 	}
 
 	virtual void OnPaint()
@@ -940,12 +938,12 @@
 
 		BuildVehicleList(this, owner, index, window_type);
 		SortVehicleList(this);
-		SetVScrollCount(this, this->vehicles.list_length);
+		SetVScrollCount(this, this->vehicles.Length());
 
 		/* draw the widgets */
 		switch (window_type) {
 			case VLW_SHARED_ORDERS: /* Shared Orders */
-				if (this->vehicles.list_length == 0) {
+				if (this->vehicles.Length() == 0) {
 					/* We can't open this window without vehicles using this order
 					* and we should close the window when deleting the order      */
 					NOT_REACHED();
@@ -981,7 +979,7 @@
 			default: NOT_REACHED(); break;
 		}
 
-		this->SetWidgetsDisabledState(this->vehicles.list_length == 0,
+		this->SetWidgetsDisabledState(this->vehicles.Length() == 0,
 			VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN,
 			VLW_WIDGET_STOP_ALL,
 			VLW_WIDGET_START_ALL,
@@ -994,9 +992,9 @@
 		/* draw arrow pointing up/down for ascending/descending sorting */
 		this->DrawSortButtonState(VLW_WIDGET_SORT_ORDER, this->vehicles.flags & VL_DESC ? SBS_DOWN : SBS_UP);
 
-		max = min(this->vscroll.pos + this->vscroll.cap, this->vehicles.list_length);
+		max = min(this->vscroll.pos + this->vscroll.cap, this->vehicles.Length());
 		for (i = this->vscroll.pos; i < max; ++i) {
-			const Vehicle *v = this->vehicles.sort_list[i];
+			const Vehicle *v = this->vehicles[i];
 			StringID str;
 
 			SetDParam(0, v->GetDisplayProfitThisYear());
@@ -1049,9 +1047,9 @@
 
 				id_v += this->vscroll.pos;
 
-				if (id_v >= this->vehicles.list_length) return; // click out of list bound
+				if (id_v >= this->vehicles.Length()) return; // click out of list bound
 
-				v = this->vehicles.sort_list[id_v];
+				v = this->vehicles[id_v];
 
 				ShowVehicleViewWindow(v);
 			} break;
@@ -1100,7 +1098,7 @@
 				}
 				break;
 			case VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN:
-				assert(this->vehicles.list_length != 0);
+				assert(this->vehicles.Length() != 0);
 
 				switch (index) {
 					case 0: /* Replace window */