www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - AA.clear()?

reply Nick Sabalausky <SeeWebsiteToContactMe semitwist.com> writes:
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?
Feb 16 2013
next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Feb 16 2013
prev sibling next sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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
Feb 16 2013
parent Nick Sabalausky <SeeWebsiteToContactMe semitwist.com> writes:
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 ;) )
Feb 16 2013
prev sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Feb 16 2013