[Issue 2255] New: AA.remove() doesn't remove AA element when	iterating using foreach
    d-bugmail at puremagic.com 
    d-bugmail at puremagic.com
       
    Wed Jul 30 14:28:19 PDT 2008
    
    
  
http://d.puremagic.com/issues/show_bug.cgi?id=2255
           Summary: AA.remove() doesn't remove AA element when iterating
                    using foreach
           Product: D
           Version: 2.017
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: dsimcha at yahoo.com
It appears that using the .remove function to remove an element from an
associative array does not always work when iterating over the AA with a
foreach loop.  Some elements are removed and some aren't.  This issue appears
on both DMD 2.017 and DMD 1.033.  It also occurs on GDC 0.24, indicating that
it's a front end issue.
Here are two test cases:
//This one works.
import std.stdio;
void main () {
    uint[uint] stuff;
    for(uint i = 0; i < 10_000; i++) {
        stuff[i] = i;
    }
    writefln(stuff.length);  //Should be 10_000.
    foreach(k; stuff.keys) {  //Only this line is different between cases.
        stuff.remove(k);
    }
    writefln(stuff.length);  //Should be 0.  Is.
}
//This one doesn't.
import std.stdio;
void main () {
    uint[uint] stuff;
    for(uint i = 0; i < 10_000; i++) {
        stuff[i] = i;
    }
    writefln(stuff.length);  //Should be 10_000.
    foreach(k, v; stuff) {  //Only this line is different between cases.
        stuff.remove(k);
    }
    writefln(stuff.length);  //Should be 0.  Actually is about 4_000.
}
-- 
    
    
More information about the Digitalmars-d-bugs
mailing list