예제 #1
0
        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);
        }
예제 #2
0
        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);
        }