1
1
use super :: * ;
2
2
3
- impl < NodeData , DataType , ValueType > Graph < NodeData , DataType , ValueType > {
3
+ impl < NodeData , DataType , ValueType > Graph < NodeData , DataType , ValueType >
4
+ where
5
+ DataType : DataTypeTrait ,
6
+ {
4
7
pub fn new ( ) -> Self {
5
8
Self {
6
9
nodes : SlotMap :: default ( ) ,
7
10
inputs : SlotMap :: default ( ) ,
8
11
outputs : SlotMap :: default ( ) ,
9
- connections : SecondaryMap :: default ( ) ,
12
+ incoming : SecondaryMap :: default ( ) ,
13
+ outgoing : SecondaryMap :: default ( ) ,
10
14
}
11
15
}
12
16
@@ -64,37 +68,90 @@ impl<NodeData, DataType, ValueType> Graph<NodeData, DataType, ValueType> {
64
68
}
65
69
66
70
pub fn remove_node ( & mut self , node_id : NodeId ) {
67
- self . connections
68
- . retain ( |i, o| !( self . outputs [ * o] . node == node_id || self . inputs [ i] . node == node_id) ) ;
69
71
let inputs: SVec < _ > = self [ node_id] . input_ids ( ) . collect ( ) ;
70
72
for input in inputs {
71
- self . inputs . remove ( input) ;
73
+ self . remove_incoming_connections ( input) ;
72
74
}
73
75
let outputs: SVec < _ > = self [ node_id] . output_ids ( ) . collect ( ) ;
74
76
for output in outputs {
75
- self . outputs . remove ( output) ;
77
+ self . remove_outgoing_connections ( output) ;
76
78
}
77
79
self . nodes . remove ( node_id) ;
78
80
}
79
81
80
- pub fn remove_connection ( & mut self , input_id : InputId ) -> Option < OutputId > {
81
- self . connections . remove ( input_id)
82
+ pub fn remove_connection ( & mut self , output_id : OutputId , input_id : InputId ) {
83
+ self . outgoing [ output_id] . retain ( |& mut x| x != input_id) ;
84
+ self . incoming [ input_id] . retain ( |& mut x| x != output_id) ;
85
+ }
86
+
87
+ pub fn remove_incoming_connections ( & mut self , input_id : InputId ) {
88
+ if let Some ( outputs) = self . incoming . get ( input_id) {
89
+ for & output in outputs {
90
+ self . outgoing [ output] . retain ( |& mut x| x != input_id) ;
91
+ }
92
+ }
93
+ self . incoming . remove ( input_id) ;
94
+ }
95
+
96
+ pub fn remove_outgoing_connections ( & mut self , output_id : OutputId ) {
97
+ if let Some ( inputs) = self . outgoing . get ( output_id) {
98
+ for & input in inputs {
99
+ self . incoming [ input] . retain ( |& mut x| x != output_id) ;
100
+ }
101
+ }
102
+ self . outgoing . remove ( output_id) ;
82
103
}
83
104
84
105
pub fn iter_nodes ( & self ) -> impl Iterator < Item = NodeId > + ' _ {
85
106
self . nodes . iter ( ) . map ( |( id, _) | id)
86
107
}
87
108
88
109
pub fn add_connection ( & mut self , output : OutputId , input : InputId ) {
89
- self . connections . insert ( input, output) ;
110
+ if self . get_input ( input) . typ . mergeable ( ) {
111
+ self . incoming
112
+ . entry ( input)
113
+ . expect ( "Old InputId" )
114
+ . or_default ( )
115
+ . push ( output) ;
116
+ } else {
117
+ self . remove_incoming_connections ( input) ;
118
+ let mut v = SVec :: new ( ) ;
119
+ v. push ( output) ;
120
+ self . incoming . insert ( input, v) ;
121
+ }
122
+
123
+ if self . get_output ( output) . typ . splittable ( ) {
124
+ self . outgoing
125
+ . entry ( output)
126
+ . expect ( "Old OutputId" )
127
+ . or_default ( )
128
+ . push ( input) ;
129
+ } else {
130
+ self . remove_outgoing_connections ( output) ;
131
+ let mut v = SVec :: new ( ) ;
132
+ v. push ( input) ;
133
+ self . outgoing . insert ( output, v) ;
134
+ }
90
135
}
91
136
92
137
pub fn iter_connections ( & self ) -> impl Iterator < Item = ( InputId , OutputId ) > + ' _ {
93
- self . connections . iter ( ) . map ( |( o, i) | ( o, * i) )
138
+ self . incoming
139
+ . iter ( )
140
+ . flat_map ( |( o, inputs) | inputs. iter ( ) . map ( move |& i| ( o, i) ) )
141
+ }
142
+
143
+ pub fn incoming ( & self , input : InputId ) -> & [ OutputId ] {
144
+ self . incoming
145
+ . get ( input)
146
+ . map ( |x| x. as_slice ( ) )
147
+ . unwrap_or ( & [ ] )
94
148
}
95
149
96
- pub fn connection ( & self , input : InputId ) -> Option < OutputId > {
97
- self . connections . get ( input) . copied ( )
150
+ pub fn outgoing ( & self , output : OutputId ) -> & [ InputId ] {
151
+ self . outgoing
152
+ . get ( output)
153
+ . map ( |x| x. as_slice ( ) )
154
+ . unwrap_or ( & [ ] )
98
155
}
99
156
100
157
pub fn any_param_type ( & self , param : AnyParameterId ) -> Result < & DataType , EguiGraphError > {
@@ -114,21 +171,23 @@ impl<NodeData, DataType, ValueType> Graph<NodeData, DataType, ValueType> {
114
171
}
115
172
}
116
173
117
- impl < NodeData , DataType , ValueType > Default for Graph < NodeData , DataType , ValueType > {
174
+ impl < NodeData , DataType : DataTypeTrait , ValueType > Default
175
+ for Graph < NodeData , DataType , ValueType >
176
+ {
118
177
fn default ( ) -> Self {
119
178
Self :: new ( )
120
179
}
121
180
}
122
181
123
182
impl < NodeData > Node < NodeData > {
124
- pub fn inputs < ' a , DataType , DataValue > (
183
+ pub fn inputs < ' a , DataType : DataTypeTrait , DataValue > (
125
184
& ' a self ,
126
185
graph : & ' a Graph < NodeData , DataType , DataValue > ,
127
186
) -> impl Iterator < Item = & InputParam < DataType , DataValue > > + ' a {
128
187
self . input_ids ( ) . map ( |id| graph. get_input ( id) )
129
188
}
130
189
131
- pub fn outputs < ' a , DataType , DataValue > (
190
+ pub fn outputs < ' a , DataType : DataTypeTrait , DataValue > (
132
191
& ' a self ,
133
192
graph : & ' a Graph < NodeData , DataType , DataValue > ,
134
193
) -> impl Iterator < Item = & OutputParam < DataType > > + ' a {
0 commit comments