Jump to content


Photo

Question on ds lists


  • Please log in to reply
5 replies to this topic

#1 sneaky666

sneaky666

    536e65616b79

  • GMC Member
  • 1034 posts
  • Version:GM8

Posted 11 March 2012 - 11:32 PM

I have 3 questions on ds lists.

1.
Each time a list is made, its index goes up by one, so lets say index 0, 1 and 2 exists.
If I destroy index 1, then make a new list, will its index be 1 or will it move to the next one, so it will be 3?

2.
How do I properly iterate though a list if (during the process) items from the list gets deleted? So in the end, all items of the list was accessed (excluding the ones that were deleted.)

3.
To properly delete a list, do I need to clear the list first, then destroy it, or just destroy it?
  • 0

#2 greep

greep

    Menaces with Spikes

  • GMC Member
  • 2297 posts
  • Version:GM7

Posted 12 March 2012 - 01:01 AM

1) I have no idea

2) if this is your code:

for (i = 0; i < ds_list_size(theList); i += 1)
{
if (someCondition)
{
ds_list_delete(theList,i)
}
}

change it to

for (i = 0; i < ds_list_size(theList); i += 1)
{
if (someCondition)
{
ds_list_delete(theList,i)
i -= 1
}

}

3) just destroy it I believe
  • 0

#3 sneaky666

sneaky666

    536e65616b79

  • GMC Member
  • 1034 posts
  • Version:GM8

Posted 12 March 2012 - 04:02 AM

Also for a for loop, If I were to do this:

for(i=0;i<ds_list_size(a);i+=1) {

}

For each iteration, does it re-evaluate the size of the list, or does it evaluate it once in the beginning, then use the value to compare for exit condition for all the iterations?
  • 0

#4 greep

greep

    Menaces with Spikes

  • GMC Member
  • 2297 posts
  • Version:GM7

Posted 12 March 2012 - 05:44 AM

each iteration I believe, otherwise my #2 idea wouldn't work. I'd double check by trying it first, though.
  • 0

#5 IceMetalPunk

IceMetalPunk

    InfiniteIMPerfection

  • Retired Staff
  • 9260 posts
  • Version:Unknown

Posted 12 March 2012 - 08:01 AM

1) GM will re-use freed ID's. So in your example, the new list would have an ID of 1.

2) As greep said, just subtract 1 from your counter every time you delete a list entry at the counter's position. A for() loop will re-evaluate the condition at the beginning of each iteration, so the size will be adjusted appropriately. In fact, here's a quick rundown of FOR loop execution for future reference:

Given a loop of the form for (INITIALIZATION; CONDITION; ITERATION) { BODY }, the following happens:
  • First, the initialization is executed (usually something like i=0, but can be any code).
  • Then the condition is executed. If it returns true, the BODY is executed.
  • Finally, the ITERATION code is executed (usually i+=1, but can be anything) and then the previous step occurs until the condition returns FALSE.
Again, note that all parts of the loop can be any code at all, including simple script calls, just variables, etc. So this is perfectly valid:

for (someInitScript(); someVar; whatever=2) {
  someVar-=1;
}

Assuming someVar started out as a number greater than 0, this will first call someInitScript(), then constantly loop until someVar reaches 0. Each iteration, "whatever" will always be set to 2. It's not useful, but it's completely valid and predictable.

3) If your list contains only numbers and strings, you can simply destroy it and GM will handle the memory cleanup for you. However, if you have something like nested lists (a list where the entries are ID's of other lists), GM will NOT automatically destroy the inner lists. You'll have to destroy those yourself. For example:

basicList = ds_list_create();
ds_list_add(basicList, 1);
ds_list_add(basicList, "Two");
ds_list_destroy(basicList); // Perfectly fine and safe

nestedList = ds_list_create();
nest1 = ds_list_create();
ds_list_add(nestedList, nest1);
nest2 = ds_list_create();
ds_list_add(nestedList, nest2);
nest3 = ds_list_create();
ds_list_add(nestedList, nest3);

// ds_list_destroy(nestedList) will NOT completely cleanup.

/* The proper way to do it is below: loop through the list, destroying the nested lists first, then destroy the outer list */
for (p=0; p < ds_list_size(nestedList); p+=1) {
  ds_list_destroy(ds_list_find_value(nestedList, p));
}
ds_list_destroy(nestedList);

-IMP

Edited by IceMetalPunk, 12 March 2012 - 08:02 AM.

  • 0

#6 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Global Moderators
  • 14395 posts
  • Version:GM:Studio

Posted 12 March 2012 - 08:23 PM

I have 3 questions on ds lists.

1.
Each time a list is made, its index goes up by one, so lets say index 0, 1 and 2 exists.
If I destroy index 1, then make a new list, will its index be 1 or will it move to the next one, so it will be 3?

This is not required knowledge, you must assume you can not know the internal behavior. In later release, these numbers may be entirely different.

2.
How do I properly iterate though a list if (during the process) items from the list gets deleted? So in the end, all items of the list was accessed (excluding the ones that were deleted.)


The for loop was mentioned.

for(i = 0; i < ds_list_size(list); i+=1)
{
//you code;
}

You asked
is ds_list_size(i) get called each iteration. Yes, that is why, if the function is costly, you should store the size in a variable, that is if it will not change in the loop;
s = ds_list_size(list);
for(i = 0; i < s; i+=1)
{
//you code;
}
an equivalent of the for loop is this
//for(i = 0; i < ds_list_size(list); i+=1)
i = 0;
while(1)
{
// you code;

// exit logic
//for(i = 0; i < ds_list_size(list); i+=1)
if(i < ds_list_size(list)) break;
//what to do after each step
//for(i = 0; i < ds_list_size(list); i+=1)
i+=1;
}

in your case, the fastest method would be to
s = ds_list_size(list);
i = 0;
repeat(s)
{
item = ds_list_find_value(list,i);
if(Item stays in list)
{
i+=1;
}
else
{
delete item of i;
i stays where it is,
}
}

what this did is remove that
if(i < ds_list_size(list)) break; part of the logical process, a function that did nothing for you each step.

3.
To properly delete a list, do I need to clear the list first, then destroy it, or just destroy it?


Answered.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users