my-ecs 0.1.2

An Entity Component System (ECS) library
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
use super::{ent::entity::EntityIndex, resource::ResourceIndex};
use crate::FxBuildHasher;
use my_utils::ds::{AsDedupVec, DedupVec, GenQueue, OptVec};
use std::{fmt::Debug, hash::BuildHasher, ops::Deref};

/// A struct containing [`WaitQueue`]s for each component, resource, and entity.
///
/// Their indices are the same with the ones from the data containers.
#[derive(Debug)]
pub(crate) struct WaitQueues {
    ent_queues: OptVec<OptVec<WaitQueue, FxBuildHasher>, FxBuildHasher>,
    res_queues: OptVec<WaitQueue, FxBuildHasher>,
    generation: u64,
}

impl WaitQueues {
    const INIT_GEN: u64 = 0;

    pub(super) fn new() -> Self {
        Self {
            ent_queues: OptVec::new(),
            res_queues: OptVec::new(),
            generation: Self::INIT_GEN,
        }
    }

    /// Takes O(n) time.
    #[allow(unused)]
    pub(super) fn is_all_queue_empty(&self) -> bool {
        self.ent_queues
            .pairs()
            .flat_map(|(_, cols)| cols.pairs())
            .all(|(_, col)| col.is_empty())
            && self.res_queues.pairs().all(|(_, col)| col.is_empty())
    }

    pub(super) fn initialize_resource_queue(&mut self, index: usize) {
        self.res_queues.extend_set(index, WaitQueue::new());
    }

    pub(super) fn enqueue(&mut self, wait: &WaitIndices, retry: &mut WaitRetryIndices) -> bool {
        retry.clean = if retry.clean {
            self._enqueue(wait, retry)
        } else {
            self._check_availability(retry)
        };
        retry.clean
    }

    pub(super) fn dequeue(&mut self, wait: &WaitIndices) {
        // Dequeues component read & write requests from their wait queues.
        let wait_read_iter = wait.read.iter().map(|(ei, ci)| (ei.index(), *ci));
        let wait_write_iter = wait.write.iter().map(|(ei, ci)| (ei.index(), *ci));
        dequeue_comp(&mut self.ent_queues, wait_read_iter);
        dequeue_comp(&mut self.ent_queues, wait_write_iter);

        // Dequeues resource read & write requests from their wait queues.
        dequeue_res(
            &mut self.res_queues,
            wait.res_read.iter().map(ResourceIndex::index),
        );
        dequeue_res(
            &mut self.res_queues,
            wait.res_write.iter().map(ResourceIndex::index),
        );

        // Dequeues entity write requests from their wait queues.
        dequeue_ent(
            &mut self.ent_queues,
            wait.ent_write.iter().map(EntityIndex::index),
        );

        // Increases generation.
        self.generation += 1;

        // === Internal helper functions ===

        /// Dequeues an item from component wait queues. The component queues are picked up by
        /// `wait` indices.
        fn dequeue_comp<S, I>(ent_queues: &mut OptVec<OptVec<WaitQueue, S>, S>, wait_iter: I)
        where
            S: BuildHasher,
            I: Iterator<Item = (usize, usize)>,
        {
            for (ei, ci) in wait_iter {
                let comp_queues = ent_queues.get_mut(ei).unwrap();
                let queue = comp_queues.get_mut(ci).unwrap();
                queue.pop();
            }
        }

        /// Dequeues an item from resource wait queues. The component queues are picked up by `wait`
        /// indices.
        fn dequeue_res<S, I>(res_queues: &mut OptVec<WaitQueue, S>, wait_iter: I)
        where
            S: BuildHasher,
            I: Iterator<Item = usize>,
        {
            for resi in wait_iter {
                let queue = res_queues.get_mut(resi).unwrap();
                queue.pop();
            }
        }

        /// Dequeues an item from component wait queues. The component queues are picked up by
        /// `wait` indices.
        fn dequeue_ent<S, I>(ent_queues: &mut OptVec<OptVec<WaitQueue, S>, S>, wait_iter: I)
        where
            S: BuildHasher,
            I: Iterator<Item = usize>,
        {
            for ei in wait_iter {
                let comp_queues = ent_queues.get_mut(ei).unwrap();
                for (_, queue) in comp_queues.pairs_mut() {
                    queue.pop();
                }
            }
        }
    }

    /// Enqueues requests for access authority of components or resources according to `wait`
    /// indices.
    ///
    /// If they're good to be accessed now, returns true. Otherwise, returns false, and then failed
    /// indices will be inserted into `retry`.
    fn _enqueue(&mut self, wait: &WaitIndices, retry: &mut WaitRetryIndices) -> bool {
        let mut res = true;

        // Enqueues component read & write requests into their wait queues.
        let wait_read_iter = wait.read.iter().map(|(ei, ci)| (ei.index(), *ci));
        let wait_write_iter = wait.write.iter().map(|(ei, ci)| (ei.index(), *ci));
        res &= enqueue_comp(
            &mut self.ent_queues,
            RW::Read,
            wait_read_iter,
            &mut retry.read,
        );
        res &= enqueue_comp(
            &mut self.ent_queues,
            RW::Write,
            wait_write_iter,
            &mut retry.write,
        );

        // Enqueues resource read & write requests into their wait queues.
        let wait_res_read_iter = wait.res_read.iter().map(ResourceIndex::index);
        let wait_res_write_iter = wait.res_write.iter().map(ResourceIndex::index);
        res &= enqueue_res(
            &mut self.res_queues,
            RW::Read,
            wait_res_read_iter,
            &mut retry.res_read,
        );
        res &= enqueue_res(
            &mut self.res_queues,
            RW::Write,
            wait_res_write_iter,
            &mut retry.res_write,
        );

        // Enqueues entity write requests into their wait queues.
        res &= enqueue_ent(
            &mut self.ent_queues,
            wait.ent_write.iter().map(EntityIndex::index),
            &mut retry.ent_write,
        );

        return res;

        // === Internal helper functions ===

        /// Enqueues waiting signal into component wait queues according to `wait` indices.
        ///
        /// If all of them are positioned to the front of corresponding queues, returns true, which
        /// means they're allowed to access corresponding components. Otherwise, returns false and
        /// then indices needed to retry later are pushed `retry`.
        ///
        /// # Panics
        ///
        /// Panics if any index in `wait` is out of bounds.
        fn enqueue_comp<S, I>(
            ent_queues: &mut OptVec<OptVec<WaitQueue, S>, S>,
            rw: RW,
            wait_iter: I,
            retry: &mut Vec<(u64, usize, usize)>,
        ) -> bool
        where
            S: BuildHasher,
            I: Iterator<Item = (usize, usize)>,
        {
            debug_assert!(retry.is_empty());

            let mut available = true;
            for (ei, ci) in wait_iter {
                let comp_queues = ent_queues.get_mut(ei).unwrap();
                let queue = comp_queues.get_mut(ci).unwrap();
                let target_gen = queue.push(rw);
                if target_gen != queue.generation() {
                    retry.push((target_gen, ei, ci));
                    available = false;
                }
            }
            available
        }

        /// Enqueues waiting signal into resource wait queues according to `wait` indices.
        ///
        /// If all of them are positioned to the front of corresponding queues, returns true, which
        /// means they're allowed to access corresponding resources. Otherwise, returns false and
        /// then indices needed to retry later are pushed `retry`.
        ///
        /// # Panics
        ///
        /// Panics if any index in `wait` is out of bounds.
        fn enqueue_res<S, I>(
            res_queues: &mut OptVec<WaitQueue, S>,
            rw: RW,
            wait_iter: I,
            retry: &mut Vec<(u64, usize)>,
        ) -> bool
        where
            S: BuildHasher,
            I: Iterator<Item = usize>,
        {
            debug_assert!(retry.is_empty());

            let mut available = true;
            for ri in wait_iter {
                let queue = res_queues.get_mut(ri).unwrap();
                let target_gen = queue.push(rw);
                if target_gen != queue.generation() {
                    retry.push((target_gen, ri));
                    available = false;
                }
            }
            available
        }

        /// Enqueues waiting signal into component wait queues according to `wait` indices.
        ///
        /// If all of them are positioned to the front of corresponding queues, returns true, which
        /// means they're allowed to access corresponding components. Otherwise, returns false and
        /// then indices needed to retry later are pushed `retry`.
        ///
        /// # Panics
        ///
        /// Panics if any index in `wait` is out of bounds.
        fn enqueue_ent<S, I>(
            ent_queues: &mut OptVec<OptVec<WaitQueue, S>, S>,
            wait_iter: I,
            retry: &mut Vec<(u64, usize, usize)>,
        ) -> bool
        where
            S: BuildHasher,
            I: Iterator<Item = usize>,
        {
            debug_assert!(retry.is_empty());

            let mut available = true;
            for ei in wait_iter {
                let comp_queues = ent_queues.get_mut(ei).unwrap();
                for (ci, queue) in comp_queues.pairs_mut() {
                    let target_gen = queue.push(RW::Write);
                    if target_gen != queue.generation() {
                        retry.push((target_gen, ei, ci));
                        available = false;
                    }
                }
            }
            available
        }
    }

    fn _check_availability(&mut self, retry: &mut WaitRetryIndices) -> bool {
        // Checks all requests once again.
        return check_comp(&self.ent_queues, &mut retry.read)
            && check_comp(&self.ent_queues, &mut retry.write)
            && check_res(&self.res_queues, &mut retry.res_read)
            && check_res(&self.res_queues, &mut retry.res_write)
            && check_comp(&self.ent_queues, &mut retry.ent_write);

        // === Internal helper functions ===

        /// Checks component availability according to `retry` indices.
        ///
        /// Passed items in `retry` will be removed, but the failed items will be left, so they'll
        /// be checked again later. If all items are ejected, returns true.
        fn check_comp<S>(
            ent_queues: &OptVec<OptVec<WaitQueue, S>, S>,
            retry: &mut Vec<(u64, usize, usize)>,
        ) -> bool
        where
            S: BuildHasher,
        {
            while let Some((target_gen, ei, ci)) = retry.pop() {
                let comp_queues = ent_queues.get(ei).unwrap();
                let queue = comp_queues.get(ci).unwrap();
                if target_gen != queue.generation() {
                    retry.push((target_gen, ei, ci));
                    return false;
                }
            }
            true
        }

        /// Checks resource availability according to `retry` indices.
        ///
        /// Passed items in `retry` will be removed, but the failed items will be left, so they'll
        /// be checked again later. If all items are ejected, returns true.
        fn check_res<S>(res_queues: &OptVec<WaitQueue, S>, retry: &mut Vec<(u64, usize)>) -> bool
        where
            S: BuildHasher,
        {
            while let Some((target_gen, resi)) = retry.pop() {
                let queue = res_queues.get(resi).unwrap();
                if target_gen != queue.generation() {
                    retry.push((target_gen, resi));
                    return false;
                }
            }
            true
        }
    }

    /// Makes wait queues for an entity.
    ///
    /// This makes a queue at the position `ei`. Also, it makes inner queues for components of the
    /// entity as much as `ncol`. If there was a queue at the position, it will be dropped first.
    pub(super) fn initialize_entity_queue(&mut self, ei: usize, ncol: usize) {
        // Prepares wait queues for columns.
        let mut cols = OptVec::default();
        for _ in 0..ncol {
            cols.push(Some(WaitQueue::new()));
        }
        self.ent_queues.extend_set(ei, cols);
    }
}

impl Default for WaitQueues {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(Debug)]
#[repr(transparent)]
struct WaitQueue(GenQueue<(RW, u32)>);

impl WaitQueue {
    fn new() -> Self {
        Self(GenQueue::new())
    }

    fn push(&mut self, rw: RW) -> u64 {
        if rw == RW::Read {
            if let Some(back) = self.0.back_mut() {
                if back.0 == RW::Read {
                    back.1 += 1;
                    return self.generation() + (self.len() - 1) as u64;
                }
            }
        }
        self.0.push_back((rw, 1));
        self.generation() + (self.len() - 1) as u64
    }

    fn pop(&mut self) {
        let front = self.0.front_mut().unwrap();
        if front.1 > 1 {
            front.1 -= 1;
        } else {
            self.0.pop_front();
        }
    }
}

impl Deref for WaitQueue {
    type Target = GenQueue<(RW, u32)>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

/// Read or write
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
pub(super) enum RW {
    Read,
    Write,
}

#[derive(Debug)]
pub(super) struct WaitIndices {
    /// Wait queue index pairs to read-only components.
    ///
    /// Each pair is the same as entity container index and a specific column index.
    pub(super) read: DedupVec<(EntityIndex, usize), false>,

    /// Wait queue index pairs to writable components.
    ///
    /// Each pair is the same as entity container index and a specific column index.
    pub(super) write: DedupVec<(EntityIndex, usize), false>,

    /// Wait queue indices to read-only resources.
    pub(super) res_read: DedupVec<ResourceIndex, false>,

    /// Wait queue indices to writable resources.
    pub(super) res_write: DedupVec<ResourceIndex, false>,

    /// Wait queue indices to writable entity container.
    ///
    /// Each index is the same as entity container index.
    pub(super) ent_write: DedupVec<EntityIndex, false>,
}

#[derive(Debug)]
pub(super) struct WaitRetryIndices {
    /// Not ready [`WaitIndices::read`] and their target generations.
    pub(super) read: Vec<(u64, usize, usize)>,

    /// Not ready [`WaitIndices::write`] and their target generations.
    pub(super) write: Vec<(u64, usize, usize)>,

    /// Not ready [`WaitIndices::res_read`] and their target generations.
    pub(super) res_read: Vec<(u64, usize)>,

    /// Not ready [`WaitIndices::res_write`] and their target generations.
    pub(super) res_write: Vec<(u64, usize)>,

    /// Not ready [`WaitIndices::ent_write`] and their target generations.
    pub(super) ent_write: Vec<(u64, usize, usize)>,

    /// If it's not clean, that means it's in the middle of availability check. So we can continue
    /// the check rather than doing it from the beginning.
    pub(super) clean: bool,
}

impl WaitRetryIndices {
    pub(super) const fn new() -> Self {
        Self {
            read: Vec::new(),
            write: Vec::new(),
            res_read: Vec::new(),
            res_write: Vec::new(),
            ent_write: Vec::new(),
            clean: true,
        }
    }
}