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
use crate::{
buffer::Buffer,
node::Input,
node::Node,
BoxedNode,
};
use hashbrown::HashMap;
use petgraph::data::{DataMap, DataMapMut};
use petgraph::visit::{
Data, DfsPostOrder, GraphBase, IntoNeighborsDirected, Reversed, Visitable,
};
use petgraph::{Incoming};
pub struct Processor<G, const N: usize>
where
G: Visitable,
{
dfs_post_order: DfsPostOrder<G::NodeId, G::Map>,
inputs: HashMap<usize, Input<N>>,
}
pub struct NodeData<T: ?Sized, const N: usize> {
pub buffers: Vec<Buffer<N>>,
pub node: T,
}
impl<G, const N: usize> Processor<G, N>
where
G: Visitable + petgraph::visit::NodeIndexable,
{
pub fn with_capacity(max_nodes: usize) -> Self
where
G::Map: Default,
{
let mut dfs_post_order = DfsPostOrder::default();
dfs_post_order.stack = Vec::with_capacity(max_nodes);
let inputs = HashMap::new(); Self {
dfs_post_order,
inputs,
}
}
pub fn process<T>(&mut self, graph: &mut G, node: G::NodeId)
where
G: Data<NodeWeight = NodeData<T, N>> + DataMapMut,
for<'a> &'a G: GraphBase<NodeId = G::NodeId> + IntoNeighborsDirected,
T: Node<N>,
{
process(self, graph, node)
}
}
impl<T, const N: usize> NodeData<T, N> {
pub fn new(node: T, buffers: Vec<Buffer<N>>) -> Self {
NodeData { node, buffers }
}
pub fn new1(node: T) -> Self {
Self::new(node, vec![Buffer::SILENT])
}
pub fn new2(node: T) -> Self {
Self::new(node, vec![Buffer::SILENT; 2])
}
pub fn multi_chan_node(chan: usize, node: T) -> Self {
Self::new(node, vec![Buffer::SILENT; chan])
}
}
#[cfg(feature = "node-boxed")]
impl<const N: usize> NodeData<BoxedNode<N>, N> {
pub fn boxed<T>(node: T, buffers: Vec<Buffer<N>>) -> Self
where
T: 'static + Node<N>,
{
NodeData::new(BoxedNode(Box::new(node)), buffers)
}
pub fn boxed1<T>(node: T) -> Self
where
T: 'static + Node<N>,
{
Self::boxed(node, vec![Buffer::SILENT])
}
pub fn boxed2<T>(node: T) -> Self
where
T: 'static + Node<N>,
{
Self::boxed(node, vec![Buffer::SILENT, Buffer::SILENT])
}
}
pub fn process<G, T, const N: usize>(
processor: &mut Processor<G, N>,
graph: &mut G,
node: G::NodeId,
) where
G: Data<NodeWeight = NodeData<T, N>> + DataMapMut + Visitable + petgraph::visit::NodeIndexable,
for<'a> &'a G: GraphBase<NodeId = G::NodeId> + IntoNeighborsDirected,
T: Node<N>,
{
const NO_NODE: &str = "no node exists for the given index";
processor.dfs_post_order.reset(Reversed(&*graph));
processor.dfs_post_order.move_to(node);
while let Some(n) = processor.dfs_post_order.next(Reversed(&*graph)) {
let data: *mut NodeData<T, N> = graph.node_weight_mut(n).expect(NO_NODE) as *mut _;
processor.inputs.clear();
for in_n in (&*graph).neighbors_directed(n, Incoming) {
if n == in_n {
continue;
}
let input_container = graph.node_weight(in_n).expect(NO_NODE);
let input = Input::new(&input_container.buffers, (&*graph).to_index(in_n));
processor.inputs.insert((&*graph).to_index(in_n), input);
}
unsafe {
(*data)
.node
.process(&mut processor.inputs, &mut (*data).buffers);
}
}
}