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
use std::{collections::HashSet, ops::Index};
use smallvec::SmallVec;
use super::{
archetype::{Archetype, ArchetypeIndex},
component::{Component, ComponentTypeId},
};
#[derive(Default)]
pub struct GroupDef {
components: Vec<ComponentTypeId>,
}
impl GroupDef {
pub fn new() -> Self {
Self::default()
}
pub fn from_vec(components: Vec<ComponentTypeId>) -> Self {
for (i, component) in components.iter().enumerate() {
assert!(
!components[(i + 1)..].contains(component),
"groups must contain unique components"
);
}
Self { components }
}
pub fn add(&mut self, element: ComponentTypeId) {
assert!(
!self.components.contains(&element),
"groups must contain unique components"
);
self.components.push(element);
}
}
impl From<Vec<ComponentTypeId>> for GroupDef {
fn from(components: Vec<ComponentTypeId>) -> Self {
Self::from_vec(components)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SubGroup(pub usize);
#[doc(hidden)]
#[derive(Debug)]
pub struct Group {
components: SmallVec<[(ComponentTypeId, usize); 5]>,
archetypes: Vec<ArchetypeIndex>,
}
impl Group {
fn new<T: IntoIterator<Item = ComponentTypeId>>(components: T) -> Self {
let components: SmallVec<[(ComponentTypeId, usize); 5]> =
components.into_iter().map(|type_id| (type_id, 0)).collect();
let mut seen = HashSet::new();
for (type_id, _) in &components {
if seen.contains(type_id) {
panic!("groups must contain unique components");
}
seen.insert(*type_id);
}
Self {
components,
archetypes: Vec::new(),
}
}
fn matches(&self, components: &[ComponentTypeId]) -> Option<SubGroup> {
let mut subgroup = None;
for (i, (type_id, _)) in self.components.iter().enumerate() {
if !components.contains(type_id) {
break;
}
subgroup = Some(SubGroup(i));
}
subgroup
}
pub(crate) fn exact_match(&self, components: &[ComponentTypeId]) -> Option<SubGroup> {
let mut subgroup = SubGroup(0);
let mut count = 0;
for (i, (type_id, _)) in self.components.iter().enumerate() {
if !components.contains(type_id) {
break;
}
subgroup = SubGroup(i);
count += 1;
}
if count == components.len() {
Some(subgroup)
} else {
None
}
}
pub(crate) fn components(&'_ self) -> impl Iterator<Item = ComponentTypeId> + '_ {
self.components.iter().map(|(c, _)| *c)
}
pub(crate) fn try_insert(
&mut self,
arch_index: ArchetypeIndex,
archetype: &Archetype,
) -> Option<usize> {
if let Some(SubGroup(subgroup_index)) = self.matches(&archetype.layout().component_types())
{
let (_, group_end) = &mut self.components[subgroup_index];
let index = *group_end;
self.archetypes.insert(index, arch_index);
for (_, separator) in &mut self.components[..(subgroup_index + 1)] {
*separator += 1;
}
Some(index)
} else {
None
}
}
}
impl Index<SubGroup> for Group {
type Output = [ArchetypeIndex];
fn index(&self, SubGroup(index): SubGroup) -> &Self::Output {
let (_, group_separator) = self.components[index];
&self.archetypes[..group_separator]
}
}
impl From<GroupDef> for Group {
fn from(def: GroupDef) -> Self {
Self::new(def.components)
}
}
pub trait GroupSource {
fn to_group() -> GroupDef;
}
macro_rules! group_tuple {
($head_ty:ident) => {
impl_group_tuple!($head_ty);
};
($head_ty:ident, $( $tail_ty:ident ),*) => {
impl_group_tuple!($head_ty, $( $tail_ty ),*);
group_tuple!($( $tail_ty ),*);
};
}
macro_rules! impl_group_tuple {
( $( $ty: ident ),* ) => {
impl<$( $ty: Component ),*> GroupSource for ($( $ty, )*) {
fn to_group() -> GroupDef {
let mut group = GroupDef::new();
$(
group.add(ComponentTypeId::of::<$ty>());
)*
group
}
}
};
}
group_tuple!(A, B, C, D, E, F, G, H, I);