Skip to content

Instantly share code, notes, and snippets.

@ricejasonf
Last active November 26, 2015 02:27
Show Gist options
  • Save ricejasonf/2f2e03b31fe9dfb09333 to your computer and use it in GitHub Desktop.
Save ricejasonf/2f2e03b31fe9dfb09333 to your computer and use it in GitHub Desktop.
Benchmark checking for duplicate types.
#include<boost/hana.hpp>
namespace hana = boost::hana;
auto list_with_no_dups =
hana::unpack(
hana::range_c<int, 0, 50>,
[](auto... i) {
return hana::tuple_c<int, i...>;
});
auto list_with_dups =
hana::unpack(
hana::range_c<int, 0, 50>,
[](auto... i) {
return hana::tuple_c<int, (i % 10)...>;
});
template <typename Foldable>
/*constexpr*/ auto has_duplicates(Foldable const& foldable) {
return hana::unpack(foldable,
[&](auto const& ...x) {
constexpr std::size_t counts[] = {
decltype(hana::count(foldable, x))::value...
};
return hana::bool_c<
hana::detail::find_if(counts, counts + sizeof...(x), hana::_ > 1u)
!=
counts + sizeof...(x)
>;
});
}
int main() {
BOOST_HANA_CONSTANT_CHECK(!has_duplicates(list_with_no_dups));
BOOST_HANA_CONSTANT_CHECK(has_duplicates(list_with_dups));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment