AA.clear()?

Jonathan M Davis jmdavisProg at gmx.com
Sat Feb 16 17:06:20 PST 2013


On Saturday, February 16, 2013 16:58:27 H. S. Teoh wrote:
> On Sat, Feb 16, 2013 at 07:20:29PM -0500, Nick Sabalausky wrote:
> > Didn't there used to be an AA.clear (maybe a different name?) to clear
> > an assoc array? There doesn't appear to be any mention of it here:
> > 
> > http://dlang.org/hash-map
> > 
> > Did something happen to it, or am I just remembering wrong?
> 
> Since AA's are reference types, couldn't you just assign a null AA to
> it?

Sure, which is what destroy/clear does. But that's fundamentally different from 
removing all of the elements from the AA. If you have multiple references to 
the same AA, then destroy doesn't affect them all, just the one that you 
destroyed, whereas if you could clear out the elements in the AA, then every 
reference to it would be affected.

Now, if you only have one reference to the AA, it might be more efficient to 
just call destroy on it or set it to null, depending on what the GC does 
(explicitly removing the elements could be costly, but it might also make it 
so the the GC was quicker to collect them). But regardless, there _is_ a 
fundamental difference between nullifying a reference to an AA and removing all 
of the elements from an AA.

Right now, I believe that the only way to remove them all is to iterate over 
all of the keys and remove them one by one, and I'm sure that the AA 
implementation could be more efficient about it than that if it have such a 
function (since it wouldn't have to look up every element in itself, just 
iterate through them and remove them).

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list