sound_flow/graph/
compiled_graph.rs

1use crate::graph::Instruction;
2use crate::{Node, Sockets};
3use std::collections::HashMap;
4use std::hash::Hash;
5
6#[derive(Debug, Clone)]
7pub struct CompiledGraph<Id: Hash + Eq + Copy, N: Node> {
8    pub(crate) instructions: Vec<Instruction<N>>,
9    pub(crate) buffers: Vec<N::Data>,
10    pub(crate) lookup: HashMap<Id, usize>,
11}
12
13#[derive(Copy, Clone, Debug)]
14pub struct NodeIndex(usize);
15
16impl<Id: Hash + Eq + Copy, N: Node> CompiledGraph<Id, N> {
17    pub fn rt_transfer_state(&mut self, _source: Self) {
18        todo!()
19    }
20
21    pub fn rt_compute(&mut self, context: &N::Context) {
22        for instruction in self.instructions.iter_mut() {
23            let sockets = Sockets {
24                socket_inputs: &*instruction.socket_inputs,
25                socket_outputs: &*instruction.socket_outputs,
26                buffers: &mut *self.buffers,
27            };
28            instruction.node.rt_process(context, sockets)
29        }
30    }
31
32    pub fn node_index(&self, id: Id) -> Option<NodeIndex> {
33        self.lookup.get(&id).map(|&i| NodeIndex(i))
34    }
35
36    pub fn node(&self, index: NodeIndex) -> &N {
37        &self.instructions[index.0].node
38    }
39
40    pub fn node_mut(&mut self, index: NodeIndex) -> &mut N {
41        &mut self.instructions[index.0].node
42    }
43}