提升hana获得第一匹配的索引

Boost hana get index of first matching

本文关键字:一匹 索引 hana 提升      更新时间:2023-10-16

因此,我正试图使用boost::hana制作一个库,该库需要基于值获取元素索引的功能:

constexpr auto tup = boost::hana::make_tuple(3_c, boost::hana::type_c<bool>);
auto index = get_index_of_first_matching(tup, boost::hana::type_c<bool>);
//   ^^^^^ would be a boost::hana::int_<1>

有可能的方法吗?更好的是,它已经在hana中了,而我对此一无所知吗?

感谢您的支持!

Hana没有提供开箱即用的算法。如果它看起来是一个非常想要的功能,我可以很容易地添加这样的算法。它可能很适合作为任何Iterable接口的一部分,因为Iterables是索引有意义的序列。

就目前而言,我会选择与@cv_and_he在评论中提出的非常接近的内容:

#include <boost/hana.hpp>
namespace hana = boost::hana;
template <typename Iterable, typename T>
constexpr auto index_of(Iterable const& iterable, T const& element) {
    auto size = decltype(hana::size(iterable)){};
    auto dropped = decltype(hana::size(
        hana::drop_while(iterable, hana::not_equal.to(element))
    )){};
    return size - dropped;
}
constexpr auto tuple = hana::make_tuple(hana::int_c<3>, hana::type_c<bool>);
constexpr auto index = index_of(tuple, hana::type_c<bool>);
static_assert(index == hana::size_c<1>, "");
int main() { }

关于上述代码的一些注意事项。首先,Hana中的索引必须是非负的,所以使用无符号类型可能是个好主意。其次,我使用hana::drop_while而不是hana::take_while,因为前者只需要Iterable,而后者需要Sequence。虽然看起来我在做更多的工作(计算两次大小),但事实证明,计算你会遇到的大多数序列的大小非常快,所以这并不是一个真正的问题。最后,我将hana::size(hana::drop_while(...))封装在decltype中,这确保了在运行时不会完成任何工作。

如何使用boost::detail::index_if:

#include <boost/hana.hpp>
template <typename Haystack, typename Needle>
constexpr auto get_index_of_first_matching(Haystack&&, Needle&& n)
{
  using Pred = decltype(boost::hana::equal.to(n));
  using Pack = typename boost::hana::detail::make_pack<Haystack>::type;
  constexpr auto index = boost::hana::detail::index_if<Pred, Pack>::value;
  return boost::hana::int_c<index>;
}
int main()
{
  using namespace boost::hana::literals;
  constexpr auto tup = boost::hana::make_tuple(3_c, boost::hana::type_c<bool>);
  constexpr auto index = get_index_of_first_matching(tup, boost::hana::type_c<bool>);
  static_assert(index == boost::hana::int_c<1>, "index is wrong");
  return 0;
}