Brain stops! Play how to count the keys in TMap?

BlitzMax Forums/BlitzMax Beginners Area/Brain stops! Play how to count the keys in TMap?

Arowx(Posted 2009) [#1]
That's it really how do I get the count of elements in a Map?


Brucey(Posted 2009) [#2]
Subclass it, and override the Insert/Remove to keep a count yourself? :-)


Arowx(Posted 2009) [#3]
TMapCounter hmm just wrap the insert and remove calls...

Yep that would be the clever way to do it!

But I've gone for the the hack of reading through all the keys and adding them to a list then counting them! ;o)

Did I mention that my brain doesn't seem to wake up until early evening and then not for long!


Brucey(Posted 2009) [#4]
TMapCounter hmm just wrap the insert and remove calls...

And Clear() too, I suppose.

But I've gone for the the hack of reading through all the keys and adding them to a list then counting them!

That's certainly one way to do it ;-)


GfK(Posted 2009) [#5]
But I've gone for the the hack of reading through all the keys and adding them to a list then counting them! ;o)
If you must do it that way, you don't need to add them to a list then count the list - just increment a counter.


Arowx(Posted 2009) [#6]
If you must do it that way, you don't need to add them to a list then count the list - just increment a counter.


Yep but I'm streaming them to a file next, so I thought I might as well keep the keys in a list to loop through accessing the values for streaming, but I could be wrong maybe I should benchmark it ;o)


Azathoth(Posted 2009) [#7]
For EachIn?


plash(Posted 2009) [#8]
From duct.objectmap:


NOTE: This does not check if it should really add to the counter if you set a value to a key that has already been set.


kfprimm(Posted 2009) [#9]
Function CountMapKeys(map:TMap)
	Local count
	For Local key=EachIn MapKeys(map)
		count:+1
	Next
	Return count
End Function

Something like this, maybe?


N(Posted 2009) [#10]
While we're posting random solutions...


Costs more to insert/remove items, since I decided not to override the private methods to handle counting and instead stuck to the public interface like a good boy would.


plash(Posted 2009) [#11]
Costs more to insert/remove items
The speed bit is why I did not do that to my type. Also, I hardly ever use the count feature.