@@ -30,6 +30,7 @@ export function createNodeMap(newState, addShapes) {
3030
3131 var start , end , nodeFound , index , node ;
3232 var vin_node = null ;
33+ var crushedNodes = [ ] ;
3334 for ( const w of newState . wires ) {
3435 if ( w == null ) continue ; //skip deleted wires
3536 const startEl = elementMapper ( newState . shapes [ w . start . shapeID ] ) ;
@@ -39,28 +40,88 @@ export function createNodeMap(newState, addShapes) {
3940 end = { ...endEl , port : w . end . connectorID } ;
4041 components [ startEl . name ] . portConnections [ w . start . connectorID ] = true ;
4142 components [ endEl . name ] . portConnections [ w . end . connectorID ] = true ;
43+ crushedNodes . push ( [ start , end ] )
4244
43- if ( startEl . type == "gnd" || endEl . type == "gnd" ) continue ; //don't add ground nodes to the node map
45+ // if (startEl.type == "gnd" || endEl.type == "gnd") continue; //don't add ground nodes to the node map
4446
45- nodeFound = false ;
46- for ( index in nodeMapPre ) {
47- node = nodeMapPre [ index ] ;
47+ // nodeFound = false;
48+ // for (index in nodeMapPre) {
49+ // node = nodeMapPre[index];
50+ // for (const conn_index in node) {
51+ // const conn = node[conn_index];
52+ // if (conn.name == start.name && conn.port == start.port) {
53+ // nodeMapPre[index].push(end);
54+ // nodeFound = true;
55+ // } else if (conn.name == end.name && conn.port == end.port) {
56+ // nodeMapPre[index].push(start);
57+ // nodeFound = true;
58+ // }
59+ // }
60+ // }
61+ // if (!nodeFound) nodeMapPre.push([start, end]);
62+ }
63+ // console.log("new node map", JSON.parse(JSON.stringify(nodeMapPre)));
64+
65+ var wasChanged
66+ do {
67+ wasChanged = false ;
68+ outerLoop: for ( index in crushedNodes ) {
69+ // node = crushedNodes[index];
70+ for ( const conn_index in crushedNodes [ index ] ) {
71+ const conn = crushedNodes [ index ] [ conn_index ] ;
72+ //check if conn exists in any other node, then merge the nodes
73+ for ( const nodeIndex in crushedNodes ) {
74+ if ( nodeIndex == index ) continue ; //skip the current node
75+ const node = crushedNodes [ nodeIndex ] ;
76+ for ( const conn2_index in node ) {
77+ const conn2 = node [ conn2_index ] ;
78+ if ( conn . name == conn2 . name && conn . port == conn2 . port ) {
79+ // console.log("found a match", conn, conn2, "in node", node);
80+ //merge the nodes
81+ // if (nodeIndex != index) {
82+ // console.log("merging nodes", index, "and", nodeIndex);
83+ crushedNodes [ nodeIndex ] = [ ...crushedNodes [ nodeIndex ] , ...crushedNodes [ index ] ] ;
84+ crushedNodes . splice ( index , 1 ) ;
85+ wasChanged = true ;
86+ break outerLoop; //break to avoid checking the rest of the connections
87+ // }
88+ }
89+ }
90+ }
91+ }
92+ }
4893
49- for ( const conn_index in node ) {
50- const conn = node [ conn_index ] ;
51- if ( conn . name == start . name && conn . port == start . port ) {
52- nodeMapPre [ index ] . push ( end ) ;
53- nodeFound = true ;
54- } else if ( conn . name == end . name && conn . port == end . port ) {
55- nodeMapPre [ index ] . push ( start ) ;
56- nodeFound = true ;
94+ } while ( wasChanged ) ;
95+
96+ //delete the node containing gnd
97+ outerLoop : for ( const index in crushedNodes ) {
98+ const node = crushedNodes [ index ] ;
99+ for ( const conn of node ) {
100+ if ( conn . type == "gnd" ) {
101+ // console.log("found gnd node", node, "at index", index);
102+ crushedNodes . splice ( index , 1 ) ;
103+ break outerLoop; //break to avoid checking the rest of the connections
104+ }
105+ }
106+ }
107+
108+ //remove duplicates from crushedNodes
109+ for ( const n in crushedNodes ) {
110+ for ( let i = 0 ; i < crushedNodes [ n ] . length ; i ++ ) {
111+ const conn = crushedNodes [ n ] [ i ] ;
112+ for ( let j = i + 1 ; j < crushedNodes [ n ] . length ; j ++ ) {
113+ const conn2 = crushedNodes [ n ] [ j ] ;
114+ // console.log("conns", conn, conn2);
115+ if ( conn . name == conn2 . name && conn . port == conn2 . port ) {
116+ // console.log("found a duplicate", conn, "in node", crushedNodes[n]);
117+ crushedNodes [ n ] . splice ( j , 1 ) ; //remove the duplicate
118+ j -- ; //decrement j to account for the removed element
57119 }
58120 }
59121 }
60- if ( ! nodeFound ) nodeMapPre . push ( [ start , end ] ) ;
61122 }
62- // console.log("new node map ", nodeMapPre );
63- // console.log("components", components) ;
123+ // console.log("crushedNodes ", crushedNodes );
124+ nodeMapPre = crushedNodes ;
64125
65126 //remove components that are not fully connected
66127 for ( const [ key , value ] of Object . entries ( components ) ) {
@@ -105,6 +166,7 @@ export function createNodeMap(newState, addShapes) {
105166 // console.log("connected elements to vin", connected_elements);
106167
107168 // const nodeMap = nodeMapPre.filter(node => connected_nodes.includes(node));
169+ // console.log("pre map", nodeMapPre)
108170 nodeMap = connected_nodes . map ( ( node ) => nodeMapPre [ node ] ) ;
109171 // console.log("final node map", nodeMap);
110172 //find all nodes that have a path to vin_node
@@ -121,16 +183,6 @@ export function createNodeMap(newState, addShapes) {
121183 } ;
122184 }
123185 fullyConnectedComponents [ comp . name ] . ports [ comp . port ] = i ;
124-
125- // //FIXME - can these maps be eliminated now fullyConnectedComponents is more detailed?
126- // if (comp.type == "opamp") {
127- // if (!(comp.name in opAmpsMap)) opAmpsMap[comp.name] = [null, null, null];
128- // opAmpsMap[comp.name][comp.port] = i;
129- // // } else if (comp.type == "iprobe") {
130- // // if (!(comp.name in iprbMap)) iprbMap[comp.name] = [null, null];
131- // // iprbMap[comp.name][comp.port] = i;
132- // // if (comp.port == 0) iprbNode = i;
133- // }
134186 } ) ;
135187 } ) ;
136188 }
0 commit comments