[Issue 10046] Wrong insertion of Tuple in associative array

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Oct 27 11:25:29 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=10046


joanbrugueram at gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |joanbrugueram at gmail.com


--- Comment #1 from joanbrugueram at gmail.com 2013-10-27 11:25:25 PDT ---
Very annoying bug since it basically kills "multiindexing" using associative
arrays. As far as I know it's simply because Tuple hasn't implemented toHash.

It can be worked around by using an structure with a toHash function (WARNING:
The documentation on associative arrays says it's called opHash. It's called
toHash!).

Here's my drop-in replacement for 2-tuples. Feel free to use it freely for
whatever you want. It works in my (string, string) case but I haven't tested it
for other types, but it should work.

****

// Workaround for http://d.puremagic.com/issues/show_bug.cgi?id=10046
static struct MyTuple(A, B)
{
    private A first;
    private B second;
    auto ref opIndex(size_t index)
    {
        switch (index)
        {
            case 0: return first;
            case 1: return second;
            default: assert(0);
        }
    }
    const hash_t toHash()
    {
        return typeid(A).getHash(&first) ^ typeid(B).getHash(&second);
    }
    const bool opEquals(ref const MyTuple!(A, B) s)
    {
        return first == s.first && second == s.second;
    }
    const int opCmp(ref const MyTuple!(A, B) s)
    {
        if (first != s.first)
            return (first < s.first) ? -1 : 1;
        if (second != s.second)
            return (second < s.second) ? -1 : 1;
        return 0;
    }
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list