AA strange behavior

Georg Wrede georg.wrede at nospam.org
Tue Jun 13 02:18:13 PDT 2006



Carlos Santander wrote:
> Oskar Linde escribió:
>> 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;

Hmm.

Of course reuse is good. Greenpeace Likes Reuse(tm)!

But is there really enough merit in reusing a hash, as compared with 
using a new one? I mean, in both cases we are effectively abandoning the 
buckets and the nodes to GC. -- To reuse /them/ would give savings, but 
I'm unable to believe it's worth the effort, or even smart at all.

What's the real cost of creating a new hash, compared with emptying the 
old one?

((Besides, too much needless reuse only makes code harder to understand.))

---

FWIW, if reusing hashes really does turn out more efficient (or smarter 
and not more error prone), and become the Recommended Practice, then I, 
too, absolutely vote for aa.clear()!

And if not, we sure as heck should _not_ implement it!



More information about the Digitalmars-d-learn mailing list