shipyard/track/
insertion_modification_deletion.rs

1use crate::component::Component;
2use crate::entity_id::EntityId;
3use crate::seal::Sealed;
4use crate::sparse_set::SparseSet;
5use crate::track::InsertionAndModificationAndDeletion;
6use crate::tracking::{
7    map_deletion_data, DeletionTracking, InsertionTracking, ModificationTracking,
8    RemovalOrDeletionTracking, Tracking, TrackingTimestamp,
9};
10
11impl Sealed for InsertionAndModificationAndDeletion {}
12
13impl Tracking for InsertionAndModificationAndDeletion {
14    const VALUE: u32 = 0b0111;
15
16    fn name() -> &'static str {
17        "Insertion and Modification and Deletion"
18    }
19
20    #[inline]
21    fn is_inserted<T: Component>(
22        sparse_set: &SparseSet<T>,
23        entity: EntityId,
24        last: TrackingTimestamp,
25        current: TrackingTimestamp,
26    ) -> bool {
27        if let Some(dense) = sparse_set.index_of(entity) {
28            sparse_set.insertion_data[dense].is_within(last, current)
29        } else {
30            false
31        }
32    }
33
34    #[inline]
35    fn is_modified<T: Component>(
36        sparse_set: &SparseSet<T>,
37        entity: EntityId,
38        last: TrackingTimestamp,
39        current: TrackingTimestamp,
40    ) -> bool {
41        if let Some(dense) = sparse_set.index_of(entity) {
42            sparse_set.modification_data[dense].is_within(last, current)
43        } else {
44            false
45        }
46    }
47
48    fn is_deleted<T: Component>(
49        sparse_set: &SparseSet<T>,
50        entity: EntityId,
51        last: TrackingTimestamp,
52        current: TrackingTimestamp,
53    ) -> bool {
54        sparse_set
55            .deletion_data
56            .iter()
57            .any(|(id, timestamp, _)| *id == entity && timestamp.is_within(last, current))
58    }
59}
60
61impl InsertionTracking for InsertionAndModificationAndDeletion {}
62impl ModificationTracking for InsertionAndModificationAndDeletion {}
63impl DeletionTracking for InsertionAndModificationAndDeletion {}
64impl RemovalOrDeletionTracking for InsertionAndModificationAndDeletion {
65    #[allow(trivial_casts)]
66    fn removed_or_deleted<T: Component>(
67        sparse_set: &SparseSet<T>,
68    ) -> core::iter::Chain<
69        core::iter::Map<
70            core::slice::Iter<'_, (EntityId, TrackingTimestamp, T)>,
71            for<'r> fn(&'r (EntityId, TrackingTimestamp, T)) -> (EntityId, TrackingTimestamp),
72        >,
73        core::iter::Copied<core::slice::Iter<'_, (EntityId, TrackingTimestamp)>>,
74    > {
75        sparse_set
76            .deletion_data
77            .iter()
78            .map(map_deletion_data as _)
79            .chain([].iter().copied())
80    }
81
82    fn clear_all_removed_and_deleted<T: Component>(sparse_set: &mut SparseSet<T>) {
83        sparse_set.deletion_data.clear();
84    }
85
86    fn clear_all_removed_and_deleted_older_than_timestamp<T: Component>(
87        sparse_set: &mut SparseSet<T>,
88        timestamp: TrackingTimestamp,
89    ) {
90        sparse_set
91            .deletion_data
92            .retain(|(_, t, _)| timestamp.is_older_than(*t));
93    }
94}