Skip to main content

ixa/entity/index/
mod.rs

1//! Index types for property-value lookups.
2
3use crate::entity::{Entity, EntityId};
4use crate::hashing::IndexSet;
5use crate::prelude::{IndexableProperty, Property};
6
7mod full_index;
8mod value_count_index;
9
10pub use full_index::*;
11pub use value_count_index::*;
12
13#[derive(Debug)]
14pub enum IndexSetResult<'a, E: Entity> {
15    /// The index type cannot satisfy the query.
16    Unsupported,
17    /// The set is empty.
18    Empty,
19    /// A reference to the index set.
20    Set(&'a IndexSet<EntityId<E>>),
21}
22
23#[derive(PartialEq, Eq, Debug)]
24pub enum IndexCountResult {
25    /// The index type cannot satisfy the query.
26    Unsupported,
27    /// The count of entities.
28    Count(usize),
29}
30
31#[derive(Debug, Copy, Clone, PartialEq, Eq)]
32pub enum PropertyIndexType {
33    Unindexed,
34    FullIndex,
35    ValueCountIndex,
36}
37
38impl PropertyIndexType {
39    /// Constructs an unattached empty index of this type. Returns `None` only for `Unindexed`.
40    pub(crate) fn new_property_index<E, P>(self) -> Option<Box<dyn PropertyIndex<E, P>>>
41    where
42        E: Entity,
43        P: IndexableProperty<E>,
44    {
45        match self {
46            Self::Unindexed => None,
47            Self::FullIndex => Some(Box::new(FullIndex::<E, P>::new())),
48            Self::ValueCountIndex => Some(Box::new(ValueCountIndex::<E, P>::new())),
49        }
50    }
51}
52
53pub trait PropertyIndex<E: Entity, P: Property<E>> {
54    #[must_use]
55    fn index_type(&self) -> PropertyIndexType;
56
57    #[must_use]
58    fn get_index_set_result(&self, value: &P) -> IndexSetResult<'_, E>;
59
60    #[must_use]
61    fn get_index_count_result(&self, value: &P) -> IndexCountResult;
62
63    fn remove_entity(&mut self, value: &P, entity_id: EntityId<E>);
64
65    fn add_entity(&mut self, value: &P, entity_id: EntityId<E>);
66}