Boost logo

Boost :

Subject: Re: [boost] [Hana] Announcing Hana's formal review next week (June 10th)
From: Paul Fultz II (pfultz2_at_[hidden])
Date: 2015-06-13 18:13:40


> >
> > > The problem with this approach is that equality of keys can't be more
> > general
> > > than type identity.
> >
> > Instead a special "hash"-like function could be provided that would
> > transform
> > the keys to a type where identity could be used. This might be faster
> than
> > using linear/binary search, since the the compiler can do the lookup
> based
> > on identity.
> « []
>
> I have thought about that already, but how would you define such a hash
> function for _heterogeneous_ objects?

Its not a real hash function. It just would return an object(ie type) where
identity(ie `std::is_same`) can be used. That is it will apply the function
as
a transform to the key, and use the result to lookup the key using name
lookup. It could default to being identity:

    auto m = make_hash_map(
        identity,
        make_pair(type<T>, x),
        make_pair(type<U>, y)
    );
    assert(m[type<T>] == x);

However, if the user wants store intregral constants, and wants it to work
with any integral constant, then a custom "hash" function could be used,
which
would convert every integral constant of the same value to the same type:

    auto hash_intregral_constant = [](auto x) ->
        integral_constant<int, decltype(x)::value>
    { return {}; };

    auto m = make_hash_map(
        hash_intregral_constant,
        make_pair(int_<0>, x),
        make_pair(int_<1>, y)
    );
    assert(m[0_c] == x);
    assert(m[std::integral_constant<int, 0>()] == x);

Of course, now the user can only use integral constants as keys. However, if
the user wanted to use non integral constants and have them compare using
`std::is_same` then the user could just write something like this:

    auto m = make_hash_map(
        overload_linear(hash_intregral_constant, identity),
        make_pair(0_c, x),
        make_pair(1_c, y),
        make_pair(type<T>, x),
        make_pair(type<U>, y)
    );
    assert(m[type<T>] == x);
    assert(m[0_c] == x);
    assert(m[std::integral_constant<int, 0>()] == x);

Paul

--
View this message in context: http://boost.2283326.n4.nabble.com/Boost-Hana-Announcing-Hana-s-formal-review-next-week-June-10th-tp4676785p4677097.html
Sent from the Boost - Dev mailing list archive at Nabble.com.

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk