Boost logo

Boost :

Subject: Re: [boost] [Hana] Announcing Hana's formal review next week (June 10th)
From: Larry Evans (cppljevans_at_[hidden])
Date: 2015-06-16 12:41:07


On 06/13/2015 09:52 AM, Louis Dionne wrote:
> Larry Evans <cppljevans <at> suddenlink.net> writes:
>
>>
>> On 06/09/2015 11:00 AM, Louis Dionne wrote:
>> [snip]
>>>
>>> Indeed, you are using a Map when what you really want is a Tuple, since
>>> your keys are integers. Here's the version I would have written:
>>>
>>> #include <boost/hana/integral_constant.hpp>
>>> #include <boost/hana/range.hpp>
>>> #include <boost/hana/tuple.hpp>
>>> using namespace boost::hana;
>>>
>>> static constexpr int map_size = 100;
>>>
>>> void run() {
>>> constexpr auto hana_range = range(int_<0>, int_<map_size>);
>>> auto hana_tuple = unpack(hana_range, [](auto ...n) {
>>> return tuple_t<std::array<char, decltype(n)::value>...>;
>>> });
>>>
>>> auto Array = hana_tuple[int_<69>];
>>>
>>> // instantiate the array
>>> constexpr decltype(Array)::type arr = {{0}};
>>> static_assert(sizeof(arr) == 69, "");
>>> }
>>>
>>> However, I can easily imagine another example where you actually need the
>>> functionality of a Map. See below for performance comments.
>>>
>> [snip]
>> Why couldn't map be implemented as a tuple, as shown here:
>>
>> https://gist.github.com/cppljevans/8e545e8d83946cd74311
>>
>> Wouldn't that eliminate the difference in performance between
>> map and tuple?
>
> The problem with this approach is that equality of keys can't be more general
> than type identity. In other words, in your example, writing
>
> get<_ulong<1>>(mud)
>
> would fail because there is no _ulong<1> key, but only a _uint<1>, even
> though they compare equal. I want to determine the equality of keys with
> the `equal` function, which is more general. Of course, in the specific
> case of a Map mapping types (and only types) to values, then your approach
> could be used, and it is in fact exactly what I had in mind.
>

OK. I hadn't thought of that. OTOH, why not just patch the gist map
with a method for doing the get_key using the more general `equal`
function. Then you could have both the fast find with get, and the
more general get_key using the `equal` function?

BTW, I'm trying to do this at the moment; however, I'm having
a tough time figuring out how to do it. I've looked at:

https://github.com/ldionne/hana/blob/master/include/boost/hana/map.hpp#L44

and guessed that maybe I can start by using superclasses for
my gist map the same as the at map.hpp#L44, and then maybe
reading http://ldionne.com/hana/index.html#tutorial-extending.
Is that what you'd recommend?

[snip]


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