Bug 8435 – BigInts don't work well in associative arrays
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
druntime
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2012-07-25T07:44:00Z
Last change time
2013-07-22T18:50:25Z
Keywords
pull, wrong-code
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2012-07-25T07:44:03Z
import std.bigint;
void main() {
int[BigInt] aa;
aa[BigInt(100)] = 1;
assert(BigInt(100) in aa); // assert error
}
While this is OK:
import std.bigint;
void main() {
int[BigInt] aa;
auto x = BigInt(100);
aa[x] = 1;
assert(x in aa); // assert passes
}
Comment #1 by hsteoh — 2013-07-07T18:11:30Z
cf. #10118.
Comment #2 by hsteoh — 2013-07-07T18:12:15Z
Gah, I meant bug #10118.
Comment #3 by hsteoh — 2013-07-07T20:44:34Z
This bug is caused by two problems:
1) BigInt does not define toHash(), so two different instances of BigInt will always have a distinct hash, even if the values they represent are equal.
2) For some reason, typeid(BigInt).compare() will always return non-zero for two distinct instances of BigInt; this causes the AA code to think the two BigInts are not equal even if their hash is the same.
I have the fix for (1), but still investigating (2); it may be a compiler bug, I'm not sure yet.
Gah, ignore the previous note, I posted it to the wrong bug.
Comment #8 by k.hara.pg — 2013-07-08T19:45:18Z
*** Issue 10118 has been marked as a duplicate of this issue. ***
Comment #9 by k.hara.pg — 2013-07-08T19:46:02Z
From bug 10118:
> Associative arrays with BigInts as keys are unusable:
>
> import std.bigint, std.stdio;
> void main()
> {
> int[BigInt] a;
> a[BigInt(3)] = 3;
> a[BigInt(3)] = 4;
> writeln(a);
> }
>
> Prints:
>
> [3:3, 3:4]
>
> Apparently duplicate keys.
>
> Probably related to Issue 8435.
>
> I thought this was a consequence of Issue 3789 because BigInt is a struct
> containing a string. But that was resolved recently, and this bug still appears
> in 2.063 beta.
Comment #10 by github-bugzilla — 2013-07-22T17:44:28Z