Static Associative Arrays

Christophe travert at phare.normalesup.org
Thu Apr 12 05:35:53 PDT 2012


Jonathan M Davis , dans le message (digitalmars.D.learn:34332), a
 écrit :
> On Sunday, April 08, 2012 01:24:02 Caligo wrote:
>> On Sat, Apr 7, 2012 at 11:01 PM, Jonathan M Davis <jmdavisProg at gmx.com> 
> wrote:
>> > What do you mean my static associative arrays? Are you asking why you
>> > can't
>> > initialize a static variable which is an AA at compile time? e.g.
>> > 
>> > - Jonathan M Davis
>> 
>> The same way I can create a static array:
>> 
>> int[4] = [1, 3, 4, 8];  // has value semantics
>> 
>> and dynamic arrays:
>> 
>> int[] = [1, 4, 2, 4];  // has reference semantics
>> 
>> I want an associative array that has value semantics and it's size
>> doesn't change, just like static arrays.
> 
> Associative arrays are always on the heap. They always have reference 
> semantics. It would be very expensive to have an AA with value semantics. They 
> contains pointers all over the place. It would be equivalent to calling dup on 
> them every time that you pass them to anything. And trying to put one on the 
> stack would get very messy because all of the pointers involved. AAs are 
> _completely_ different from dynamic and static arrays. Aside from the fact that 
> they both have array in their name and both allow indexing of a sort, there's 
> really no relation between them at all.

Yet, one could imagine an associative array with a fixed size and fixed 
indexes, with no pointers. We could define a simple structure with a 
static array to store the data and methods to find the element 
associated with a key. But we could not use this structure in place of a 
classic associative array (for that we would need pointers obviously).


More information about the Digitalmars-d-learn mailing list