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
use crate::context::AudioNodeId;
use crate::render::graph::Node;

use std::cell::RefCell;
use std::ops::{Index, IndexMut};

pub(crate) struct NodeCollection {
    nodes: Vec<Option<RefCell<Node>>>,
}

impl NodeCollection {
    pub fn new() -> Self {
        let mut instance = Self {
            nodes: Vec::with_capacity(64),
        };
        instance.ensure_capacity(64);
        instance
    }

    pub fn is_empty(&self) -> bool {
        self.nodes.is_empty()
    }

    #[inline(always)]
    fn ensure_capacity(&mut self, new_len: usize) {
        self.nodes
            .resize_with(new_len.max(self.nodes.len()), || None);
    }

    #[inline(always)]
    pub fn insert(&mut self, index: AudioNodeId, value: RefCell<Node>) {
        let index = index.0 as usize;
        self.ensure_capacity(index + 1);
        self.nodes[index] = Some(value);
    }

    #[inline(always)]
    pub fn remove(&mut self, index: AudioNodeId) -> RefCell<Node> {
        self.nodes[index.0 as usize]
            .take()
            .expect("Unable to remove non-existing Node in NodeCollection")
    }

    #[inline(always)]
    pub fn keys(&self) -> impl Iterator<Item = AudioNodeId> + '_ {
        self.nodes
            .iter()
            .enumerate()
            .filter_map(|(i, v)| v.as_ref().and(Some(AudioNodeId(i as u64))))
    }

    #[inline(always)]
    pub fn values_mut(&mut self) -> impl Iterator<Item = &mut RefCell<Node>> {
        self.nodes.iter_mut().filter_map(Option::as_mut)
    }

    #[inline(always)]
    pub fn get(&self, index: AudioNodeId) -> Option<&RefCell<Node>> {
        self.nodes[index.0 as usize].as_ref()
    }

    #[inline(always)]
    pub fn get_mut(&mut self, index: AudioNodeId) -> Option<&mut RefCell<Node>> {
        self.nodes[index.0 as usize].as_mut()
    }

    #[inline(always)]
    pub fn retain<F>(&mut self, mut f: F)
    where
        F: FnMut(AudioNodeId, &mut RefCell<Node>) -> bool,
    {
        self.nodes.iter_mut().enumerate().for_each(|(i, opt)| {
            if let Some(v) = opt.as_mut() {
                if !f(AudioNodeId(i as u64), v) {
                    *opt = None;
                }
            }
        })
    }
}

impl Index<AudioNodeId> for NodeCollection {
    type Output = RefCell<Node>;

    #[track_caller]
    #[inline(always)]
    fn index(&self, index: AudioNodeId) -> &Self::Output {
        self.nodes
            .get(index.0 as usize)
            .unwrap_or_else(|| panic!("Unexpected index {} for NodeCollection", index.0))
            .as_ref()
            .unwrap_or_else(|| panic!("Index {} for dropped Node in NodeCollection", index.0))
    }
}

impl IndexMut<AudioNodeId> for NodeCollection {
    #[track_caller]
    #[inline(always)]
    fn index_mut(&mut self, index: AudioNodeId) -> &mut Self::Output {
        self.nodes
            .get_mut(index.0 as usize)
            .unwrap_or_else(|| panic!("Unexpected index {} for NodeCollection", index.0))
            .as_mut()
            .unwrap_or_else(|| panic!("Index {} for dropped Node in NodeCollection", index.0))
    }
}