Jump to page: 1 2
Thread overview
associative array: unexpected results after static initialization
Nov 30, 2017
kdevel
Nov 30, 2017
H. S. Teoh
Nov 30, 2017
H. S. Teoh
Dec 01, 2017
H. S. Teoh
Dec 01, 2017
H. S. Teoh
Dec 01, 2017
kdevel
Dec 01, 2017
kdevel
Dec 01, 2017
H. S. Teoh
Dec 01, 2017
kdevel
Dec 01, 2017
H. S. Teoh
Dec 01, 2017
H. S. Teoh
Dec 02, 2017
kdevel
November 30, 2017
This program

```
void aa_stat(T, U) (T[U] aa)
{
   import std.stdio;
   writefln ("aa        : %s", aa);
   writefln ("aa.length : %d", aa.length);
   writefln ("aa.keys   : %s", aa.keys);
   writefln ("aa.values : %s", aa.values);
   foreach (k, v; aa)
      writeln (k, ": ", v);
}

void main ()
{
   string[int] aa = [
      0: "null",
      0: "eins"
   ];
   aa.aa_stat;
}
```

produces this output:

   aa        : [0:"eins", ]
   aa.length : 2
   aa.keys   : [0, 32767]
   aa.values : ["eins", ""]
   0: eins

I would have expected either a compile time or a runtime error or this result:

   aa        : [0:"eins"]
   aa.length : 1
   aa.keys   : [0]
   aa.values : ["eins"]
   0: eins

What's happening here?

November 30, 2017
On Thu, Nov 30, 2017 at 11:50:13PM +0000, kdevel via Digitalmars-d-learn wrote:
> This program
> 
> ``` void aa_stat(T, U) (T[U] aa) { import std.stdio; writefln ("aa
> : %s", aa); writefln ("aa.length : %d", aa.length); writefln ("aa.keys
> : %s", aa.keys); writefln ("aa.values : %s", aa.values); foreach (k,
> v; aa) writeln (k, ": ", v); }
> 
> void main () { string[int] aa = [ 0: "null", 0: "eins" ]; aa.aa_stat;
> } ```
> 
> produces this output:
> 
>    aa        : [0:"eins", ] aa.length : 2 aa.keys   : [0, 32767]
>    aa.values : ["eins", ""] 0: eins
[...]

This looks like a bug in the implementation of AA literals.  Please file a bug at http://issues.dlang.org/.


--T
November 30, 2017
Hmm, which compiler are you using, and which version?  I cannot reproduce this bug with DMD git head.  It may be have fixed since the release you're using?


--T
November 30, 2017
On Thu, Nov 30, 2017 at 03:57:37PM -0800, H. S. Teoh via Digitalmars-d-learn wrote:
> Hmm, which compiler are you using, and which version?  I cannot reproduce this bug with DMD git head.  It may be have fixed since the release you're using?
[...]

Sorry, I was testing the wrong code.  This problem does still happen on DMD git head.  Looking into the code right now...  Please still file the bug if you haven't already.


T

-- 
"How are you doing?" "Doing what?"
November 30, 2017
Seems like this problem is already known:

	https://issues.dlang.org/show_bug.cgi?id=15290

Here's the fix:

	https://github.com/dlang/druntime/pull/1980


--T
December 01, 2017
On Friday, 1 December 2017 at 00:42:00 UTC, H. S. Teoh wrote:
> Here's the fix:
>
> 	https://github.com/dlang/druntime/pull/1980

Great. By the way: It it true, that there cannot be more than 2^32 keys in an associative array (due to the use of uint)?
December 01, 2017
On Friday, 1 December 2017 at 00:42:00 UTC, H. S. Teoh wrote:
> Here's the fix:
>
> 	https://github.com/dlang/druntime/pull/1980

And wouldn't it be reasonable to add

   assert(aa.values == [ "b" ]);

to the unittest?
December 01, 2017
On 12/1/17 11:01 AM, kdevel wrote:
> On Friday, 1 December 2017 at 00:42:00 UTC, H. S. Teoh wrote:
>> Here's the fix:
>>
>>     https://github.com/dlang/druntime/pull/1980
> 
> Great. By the way: It it true, that there cannot be more than 2^32 keys in an associative array (due to the use of uint)?

Yes, and technically even less because there is a requirement to grow the array when it's 4/5 full. But such a thing is easily fixed if we need to.

Note that due to the way it's currently implemented (and you aren't going to get much smaller than this), each key takes 16 bytes (bucket ptr + hash) + at least 16 bytes per key/value pair (depending on what's stored there).

So you are talking minimum 4GB * 32 = 128GB RAM required if you wanted to have such an AA!

-Steve
December 01, 2017
On Fri, Dec 01, 2017 at 04:06:50PM +0000, kdevel via Digitalmars-d-learn wrote:
> On Friday, 1 December 2017 at 00:42:00 UTC, H. S. Teoh wrote:
> > Here's the fix:
> > 
> > 	https://github.com/dlang/druntime/pull/1980
> 
> And wouldn't it be reasonable to add
> 
>    assert(aa.values == [ "b" ]);
> 
> to the unittest?

I did think about adding that, but then I didn't want the unittest to dictate which value should take precedence when there are duplicate keys, since that is currently implementation-dependent.  At the very least, it merits further discussion.


T

-- 
Never trust an operating system you don't have source for! -- Martin Schulze
December 01, 2017
On Friday, 1 December 2017 at 16:23:33 UTC, H. S. Teoh wrote:
> On Fri, Dec 01, 2017 at 04:06:50PM +0000, kdevel via Digitalmars-d-learn wrote:
>> On Friday, 1 December 2017 at 00:42:00 UTC, H. S. Teoh wrote:
>> > Here's the fix:
>> > 
>> > 	https://github.com/dlang/druntime/pull/1980
>> 
>> And wouldn't it be reasonable to add
>> 
>>    assert(aa.values == [ "b" ]);
>> 
>> to the unittest?
>
> I did think about adding that, but then I didn't want the unittest to dictate which value should take precedence when there are duplicate keys, since that is currently implementation-dependent.  At the very least, it merits further discussion.

I personally would have preferred a run time or even a compile-time error in the case of my orginal example. That would support the "fail fast" philosohphy. IMHO it makes no sense at all to initialize an AA in such a way. The way the initialization works hides copy-and-paste errors like the one of my example code.
« First   ‹ Prev
1 2