montagejs / collections

This package contains JavaScript implementations of common data structures with idiomatic interfaces.
http://www.collectionsjs.com
Other
2.1k stars 183 forks source link

modification at time of iteration #9

Open Yaffle opened 11 years ago

Yaffle commented 11 years ago

Hello

var set = new Set();
set.add(1);
set.add(2);
set.add(3);
set.add(4);

var iterator = set.iterate ? set.iterate() : set.iterator();
var k = 0;

while (true) {
  next = iterator.next();
  console.log(next);
  if (k === 0) {
    set.delete(2);
    set.delete(1);
    set.delete(0);        
  }
  k++;
}
set.add(1, 1);

this code prints "1; 3; 4" in FF 18 with "native" Set and "1; 2; 3; 4" with your code

could you please document how your iteration works with modification

kriskowal commented 11 years ago

Thanks for surfacing the issue. While I’m at it, I probably ought to abandon the fight for "iterate" over "iterator".

kriskowal commented 11 years ago

I will need to revisit this. Neither behavior makes much sense. At present, Set is a combination of a FastSet and a List. The List dictates iteration order and allows modifications during iteration. I think this is a bug caused by the iterator walking links of the list that are no longer associated with the Set.