proof-engine 0.1.1

A mathematical rendering engine for Rust. Every visual is the output of a mathematical function.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
//! Component storage primitives for the ECS.
//!
//! The primary storage is [`ComponentStorage<T>`], a **sparse set** implementation:
//! - A sparse array (`Vec<Option<usize>>`) maps entity slot indices to dense indices.
//! - A dense array (`Vec<T>`) holds the actual component values contiguously.
//! - A parallel `Vec<Entity>` records which entity owns each dense slot.
//!
//! This gives O(1) insert/remove/lookup and cache-friendly iteration.
//!
//! For type-erased storage needed by [`crate::ecs::world::World`], the
//! [`AnyComponentStorage`] trait and [`TypedStorage<T>`] wrapper provide
//! downcasting support.

use std::any::{Any, TypeId};
use std::cell::RefCell;
use std::marker::PhantomData;

use super::entity::Entity;

// ---------------------------------------------------------------------------
// Component trait
// ---------------------------------------------------------------------------

/// Marker trait for all component types.
/// Automatically implemented for any `'static + Send + Sync` type.
pub trait Component: 'static + Send + Sync {}
impl<T: 'static + Send + Sync> Component for T {}

// ---------------------------------------------------------------------------
// ComponentStorage<T>
// ---------------------------------------------------------------------------

/// Sparse-set component storage for component type `T`.
///
/// # Complexity
/// | Operation | Complexity |
/// |-----------|-----------|
/// | `insert`  | O(1) amortised |
/// | `remove`  | O(1)      |
/// | `get`     | O(1)      |
/// | iteration | O(n) dense|
#[derive(Debug)]
pub struct ComponentStorage<T> {
    /// Maps entity slot id → index into the dense arrays. `None` = absent.
    sparse: Vec<Option<usize>>,
    /// Dense component values — tightly packed, cache-friendly.
    dense: Vec<T>,
    /// Dense entity list — parallel to `dense`, records which entity owns each slot.
    entities: Vec<Entity>,
    /// Change-detection tick — incremented when components are modified.
    change_tick: u64,
    /// Per-component added/changed ticks for fine-grained change detection.
    added_ticks: Vec<u64>,
    changed_ticks: Vec<u64>,
}

impl<T: Component> ComponentStorage<T> {
    /// Create an empty storage.
    pub fn new() -> Self {
        Self {
            sparse: Vec::new(),
            dense: Vec::new(),
            entities: Vec::new(),
            change_tick: 0,
            added_ticks: Vec::new(),
            changed_ticks: Vec::new(),
        }
    }

    /// Create storage with pre-allocated sparse capacity.
    pub fn with_capacity(sparse_cap: usize, dense_cap: usize) -> Self {
        Self {
            sparse: Vec::with_capacity(sparse_cap),
            dense: Vec::with_capacity(dense_cap),
            entities: Vec::with_capacity(dense_cap),
            change_tick: 0,
            added_ticks: Vec::with_capacity(dense_cap),
            changed_ticks: Vec::with_capacity(dense_cap),
        }
    }

    fn ensure_sparse(&mut self, id: usize) {
        if id >= self.sparse.len() {
            self.sparse.resize(id + 1, None);
        }
    }

    /// Insert or replace the component for `entity`.
    /// Returns the old component if one existed.
    pub fn insert(&mut self, entity: Entity, component: T) -> Option<T> {
        let id = entity.id() as usize;
        self.ensure_sparse(id);
        self.change_tick += 1;
        let tick = self.change_tick;

        if let Some(dense_idx) = self.sparse[id] {
            // Replace existing.
            let old = std::mem::replace(&mut self.dense[dense_idx], component);
            self.changed_ticks[dense_idx] = tick;
            Some(old)
        } else {
            // New entry.
            let dense_idx = self.dense.len();
            self.sparse[id] = Some(dense_idx);
            self.dense.push(component);
            self.entities.push(entity);
            self.added_ticks.push(tick);
            self.changed_ticks.push(tick);
            None
        }
    }

    /// Remove the component for `entity`. Returns it if present.
    pub fn remove(&mut self, entity: Entity) -> Option<T> {
        self.remove_and_return(entity)
    }

    /// Remove the component for `entity`. Returns it if present.
    pub fn remove_and_return(&mut self, entity: Entity) -> Option<T> {
        let id = entity.id() as usize;
        if id >= self.sparse.len() {
            return None;
        }
        let dense_idx = match self.sparse[id] {
            Some(i) => i,
            None => return None,
        };

        self.sparse[id] = None;
        let last_idx = self.dense.len() - 1;

        // If not last, move last element into the gap.
        if dense_idx != last_idx {
            let moved_entity = self.entities[last_idx];
            self.sparse[moved_entity.id() as usize] = Some(dense_idx);
        }

        let value = self.dense.swap_remove(dense_idx);
        self.entities.swap_remove(dense_idx);
        self.added_ticks.swap_remove(dense_idx);
        self.changed_ticks.swap_remove(dense_idx);

        Some(value)
    }

    /// Returns `true` if `entity` has this component.
    #[inline]
    pub fn contains(&self, entity: Entity) -> bool {
        let id = entity.id() as usize;
        id < self.sparse.len() && self.sparse[id].is_some()
    }

    /// Get an immutable reference to the component for `entity`.
    #[inline]
    pub fn get(&self, entity: Entity) -> Option<&T> {
        let id = entity.id() as usize;
        if id >= self.sparse.len() {
            return None;
        }
        let dense_idx = self.sparse[id]?;
        Some(&self.dense[dense_idx])
    }

    /// Get a mutable reference to the component for `entity`.
    #[inline]
    pub fn get_mut(&mut self, entity: Entity) -> Option<&mut T> {
        let id = entity.id() as usize;
        if id >= self.sparse.len() {
            return None;
        }
        let dense_idx = self.sparse[id]?;
        self.change_tick += 1;
        self.changed_ticks[dense_idx] = self.change_tick;
        Some(&mut self.dense[dense_idx])
    }

    /// Number of components stored.
    #[inline]
    pub fn len(&self) -> usize {
        self.dense.len()
    }

    /// Returns `true` if no components are stored.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.dense.is_empty()
    }

    /// Clear all components.
    pub fn clear(&mut self) {
        self.sparse.iter_mut().for_each(|s| *s = None);
        self.dense.clear();
        self.entities.clear();
        self.added_ticks.clear();
        self.changed_ticks.clear();
    }

    /// Iterate over `(entity, component)` pairs.
    pub fn iter(&self) -> impl Iterator<Item = (&Entity, &T)> {
        self.entities.iter().zip(self.dense.iter())
    }

    /// Iterate over `(entity, &mut component)` pairs.
    pub fn iter_mut(&mut self) -> impl Iterator<Item = (&Entity, &mut T)> {
        self.entities.iter().zip(self.dense.iter_mut())
    }

    /// Iterate over component values only (dense, cache-friendly).
    pub fn values(&self) -> impl Iterator<Item = &T> {
        self.dense.iter()
    }

    /// Iterate over mutable component values only.
    pub fn values_mut(&mut self) -> impl Iterator<Item = &mut T> {
        self.dense.iter_mut()
    }

    /// Iterate over entities that have this component.
    pub fn entities(&self) -> impl Iterator<Item = &Entity> {
        self.entities.iter()
    }

    /// Get the dense index for an entity (internal helper).
    #[inline]
    pub fn dense_index(&self, entity: Entity) -> Option<usize> {
        let id = entity.id() as usize;
        if id >= self.sparse.len() { None } else { self.sparse[id] }
    }

    /// Sort the dense arrays by entity (useful for deterministic iteration).
    pub fn sort_by_entity(&mut self) {
        // Build indices sorted by entity
        let mut indices: Vec<usize> = (0..self.dense.len()).collect();
        indices.sort_by_key(|&i| self.entities[i]);

        // Apply permutation in-place using a temporary.
        let new_entities: Vec<Entity> = indices.iter().map(|&i| self.entities[i]).collect();
        let new_added: Vec<u64> = indices.iter().map(|&i| self.added_ticks[i]).collect();
        let new_changed: Vec<u64> = indices.iter().map(|&i| self.changed_ticks[i]).collect();

        // Apply the permutation to self.dense using a swap-cycle approach.
        // This works in-place without requiring T: Default or Clone.
        let mut placed = vec![false; self.dense.len()];
        for start in 0..self.dense.len() {
            if placed[start] { continue; }
            let mut current = start;
            loop {
                let target = indices[current];
                if target == start || placed[target] {
                    placed[current] = true;
                    break;
                }
                self.dense.swap(current, target);
                placed[current] = true;
                current = target;
            }
        }

        self.entities = new_entities;
        self.added_ticks = new_added;
        self.changed_ticks = new_changed;

        // Rebuild sparse from new entity positions.
        for (dense_idx, entity) in self.entities.iter().enumerate() {
            let id = entity.id() as usize;
            self.sparse[id] = Some(dense_idx);
        }
    }

    /// Returns components added since `tick`.
    pub fn iter_added_since(&self, tick: u64) -> impl Iterator<Item = (&Entity, &T)> {
        self.entities
            .iter()
            .zip(self.dense.iter())
            .zip(self.added_ticks.iter())
            .filter_map(move |((e, c), &t)| if t > tick { Some((e, c)) } else { None })
    }

    /// Returns components changed since `tick`.
    pub fn iter_changed_since(&self, tick: u64) -> impl Iterator<Item = (&Entity, &T)> {
        self.entities
            .iter()
            .zip(self.dense.iter())
            .zip(self.changed_ticks.iter())
            .filter_map(move |((e, c), &t)| if t > tick { Some((e, c)) } else { None })
    }

    /// Current change tick.
    pub fn change_tick(&self) -> u64 {
        self.change_tick
    }

    /// Bump the change tick externally (e.g., when the world ticks).
    pub fn advance_tick(&mut self) {
        self.change_tick += 1;
    }

    /// Get a component by its dense index (unchecked — panics on OOB).
    #[inline]
    pub fn get_by_dense_index(&self, idx: usize) -> &T {
        &self.dense[idx]
    }

    /// Get a mutable component by dense index.
    #[inline]
    pub fn get_mut_by_dense_index(&mut self, idx: usize) -> &mut T {
        &mut self.dense[idx]
    }

    /// Drain all components, returning an iterator of `(Entity, T)` pairs.
    pub fn drain(&mut self) -> impl Iterator<Item = (Entity, T)> + '_ {
        self.sparse.iter_mut().for_each(|s| *s = None);
        let entities = std::mem::take(&mut self.entities);
        let dense = std::mem::take(&mut self.dense);
        self.added_ticks.clear();
        self.changed_ticks.clear();
        entities.into_iter().zip(dense.into_iter())
    }

    /// Returns true if ALL entities in `entities` have this component.
    pub fn contains_all(&self, entities: &[Entity]) -> bool {
        entities.iter().all(|e| self.contains(*e))
    }

    /// Returns true if ANY entity in `entities` has this component.
    pub fn contains_any(&self, entities: &[Entity]) -> bool {
        entities.iter().any(|e| self.contains(*e))
    }
}

impl<T: Component> Default for ComponentStorage<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: Component + Clone> ComponentStorage<T> {
    /// Clone the component for `entity` without borrowing the whole storage.
    pub fn clone_component(&self, entity: Entity) -> Option<T> {
        self.get(entity).cloned()
    }
}

// ---------------------------------------------------------------------------
// AnyComponentStorage — type-erased trait
// ---------------------------------------------------------------------------

/// Type-erased interface for component storage, allowing [`World`] to hold
/// heterogeneous storages in a `HashMap<TypeId, Box<dyn AnyComponentStorage>>`.
pub trait AnyComponentStorage: Any + Send + Sync {
    /// The `TypeId` of the component type this storage holds.
    fn component_type_id(&self) -> TypeId;

    /// Remove the component for `entity` (value is dropped).
    fn remove_erased(&mut self, entity: Entity);

    /// Returns `true` if `entity` has this component.
    fn contains_erased(&self, entity: Entity) -> bool;

    /// Number of components stored.
    fn len_erased(&self) -> usize;

    /// Clear all components.
    fn clear_erased(&mut self);

    /// Expose as `&dyn Any` for downcasting.
    fn as_any(&self) -> &dyn Any;

    /// Expose as `&mut dyn Any` for downcasting.
    fn as_any_mut(&mut self) -> &mut dyn Any;

    /// Remove all components for entities that are no longer alive.
    fn remove_stale(&mut self, alive: &[Entity]);
}

// ---------------------------------------------------------------------------
// TypedStorage<T> — AnyComponentStorage wrapper
// ---------------------------------------------------------------------------

/// Wraps `ComponentStorage<T>` inside a `RefCell` and implements
/// [`AnyComponentStorage`] for type-erased use in [`World`].
pub struct TypedStorage<T: Component> {
    pub(crate) inner: RefCell<ComponentStorage<T>>,
}

impl<T: Component> TypedStorage<T> {
    pub fn new() -> Self {
        Self {
            inner: RefCell::new(ComponentStorage::new()),
        }
    }

    /// Borrow the inner storage immutably.
    pub fn borrow(&self) -> std::cell::Ref<'_, ComponentStorage<T>> {
        self.inner.borrow()
    }

    /// Borrow the inner storage mutably.
    pub fn borrow_mut(&self) -> std::cell::RefMut<'_, ComponentStorage<T>> {
        self.inner.borrow_mut()
    }
}

impl<T: Component> Default for TypedStorage<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: Component> std::fmt::Debug for TypedStorage<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "TypedStorage<{}>(len={})", std::any::type_name::<T>(), self.inner.borrow().len())
    }
}

// SAFETY: T: Send + Sync, RefCell<ComponentStorage<T>> is not Send by default,
// but we uphold exclusive-access invariants through the ECS world borrow rules.
unsafe impl<T: Component> Send for TypedStorage<T> {}
unsafe impl<T: Component> Sync for TypedStorage<T> {}

impl<T: Component> AnyComponentStorage for TypedStorage<T> {
    fn component_type_id(&self) -> TypeId {
        TypeId::of::<T>()
    }

    fn remove_erased(&mut self, entity: Entity) {
        self.inner.borrow_mut().remove_and_return(entity);
    }

    fn contains_erased(&self, entity: Entity) -> bool {
        self.inner.borrow().contains(entity)
    }

    fn len_erased(&self) -> usize {
        self.inner.borrow().len()
    }

    fn clear_erased(&mut self) {
        self.inner.borrow_mut().clear();
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }

    fn remove_stale(&mut self, alive: &[Entity]) {
        let mut storage = self.inner.borrow_mut();
        let stale: Vec<Entity> = storage
            .entities()
            .copied()
            .filter(|e| !alive.contains(e))
            .collect();
        for e in stale {
            storage.remove_and_return(e);
        }
    }
}

// ---------------------------------------------------------------------------
// ComponentVec — helper newtype for boxed storage
// ---------------------------------------------------------------------------

/// A `Box<dyn AnyComponentStorage>` with a convenient downcasting API.
pub struct ComponentVec(pub Box<dyn AnyComponentStorage>);

impl ComponentVec {
    pub fn new<T: Component>() -> Self {
        Self(Box::new(TypedStorage::<T>::new()))
    }

    /// Downcast to `&TypedStorage<T>`, or `None` if the type doesn't match.
    pub fn downcast_ref<T: Component>(&self) -> Option<&TypedStorage<T>> {
        self.0.as_any().downcast_ref::<TypedStorage<T>>()
    }

    /// Downcast to `&mut TypedStorage<T>`.
    pub fn downcast_mut<T: Component>(&mut self) -> Option<&mut TypedStorage<T>> {
        self.0.as_any_mut().downcast_mut::<TypedStorage<T>>()
    }

    /// Convenience: get a component from the storage.
    pub fn get_component<T: Component>(&self, entity: Entity) -> Option<std::cell::Ref<'_, ComponentStorage<T>>> {
        let typed = self.downcast_ref::<T>()?;
        let guard = typed.borrow();
        // We can't return a Ref to a sub-field, so just return the guard.
        // Caller can call .get(entity) on it.
        Some(guard)
    }

    /// Delegate to inner storage.
    pub fn contains(&self, entity: Entity) -> bool {
        self.0.contains_erased(entity)
    }

    pub fn remove(&mut self, entity: Entity) {
        self.0.remove_erased(entity);
    }

    pub fn len(&self) -> usize {
        self.0.len_erased()
    }

    pub fn is_empty(&self) -> bool {
        self.0.len_erased() == 0
    }

    pub fn component_type_id(&self) -> TypeId {
        self.0.component_type_id()
    }
}

impl std::fmt::Debug for ComponentVec {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "ComponentVec(TypeId={:?}, len={})", self.component_type_id(), self.len())
    }
}

// ---------------------------------------------------------------------------
// StorageSet — a collection of typed storages identified by TypeId
// ---------------------------------------------------------------------------

/// A map of `TypeId -> ComponentVec`, used internally by [`World`].
#[derive(Debug, Default)]
pub struct StorageSet {
    storages: std::collections::HashMap<TypeId, ComponentVec>,
}

impl StorageSet {
    pub fn new() -> Self {
        Self::default()
    }

    /// Get or insert storage for component type `T`.
    pub fn get_or_insert<T: Component>(&mut self) -> &ComponentVec {
        self.storages
            .entry(TypeId::of::<T>())
            .or_insert_with(ComponentVec::new::<T>)
    }

    /// Get or insert storage for component type `T` (mutable).
    pub fn get_or_insert_mut<T: Component>(&mut self) -> &mut ComponentVec {
        self.storages
            .entry(TypeId::of::<T>())
            .or_insert_with(ComponentVec::new::<T>)
    }

    /// Get immutable storage for `T`, returns `None` if not yet registered.
    pub fn get<T: Component>(&self) -> Option<&ComponentVec> {
        self.storages.get(&TypeId::of::<T>())
    }

    /// Get mutable storage for `T`.
    pub fn get_mut<T: Component>(&mut self) -> Option<&mut ComponentVec> {
        self.storages.get_mut(&TypeId::of::<T>())
    }

    /// Remove all components for `entity` across all storages.
    pub fn remove_entity(&mut self, entity: Entity) {
        for cv in self.storages.values_mut() {
            cv.remove(entity);
        }
    }

    /// Remove stale entries (entities no longer alive).
    pub fn remove_stale(&mut self, alive: &[Entity]) {
        for cv in self.storages.values_mut() {
            cv.0.remove_stale(alive);
        }
    }

    /// Number of component types registered.
    pub fn type_count(&self) -> usize {
        self.storages.len()
    }

    /// Total components across all storages.
    pub fn total_component_count(&self) -> usize {
        self.storages.values().map(|cv| cv.len()).sum()
    }

    /// Clear all storages.
    pub fn clear_all(&mut self) {
        for cv in self.storages.values_mut() {
            cv.0.clear_erased();
        }
    }

    /// Iterate over all storage entries.
    pub fn iter(&self) -> impl Iterator<Item = (&TypeId, &ComponentVec)> {
        self.storages.iter()
    }

    pub fn iter_mut(&mut self) -> impl Iterator<Item = (&TypeId, &mut ComponentVec)> {
        self.storages.iter_mut()
    }

    /// Check if a component type is registered.
    pub fn has_type<T: Component>(&self) -> bool {
        self.storages.contains_key(&TypeId::of::<T>())
    }

    /// Check entity has component T.
    pub fn contains<T: Component>(&self, entity: Entity) -> bool {
        self.get::<T>().map_or(false, |cv| cv.contains(entity))
    }

    /// Insert component T for entity.
    pub fn insert_component<T: Component>(&mut self, entity: Entity, component: T) {
        let cv = self.get_or_insert_mut::<T>();
        cv.downcast_mut::<T>()
            .expect("storage type mismatch")
            .borrow_mut()
            .insert(entity, component);
    }

    /// Remove component T for entity, returning it.
    pub fn remove_component<T: Component>(&mut self, entity: Entity) -> Option<T> {
        let cv = self.get_mut::<T>()?;
        cv.downcast_mut::<T>()
            .expect("storage type mismatch")
            .borrow_mut()
            .remove_and_return(entity)
    }
}

// ---------------------------------------------------------------------------
// Archetype — groups entities with the same component composition
// ---------------------------------------------------------------------------

/// Tracks which component types an entity has.
/// Used for accelerated query matching.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ComponentMask {
    /// Sorted list of TypeIds present.
    types: Vec<TypeId>,
}

impl ComponentMask {
    pub fn empty() -> Self {
        Self { types: Vec::new() }
    }

    pub fn with<T: Component>(mut self) -> Self {
        let tid = TypeId::of::<T>();
        if let Err(pos) = self.types.binary_search(&tid) {
            self.types.insert(pos, tid);
        }
        self
    }

    pub fn add<T: Component>(&mut self) {
        let tid = TypeId::of::<T>();
        if let Err(pos) = self.types.binary_search(&tid) {
            self.types.insert(pos, tid);
        }
    }

    pub fn remove<T: Component>(&mut self) {
        let tid = TypeId::of::<T>();
        if let Ok(pos) = self.types.binary_search(&tid) {
            self.types.remove(pos);
        }
    }

    pub fn has<T: Component>(&self) -> bool {
        self.types.binary_search(&TypeId::of::<T>()).is_ok()
    }

    pub fn has_type(&self, tid: &TypeId) -> bool {
        self.types.binary_search(tid).is_ok()
    }

    pub fn contains_all(&self, required: &[TypeId]) -> bool {
        required.iter().all(|t| self.has_type(t))
    }

    pub fn contains_none(&self, excluded: &[TypeId]) -> bool {
        excluded.iter().all(|t| !self.has_type(t))
    }

    pub fn len(&self) -> usize {
        self.types.len()
    }

    pub fn is_empty(&self) -> bool {
        self.types.is_empty()
    }

    pub fn types(&self) -> &[TypeId] {
        &self.types
    }
}

impl Default for ComponentMask {
    fn default() -> Self {
        Self::empty()
    }
}

// ---------------------------------------------------------------------------
// Unit tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ecs::entity::EntityAllocator;

    fn make_entity(id: u32, gen: u32) -> Entity {
        Entity::new(id, gen)
    }

    #[test]
    fn test_insert_get() {
        let mut storage: ComponentStorage<i32> = ComponentStorage::new();
        let e = make_entity(0, 0);
        storage.insert(e, 42);
        assert_eq!(storage.get(e), Some(&42));
        assert!(storage.contains(e));
        assert_eq!(storage.len(), 1);
    }

    #[test]
    fn test_replace() {
        let mut storage: ComponentStorage<i32> = ComponentStorage::new();
        let e = make_entity(0, 0);
        storage.insert(e, 10);
        let old = storage.remove_and_return(e);
        assert_eq!(old, Some(10));
        storage.insert(e, 20);
        assert_eq!(storage.get(e), Some(&20));
    }

    #[test]
    fn test_remove_swap() {
        let mut storage: ComponentStorage<i32> = ComponentStorage::new();
        let e0 = make_entity(0, 0);
        let e1 = make_entity(1, 0);
        let e2 = make_entity(2, 0);
        storage.insert(e0, 100);
        storage.insert(e1, 200);
        storage.insert(e2, 300);

        let removed = storage.remove_and_return(e1);
        assert_eq!(removed, Some(200));
        assert!(!storage.contains(e1));
        assert!(storage.contains(e0));
        assert!(storage.contains(e2));
        assert_eq!(storage.len(), 2);
        assert_eq!(storage.get(e0), Some(&100));
        assert_eq!(storage.get(e2), Some(&300));
    }

    #[test]
    fn test_iter() {
        let mut storage: ComponentStorage<u32> = ComponentStorage::new();
        let e0 = make_entity(0, 0);
        let e1 = make_entity(1, 0);
        storage.insert(e0, 1);
        storage.insert(e1, 2);

        let mut pairs: Vec<(Entity, u32)> = storage.iter().map(|(&e, &v)| (e, v)).collect();
        pairs.sort_by_key(|(e, _)| e.id());
        assert_eq!(pairs, vec![(e0, 1), (e1, 2)]);
    }

    #[test]
    fn test_get_mut() {
        let mut storage: ComponentStorage<String> = ComponentStorage::new();
        let e = make_entity(5, 0);
        storage.insert(e, "hello".to_string());
        if let Some(val) = storage.get_mut(e) {
            val.push_str(" world");
        }
        assert_eq!(storage.get(e).map(|s| s.as_str()), Some("hello world"));
    }

    #[test]
    fn test_clear() {
        let mut storage: ComponentStorage<i32> = ComponentStorage::new();
        for i in 0..10u32 {
            storage.insert(make_entity(i, 0), i as i32);
        }
        assert_eq!(storage.len(), 10);
        storage.clear();
        assert_eq!(storage.len(), 0);
        for i in 0..10u32 {
            assert!(!storage.contains(make_entity(i, 0)));
        }
    }

    #[test]
    fn test_typed_storage_any_downcast() {
        let mut ts = TypedStorage::<f32>::new();
        let e = make_entity(0, 0);
        ts.borrow_mut().insert(e, 3.14f32);

        let boxed: Box<dyn AnyComponentStorage> = Box::new(ts);
        let typed = boxed.as_any().downcast_ref::<TypedStorage<f32>>().unwrap();
        assert_eq!(typed.borrow().get(e), Some(&3.14f32));
    }

    #[test]
    fn test_storage_set_insert_remove() {
        let mut set = StorageSet::new();
        let e = make_entity(0, 0);
        set.insert_component::<i32>(e, 99);
        assert!(set.contains::<i32>(e));
        let val = set.remove_component::<i32>(e);
        assert_eq!(val, Some(99));
        assert!(!set.contains::<i32>(e));
    }

    #[test]
    fn test_component_mask() {
        let mask = ComponentMask::empty()
            .with::<i32>()
            .with::<f32>()
            .with::<String>();
        assert!(mask.has::<i32>());
        assert!(mask.has::<f32>());
        assert!(mask.has::<String>());
        assert!(!mask.has::<u8>());
        assert_eq!(mask.len(), 3);
    }

    #[test]
    fn test_change_ticks() {
        let mut storage: ComponentStorage<i32> = ComponentStorage::new();
        let e = make_entity(0, 0);
        storage.insert(e, 10);
        let tick_after_insert = storage.change_tick();
        storage.get_mut(e).map(|v| *v = 20);
        let tick_after_mut = storage.change_tick();
        assert!(tick_after_mut > tick_after_insert);

        let changed: Vec<_> = storage.iter_changed_since(tick_after_insert).collect();
        assert_eq!(changed.len(), 1);
    }
}