bool ISpaceManager.TryAddLevel <T>(Dimension <T> dimension) { if (dimension.HeadDimPoint == null || dimension.HeadDimPoint.TailLink == null) { return(false); } var topLeft = dimension.HeadDimPoint.TailLink; if (topLeft.CountConnectionsRight() == 0) { return(false); } var left = new DimensionLink <T>((byte)(topLeft.Level + 1)); topLeft.AssignUpper(left); var topRight = dimension.TailDimPoint.TailLink; var right = new DimensionLink <T>((byte)(topRight.Level + 1)); topRight.AssignUpper(right); left.AssignNext(right); dimension.HeadDimPoint.TailLink = left; dimension.TailDimPoint.TailLink = right; return(true); }
bool ISpaceManager.TryExtendUp <T>(DimensionLink <T> toExtendUp, out DimensionLink <T> nextUpExtension) { nextUpExtension = null; if (toExtendUp == null || // nothing to extend toExtendUp.Upper != null || // alrady extended toExtendUp.Next == null || // cannot extend up right edge toExtendUp.Prev == null || // cannot extend up left edge toExtendUp.Next.Upper != null || // no need to extend up if neighbours are extended up toExtendUp.Prev.Upper != null) { return(false); } var leftCorner = toExtendUp.PrevUntil((n, i) => n.Upper != null); var rightCorner = toExtendUp.NextUntil((n, i) => n.Upper != null); if (leftCorner == null || rightCorner == null) { return(false); } var left = leftCorner.Upper; var right = rightCorner.Upper; var upper = new DimensionLink <T>((byte)(toExtendUp.Level + 1)); toExtendUp.AssignUpper(upper); left.AssignNext(upper); upper.AssignNext(right); toExtendUp.DimPoint.TailLink = upper; nextUpExtension = upper.GetSiblingExtensionCandidate(); return(true); }
bool ISpaceManager.AddPoint <T>(Dimension <T> dimension, SpacePoint <T> sp, float position) { if (dimension == null || sp == null) { return(false); } MustBe.Null(sp.Dimensions[dimension.Index], () => "space point already has assigned the dimension with index=" + dimension.Index); if (dimension.HeadDimPoint == null) { var dp = new DimensionPoint <T>(dimension) { Position = position }; dp.HeadLink = dp.TailLink = new DimensionLink <T>(0, dp); dp.AddPoint(sp); dimension.HeadDimPoint = dimension.TailDimPoint = dp; dimension.Count = 1; return(true); } // try to find existing dimension point DimensionPoint <T> left; DimensionPoint <T> right; if (_spaceManager.TryFindDimensionPoint(dimension, position, out left, out right)) { // if found add the space point to it return(left.AddPoint(sp)); } // new head if (left == null) { AppendNewHead(dimension, sp, position); dimension.Count++; return(true); } // new tail if (right == null) { AppendNewTail(dimension, sp, position); dimension.Count++; return(true); } // new in between var newPoint = new DimensionPoint <T>(dimension) { Position = position }; newPoint.AddPoint(sp); var newLink = new DimensionLink <T>(0, newPoint); newPoint.HeadLink = newLink; newPoint.TailLink = newLink; left.HeadLink.AssignNext(newLink); newLink.AssignNext(right.HeadLink); // for fast movers do not normalize the tree if (!sp.IsFastMover) { _spaceManager.TryAddLevel(dimension); var toExtendUp = newLink.GetSiblingExtensionCandidate(); while (_spaceManager.TryExtendUp(toExtendUp, out toExtendUp)) { _spaceManager.TryAddLevel(dimension); } } dimension.Count++; return(true); }
private bool PopulateDimension <T>(Dimension <T> dimension, List <SpacePointSource <T> > points) { if (points.Count == 0) { return(false); } points.Sort(new SpacePointComparer <T>(dimension.Index)); //points.Sort((a, b) => a.InitialPositions[dimension.Index].CompareTo(b.InitialPositions[dimension.Index])); DimensionPoint <T> head = null; DimensionPoint <T> curr = null; DimensionPoint <T> prev = null; for (var i = 0; i < points.Count; ++i) { var sp = points[i]; if (sp == null) { continue; } var position = sp.InitialPositions[dimension.Index]; if (prev != null) { if (dimension.Eq(prev.Position, position)) { prev.AddPoint(sp); continue; } } curr = new DimensionPoint <T>(dimension) { Position = position }; curr.HeadLink = new DimensionLink <T>(0, curr); curr.TailLink = curr.HeadLink; curr.AddPoint(sp); if (prev != null) { prev.HeadLink.AssignNext(curr.HeadLink); } else { head = curr; } prev = curr; } dimension.HeadDimPoint = head; dimension.TailDimPoint = curr; dimension.Count += points.Count; // ReSharper disable PossibleNullReferenceException DimensionLink <T> link = dimension.HeadDimPoint.HeadLink; // ReSharper restore PossibleNullReferenceException // if this is the top level if (link.Next == null || link.Next.Next == null) { return(true); } DimensionLink <T> prevUpper = null; DimensionLink <T> firstUpper = null; while (link != null) { var upper = new DimensionLink <T>((byte)(link.Level + 1)); if (firstUpper == null) { firstUpper = upper; } link.AssignUpper(upper); link.DimPoint.TailLink = upper; if (prevUpper != null) { prevUpper.AssignNext(upper); } // if this is the end if (link.Next == null) { link = firstUpper; firstUpper = null; prevUpper = null; // if this is the top level if (link.Next == null || link.Next.Next == null) { return(true); } continue; } link = link.Next.Next; // the next is the end and it is two links away if (link.Next != null && link.Next.Next == null) { link = link.Next; } prevUpper = upper; } return(true); }