ds_list inside ds_map

Pfap

Member
From my quick overview of the manual it seems to state that ds_lists and ds_maps should only be combined when working with json. Is the manual only talking about data that would need to be saved?

I did some tests and what I have works, but is it a bad idea to use it like the below?

Create event:

Code:
my_map = ds_map_create();
my_list = ds_list_create();


ds_list_add(my_list,1,2,3,4,5,6,7,8,9,10,11,12);

ds_map_add(my_map,1,my_list);
ds_map_add(my_map,2,my_list);
ds_map_add(my_map,3,my_list);
ds_map_add(my_map,4,my_list);
ds_map_add(my_map,5,my_list);

Tap event of test object:

Code:
var r1,r2,r3,r4,r5;

r1 = ds_map_find_value(my_map,1);
r2 = ds_map_find_value(my_map,2);
r3 = ds_map_find_value(my_map,3);
r4 = ds_map_find_value(my_map,4);
r5 = ds_map_find_value(my_map,5);

var l1,l2,l3,l4,l5;

var i = 0;
repeat(12){
 
 show_debug_message(ds_list_find_value(r1,i));
 show_debug_message(ds_list_find_value(r2,i));
 show_debug_message(ds_list_find_value(r3,i));
 show_debug_message(ds_list_find_value(r4,i));
 show_debug_message(ds_list_find_value(r5,i));
 
 
 i += 1;
 
}

I'm not going to need to save any of the values and I don't want to use an array because I'm not sure how many positions may end up getting used.


To restate my question: is using ds_maps and ds_lists like above a good practice?
 
S

Sinaz20

Guest
It's ok practice. The map is just holding the indexes to the lists... so they aren't technically inside the maps.

Currently, though, you can't chain accessors... so getting the data is always a two step process (as you've discovered):

var list = my_map[? key]; //extract the index of the list at 'key'
var item = list[| 1]; //get item 1 from the list

I do this a lot... though, typically just with small arrays of data in maps organized by string keys.

Just be mindful of using that clean up event. Get used to writing loops to iterate through data-structures since that's the only way you're going to be able to access the nested structures for clean up.
 

Pfap

Member
To be clear, you are talking about deleting the lists before the map? Something like this?

Code:
//destroy my 5 lists
var i = 1,list;

repeat(5){
 
 
 list = ds_map_find_value(my_map,i);

 ds_list_destroy(list);
 i += 1;
}


//destroy the map
ds_map_destroy(my_map);
 
S

Sinaz20

Guest
Yeah- and just in general- cleaning up your data structures when objects that manage them are destroyed. In this case because it's very easy for these data structures to become lost in memory if you are manipulating the maps in a lot of places.

But, maps... usually take a more complicated loop than that until we get foreach loops implemented... consider that maps usually use keys that are not so neat as a consecutive set of integers...

Code:
var key = ds_map_find_first(map);
while(!is_undefined(key)){
    ds_list_destroy(key);
    key = ds_map_find_next(map, key);
}
ds_map_destroy(map);
 

Pfap

Member
Yeah- and just in general- cleaning up your data structures when objects that manage them are destroyed. In this case because it's very easy for these data structures to become lost in memory if you are manipulating the maps in a lot of places.

But, maps... usually take a more complicated loop than that until we get foreach loops implemented... consider that maps usually use keys that are not so neat as a consecutive set of integers...

Code:
var key = ds_map_find_first(map);
while(!is_undefined(key)){
    ds_list_destroy(key);
    key = ds_map_find_next(map, key);
}
ds_map_destroy(map);


Cool, that is a neat way to handle it.
Thanks
 
Top