Number of instances of a type

Blitz3D Forums/Blitz3D Beginners Area/Number of instances of a type

I know this is probaly really easy, but I want to know if theres an easy way to get the amount of instances in a type.

I mean if I have a type called thing, I want to know how many things there are.

After alternate help, I have solved the problem, I'll do it by adding it up. the way I was going to:P

Just a thought....

If types are actually allocated using some kind of reference counting and/or standard library list functions, there might be a way of using thing.Last - thing.First and dividing by the size of the type to calculate the number without having to iterate through the list.

I don't think we have a sizeof() function, but there might be ways of calculating it for a given 'thing' if speed was going to be an issue.

Anyone tried anything like this ??

You can use a function for counting all Things:
Type Thing
	Field Name$
End Type

SeedRnd MilliSecs()

; Create a random number of Thing's
For i = 1 To Rand(10,100)
	temppointer.Thing = New Thing

Next

Print CountThings()
WaitKey()
End



Function CountThings()
	Local Count

	; Loop through all Thing's and count them
	For temppointer.Thing = Each Thing
		Count = Count + 1
	Next

	Return Count
End Function


Why waste time looping. Just use a Global variable to keep track and add 1 when using New and subtract 1 when using Delete. That's what an internal type counter would do anyways.

If types are actually allocated using some kind of reference counting and/or standard library list functions, there might be a way of using thing.Last - thing.First and dividing by the size of the type to calculate the number without having to iterate through the list.


Unfortunately, this method can't/shouldn't be used as type instances aren't necessarily allocated contiguously in memory. Instead, each type instance is linked via forward/backward pointers which can be accessed using the Before/After commands.