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
use std::marker;
use crate::{
entity::{EntitiesStorage, Entity},
storage::{IntoSendStorage, FastIndexExt, ReadOnlyStorage}
};
use super::{DataAccesses, FromComponent, SafeIter, UnorderedData};
use crate::storage::StorageRef;
use crate::bitmask::{Bitmask, MaskType};
use std::ops::Deref;
#[cfg(feature = "parallel_iter")]
use rayon::prelude::*;
#[cfg(feature = "parallel_iter")]
use rayon::iter::plumbing::UnindexedConsumer;
use std::usize;
use std::any::TypeId;
#[cfg(components_64)]
use num_traits::Zero;
pub struct HasOption<T: 'static> {
has: bool,
_marker: marker::PhantomData<T>
}
impl<'a, T: 'static> FromComponent<'a, bool> for HasOption<T> {
fn from_component(has: bool) -> HasOption<T>{
HasOption{
has,
_marker: marker::PhantomData,
}
}
}
impl<T: 'static> Deref for HasOption<T> {
type Target = bool;
fn deref(&self) -> &bool{
&self.has
}
}
pub struct IterHasOption<'a, T> {
ptr: *const (Entity, MaskType),
end: *const (Entity, MaskType),
storage: StorageHasOption<'a, T>,
}
impl<'a, T> Iterator for IterHasOption<'a, T>
where T: 'static,
{
type Item = bool;
#[inline]
fn next(&mut self) -> Option<bool>{
while self.ptr != self.end {
unsafe {
let e = &(*self.ptr).0;
self.ptr = self.ptr.add(1);
if e.guid != usize::MAX {
return Some(self.storage.get_unchecked(e.guid));
}
}
}
None
}
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.end as usize - self.ptr as usize;
(0, Some(len))
}
}
pub struct StorageHasOption<'a, T> {
entities_masks: &'a [(Entity, MaskType)],
component_mask: MaskType,
_marker_t: marker::PhantomData<T>,
}
impl<'a, 'r, T> FastIndexExt for StorageHasOption<'a, T>
where T: 'static,
{
type FastIndex = bool;
type StaticTypeId = HasOption<T>;
fn fast_index(&self, guid: usize) -> bool {
self.entities_masks[guid].1.clone() & self.component_mask.clone() != MaskType::zero()
}
}
impl<'a, 'r, T> StorageRef<'r> for StorageHasOption<'a, T>
where T: 'static,
{
type Data = HasOption<T>;
type Component = bool;
unsafe fn get_fast_unchecked(&mut self, idx: bool) -> bool{
idx
}
unsafe fn get_unchecked(&mut self, guid: usize) -> bool{
self.entities_masks[guid].1.clone() & self.component_mask.clone() != MaskType::zero()
}
fn contains(&self, _guid: usize) -> bool{
true
}
}
impl<'a, 'r, T: 'static> IntoSendStorage<'r> for StorageHasOption<'a, T> {
type SendStorage = StorageHasOption<'a, T>;
fn into_send_storage(&'r mut self) -> Self::SendStorage {
StorageHasOption{
entities_masks: self.entities_masks,
component_mask: self.component_mask.clone(),
_marker_t: marker::PhantomData,
}
}
}
unsafe impl<'a, T> ReadOnlyStorage for StorageHasOption<'a, T>{}
impl<'a, T: 'static> StorageHasOption<'a, T> {
pub fn iter<'i>(&'i mut self) -> IterHasOption<'a, T> {
IterHasOption{
ptr: self.entities_masks.as_ptr(),
end: unsafe{ self.entities_masks.as_ptr().add(self.entities_masks.len()) },
storage: self.into_send_storage(),
}
}
}
#[cfg(feature = "parallel_iter")]
impl<'a, T> StorageHasOption<'a, T> {
pub fn par_iter<'i>(&'i mut self) -> HasOptionParIter<'i> {
HasOptionParIter::new(self.entities_masks, self.component_mask.clone())
}
}
unsafe impl<T> SafeIter<'_> for HasOption<T> {}
impl<'a, T: 'static> UnorderedData<'a> for HasOption<T> {
type Iter = IterHasOption<'a, T>;
type IterMut = IterHasOption<'a, T>;
type Components = T;
type ComponentsRef = bool;
type Storage = StorageHasOption<'a, T>;
fn query_mask<E: EntitiesStorage>(_: &E) -> Bitmask {
crate::Bitmask::all()
}
fn into_iter<E: EntitiesStorage>(entities: &'a E) -> Self::Iter {
let storage = <Self as UnorderedData>::storage(entities).unwrap();
IterHasOption {
ptr: storage.entities_masks.as_ptr(),
end: unsafe{ storage.entities_masks.as_ptr().add(storage.entities_masks.len()) },
storage,
}
}
fn into_iter_mut<E: EntitiesStorage>(entities: &'a E) -> Self::Iter {
Self::into_iter(entities)
}
fn storage<E: EntitiesStorage>(entities: &'a E) -> Option<Self::Storage> {
let entities_masks = entities.entities_ref();
let component_mask = entities.component_mask::<T>();
Some(StorageHasOption{
entities_masks,
component_mask,
_marker_t: marker::PhantomData,
})
}
}
impl<T: 'static> DataAccesses for HasOption<T> {
fn reads() -> Vec<TypeId>{ vec![] }
fn writes() -> Vec<TypeId>{ vec![] }
}
#[cfg(feature = "parallel_iter")]
pub struct HasOptionParIter<'a>{
entities_masks: &'a [(Entity, MaskType)],
component_mask: MaskType,
}
#[cfg(feature = "parallel_iter")]
impl<'a> HasOptionParIter<'a>{
pub fn new(entities_masks: &'a [(Entity, MaskType)], component_mask: MaskType) -> HasOptionParIter<'a>{
HasOptionParIter{
entities_masks,
component_mask,
}
}
}
#[cfg(feature = "parallel_iter")]
impl<'a> ParallelIterator for HasOptionParIter<'a> {
type Item = bool;
#[inline]
fn drive_unindexed<C>(self, consumer: C) -> C::Result
where C: UnindexedConsumer<Self::Item>
{
let component_mask = self.component_mask.clone();
self.entities_masks.par_iter()
.map(move |(_,mask)| mask & component_mask.clone() != MaskType::zero())
.drive_unindexed(consumer)
}
}