private ConstrainResult ApplyConstraint(ConstraintListWrapper constraint) { BoardState state = boardManager.CurrentLayer; IBoardView boardView = constraint.CreateBoardView(state); long oldCost = boardView.ConstraintState.Cost; Debug.Assert(oldCost >= 0); constraint.Constraint.CalculateEstimatedCost(boardView); long newCost = boardView.ConstraintState.Cost; Debug.Assert(newCost <= oldCost); if (newCost <= ENUMERATION_THRESHOLD || oldCost == newCost) { // We've judged that it is cheap enough to brute force hit this, or we don't have any other option return(constraint.Constraint.ConstrainBoard(boardView)); } else { constraint.IsDirty = true; dirtyConstraints.Enqueue(newCost, constraint); return(ConstrainResult.Success); } }
private ConstrainResult ApplyConstraintsLoop() { uint tick = 0; uint tickReport = 1; while (dirtyConstraints.Count > 0) { ConstraintListWrapper constraint = dirtyConstraints.Dequeue(); Debug.Assert(constraint.IsDirty); constraint.IsDirty = false; debugConstraintIndex = constraint.Index; debugConstraintIsRow = constraint.IsRow; var result = ApplyConstraint(constraint); if (result == ConstrainResult.NoSolution) { return(ConstrainResult.NoSolution); } tick++; if (tick == tickReport) { Debug.WriteLine("TICK " + tick.ToString()); tickReport = tickReport << 1; } } return(ConstrainResult.Success); }
private void Enqueue(ConstraintListWrapper constraint) { var state = boardManager.CurrentLayer; var constraintStates = constraint.IsRow ? state.RowConstraintStates : state.ColConstraintStates; long cost = constraintStates[constraint.Index].Cost; constraint.IsDirty = true; dirtyConstraints.Enqueue(cost, constraint); }