Associative value order

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Aug 6 11:52:00 PDT 2014


On Wednesday, 6 August 2014 at 18:33:16 UTC, Ary Borenszweig 
wrote:
> On 8/6/14, 2:59 PM, H. S. Teoh via Digitalmars-d-learn wrote:
>> On Wed, Aug 06, 2014 at 05:54:23PM +0000, Patrick via 
>> Digitalmars-d-learn wrote:
>>> I know that there is no prescribed order that the .values 
>>> array will
>>> be sorted in, however I'm curious if the order is 
>>> deterministic based
>>> on keys.
>>>
>>> If I have two associative arrays with strings for keys and 
>>> ints for
>>> values and they each have an identical set of keys, would the 
>>> .values
>>> property return the values in the same order?
>>
>> In the current implementation, yes. But I think it's a bad 
>> idea to rely
>> on that, since a future implementation might no longer do 
>> this. (E.g.,
>> if we use a different hash collision resolution algorithm that 
>> is
>> sensitive to insertion order.)
>>
>> It is probably safest to make an array of keys with .keys, and 
>> sort the
>> array in the order you want.
>>
>>
>> T
>>
>
> Why is a dictionary something built-in the language? Can't it 
> be some standard library class/struct with syntax sugar for 
> creation? All of these questions about associative arrays 
> wouldn't exist if the source code for these operations was 
> available.
>
> (it's probably available, but buried in some C++ code, I guess, 
> on in dmd?)

There is ongoing work to do exactly this, see for example:
https://github.com/D-Programming-Language/druntime/pull/482#issuecomment-28486561
https://github.com/D-Programming-Language/druntime/pull/676


More information about the Digitalmars-d-learn mailing list