Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
February 17, 2013 AA.clear()? | ||||
---|---|---|---|---|
| ||||
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? |
February 17, 2013 Re: AA.clear()? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | On Saturday, February 16, 2013 19:20:29 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?
No. It never existed. It was simply the clear function in object.d which was renamed to destroy (though an alias to clear still exists).
And all that does is set the AA variable to null.
- Jonathan M Davis
|
February 17, 2013 Re: AA.clear()? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | 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? T -- I am not young enough to know everything. -- Oscar Wilde |
February 17, 2013 Re: AA.clear()? | ||||
---|---|---|---|---|
| ||||
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
|
February 17, 2013 Re: AA.clear()? | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On Sat, 16 Feb 2013 16:58:27 -0800
"H. S. Teoh" <hsteoh@quickfur.ath.cx> 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?
>
Yes, but I think that means an additional re-allocation next time
something's added. (Jonathan's answer is probably better though ;) )
|
Copyright © 1999-2021 by the D Language Foundation