Number of instances of a type

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

Agamer(Posted 2005) [#1]
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.


Agamer(Posted 2005) [#2]
After alternate help, I have solved the problem, I'll do it by adding it up. the way I was going to:P


BlackJumper(Posted 2005) [#3]
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 ??


PowerPC603(Posted 2005) [#4]
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



WolRon(Posted 2005) [#5]
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.


big10p(Posted 2005) [#6]
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.