Dynamic Resizing

BlitzMax Forums/BlitzMax Beginners Area/Dynamic Resizing

And I want also to thank PaulJG for supplying this problem, because I was actually looking for some similar code.

I want to create a spacesim, where a datafile stated how many space-sectors there are, how many stations each sector has, and how many commodities each station has.
Each station can have a different number of commodities, and each sector can have a different number of stations.

I was creating code with types, which have arrays, where each index points at another type, which also had arrays (like my first expanded code at this thread).
Now I'm using Extron's approach and reduced my code significantly.

Now I can do: "Universe.NewStation", which automatically adds a new station to the last added sector.
When I do "Universe.NewCommodity", this adds a new commodity to the last added station, which was last added to a sector.

Type TUniverse
	Field NumSec%
	Field ASectors:TSector[]

	Field NumStat%
	Field AStations:TStation[][]

	Field NumComm%
	Field AComm:TCommodity[][][]

	Method NewSector()
		' Set variables (increase sectornumber and reset all others)
		NumSec% = NumSec% + 1
		NumStat% = 0
		NumComm% = 0

		' Resize the "ASectors"-array to include a new sector
		ASectors = ASectors[..NumSec%]
		' Also preset first index of AStations to have the same number of indexes as there are sectors
		AStations = AStations[..NumSec%]

		' Create a new "TSector"-type-instance at this new "ASectors"-index
		ASectors[NumSec% - 1] = New TSector
		' Load data into this new type-instance from the "Universe.txt" datafile
		LoadSectorData(ASectors[NumSec% - 1])
	End Method
	Method LoadSectorData(SectorPointer:TSector)
		' Temporary loadroutine (sets a name for the sector)
		SectorPointer.Name$ = "Sector " + NumSec%
	End Method



	Method NewStation()
		' This method creates a new station inside the last sector

		' Set variables (increase stationnumber and reset others)
		NumStat% = NumStat% + 1
		NumComm% = 0

		' Resize the "AStations"-array to include a new station
		AStations[NumSec% - 1] = AStations[NumSec% - 1][..NumStat%]
		' Also preset first and second indexes of Acomm to have the same number of indexes as there
		' are sectors and stations
		AComm = AComm[..NumSec%]
		AComm[NumSec% - 1] = AComm[NumSec% - 1][..NumStat%]

		' Create a new "TStation"-type-instance at this new "AStations"-index
		AStations[NumSec% - 1][NumStat% - 1] = New TStation
		' Load data into this new type-instance from the "Universe.txt" datafile
		LoadStationData(AStations[NumSec% - 1][NumStat% - 1])
	End Method
	Method LoadStationData(StationPointer:TStation)
		' Temporary loadroutine (sets a name for the station)
		StationPointer.Name$ = "Station " + NumStat%
	End Method



	Method NewCommodity()
		' This method creates a new commodity inside the last station, which is inside the last sector

		' Set variables (increase commoditynumber)
		NumComm% = NumComm% + 1

		' Resize the "AComm"-array to include a new commodity
		AComm[NumSec% - 1][NumStat% - 1] = AComm[NumSec% - 1][NumStat% - 1][..NumComm%]

		' Create a new "TCommodity"-type-instance at this new "AComm"-index
		AComm[NumSec% - 1][NumStat% - 1][NumComm% - 1] = New TCommodity
		' Load data into this new type-instance from the "Universe.txt" datafile
		LoadCommodityData(AComm[NumSec% - 1][NumStat% - 1][NumComm% - 1])
	End Method
	Method LoadCommodityData(CommodityPointer:TCommodity)
		' Temporary loadroutine (sets a name for the commodity)
		CommodityPointer.Name$ = "Commodity " + NumComm%
	End Method
End Type

Type TSector
	Field Name$
End Type

Type TStation
	Field Name$
End Type

Type TCommodity
	Field Name$
End Type



SeedRnd(MilliSecs())

' Add a random number of sectors, which all have a random number
' of stations, which in turn all have a random number of commodities
Universe:TUniverse = New TUniverse
For i = 0 To Rand(1, 10)
	Universe.NewSector()
	For j = 0 To Rand(1, 10)
		Universe.NewStation()
		For k = 0 To Rand(1, 10)
			Universe.NewCommodity()
		Next
	Next
Next

' Print all names of all sectors, stations and commodities inside the universe
For i = 0 To (Universe.ASectors.length - 1)
	Print Universe.ASectors[i].Name$

	For j = 0 To (Universe.AStations[i].length - 1)
		Print "    " + Universe.AStations[i][j].Name$

		For k = 0 To (Universe.AComm[i][j].length - 1)
			Print "        " + Universe.AComm[i][j][k].Name$
		Next

		Print
	Next

	Print
Next


Now all code is inside the TUniverse type.
Previously all code existed in separate types.
The loading routines must be included next, but I don't expect any problems here.

I was searching for the same thing, a multidimensional array with a dynamically secondary index (or how would you call it).

So you see, helping others can help yourself too.
This thread certainly helped my own project.