calling remove() mid iterator?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

calling remove() mid iterator?

Alex Liebert
I've just realized I have a case where i Do this:

for(i in object.iterator())
{

if(condition)
{
  object.remove(i);
}

}

this is obviously not safe to do in an for indexed loop without adjusting the value...is it 'safe' in an iterator() due to some magic, or should i be pushing the removes to a queue to do in a separate loop?

Thanks,

Alex


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: calling remove() mid iterator?

Dion Whitehead Amago
I think I asked this question a while back, and did some tests.  IIRC,
the answer is no, it's not safe to remove mid-iteration.

On Tue, Aug 23, 2011 at 7:42 PM, Alex Liebert <[hidden email]> wrote:

> I've just realized I have a case where i Do this:
> for(i in object.iterator())
> {
>
> if(condition)
> {
>   object.remove(i);
> }
> }
> this is obviously not safe to do in an for indexed loop without adjusting
> the value...is it 'safe' in an iterator() due to some magic, or should i be
> pushing the removes to a queue to do in a separate loop?
> Thanks,
> Alex
>
> --
> haXe - an open source web programming language
> http://haxe.org
>

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: calling remove() mid iterator?

Alex Liebert
thank you!  Was just messing around with it myself, and i think in the case of fastlist it is ok:

public function remove( v : T ) : Bool {
var prev = null;
var l = head;
while( l != null ) {
if( l.elt == v ) {
if( prev == null )
head = l.next;
else
prev.next = l.next;
break;
}
prev = l;
l = l.next;
}
return (l != null);
}

On Tue, Aug 23, 2011 at 7:43 PM, Dion Whitehead Amago <[hidden email]> wrote:
I think I asked this question a while back, and did some tests.  IIRC,
the answer is no, it's not safe to remove mid-iteration.

On Tue, Aug 23, 2011 at 7:42 PM, Alex Liebert <[hidden email]> wrote:
> I've just realized I have a case where i Do this:
> for(i in object.iterator())
> {
>
> if(condition)
> {
>   object.remove(i);
> }
> }
> this is obviously not safe to do in an for indexed loop without adjusting
> the value...is it 'safe' in an iterator() due to some magic, or should i be
> pushing the removes to a queue to do in a separate loop?
> Thanks,
> Alex
>
> --
> haXe - an open source web programming language
> http://haxe.org
>

--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: calling remove() mid iterator?

Nicolas Cannasse
In reply to this post by Dion Whitehead Amago
Le 24/08/2011 04:43, Dion Whitehead Amago a écrit :
> I think I asked this question a while back, and did some tests.  IIRC,
> the answer is no, it's not safe to remove mid-iteration.

It is not safe for Array, but safe for List/FastList

Best,
Nicolas

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: calling remove() mid iterator?

Benjamin Dasnois
Hi Nicolas,

Can we consider this is true for *all* platforms? If so I'm going to add this in the API documentation.

Regards,

On Wed, Aug 24, 2011 at 10:05 AM, Nicolas Cannasse <[hidden email]> wrote:
Le 24/08/2011 04:43, Dion Whitehead Amago a écrit :

I think I asked this question a while back, and did some tests.  IIRC,
the answer is no, it's not safe to remove mid-iteration.

It is not safe for Array, but safe for List/FastList

Best,
Nicolas

--
haXe - an open source web programming language
http://haxe.org



--
DASNOIS Benjamin
http://www.benjamindasnois.com

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: calling remove() mid iterator?

Nicolas Cannasse
Le 24/08/2011 10:09, Benjamin Dasnois a écrit :
> Hi Nicolas,
>
> Can we consider this is true for *all* platforms? If so I'm going to add
> this in the API documentation.

Yes.

Nicolas

--
haXe - an open source web programming language
http://haxe.org