Skip to content

Commit 0888c31

Browse files
committed
modification following new definitions of intrinsics
1 parent 89ffa74 commit 0888c31

File tree

5 files changed

+76
-128
lines changed

5 files changed

+76
-128
lines changed

src/store/componentEvaluator.ts

Lines changed: 21 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,10 @@ import type {
77
} from "../pages/edit/Editor/store/slices/core";
88
import type { CCComponentId } from "./component";
99
import type { CCComponentPin, CCComponentPinId } from "./componentPin";
10-
import * as intrinsics from "./intrinsics";
11-
import {
12-
aggregate,
13-
and,
14-
decompose,
15-
definitionByComponentId,
16-
flipflop,
17-
input,
18-
not,
19-
or,
20-
xor,
21-
} from "./intrinsics/definitions";
10+
import { definitionByComponentId, flipflop } from "./intrinsics/definitions";
2211
import type { CCNodeId } from "./node";
2312
import type { CCNodePin, CCNodePinId } from "./nodePin";
2413

25-
function getNodePinIdByComponentPinId(
26-
nodePins: CCNodePin[],
27-
componentPinId: CCComponentPinId,
28-
): CCNodePin {
29-
const nodePin = nullthrows(
30-
nodePins.find(
31-
(nodePin: CCNodePin) => nodePin.componentPinId === componentPinId,
32-
),
33-
);
34-
return nodePin;
35-
}
36-
3714
function createInput(
3815
nodePins: CCNodePin[],
3916
inputPin: Record<string, CCComponentPin>,
@@ -57,26 +34,22 @@ function createInput(
5734
function createOutputShape(
5835
store: CCStore,
5936
nodePins: CCNodePin[],
60-
outputPin: Record<string, CCComponentPin>,
61-
): Record<string, { multiplicity: number }[]> {
62-
const outputShape: Record<string, { multiplicity: number }[]> = {};
63-
for (const key in outputPin) {
64-
const componentPin = nullthrows(outputPin[key]);
65-
const targetNodePins = nodePins.filter(
66-
(nodePin: CCNodePin) => componentPin.id === nodePin.componentPinId,
37+
outputPin: CCComponentPin,
38+
): { multiplicity: number }[] {
39+
const targetNodePins = nodePins.filter(
40+
(nodePin: CCNodePin) => outputPin.id === nodePin.componentPinId,
41+
);
42+
targetNodePins.sort((a, b) => a.order - b.order);
43+
const multiplicity = targetNodePins.map((nodePin: CCNodePin) => {
44+
const multiplexability = store.nodePins.getNodePinMultiplexability(
45+
nodePin.id,
6746
);
68-
targetNodePins.sort((a, b) => a.order - b.order);
69-
const multiplicity = targetNodePins.map((nodePin: CCNodePin) => {
70-
const multiplexability = store.nodePins.getNodePinMultiplexability(
71-
nodePin.id,
72-
);
73-
if (multiplexability.isMultiplexable) {
74-
return 1;
75-
}
76-
return multiplexability.multiplicity;
77-
});
78-
outputShape[key] = multiplicity.map((multiplicity) => ({ multiplicity }));
79-
}
47+
if (multiplexability.isMultiplexable) {
48+
return 1;
49+
}
50+
return multiplexability.multiplicity;
51+
});
52+
const outputShape = multiplicity.map((multiplicity) => ({ multiplicity }));
8053
return outputShape;
8154
}
8255

@@ -111,9 +84,8 @@ function simulateIntrinsic(
11184
previousInput,
11285
);
11386
const outputValues = new Map<CCNodePinId, SimulationValue>();
114-
const componentPin = nullthrows(outputPin["Out" as any]);
11587
const targetNodePins = nodePins.filter(
116-
(nodePin: CCNodePin) => componentPin.id === nodePin.componentPinId,
88+
(nodePin: CCNodePin) => outputPin.id === nodePin.componentPinId,
11789
);
11890
targetNodePins.sort((a, b) => a.order - b.order);
11991
for (let i = 0; i < output.length; i++) {
@@ -263,17 +235,13 @@ function simulateNode(
263235
);
264236
outputValues.set(parentNodePin.id, outputValue);
265237
}
266-
if (
267-
currentComponentId ===
268-
intrinsics.flipFlopIntrinsicComponentDefinition.component.id
269-
) {
238+
if (currentComponentId === flipflop.id) {
270239
visitedFlipFlops.add(currentNodeId);
271240
}
272241
}
273242
}
274243
} else if (
275-
currentComponentId ===
276-
intrinsics.flipFlopIntrinsicComponentDefinition.component.id &&
244+
currentComponentId === flipflop.id &&
277245
!visitedFlipFlops.has(currentNodeId)
278246
) {
279247
const frame = previousFrame
@@ -319,10 +287,7 @@ function simulateNode(
319287
);
320288
outputValues.set(parentNodePin.id, outputValue);
321289
}
322-
if (
323-
currentComponentId ===
324-
intrinsics.flipFlopIntrinsicComponentDefinition.component.id
325-
) {
290+
if (currentComponentId === flipflop.outputPin.componentId) {
326291
visitedFlipFlops.add(currentNodeId);
327292
}
328293
}
@@ -466,10 +431,7 @@ export default function simulateComponent(
466431
);
467432
outputValues.set(parentComponentPin.id, outputValue);
468433
}
469-
if (
470-
currentComponentId ===
471-
intrinsics.flipFlopIntrinsicComponentDefinition.component.id
472-
) {
434+
if (currentComponentId === flipflop.outputPin.componentId) {
473435
visitedFlipFlops.add(currentNodeId);
474436
}
475437
}

src/store/componentPin.ts

Lines changed: 32 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,17 @@ import invariant from "tiny-invariant";
44
import type { Opaque } from "type-fest";
55
import type CCStore from ".";
66
import type { CCComponentId } from "./component";
7-
import * as intrinsic from "./intrinsics";
7+
import {
8+
aggregate,
9+
and,
10+
broadcast,
11+
decompose,
12+
flipflop,
13+
input,
14+
not,
15+
or,
16+
xor,
17+
} from "./intrinsics/definitions";
818
import type { CCNodePinId } from "./nodePin";
919

1020
export type CCComponentPin = {
@@ -233,66 +243,39 @@ export class CCComponentPinStore extends EventEmitter<CCComponentPinStoreEvents>
233243
const pin = this.#pins.get(pinId);
234244
invariant(pin);
235245
switch (pin.id) {
236-
case nullthrows(intrinsic.andIntrinsicComponentDefinition.inputPins[0])
237-
.id:
238-
case nullthrows(intrinsic.andIntrinsicComponentDefinition.inputPins[1])
239-
.id:
240-
case nullthrows(intrinsic.andIntrinsicComponentDefinition.outputPins[0])
241-
.id:
242-
case nullthrows(intrinsic.orIntrinsicComponentDefinition.inputPins[0]).id:
243-
case nullthrows(intrinsic.orIntrinsicComponentDefinition.inputPins[1]).id:
244-
case nullthrows(intrinsic.orIntrinsicComponentDefinition.outputPins[0])
245-
.id:
246-
case nullthrows(intrinsic.notIntrinsicComponentDefinition.inputPins[0])
247-
.id:
248-
case nullthrows(intrinsic.notIntrinsicComponentDefinition.outputPins[0])
249-
.id:
250-
case nullthrows(intrinsic.xorIntrinsicComponentDefinition.inputPins[0])
251-
.id:
252-
case nullthrows(intrinsic.xorIntrinsicComponentDefinition.inputPins[1])
253-
.id:
254-
case nullthrows(intrinsic.xorIntrinsicComponentDefinition.outputPins[0])
255-
.id:
256-
case nullthrows(intrinsic.inputIntrinsicComponentDefinition.inputPins[0])
257-
.id:
258-
case nullthrows(intrinsic.inputIntrinsicComponentDefinition.outputPins[0])
259-
.id:
260-
case nullthrows(
261-
intrinsic.flipFlopIntrinsicComponentDefinition.inputPins[0],
262-
).id:
263-
case nullthrows(
264-
intrinsic.flipFlopIntrinsicComponentDefinition.outputPins[0],
265-
).id: {
246+
case nullthrows(and.inputPin.A.id):
247+
case nullthrows(and.inputPin.B.id):
248+
case nullthrows(and.outputPin.id):
249+
case nullthrows(or.inputPin.A.id):
250+
case nullthrows(or.inputPin.B.id):
251+
case nullthrows(or.outputPin.id):
252+
case nullthrows(not.inputPin.A.id):
253+
case nullthrows(not.outputPin.id):
254+
case nullthrows(xor.inputPin.A.id):
255+
case nullthrows(xor.inputPin.B.id):
256+
case nullthrows(xor.outputPin.id):
257+
case nullthrows(input.inputPin.A.id):
258+
case nullthrows(input.outputPin.id):
259+
case nullthrows(flipflop.inputPin.In.id):
260+
case nullthrows(flipflop.outputPin.id): {
266261
return { isMultiplexable: true };
267262
}
268-
case nullthrows(
269-
intrinsic.aggregateIntrinsicComponentDefinition.inputPins[0],
270-
).id: {
263+
case nullthrows(aggregate.inputPin.In.id): {
271264
return "undecidable";
272265
}
273-
case nullthrows(
274-
intrinsic.aggregateIntrinsicComponentDefinition.outputPins[0],
275-
).id: {
266+
case nullthrows(aggregate.outputPin.id): {
276267
return "undecidable";
277268
}
278-
case nullthrows(
279-
intrinsic.decomposeIntrinsicComponentDefinition.outputPins[0],
280-
).id: {
269+
case nullthrows(decompose.outputPin.id): {
281270
return "undecidable";
282271
}
283-
case nullthrows(
284-
intrinsic.decomposeIntrinsicComponentDefinition.inputPins[0],
285-
).id: {
272+
case nullthrows(decompose.inputPin.In.id): {
286273
return "undecidable";
287274
}
288-
case nullthrows(
289-
intrinsic.broadcastIntrinsicComponentDefinition.inputPins[0],
290-
).id: {
275+
case nullthrows(broadcast.inputPin.In.id): {
291276
return { isMultiplexable: false, multiplicity: 1 };
292277
}
293-
case nullthrows(
294-
intrinsic.broadcastIntrinsicComponentDefinition.outputPins[0],
295-
).id: {
278+
case nullthrows(broadcast.outputPin.id): {
296279
return "undecidable";
297280
}
298281
default: {

src/store/intrinsics/base.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type Props<In extends string, Out extends string> = {
1515
out: Record<Out, PropsPin>;
1616
evaluate: (
1717
input: Record<In, SimulationValue[]>,
18-
outputShape: Record<Out, { multiplicity: number }[]>,
18+
outputShape: { multiplicity: number }[],
1919
previousInput: Record<In, SimulationValue[]>,
2020
) => SimulationValue[];
2121
};
@@ -41,10 +41,10 @@ export class IntrinsicComponentDefinition<
4141
readonly component: CCComponent;
4242
readonly allPins: CCComponentPin[] = [];
4343
readonly inputPin: Record<In, CCComponentPin>;
44-
readonly outputPin: Record<Out, CCComponentPin>;
44+
readonly outputPin: CCComponentPin;
4545
readonly evaluate: (
4646
input: Record<In, SimulationValue[]>,
47-
outputShape: Record<Out, { multiplicity: number }[]>,
47+
outputShape: { multiplicity: number }[],
4848
previousInput: Record<In, SimulationValue[]>,
4949
) => SimulationValue[];
5050

@@ -70,17 +70,20 @@ export class IntrinsicComponentDefinition<
7070
this.allPins.push(pin);
7171
return pin;
7272
});
73-
this.outputPin = mapValues(props.out, (p) => {
74-
const pin: CCComponentPin = {
75-
id: this._generateId() as CCComponentPinId,
76-
componentId: this.id,
77-
type: "output",
78-
implementation: null,
79-
order: this._lastLocalIndex++,
80-
name: p.name,
81-
};
82-
this.allPins.push(pin);
83-
return pin;
84-
});
73+
// this.outputPin = mapValues(props.out, (p) => {
74+
// const pin: CCComponentPin = {
75+
// id: this._generateId() as CCComponentPinId,
76+
// componentId: this.id,
77+
// type: "output",
78+
// implementation: null,
79+
// order: this._lastLocalIndex++,
80+
// name: p.name,
81+
// };
82+
// this.allPins.push(pin);
83+
// return pin;
84+
// });
85+
this.outputPin = this.allPins.find(
86+
(pin) => pin.type === "output",
87+
) as CCComponentPin;
8588
}
8689
}

src/store/intrinsics/definitions.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import nullthrows from "nullthrows";
22
import invariant from "tiny-invariant";
33
import type { CCComponentId } from "../component";
4-
import type { CCComponentPin, CCComponentPinId } from "../componentPin";
4+
import type { CCComponentPinId } from "../componentPin";
55
import { IntrinsicComponentDefinition } from "./base";
66
import {
77
type CCIntrinsicComponentType,
@@ -112,7 +112,7 @@ export const decompose = new IntrinsicComponentDefinition({
112112
const inputValue = input.In[0];
113113
const outputValue = new Array();
114114
let currentIndex = 0;
115-
for (const shape of outputShape.Out) {
115+
for (const shape of outputShape) {
116116
outputValue.push([
117117
...inputValue.slice(currentIndex, currentIndex + shape.multiplicity),
118118
]);
@@ -134,8 +134,8 @@ export const broadcast = new IntrinsicComponentDefinition({
134134
evaluate: (input, outputShape) => {
135135
invariant(input.In[0] && !input.In[1]);
136136
const inputValue = input.In[0];
137-
invariant(outputShape.Out[0] && !outputShape.Out[1]);
138-
const outputMultiplicity = outputShape.Out[0].multiplicity;
137+
invariant(outputShape[0] && !outputShape[1]);
138+
const outputMultiplicity = outputShape[0].multiplicity;
139139
return Array.from({ length: outputMultiplicity }, () => inputValue);
140140
},
141141
});

src/store/react/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function useContextValue() {
4040
const fromNodePin = nullthrows(
4141
tempStore.nodePins
4242
.getManyByNodeId(sampleNode1.id)
43-
.find((nodePin) => nodePin.componentPinId === and.outputPin.Out.id),
43+
.find((nodePin) => nodePin.componentPinId === and.outputPin.id),
4444
);
4545
const toNodePin = nullthrows(
4646
tempStore.nodePins

0 commit comments

Comments
 (0)