GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2010-06-17 00:43:48

flexaplex
Member
Registered: 2008-12-11
Posts: 72

ds_list_exists

Wrote this quite a while ago.

Expand//ds_list_exists(index) returns whether the ds_list with given index exists or not

if !(is_real(argument0)) {return false;}
var ds_temp, ds_temp_list, i;
ds_temp_list = ds_list_create();
ds_temp = ds_temp_list;

argument0 = round(argument0);
while (ds_temp < argument0)
{
  ds_temp = ds_list_create();
  ds_list_add(ds_temp_list, ds_temp);
}
for (i = ds_list_size(ds_temp_list) - 1; i >= 0; i -= 1)
{
  ds_list_destroy(ds_list_find_value(ds_temp_list, i));
}
ds_list_destroy(ds_temp_list);

return ds_temp != argument0;

Not sure if you will really want to use it though. It uses a bit of a trick that when creating a new list GM reuses ds_list indexes that have been destroyed.

Last edited by flexaplex (2010-06-17 14:52:52)

Offline

#2 2010-06-17 14:24:36

xot
Administrator
Registered: 2007-08-18
Posts: 1,240

Re: ds_list_exists

Not a bad approach, as long as the list being searched for doesn't have a huge id. Looks like this technique could be applied to any type of data structure.

There's one thing I think I'd change, namely the bit where you reject ids with a fractional component. The existing ds_list functions accept these for arguments, rounding to the nearest integer. I suggest "argument0 = round(argument0)" instead of your check.

It looks like it handles negative ids gracefully, but I haven't actually tested it yet. Does it?


Abusing forum power since 1986.

Offline

#3 2010-06-17 14:51:41

flexaplex
Member
Registered: 2008-12-11
Posts: 72

Re: ds_list_exists

xot wrote:

Not a bad approach, as long as the list being searched for doesn't have a huge id. Looks like this technique could be applied to any type of data structure.

There's one thing I think I'd change, namely the bit where you reject ids with a fractional component. The existing ds_list functions accept these for arguments, rounding to the nearest integer. I suggest "argument0 = round(argument0)" instead of your check.

It looks like it handles negative ids gracefully, but I haven't actually tested it yet. Does it?

Yes it can be expanded to other data structures. I think this should function be default built into GM as well with all the data structures.

It does handle negatives correctly. I agree changing to round() is better, I had not thought about that.

This is the only approach found that was able to handle things correctly.

Last edited by flexaplex (2010-06-17 14:54:30)

Offline

Board footer

Powered by FluxBB