Skip to content

Commit

Permalink
Fix row dragging leaving initial states behind
Browse files Browse the repository at this point in the history
  • Loading branch information
Strilanc committed Mar 24, 2019
1 parent a0d11f9 commit 3a17740
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
10 changes: 9 additions & 1 deletion src/circuit/CircuitDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,22 @@ class CircuitDefinition {
} else {
m.set(wire, newVal);
}
return this.withInitialStates(m);
}

/**
* @param {!Map.<!int, *>} map
* @returns {!CircuitDefinition}
*/
withInitialStates(map) {
return new CircuitDefinition(
this.numWires,
this.columns,
this.outerRowOffset,
this.outerContext,
this.customGateSet,
this.isNested,
m);
map);
}

/**
Expand Down
25 changes: 18 additions & 7 deletions src/ui/DisplayedCircuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,13 @@ class DisplayedCircuit {
newCols.push(new GateColumn(gates));
}

let newCircuitDef = this.circuitDefinition.withColumns(newCols);
let newInitialStates = seq(this.circuitDefinition.customInitialValues.entries()).
map(([k, v]) => [k + (k >= handWire ? 1 : 0), v]).
toMap(([k, _]) => k, ([_, v]) => v);
if (hand.heldRow.initialState !== undefined) {
newInitialStates.set(handWire, hand.heldRow.initialState);
}
let newCircuitDef = this.circuitDefinition.withColumns(newCols).withInitialStates(newInitialStates);

return this.withCircuit(newCircuitDef).
_withHighlightedSlot({row: handWire, col: undefined, resizeStyle: false});
Expand Down Expand Up @@ -1110,17 +1116,17 @@ class DisplayedCircuit {
return undefined;
}

let {new_circuit, row_gates} = this._cutRow(wire);
let {newCircuit, initialState, rowGates} = this._cutRow(wire);
let holdOffset = new Point(0, hand.pos.y - r.y);
return {
newCircuit: this.withCircuit(new_circuit),
newHand: hand.withHeldRow(row_gates, holdOffset)
newCircuit: this.withCircuit(newCircuit),
newHand: hand.withHeldRow({initialState, gates: rowGates}, holdOffset)
};
}

/**
* @param {!int} row
* @returns {!{new_circuit: !CircuitDefinition, row_gates: !GateColumn}}
* @returns {!{newCircuit: !CircuitDefinition, rowGates: !Array.<undefined|!Gate>, initialState: *}}
* @private
*/
_cutRow(row) {
Expand All @@ -1133,9 +1139,14 @@ class DisplayedCircuit {
col_gates.push(undefined);
cols.push(new GateColumn(col_gates));
}
let newInitialStates = seq(this.circuitDefinition.customInitialValues.entries()).
filter(([k, _]) => k !== row).
map(([k, v]) => [k - (k > row ? 1 : 0), v]).
toMap(([k, _]) => k, ([_, v]) => v);
return {
new_circuit: this.circuitDefinition.withColumns(cols),
row_gates: new GateColumn(row_gates)
newCircuit: this.circuitDefinition.withColumns(cols).withInitialStates(newInitialStates),
rowGates: row_gates,
initialState: this.circuitDefinition.customInitialValues.get(row)
};
}

Expand Down
10 changes: 5 additions & 5 deletions src/ui/Hand.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Hand {
* @param {undefined|!Gate} heldGate
* @param {undefined|!Point} holdOffset
* @param {undefined|!GateColumn} heldColumn
* @param {undefined|!GateColumn} heldRow
* @param {undefined|!{initialState: *, gates: !Array.<undefined|!Gate>}} heldRow
* @param {undefined|!Point} resizingGateSlot
*/
constructor(pos, heldGate, holdOffset, heldColumn, heldRow, resizingGateSlot) {
Expand All @@ -45,7 +45,7 @@ class Hand {
if (heldColumn !== undefined && !(heldColumn instanceof GateColumn)) {
throw new DetailedError("Bad heldColumn", args);
}
if (heldRow !== undefined && !(heldRow instanceof GateColumn)) {
if (heldRow !== undefined && !Array.isArray(heldRow.gates)) {
throw new DetailedError("Bad heldRow", args);
}
if (heldGate !== undefined && this.resizingGateSlot !== undefined) {
Expand All @@ -60,7 +60,7 @@ class Hand {
this.holdOffset = holdOffset;
/** @type {undefined|!GateColumn} */
this.heldColumn = heldColumn;
/** @type {undefined|!GateColumn} */
/** @type {undefined|!{initialState: *, gates: !Array.<undefined|!Gate>}} */
this.heldRow = heldRow;
/** @type {undefined|!Point} */
this.resizingGateSlot = resizingGateSlot;
Expand Down Expand Up @@ -159,7 +159,7 @@ class Hand {
}

/**
* @param {!GateColumn} heldRow
* @param {!{initialState: *, gates: !Array.<undefined|!Gate>}} heldRow
* @param {!Point} heldGateOffset
* @returns {!Hand}
*/
Expand All @@ -182,7 +182,7 @@ class Hand {
stableDuration() {
return this.heldGate !== undefined ? this.heldGate.stableDuration() :
this.heldColumn !== undefined ? this.heldColumn.stableDuration() :
this.heldRow !== undefined ? this.heldRow.stableDuration() :
this.heldRow !== undefined ? new GateColumn(this.heldRow.gates).stableDuration() :
Infinity;
}
}
Expand Down

0 comments on commit 3a17740

Please sign in to comment.