GPWiki.org
GPWiki.org
It is currently Tue Jun 18, 2013 9:44 pm

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Sun Oct 09, 2011 6:04 am 
Harmlessness does no harm
User avatar

Joined: Tue Sep 14, 2004 8:37 pm
Posts: 3853
Location: Ferriday, LA, US
Dipping my toe back into C#, I see I've become a bit rusty.

I have defined a List<> of objects I've defined. Every so often, I need to check for -- and remove -- any items in the list that meet a certain "death" condition. What I'm doing is using a for-loop to update all the objects, and then as they are updated, a certain value changes... If that value reaches 0, that item needs to be removed from the list on the next "death" check.

I can't use foreach, as the iterator will be nullified as I go along removing things while it's running. I can't remove the item during the for-loop update, as it will mess with the indexing... so, I'm kind of at a loss as to how to achieve my goal. I really don't know what I *can* do to get rid of these "dead" objects.

So, what should I be doing?

_________________
What most people don't understand about "enlightenment" is that it is not an end-goal; but where you find yourself just before taking a new "first step."


Top
 Profile  
 
PostPosted: Sun Oct 09, 2011 10:30 am 
Corpse Bride
User avatar

Joined: Tue Jul 01, 2008 11:44 pm
Posts: 2217
Location: England
Make your own loop logic, rather than using a For-loop

_________________
I ain't pushing no moon buttons.


Top
 Profile  
 
PostPosted: Sun Oct 09, 2011 12:08 pm 
Dexterous Droid
User avatar

Joined: Wed Aug 18, 2004 7:40 pm
Posts: 3746
Location: South Africa
Mark them as dead. When a new object gets created, place it in a dead object's slot.

Alternatively, in a for loop, you'll have to decrement your counter before you continue, otherwise you'll skip elements. The better way to do that is to iterate backwards. http://stackoverflow.com/questions/308466/how-to-modify-or-delete-items-from-an-enumerable-collection-while-iterating-throu

Code:
        List<int> test = new List<int>();
        test.Add(1);
        test.Add(2);
        test.Add(3);
        test.Add(4);
        test.Add(5);
        test.Add(6);
        test.Add(7);
        test.Add(8);
        for (int i = test.Count-1; i > -1; i--)
        {
            if(someCondition){
                test.RemoveAt(i);
            }
        }


People are saying the best way to do it is by using delegates, something I've never dabbled with. Apparently something along the lines of
Code:
List.RemoveAll(delegate(int x) { return x%2 == 0; });

_________________
Whatever the mind can conceive and believe, it can achieve


Top
 Profile  
 
PostPosted: Sun Oct 09, 2011 1:01 pm 
Corpse Bride
User avatar

Joined: Tue Jul 01, 2008 11:44 pm
Posts: 2217
Location: England
IGTHORN wrote:
Mark them as dead. When a new object gets created, place it in a dead object's slot.

Alternatively, in a for loop, you'll have to decrement your counter before you continue, otherwise you'll skip elements.


I did a similar thing with my particle emitters. When an emitter expires, some particles it has spawned may still exist, and we need keep hold of the emitter so that it's physics parameters can continue to be applied to those particles. So we need to wait for those particles to die before we can kill the emitter.

_________________
I ain't pushing no moon buttons.


Top
 Profile  
 
PostPosted: Sun Oct 09, 2011 3:18 pm 
Harmlessness does no harm
User avatar

Joined: Tue Sep 14, 2004 8:37 pm
Posts: 3853
Location: Ferriday, LA, US
Thank you, you two. :) I'll get to work on it.

Jasmine wrote:
Make your own loop logic, rather than using a For-loop

I'm not really sure I understand what you're getting at, Jasmine; could you elaborate on this?

_________________
What most people don't understand about "enlightenment" is that it is not an end-goal; but where you find yourself just before taking a new "first step."


Top
 Profile  
 
PostPosted: Sun Oct 09, 2011 4:01 pm 
Bibliotherapist
User avatar

Joined: Wed Nov 03, 2004 1:28 pm
Posts: 6751
Location: Oxford, Englandshire
I just had a dig around the remove() methods. I'm sure I was doing something like this last year and the remove method returned an iterator pointing at the node immediately before (or after, can't remember which) the deleted one (or the new head/tail if there was no previous/next available).

However, that docs make no mention of it. :confused

_________________
10 PRINT "Bad Monkey ";
20 GOTO 10


Top
 Profile  
 
PostPosted: Sun Oct 09, 2011 4:08 pm 
Corpse Bride
User avatar

Joined: Tue Jul 01, 2008 11:44 pm
Posts: 2217
Location: England
rotInMilc wrote:
Jasmine wrote:
Make your own loop logic, rather than using a For-loop

I'm not really sure I understand what you're getting at, Jasmine; could you elaborate on this?


Code:
int i = 0;
    while(i < test.Count) {
        if(someCondition){
            test.RemoveAt(i);
        else
            i=i+1;
        }
    }


_________________
I ain't pushing no moon buttons.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group