Flash platform: Element removal during iteration

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

Flash platform: Element removal during iteration

Dion Amago
If the element immediately after the current (iterator.next()) is
removed during iteration, the iterator still returns the removed
element for Lists and FastLists, but not Arrays (i.e. Arrays work as
expected).  The removal of any other elements appears to be property
handled by the iterator.

Is this a bug, or unavoidable behaviour from the iterators? It would
be nice to have the fast iteration speed of FastLists, but with the
capability of safe element removal during iteration.

Dion

Sample code that demonstrates this:

//FastList
var l = new FastList<Int>();
for (i in 0...5) {
    l.add(i);
}

var it = l.iterator();
for (i in 0...2) {
    trace("it.next()=" + it.next());//4, 3
}
var cur = it.next();
trace("cur=" + cur);//2
l.remove(cur - 1);//removes 1
trace("after removal=" + l.array());//4,3,2,0
trace("next=" + it.next());//1, should be 0


//List
var l = new List<Int>();
for (i in 0...5) {
    l.add(i);
}

var it = l.iterator();
for (i in 0...2) {
    trace("it.next()=" + it.next());//0, 1
}
var cur = it.next();
trace("cur=" + cur);//2
l.remove(cur + 1);//removes 1
trace("after removal=" + l.array());//0,1,2,4
trace("next=" + it.next());//3, should be 4


//Array
var l = new Array<Int>();
for (i in 0...5) {
    l.push(i);
}

var it = l.iterator();
for (i in 0...2) {
    trace("it.next()=" + it.next());//0, 1
}
var cur = it.next();
trace("cur=" + cur);//2
l.remove(cur + 1);//removes 1
trace("after removal=" + l.array());//0,1,2,4
trace("next=" + it.next());//4, correct

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

Re: Flash platform: Element removal during iteration

Nicolas Cannasse
Le 01/09/2010 17:07, Dion Amago a écrit :
> If the element immediately after the current (iterator.next()) is
> removed during iteration, the iterator still returns the removed
> element for Lists and FastLists, but not Arrays (i.e. Arrays work as
> expected).  The removal of any other elements appears to be property
> handled by the iterator.
>
> Is this a bug, or unavoidable behaviour from the iterators? It would
> be nice to have the fast iteration speed of FastLists, but with the
> capability of safe element removal during iteration.

This is unavoidable.

Nothing is specified regarding the removal of elements during iteration.

Only List specification tells that removing the current element during
iteration will ensure that all elements get correctly iterated.

Nicolas

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