• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

 Suggestion: ds_map_get_keys

GMWolf

aka fel666
Really simple request:
ds_map_get_keys(id) : returns an array or list populated with all keys used in map.

Advantages: much faster iteration of maps. Currently the get_next / previous system is a disaster IMO. Hard to use and very slow.

This would also be invaluable when dealing with json's who's format is unknown.

Should be trivial to implement if hashmaps or treemaps are used. So I'm awaiting a positive response :)
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
That would be convenient.

The current system is not particularly hard to use, but it seems like iteration over all keys is O(sum(1..size)) by nature of it.
 

GMWolf

aka fel666
That would be convenient.

The current system is not particularly hard to use, but it seems like iteration over all keys is O(sum(1..size)) by nature of it.
Well, sure its not hard. But it can be a little confusing. At least, it is to me when I look back at my code. One more thing to think about.
The performance is horrendous though, and is one of the reasons I proposed this.
 

gnysek

Member
What would be much better is foreach:

Code:
foreach(somevar as key:value) {

}
where somevar is ds_map id, key and value are ds_map key and values on current iteration.
 

GMWolf

aka fel666
What would be much better is foreach:

Code:
foreach(somevar as key:value) {

}
where somevar is ds_map id, key and value are ds_map key and values on current iteration.
Let's not get carried away here...
Besides, there are plenty more syntax features we should be getting before a for each loop...
 

zbox

Member
GMC Elder
What would be much better is foreach...
after spending a long time away from GM in php this is something I yearned for. I got somewhat close;
Code:
var i = iter(myMap, true), key, val;

while(iter(i)) {
   key = iterkey(i);
   val = iterval(i);
}
Put it on MP if anyone wants it, pending the great speed improvements we would get if this suggestion was implemented. For my purposes (once-off iterations) this has been a very nice way to format it.

Also if you need a persistent list of keys to operate on (and preemptive polyfill drop in for this suggestion)
Code:
///ds_map_keys(map)
//creates and returns a list of keys
var i = iter(argument0, true), l = ds_list_create();

while (iter(i)) {
    ds_list_add(l, iterkey(i));
}

return l;
 

GMWolf

aka fel666
after spending a long time away from GM in php this is something I yearned for. I got somewhat close;
Code:
var i = iter(myMap, true), key, val;

while(iter(i)) {
   key = iterkey(i);
   val = iterval(i);
}
Put it on MP if anyone wants it, pending the great speed improvements we would get if this suggestion was implemented. For my purposes (once-off iterations) this has been a very nice way to format it.

Also if you need a persistent list of keys to operate on (and preemptive polyfill drop in for this suggestion)
Code:
///ds_map_keys(map)
//creates and returns a list of keys
var i = iter(argument0, true), l = ds_list_create();

while (iter(i)) {
    ds_list_add(l, iterkey(i));
}

return l;
Again, doubt YYG would implement this anytime soon...


More importantly, you would presumably want to iterate over other data structures as well.
So we would need ds_map_iter and ds_list_iter rather than just iter.

The other problem is where to store the iteration data. It the data structure? Or would be need to have an iterator ?
In doubt YYG would want to make the data structures responsible for their own iteration. This would seem to go against the current approach to data structures....
 

zbox

Member
GMC Elder
I mean I just use it to neatly iterate over maps and thought other people might find it useful so...
 

GMWolf

aka fel666
I mean I just use it to neatly iterate over maps and thought other people might find it useful so...
Yes, but its not much use asking for a feature that doesn't line up with GMs current setup.
Remember data structure IDs are just integers. They don't hold any information about what the data structure is.

A foreach loop would be nice, if it worked with array types only.
Then you could do:
Code:
var keys = ds_map_get_keys(map);
foreach(key in keys) {
  //do stuff
}
This aligns better with GML, as arrays do have a type.
 

zbox

Member
GMC Elder
I'm not sure the relevance to my code snippet? I wasn't claiming anything was just mentioning it's a nice current-gm-version way to do something that syntactically resembles a foreach loop
 

GMWolf

aka fel666
I'm not sure the relevance to my code snippet? I wasn't claiming anything was just mentioning it's a nice current-gm-version way to do something that syntactically resembles a foreach loop
Oh, I seemed to have missed the "var I = ITER(...)" Bit on my first read through.

Yes, that resembles my abandoned gm_iterator solution too.
 

zbox

Member
GMC Elder
I use it all the time, its a lot nicer than

i = ds_find_first
for (i < map size)
i = ds_find next

etc etc. And would be very easy to port for lists and arrays but at that point just syntactical sugar madness really
 
Top