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
use std::ops::{Deref, DerefMut};
use std::collections::hash_map;
use std::collections::hash_map::{Values, ValuesMut};
use std::slice::{Iter, IterMut};
use std::any::Any;
use fnv::FnvHashMap;
use entities::*;
use bitset::*;
use join::*;
pub trait Resource : Any + Send + Sync { }
impl Resource {
#[inline]
pub unsafe fn downcast_ref_unsafe<T>(&self) -> &T {
&*(self as *const Self as *const T)
}
#[inline]
pub unsafe fn downcast_mut_unsafe<T>(&mut self) -> &mut T {
&mut *(self as *mut Self as *mut T)
}
}
pub trait StoresEntityData : Send + Sync {
fn clear(&mut self, &[Entity]);
}
impl StoresEntityData {
#[inline]
pub unsafe fn downcast_ref_unsafe<T>(&self) -> &T {
&*(self as *const Self as *const T)
}
#[inline]
pub unsafe fn downcast_mut_unsafe<T>(&mut self) -> &mut T {
&mut *(self as *mut Self as *mut T)
}
}
pub trait EntityResource : Resource + StoresEntityData {
type Api;
fn deconstruct(&self) -> (&BitSet, &Self::Api);
fn deconstruct_mut(&mut self) -> (&BitSet, &mut Self::Api);
}
macro_rules! impl_iter_entities {
($name:ident [$($read:ident),*] [$($write:ident),*] [$iter:ty]) => (
#[allow(non_snake_case)]
pub fn $name<'a, $($read,)* $($write,)* F, R>($($read: &$read,)* $($write: &mut $write,)* f: F) -> R
where $($read:EntityResource,)*
$($write:EntityResource,)*
F: FnOnce($iter, $(&$read::Api,)* $(&mut $write::Api,)*) -> R + 'a
{
$(let $read = $read.deconstruct();)*
$(let $write = $write.deconstruct_mut();)*
let iter = ($($read.0,)* $($write.0,)*).and().iter();
f(iter, $($read.1,)* $($write.1,)*)
}
)
}
impl_iter_entities!(iter_entities_r0w1 [] [W0] [BitIter<&BitSet>]);
impl_iter_entities!(iter_entities_r0w2 [] [W0, W1] [BitIter<BitSetAnd<&BitSet, &BitSet>>]);
impl_iter_entities!(iter_entities_r0w3 [] [W0, W1, W2] [BitIter<BitSetAnd<&BitSet, BitSetAnd<&BitSet, &BitSet>>>]);
impl_iter_entities!(iter_entities_r1w0 [R0] [] [BitIter<&BitSet>]);
impl_iter_entities!(iter_entities_r1w1 [R0] [W0] [BitIter<BitSetAnd<&BitSet, &BitSet>>]);
impl_iter_entities!(iter_entities_r1w2 [R0] [W0, W1] [BitIter<BitSetAnd<&BitSet, BitSetAnd<&BitSet, &BitSet>>>]);
impl_iter_entities!(iter_entities_r1w3 [R0] [W0, W1, W3] [BitIter<BitSetAnd<BitSetAnd<&BitSet, &BitSet>, BitSetAnd<&BitSet, &BitSet>>>]);
impl_iter_entities!(iter_entities_r2w0 [R0, R1] [] [BitIter<BitSetAnd<&BitSet, &BitSet>>]);
impl_iter_entities!(iter_entities_r2w1 [R0, R1] [W0] [BitIter<BitSetAnd<&BitSet, BitSetAnd<&BitSet, &BitSet>>>]);
impl_iter_entities!(iter_entities_r2w2 [R0, R1] [W0, W1] [BitIter<BitSetAnd<BitSetAnd<&BitSet, &BitSet>, BitSetAnd<&BitSet, &BitSet>>>]);
impl_iter_entities!(iter_entities_r2w3 [R0, R1] [W0, W1, W3] [BitIter<BitSetAnd<BitSetAnd<&BitSet, &BitSet>, BitSetAnd<&BitSet, BitSetAnd<&BitSet, &BitSet>>>>]);
impl_iter_entities!(iter_entities_r3w0 [R0, R1, R2] [] [BitIter<BitSetAnd<&BitSet, BitSetAnd<&BitSet, &BitSet>>>]);
impl_iter_entities!(iter_entities_r3w1 [R0, R1, R2] [W1] [BitIter<BitSetAnd<BitSetAnd<&BitSet, &BitSet>, BitSetAnd<&BitSet, &BitSet>>>]);
impl_iter_entities!(iter_entities_r3w2 [R0, R1, R2] [W1, W2] [BitIter<BitSetAnd<BitSetAnd<&BitSet, &BitSet>, BitSetAnd<&BitSet, BitSetAnd<&BitSet, &BitSet>>>>]);
impl_iter_entities!(iter_entities_r3w3 [R0, R1, R2] [W1, W2, W3] [BitIter<BitSetAnd<BitSetAnd<&BitSet, BitSetAnd<&BitSet, &BitSet>>, BitSetAnd<&BitSet, BitSetAnd<&BitSet, &BitSet>>>>]);
impl_iter_entities!(iter_entities_r4w0 [R0, R1, R2, R3] [] [BitIter<BitSetAnd<BitSetAnd<&BitSet, &BitSet>, BitSetAnd<&BitSet, &BitSet>>>]);
impl_iter_entities!(iter_entities_r4w1 [R0, R1, R2, R3] [W1] [BitIter<BitSetAnd<BitSetAnd<&BitSet, &BitSet>, BitSetAnd<&BitSet, BitSetAnd<&BitSet, &BitSet>>>>]);
impl_iter_entities!(iter_entities_r4w2 [R0, R1, R2, R3] [W1, W2] [BitIter<BitSetAnd<BitSetAnd<&BitSet, BitSetAnd<&BitSet, &BitSet>>, BitSetAnd<&BitSet, BitSetAnd<&BitSet, &BitSet>>>>]);
impl_iter_entities!(iter_entities_r4w3 [R0, R1, R2, R3] [W1, W2, W3] [BitIter<BitSetAnd<BitSetAnd<&BitSet, BitSetAnd<&BitSet, &BitSet>>, BitSetAnd<BitSetAnd<&BitSet, &BitSet>, BitSetAnd<&BitSet, &BitSet>>>>]);
pub struct MapResource<T: Any + Send + Sync> {
filter: BitSet,
storage: MapStorage<T>
}
impl<T: Any + Send + Sync> MapResource<T> {
pub fn new() -> MapResource<T> {
MapResource {
filter: BitSet::new(),
storage: MapStorage::new()
}
}
pub fn add(&mut self, entity: Entity, component: T) {
self.storage.m.insert(entity, component);
self.filter.add(entity.index());
}
pub fn remove(&mut self, entity: Entity) -> Option<T> {
let component = self.storage.m.remove(&entity);
if component.is_some() {
self.filter.remove(entity.index());
}
return component;
}
}
impl<T: Any + Send + Sync> Resource for MapResource<T> { }
impl<T: Any + Send + Sync> EntityResource for MapResource<T> {
type Api = MapStorage<T>;
fn deconstruct(&self) -> (&BitSet, &MapStorage<T>) {
(&self.filter, &self.storage)
}
fn deconstruct_mut(&mut self) -> (&BitSet, &mut MapStorage<T>) {
(&self.filter, &mut self.storage)
}
}
impl<T: Any + Send + Sync> StoresEntityData for MapResource<T> {
fn clear(&mut self, entities: &[Entity]) {
for entity in entities {
self.remove(*entity);
}
}
}
impl<T: Any + Send + Sync> Deref for MapResource<T> {
type Target = MapStorage<T>;
fn deref(&self) -> &MapStorage<T> {
&self.storage
}
}
impl<T: Any + Send + Sync> DerefMut for MapResource<T> {
fn deref_mut(&mut self) -> &mut MapStorage<T> {
&mut self.storage
}
}
pub struct MapStorage<T> {
m: FnvHashMap<Entity, T>
}
impl<T> MapStorage<T> {
fn new() -> MapStorage<T> {
MapStorage {
m: FnvHashMap::default()
}
}
pub fn get(&self, entity: Entity) -> Option<&T> {
self.m.get(&entity)
}
pub fn get_mut(&mut self, entity: Entity) -> Option<&mut T> {
self.m.get_mut(&entity)
}
pub fn components(&self) -> Values<Entity, T> {
self.m.values()
}
pub fn components_mut(&mut self) -> ValuesMut<Entity, T> {
self.m.values_mut()
}
pub fn iter(&self) -> hash_map::Iter<Entity, T> {
self.m.iter()
}
pub fn iter_mut(&mut self) -> hash_map::IterMut<Entity, T> {
self.m.iter_mut()
}
}
pub struct VecResource<T: Any + Send + Sync> {
filter: BitSet,
storage: VecStorage<T>
}
impl<T: Any + Send + Sync> Resource for VecResource<T> { }
impl<T: Any + Send + Sync> VecResource<T> {
pub fn new() -> VecResource<T> {
VecResource {
filter: BitSet::new(),
storage: VecStorage::new()
}
}
pub fn add(&mut self, entity: Entity, component: T) {
use std::ptr;
let index = entity.index() as usize;
if self.storage.v.len() <= index {
let additional = index - self.storage.v.len() + 1;
self.storage.v.reserve(additional);
unsafe { self.storage.v.set_len(index + 1); }
self.storage.g.reserve(additional);
for _ in 0..additional {
self.storage.g.push(None);
}
}
if self.storage.g[index] != None {
panic!("VecResource already contains entity data for index {}", index);
}
unsafe { ptr::write(self.storage.v.get_unchecked_mut(index), component); }
self.storage.g[index] = Some(entity.generation());
self.filter.add(entity.index());
}
pub fn remove(&mut self, entity: Entity) -> Option<T> {
use std::ptr;
let index = entity.index() as usize;
if self.storage.v.len() <= index || self.storage.g[index] != Some(entity.generation()) {
return None;
}
self.filter.remove(entity.index());
self.storage.g[index] = None;
Some(unsafe {ptr::read(&self.storage.v[index]) })
}
}
impl<T: Any + Send + Sync> EntityResource for VecResource<T> {
type Api = VecStorage<T>;
fn deconstruct(&self) -> (&BitSet, &VecStorage<T>) {
(&self.filter, &self.storage)
}
fn deconstruct_mut(&mut self) -> (&BitSet, &mut VecStorage<T>) {
(&self.filter, &mut self.storage)
}
}
impl<T: Any + Send + Sync> StoresEntityData for VecResource<T> {
fn clear(&mut self, entities: &[Entity]) {
for entity in entities {
self.remove(*entity);
}
}
}
impl<T: Any + Send + Sync> Deref for VecResource<T> {
type Target = VecStorage<T>;
fn deref(&self) -> &VecStorage<T> {
&self.storage
}
}
impl<T: Any + Send + Sync> DerefMut for VecResource<T> {
fn deref_mut(&mut self) -> &mut VecStorage<T> {
&mut self.storage
}
}
pub struct VecStorage<T> {
v: Vec<T>,
g: Vec<Option<Generation>>
}
impl<T> VecStorage<T> {
fn new() -> VecStorage<T> {
VecStorage {
v: Vec::new(),
g: Vec::new()
}
}
pub fn get(&self, entity: Entity) -> Option<&T> {
let index = entity.index() as usize;
if self.g.len() > index && self.g[index] == Some(entity.generation()) {
return Some(&self.v[index]);
}
None
}
#[inline]
pub unsafe fn get_unchecked(&self, index: Index) -> &T {
self.v.get_unchecked(index as usize)
}
pub fn get_mut(&mut self, entity: Entity) -> Option<&mut T> {
let index = entity.index() as usize;
if self.g.len() > index && self.g[index] == Some(entity.generation()) {
return Some(&mut self.v[index]);
}
None
}
#[inline]
pub unsafe fn get_unchecked_mut(&mut self, index: Index) -> &mut T {
self.v.get_unchecked_mut(index as usize)
}
pub fn iter(&self) -> VecStorageIter<T> {
VecStorageIter {
i: 0,
iter: self.v.iter(),
g: &self.g
}
}
pub fn iter_mut(&mut self) -> VecStorageIterMut<T> {
VecStorageIterMut {
i: 0,
iter: self.v.iter_mut(),
g: &self.g
}
}
}
pub struct VecStorageIter<'a, T: 'a> {
i: usize,
iter: Iter<'a, T>,
g: &'a [Option<Generation>]
}
impl<'a, T> Iterator for VecStorageIter<'a, T> {
type Item = (Entity, &'a T);
#[inline]
fn next(&mut self) -> Option<(Entity, &'a T)> {
for x in self.iter.by_ref() {
let index = self.i;
self.i = self.i + 1;
if let Some(gen) = self.g[index] {
let entity = Entity::new(index as Index, gen);
return Some((entity, x));
}
}
None
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(0, Some(self.g.len()))
}
}
pub struct VecStorageIterMut<'a, T: 'a> {
i: usize,
iter: IterMut<'a, T>,
g: &'a [Option<Generation>]
}
impl<'a, T> Iterator for VecStorageIterMut<'a, T> {
type Item = (Entity, &'a mut T);
#[inline]
fn next(&mut self) -> Option<(Entity, &'a mut T)> {
for x in self.iter.by_ref() {
let index = self.i;
self.i = self.i + 1;
if let Some(gen) = self.g[index] {
let entity = Entity::new(index as Index, gen);
return Some((entity, x));
}
}
None
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(0, Some(self.g.len()))
}
}
#[cfg(test)]
mod tests {
use super::*;
use entities::*;
#[test]
fn map_deconstruct() {
let map = &mut MapResource::<u32>::new();
let entity = Entity::new(1, 0);
map.add(entity, 5u32);
assert_eq!(*map.get(entity).unwrap(), 5u32);
let (bitset, api) = map.deconstruct();
assert!(bitset.contains(entity.index()));
assert_eq!(*api.get(entity).unwrap(), 5u32);
}
#[test]
fn map_deconstruct_mut() {
let map = &mut MapResource::<u32>::new();
let entity = Entity::new(1, 0);
map.add(entity, 5u32);
assert_eq!(*map.get(entity).unwrap(), 5u32);
let (bitset, api) = map.deconstruct_mut();
assert!(bitset.contains(entity.index()));
assert_eq!(*api.get(entity).unwrap(), 5u32);
*api.get_mut(entity).unwrap() = 6u32;
assert_eq!(*api.get(entity).unwrap(), 6u32);
}
#[test]
fn vec_iter() {
let a = Entity::new(1, 0);
let b = Entity::new(2, 0);
let c = Entity::new(4, 0);
let vec = &mut VecResource::<u32>::new();
vec.add(a, 1);
vec.add(b, 2);
vec.add(c, 3);
let mut iter = vec.iter();
assert_eq!(iter.next(), Some((a, &1u32)));
assert_eq!(iter.next(), Some((b, &2u32)));
assert_eq!(iter.next(), Some((c, &3u32)));
}
#[test]
fn vec_iter_mut() {
let a = Entity::new(1, 0);
let b = Entity::new(2, 0);
let c = Entity::new(4, 0);
let vec = &mut VecResource::<u32>::new();
vec.add(a, 1);
vec.add(b, 2);
vec.add(c, 3);
let mut iter = vec.iter_mut();
assert_eq!(iter.next(), Some((a, &mut 1u32)));
assert_eq!(iter.next(), Some((b, &mut 2u32)));
assert_eq!(iter.next(), Some((c, &mut 3u32)));
}
#[test]
fn run_iter_entities_r1w1() {
let a = Entity::new(1, 0);
let b = Entity::new(2, 0);
let c = Entity::new(4, 0);
let vec = &mut VecResource::<u32>::new();
vec.add(a, 1);
vec.add(b, 2);
vec.add(c, 3);
let map = &mut MapResource::<u32>::new();
map.add(a, 1);
map.add(c, 4);
iter_entities_r1w1(map, vec, |iter, m, v| {
for e in iter {
let x = unsafe { v.get_unchecked_mut(e) };
*x += *m.get(Entity::new(e, 0)).unwrap();
}
});
assert_eq!(vec.get(a), Some(&2u32));
assert_eq!(vec.get(b), Some(&2u32));
assert_eq!(vec.get(c), Some(&7u32));
}
}