62 template<
typename Visit>
71 std::vector<Entity> out;
80 if (at == ancestor.
raw)
return true;
101 if (before.isValid() && read(target_, before) ==
target.raw) {
102 const u32 b = before.index();
103 const u32 p = prev_[b];
105 next_[s] = before.raw;
106 prev_[b] = source.
raw;
109 const u32 l = last_[t];
113 last_[t] = source.
raw;
118 setSubtreeDepth(source, depth_[t] + 1);
119 noteRetargeted(source.
raw);
129 setSubtreeDepth(source, 0);
130 noteRetargeted(source.
raw);
142 const auto rankOf = [&rank](
Entity e) {
return e.index() < rank.size() ? rank[e.index()] :
NONE; };
143 if (std::is_sorted(scratch_.begin(), scratch_.end(), [&](
Entity a,
Entity b) { return rankOf(a) < rankOf(b); }))
return false;
144 std::stable_sort(scratch_.begin(), scratch_.end(), [&](
Entity a,
Entity b) { return rankOf(a) < rankOf(b); });
147 for (
const Entity e : scratch_) {
148 prev_[e.index()] =
prev;
167 if (i >= target_.size())
return;
168 if (target_[i] !=
NONE || first_[i] !=
NONE) markTree(entity);
169 bool changed =
false;
170 if (target_[i] !=
NONE) { detach(entity); changed =
true; }
171 for (
u32 at = first_[i]; at !=
NONE;) {
173 const u32 following = next_[c];
174 target_[c] = prev_[c] = next_[c] =
NONE;
182 first_[i] = last_[i] =
NONE;
185 if (changed) ++epoch_;
189 target_.clear(); first_.clear(); last_.clear(); next_.clear(); prev_.clear(); count_.clear(); depth_.clear();
192 retargetedOverflow_ =
true;
194 dirty_roots_.clear();
205 const bool listed = !retargetedOverflow_;
208 retargetedOverflow_ =
false;
242 const std::vector<CascadeTree>&
cascade()
const {
250 if (i >= tree_of_.size() || tree_of_[i] ==
NONE)
return {
NONE,
NONE};
251 return {tree_of_[i], slot_of_[i]};
261 std::vector<u32> target_, first_, last_, next_, prev_, count_, depth_;
262 std::vector<Entity> scratch_;
263 std::vector<u32> walk_;
267 std::vector<u32> retargeted_;
268 bool retargetedOverflow_ =
false;
269 static constexpr usize RETARGETED_CAP = 4096;
271 void noteRetargeted(
u32 raw) {
272 if (retargetedOverflow_)
return;
273 if (retargeted_.size() >= RETARGETED_CAP) { retargeted_.clear(); retargetedOverflow_ =
true;
return; }
274 retargeted_.push_back(raw);
277 mutable std::vector<CascadeTree> trees_;
278 mutable std::vector<u32> free_trees_;
280 mutable std::vector<u32> tree_of_, slot_of_;
282 mutable std::vector<u32> dirty_roots_;
283 mutable bool all_dirty_ =
true;
284 mutable std::vector<std::pair<u32, u32>> pending_;
285 static constexpr usize DIRTY_ROOTS_CAP = 1024;
288 void markTree(Entity entity) {
289 if (all_dirty_ || !entity.isValid())
return;
290 if (dirty_roots_.size() >= DIRTY_ROOTS_CAP) { all_dirty_ =
true; dirty_roots_.clear();
return; }
291 dirty_roots_.push_back(
root(entity).raw);
295 u32 rootAt(
u32 index)
const {
296 if (index >= target_.size() || target_[index] !=
NONE || first_[index] ==
NONE)
return NONE;
301 void refreshCascade()
const {
303 for (
auto& tree : trees_)
for (
const auto& e : tree.entries) forget(e.entity);
306 tree_of_.assign(target_.size(),
NONE);
307 slot_of_.assign(target_.size(),
NONE);
308 for (
u32 i = 0, n =
static_cast<u32>(target_.size()); i < n; ++i) {
309 const u32 r = rootAt(i);
313 dirty_roots_.clear();
316 if (dirty_roots_.empty())
return;
317 if (tree_of_.size() < target_.size()) {
318 tree_of_.resize(target_.size(),
NONE);
319 slot_of_.resize(target_.size(),
NONE);
321 std::sort(dirty_roots_.begin(), dirty_roots_.end());
322 dirty_roots_.erase(std::unique(dirty_roots_.begin(), dirty_roots_.end()), dirty_roots_.end());
325 for (
const u32 raw : dirty_roots_) {
327 if (t ==
NONE)
continue;
328 for (
const auto& e : trees_[t].entries) forget(e.entity);
329 trees_[t].entries.clear();
330 trees_[t].alive =
false;
331 free_trees_.push_back(t);
333 for (
const u32 raw : dirty_roots_) {
337 dirty_roots_.clear();
341 u32 treeRootedAt(Entity
root)
const {
343 if (i >= tree_of_.size() || tree_of_[i] ==
NONE || slot_of_[i] != 0)
return NONE;
344 const u32 t = tree_of_[i];
345 return trees_[t].alive && trees_[t].root ==
root ? t :
NONE;
348 void forget(Entity entity)
const {
349 const u32 i = entity.index();
350 if (i < tree_of_.size()) tree_of_[i] = slot_of_[i] =
NONE;
353 void buildTree(Entity
root)
const {
355 if (!free_trees_.empty()) { t = free_trees_.back(); free_trees_.pop_back(); }
356 else { t =
static_cast<u32>(trees_.size()); trees_.emplace_back(); }
357 auto& tree = trees_[t];
360 auto& out = tree.entries;
363 pushSources(
root.index(), 0);
364 while (!pending_.empty()) {
365 const auto [raw, parent] = pending_.back();
372 const u32 n =
static_cast<u32>(out.size());
373 for (
u32 i = 0; i < n; ++i) {
375 const u32 e = out[i].entity.index();
376 if (e >= tree_of_.size()) { tree_of_.resize(e + 1,
NONE); slot_of_.resize(e + 1,
NONE); }
380 for (
u32 i = n; i-- > 0;) {
381 const u32 p = out[i].parent;
382 if (p !=
NONE && out[i].end > out[p].end) out[p].end = out[i].end;
387 void pushSources(
u32 index,
u32 slot)
const {
391 static u32 read(
const std::vector<u32>& slots, Entity entity) {
392 const u32 i = entity.index();
393 return i < slots.size() ? slots[i] :
NONE;
396 void grow(
u32 index) {
397 if (index < target_.size())
return;
399 const usize size = std::max<usize>(
static_cast<usize>(index) + 1, target_.size() * 2);
400 target_.resize(size,
NONE); first_.resize(size,
NONE); last_.resize(size,
NONE);
401 next_.resize(size,
NONE); prev_.resize(size,
NONE); count_.resize(size, 0); depth_.resize(size, 0);
405 void detach(Entity source) {
406 const u32 s = source.index();
408 const u32 p = prev_[s], n = next_[s];
411 target_[s] = prev_[s] = next_[s] =
NONE;
419 if (r >= depth_.size() || depth_[r] ==
depth)
return;
420 const i64 delta =
static_cast<i64>(
depth) -
static_cast<i64>(depth_[r]);
422 walk_.push_back(
root.raw);
423 while (!walk_.empty()) {
424 const u32 at = walk_.back();
427 depth_[i] =
static_cast<u32>(
static_cast<i64>(depth_[i]) + delta);
Core type definitions and utilities for ESEngine.
u32 depth(Entity entity) const
Links between entity and the root of its tree: 0 for a root.
const u32 * nexts() const
void unlink(Entity source)
Takes source's target away; its own sources stay with it.
static constexpr u32 NONE
const u32 * firsts() const
u32 linkCount() const
How many entities have a target: the size of (Relation, *).
Entity target(Entity source) const
The entity's target (its parent), or INVALID_ENTITY.
bool takeRetargeted(std::vector< Entity > &out)
Hands over the entities whose target changed since the last call (moved, detached,...
CascadeAt cascadeAt(Entity entity) const
Where entity sits in cascade(), or tree NONE when it is in no tree. Current as of the last cascade() ...
Entity last(Entity target) const
bool hasTarget(Entity source) const
Entity first(Entity target) const
The first / last entity naming target (first and last child), or INVALID_ENTITY.
void onDestroy(Entity entity)
Forgets entity, which is being destroyed: it leaves its target, and its sources become roots.
u32 epoch() const
Bumped by every edit to the relation's shape, so a cache built from it can tell it is stale.
u32 count(Entity target) const
How many entities name target.
bool isDescendantOf(Entity entity, Entity ancestor) const
Whether ancestor is entity's target, or its target's, and so on up. Bounded: no cycle can form here.
std::vector< Entity > sources(Entity target) const
The entities naming target, in order: for a caller that edits the relation while it walks them.
void link(Entity source, Entity target, Entity before=INVALID_ENTITY)
Gives source the target target, placed before before among its sources (last when before is none).
Entity root(Entity entity) const
The top of entity's tree (itself when it has no target).
bool hasSources(Entity target) const
void eachSource(Entity target, Visit &&visit) const
Calls visit(source) for each entity naming target, in order. visit must not edit the relation.
Entity prev(Entity source) const
bool sortSources(Entity target, const std::vector< u32 > &rank)
Puts target's sources in the order of rank (per entity index, lower first; stable; past the end last)...
const u32 * targets() const
The arrays themselves, for a reader across the wasm boundary; valid until the next edit that grows th...
const std::vector< CascadeTree > & cascade() const
Every tree of the relation, each in cascade order: Flecs' cascade, one array per tree.
Entity next(Entity source) const
The entity after / before source among those naming the same target (siblings), or INVALID_ENTITY.
std::size_t usize
Size type for indexing and sizes.
constexpr Entity INVALID_ENTITY
Invalid entity sentinel value.
std::uint32_t u32
Unsigned 32-bit integer.
std::int64_t i64
Signed 64-bit integer.
Entity identifier with packed index + generation.
static constexpr u32 INVALID_RAW
static constexpr Entity fromRaw(u32 v)
Reconstructs an Entity from a raw packed value.
constexpr u32 index() const
Gets the entity index.
Where an entity sits in the cascade: which tree, and where in it.
One entity of a tree, in cascade order.
One tree, root first, every entity after its target and in its siblings' order (preorder).
std::vector< CascadeEntry > entries