view: refine internal functions

This commit is contained in:
Michele Caini
2023-12-15 11:29:25 +01:00
parent 80c8c76fdb
commit 7a12dc6533

View File

@@ -17,25 +17,28 @@ namespace entt {
/*! @cond TURN_OFF_DOXYGEN */
namespace internal {
template<typename Type, typename Entity>
[[nodiscard]] bool all_of(const Type *const *elem, const std::size_t len, const Entity entt) noexcept {
std::size_t pos{};
for(; pos < len && elem[pos]->contains(entt); ++pos) {}
return pos == len;
template<typename It, typename Entity>
[[nodiscard]] bool check_but(const std::size_t index, It it, It last, const Entity entt) noexcept {
const auto to = it + index;
for(; it != to && (*it)->contains(entt); ++it) {}
if(it == to) {
for(++it; it != last && (*it)->contains(entt); ++it) {}
}
return it == last;
}
template<typename Type, typename Entity>
[[nodiscard]] bool none_of(const Type *const *elem, const std::size_t len, const Entity entt) noexcept {
std::size_t pos{};
for(; pos < len && !(elem[pos] && elem[pos]->contains(entt)); ++pos) {}
return pos == len;
template<typename It, typename Entity>
[[nodiscard]] bool reject(It it, It last, const Entity entt) noexcept {
for(; (it != last) && !(*it && (*it)->contains(entt)); ++it) {}
return it == last;
}
template<typename Type>
[[nodiscard]] bool fully_initialized(const Type *const *elem, const std::size_t len) noexcept {
std::size_t pos{};
for(; pos < len && elem[pos] != nullptr; ++pos) {}
return pos == len;
template<typename It>
[[nodiscard]] bool fully_initialized(It it, It last) noexcept {
for(; (it != last) && *it; ++it) {}
return it == last;
}
template<typename Result, typename View, typename Other, std::size_t... VGet, std::size_t... VExclude, std::size_t... OGet, std::size_t... OExclude>
@@ -53,9 +56,7 @@ class view_iterator final {
using iterator_type = typename Type::const_iterator;
[[nodiscard]] bool valid(const typename iterator_type::value_type entt) const noexcept {
return ((Get != 1u) || (entt != tombstone))
&& all_of(pools.data(), index, entt) && all_of(pools.data() + index + 1u, Get - index - 1u, entt)
&& none_of(filter.data(), Exclude, entt);
return ((Get != 1u) || (entt != tombstone)) && check_but(index, pools.begin(), pools.end(), entt) && reject(filter.begin(), filter.end(), entt);
}
public:
@@ -347,7 +348,7 @@ public:
* @return True if the view is fully initialized, false otherwise.
*/
[[nodiscard]] explicit operator bool() const noexcept {
return leading && internal::fully_initialized(filter.data(), filter.size());
return leading && internal::fully_initialized(filter.begin(), filter.end());
}
/**
@@ -358,9 +359,7 @@ public:
[[nodiscard]] bool contains(const entity_type entt) const noexcept {
if(leading) {
const auto idx = leading->find(entt).index();
return (!(idx < 0 || idx > leading->begin(0).index()))
&& internal::all_of(pools.data(), index, entt) && internal::all_of(pools.data() + index + 1u, Get - index - 1u, entt)
&& internal::none_of(filter.data(), filter.size(), entt);
return (!(idx < 0 || idx > leading->begin(0).index())) && internal::check_but(index, pools.begin(), pools.end(), entt) && internal::reject(filter.begin(), filter.end(), entt);
}
return false;
@@ -411,9 +410,7 @@ class basic_view<get_t<Get...>, exclude_t<Exclude...>>: public basic_common_view
template<std::size_t Curr, typename Func, std::size_t... Index>
void each(Func &func, std::index_sequence<Index...>) const {
for(const auto curr: storage<Curr>()->each()) {
if(const auto entt = std::get<0>(curr); ((sizeof...(Get) != 1u) || (entt != tombstone))
&& internal::all_of(this->pools.data(), this->index, entt) && internal::all_of(this->pools.data() + this->index + 1u, sizeof...(Get) - this->index - 1u, entt)
&& internal::none_of(this->filter.data(), sizeof...(Exclude), entt)) {
if(const auto entt = std::get<0>(curr); ((sizeof...(Get) != 1u) || (entt != tombstone)) && internal::check_but(this->index, this->pools.begin(), this->pools.end(), entt) && internal::reject(this->filter.begin(), this->filter.end(), entt)) {
if constexpr(is_applicable_v<Func, decltype(std::tuple_cat(std::tuple<entity_type>{}, std::declval<basic_view>().get({})))>) {
std::apply(func, std::tuple_cat(std::make_tuple(entt), dispatch_get<Curr, Index>(curr)...));
} else {