AA strange behavior
Carlos Santander
csantander619 at gmail.com
Mon Jun 12 14:56:26 PDT 2006
Oskar Linde escribió:
> marc michel skrev:
>> I found a strange behavior using AAs :
>
> [snip]
>
>> foreach( char[]s, int[] i; aa ) {
>> aa.remove(s);
>> writefln("\"%s\" => removed ",s);
>> }
>
> You may not delete aa elements within a foreach loop. The foreach
> iterator will be confused.
>
>> The only workaround is to add a "while( aa.length > 0 ) " surroundind the
>> foreach loop which does aa.remove().
>
> There are other ways. For instance:
>
> foreach(key;aa.keys)
> aa.remove(key);
>
> or faster (as you seem to want to delete all elements), but ugly,
> hackish and undocumented:
>
> struct BB { void *[] buckets; size_t nodes; }
>
> ....
>
> (cast(BB*)&aa).buckets = null;
> (cast(BB*)&aa).nodes = 0;
>
> (A wish would be for the above to be implemented as aa.clear())
>
I think this would be a useful addition to AAs. It has been proposed more than
once, I don't know why Walter hasn't added it yet.
> Or if you just want to forget about your current aa instance:
>
> aa = null;
>
> /Oskar
--
Carlos Santander Bernal
More information about the Digitalmars-d-learn
mailing list