circuit-json-to-connectivity-map 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +61 -0
- package/dist/index.js.map +1 -0
- package/package.json +20 -0
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# circuit-json-to-connectivity-map
|
|
2
|
+
|
|
3
|
+
To install dependencies:
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
bun install
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
To run:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
bun run index.ts
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
This project was created using `bun init` in bun v1.1.21. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { AnySoupElement } from '@tscircuit/soup';
|
|
2
|
+
|
|
3
|
+
type NodeId = string;
|
|
4
|
+
declare function findConnectedNetworks(connections: Array<NodeId[]>): Array<{
|
|
5
|
+
netId: string;
|
|
6
|
+
connectedNodeIds: string[];
|
|
7
|
+
}>;
|
|
8
|
+
|
|
9
|
+
declare const getSoupPortConnectivityMapFromCircuitJson: (circuitJson: AnySoupElement[]) => Map<string, string[]>;
|
|
10
|
+
|
|
11
|
+
export { findConnectedNetworks, getSoupPortConnectivityMapFromCircuitJson };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// src/findConnectedNetworks.ts
|
|
2
|
+
function findConnectedNetworks(connections) {
|
|
3
|
+
const networks = /* @__PURE__ */ new Map();
|
|
4
|
+
let netCounter = 0;
|
|
5
|
+
function getOrCreateNetwork(nodeId) {
|
|
6
|
+
for (const [, network] of networks) {
|
|
7
|
+
if (network.has(nodeId)) {
|
|
8
|
+
return network;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
const newNetwork = /* @__PURE__ */ new Set();
|
|
12
|
+
networks.set(`net_${netCounter++}`, newNetwork);
|
|
13
|
+
return newNetwork;
|
|
14
|
+
}
|
|
15
|
+
for (const connection of connections) {
|
|
16
|
+
let network = null;
|
|
17
|
+
for (const nodeId of connection) {
|
|
18
|
+
if (!network) {
|
|
19
|
+
network = getOrCreateNetwork(nodeId);
|
|
20
|
+
} else if (!network.has(nodeId)) {
|
|
21
|
+
const existingNetwork = getOrCreateNetwork(nodeId);
|
|
22
|
+
if (existingNetwork !== network) {
|
|
23
|
+
for (const node of existingNetwork) {
|
|
24
|
+
network.add(node);
|
|
25
|
+
}
|
|
26
|
+
networks.delete(
|
|
27
|
+
Array.from(networks.entries()).find(
|
|
28
|
+
([, net]) => net === existingNetwork
|
|
29
|
+
)[0]
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
network.add(nodeId);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return Array.from(networks.entries()).map(([netId, connectedNodes]) => ({
|
|
37
|
+
netId,
|
|
38
|
+
connectedNodeIds: Array.from(connectedNodes)
|
|
39
|
+
}));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// src/getSourcePortConnectivityMapFromCircuitJson.ts
|
|
43
|
+
var getSoupPortConnectivityMapFromCircuitJson = (circuitJson) => {
|
|
44
|
+
const connectivityMap = /* @__PURE__ */ new Map();
|
|
45
|
+
const connections = [];
|
|
46
|
+
for (const element of circuitJson) {
|
|
47
|
+
if (element.type === "source_trace") {
|
|
48
|
+
connections.push(element.connected_source_port_ids);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const connectedNetworks = findConnectedNetworks(connections);
|
|
52
|
+
for (const network of connectedNetworks) {
|
|
53
|
+
connectivityMap.set(network.netId, network.connectedNodeIds);
|
|
54
|
+
}
|
|
55
|
+
return connectivityMap;
|
|
56
|
+
};
|
|
57
|
+
export {
|
|
58
|
+
findConnectedNetworks,
|
|
59
|
+
getSoupPortConnectivityMapFromCircuitJson
|
|
60
|
+
};
|
|
61
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/findConnectedNetworks.ts","../src/getSourcePortConnectivityMapFromCircuitJson.ts"],"sourcesContent":["type NodeId = string\n\nexport function findConnectedNetworks(\n connections: Array<NodeId[]>,\n): Array<{ netId: string; connectedNodeIds: string[] }> {\n const networks: Map<string, Set<string>> = new Map()\n let netCounter = 0\n\n function getOrCreateNetwork(nodeId: string): Set<string> {\n for (const [, network] of networks) {\n if (network.has(nodeId)) {\n return network\n }\n }\n const newNetwork = new Set<string>()\n networks.set(`net_${netCounter++}`, newNetwork)\n return newNetwork\n }\n\n for (const connection of connections) {\n let network: Set<string> | null = null\n\n for (const nodeId of connection) {\n if (!network) {\n network = getOrCreateNetwork(nodeId)\n } else if (!network.has(nodeId)) {\n const existingNetwork = getOrCreateNetwork(nodeId)\n if (existingNetwork !== network) {\n // Merge networks\n for (const node of existingNetwork) {\n network.add(node)\n }\n networks.delete(\n Array.from(networks.entries()).find(\n ([, net]) => net === existingNetwork,\n )![0],\n )\n }\n }\n network.add(nodeId)\n }\n }\n\n return Array.from(networks.entries()).map(([netId, connectedNodes]) => ({\n netId,\n connectedNodeIds: Array.from(connectedNodes),\n }))\n}\n","import type { AnySoupElement } from \"@tscircuit/soup\"\nimport { findConnectedNetworks } from \"./findConnectedNetworks\"\n\nexport const getSoupPortConnectivityMapFromCircuitJson = (\n circuitJson: AnySoupElement[],\n) => {\n const connectivityMap = new Map<string, string[]>()\n\n const connections: string[][] = []\n\n for (const element of circuitJson) {\n if (element.type === \"source_trace\") {\n connections.push(element.connected_source_port_ids)\n }\n }\n\n const connectedNetworks = findConnectedNetworks(connections)\n\n for (const network of connectedNetworks) {\n connectivityMap.set(network.netId, network.connectedNodeIds)\n }\n\n return connectivityMap\n}\n"],"mappings":";AAEO,SAAS,sBACd,aACsD;AACt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B;AACvC,oBAAgB,IAAI,QAAQ,OAAO,QAAQ,gBAAgB;AAAA,EAC7D;AAEA,SAAO;AACT;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "circuit-json-to-connectivity-map",
|
|
3
|
+
"module": "dist/index.js",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"version": "0.0.1",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "tsup-node src/index.ts --format esm --sourcemap --dts"
|
|
11
|
+
},
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"@tscircuit/soup": "^0.0.68",
|
|
14
|
+
"@types/bun": "latest",
|
|
15
|
+
"tsup": "^8.2.4"
|
|
16
|
+
},
|
|
17
|
+
"peerDependencies": {
|
|
18
|
+
"typescript": "^5.0.0"
|
|
19
|
+
}
|
|
20
|
+
}
|