AA strange behavior

Oskar Linde oskar.lindeREM at OVEgmail.com
Tue Jun 13 12:09:11 PDT 2006


Sean Kelly skrev:
> Oskar Linde wrote:
>> Georg Wrede skrev:
>>>
>>>
>>> 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;

I just realized this is wrong. I had accidentally been linking to an old 
version of Phobos. In its current incarnation, those two lines should be:

         (*(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.
>>
>> The AA is a reference type. There can be many references to the same 
>> AA. aa = null will only change one reference, while the proposed 
>> aa.clear() would clear the actual AA that all references refer to. 
>> There is a significant semantic difference.
> 
> What about "delete aa"?  Or was the goal to keep the buckets around and 
> just toss the data?

That could work as a syntax, but I think a .clear() method is clearer. 
The goal is not to keep the buckets around. Just work with multiple 
references to the same AA.

int[int] table1;
int[int] table2;
table1[1] = 1;
table2 = table1;
table1.clear();
assert(table2.length == 0);

/Oskar




More information about the Digitalmars-d-learn mailing list