Skip to content

Commit

Permalink
view: find/back/front in-place support for single type views
Browse files Browse the repository at this point in the history
  • Loading branch information
skypjack committed Jul 4, 2024
1 parent efd1844 commit 99a91d7
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/entt/entity/view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -767,9 +767,12 @@ class basic_storage_view {
[[nodiscard]] entity_type front() const noexcept {
if constexpr(Policy == deletion_policy::swap_and_pop) {
return empty() ? null : *leading->begin();
} else {
static_assert(Policy == deletion_policy::swap_only, "Unexpected storage policy");
} else if constexpr(Policy == deletion_policy::swap_only) {
return empty() ? null : *(leading->end() - leading->free_list());
} else {
static_assert(Policy == deletion_policy::in_place, "Unexpected storage policy");
const auto it = begin();
return (it == end()) ? null : *it;
}
}

Expand All @@ -779,7 +782,20 @@ class basic_storage_view {
* otherwise.
*/
[[nodiscard]] entity_type back() const noexcept {
if constexpr(Policy == deletion_policy::swap_and_pop || Policy == deletion_policy::swap_only) {
return empty() ? null : *leading->rbegin();
} else {
static_assert(Policy == deletion_policy::in_place, "Unexpected storage policy");

if(leading) {
auto it = leading->rbegin();
const auto last = leading->rend();
for(; (it != last) && (*it == tombstone); ++it) {}
return it == last ? null : *it;
}

return null;
}
}

/**
Expand All @@ -789,17 +805,15 @@ class basic_storage_view {
* iterator otherwise.
*/
[[nodiscard]] iterator find(const entity_type entt) const noexcept {
if(leading) {
if constexpr(Policy == deletion_policy::swap_and_pop) {
return leading->find(entt);
return leading ? leading->find(entt) : iterator{};
} else if constexpr(Policy == deletion_policy::swap_only) {
const auto it = leading ? leading->find(entt) : iterator{};
return leading && (static_cast<size_type>(it.index()) < leading->free_list()) ? it : iterator{};
} else {
static_assert(Policy == deletion_policy::swap_only, "Unexpected storage policy");
const auto it = leading->find(entt);
return (static_cast<size_type>(it.index()) < leading->free_list()) ? it : iterator{};
}
const auto it = leading ? leading->find(entt) : typename common_type::iterator{};
return iterator{it, {leading}, {}, 0u};
}

return iterator{};
}

/**
Expand Down

0 comments on commit 99a91d7

Please sign in to comment.