mirror of
https://github.com/BlackMATov/ecs.hpp.git
synced 2025-12-13 10:35:39 +07:00
tuple_contains speedup
This commit is contained in:
32
ecs.hpp
32
ecs.hpp
@@ -115,19 +115,29 @@ namespace ecs_hpp
|
||||
// tuple_contains
|
||||
//
|
||||
|
||||
template < typename V >
|
||||
bool tuple_contains(const std::tuple<>& t, const V& v) {
|
||||
(void)t;
|
||||
(void)v;
|
||||
return false;
|
||||
namespace impl
|
||||
{
|
||||
template < size_t I, typename V, typename... Ts >
|
||||
std::enable_if_t<I == sizeof...(Ts), bool>
|
||||
tuple_contains_impl(const std::tuple<Ts...>& t, const V& v) {
|
||||
(void)t;
|
||||
(void)v;
|
||||
return false;
|
||||
}
|
||||
|
||||
template < size_t I, typename V, typename... Ts >
|
||||
std::enable_if_t<I != sizeof...(Ts), bool>
|
||||
tuple_contains_impl(const std::tuple<Ts...>& t, const V& v) {
|
||||
if ( std::get<I>(t) == v ) {
|
||||
return true;
|
||||
}
|
||||
return tuple_contains_impl<I + 1>(t, v);
|
||||
}
|
||||
}
|
||||
|
||||
template < typename V, typename T, typename... Ts >
|
||||
bool tuple_contains(const std::tuple<T, Ts...>& t, const V& v) {
|
||||
if ( std::get<0>(t) == v ) {
|
||||
return true;
|
||||
}
|
||||
return tuple_contains(tuple_tail(t), v);
|
||||
template < typename V, typename... Ts >
|
||||
bool tuple_contains(const std::tuple<Ts...>& t, const V& v) {
|
||||
return impl::tuple_contains_impl<0>(t, v);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@@ -80,6 +80,20 @@ TEST_CASE("detail") {
|
||||
REQUIRE(tuple_tail(t3) == std::make_tuple(2, 3));
|
||||
}
|
||||
}
|
||||
SECTION("tuple_contains") {
|
||||
using namespace ecs::detail;
|
||||
{
|
||||
REQUIRE_FALSE(tuple_contains(std::make_tuple(), nullptr));
|
||||
REQUIRE_FALSE(tuple_contains(std::make_tuple(1), 0));
|
||||
REQUIRE_FALSE(tuple_contains(std::make_tuple(1), 2));
|
||||
REQUIRE(tuple_contains(std::make_tuple(1), 1));
|
||||
REQUIRE(tuple_contains(std::make_tuple(1,2,3), 1));
|
||||
REQUIRE(tuple_contains(std::make_tuple(1,2,3), 2));
|
||||
REQUIRE(tuple_contains(std::make_tuple(1,2,3), 3));
|
||||
REQUIRE_FALSE(tuple_contains(std::make_tuple(1,2,3), 0));
|
||||
REQUIRE_FALSE(tuple_contains(std::make_tuple(1,2,3), 4));
|
||||
}
|
||||
}
|
||||
SECTION("sparse_set") {
|
||||
using namespace ecs::detail;
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user