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?
| ||
That's it really how do I get the count of elements in a Map? |
| ||
Subclass it, and override the Insert/Remove to keep a count yourself? :-) |
| ||
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! |
| ||
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 ;-) |
| ||
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. |
| ||
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) |
| ||
For EachIn? |
| ||
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. |
| ||
Function CountMapKeys(map:TMap) Local count For Local key=EachIn MapKeys(map) count:+1 Next Return count End Function Something like this, maybe? |
| ||
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. |
| ||
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. |