private void InsertAfterSelf(ConstraintSegment segment) { segment.Prev = this; // Order is important segment.Next = Next; if (Next != null) { Next.Prev = segment; } Next = segment; }
public ConstraintSegment CreateNext(Constraint constraint) { Debug.Assert(Next == null); if (constraint.color == this.Constraint.color) { // We need to create a constraint side-by-side with the same color -> introduce a gap Constraint newConstraint = new Constraint { color = ColorSpace.Empty, number = 1 }; ConstraintSegment gapSegment = CreateNext(newConstraint); Debug.Assert(gapSegment.IsGap); return(gapSegment.CreateNext(constraint)); } var newSegment = new ConstraintSegment(constraint, EndIndex); InsertAfterSelf(newSegment); return(newSegment); }
public ConstrainResult ConstrainBoard(IBoardView boardView) { ConstraintSegment.CreateChain(constraintSet, out ConstraintSegment segmentBegin, out ConstraintSegment segmentEnd); // Start final colors as completely empty: no colors possible at all ColorSet[] finalColors = new ColorSet[boardView.Count]; do { IReadOnlyList <uint> segmentColors = GetColorsFromSegments(segmentBegin); if (AreCompatible(boardView, segmentColors)) { Merge(segmentColors, finalColors); } } while (segmentEnd.Bump(boardView.Count)); // TODO: this will place this constraint on the dirty list again, which is slightly redundant... var result = boardView.IntersectAll(finalColors); return(result); }
private void GrowForBump() { // Our Next has just moved up by 1 if (IsGap) { // Just grow the gap var con = Constraint; // This copies con.number++; Constraint = con; Debug.Assert(EndIndex == Next.StartIndex); return; } // We need to introduce a new gap object gap var constraint = new Constraint { color = ColorSpace.Empty, number = 1 }; ConstraintSegment gap = new ConstraintSegment(constraint, EndIndex); InsertAfterSelf(gap); }
public static void CreateChain(IEnumerable <Constraint> constraints, out ConstraintSegment begin, out ConstraintSegment end) { begin = null; end = null; ConstraintSegment current = null; foreach (Constraint constr in constraints) { if (current == null) { current = new ConstraintSegment(constr, 0); begin = current; } else { current = current.CreateNext(constr); } } end = current; }
private IReadOnlyList <uint> GetColorsFromSegments(ConstraintSegment begin) { uint[] colorSets = new uint[boardSize]; ConstraintSegment current = begin; uint emptyColor = ColorSpace.Empty; //ColorSet emptyColor = ColorSet.Empty.AddColor(ColorSpace.Empty); int i = 0; while (i != current.StartIndex) { colorSets[i] = emptyColor; i++; } while (current != null) { Debug.Assert(i == current.StartIndex); uint currentColor = current.Constraint.color; while (i != current.EndIndex) { colorSets[i] = currentColor; i++; } current = current.Next; } while (i != boardSize) { colorSets[i] = emptyColor; i++; } return(colorSets); }