Example #1
0
        static void SubtreeCheck()
        {
            var t1 = new RbTree <int>();

            t1.Add(3);
            t1.Add(2);
            t1.Add(4);
            t1.Add(1);
            t1.Add(5);
            t1.Add(0);
            t1.Add(-1);
            t1.Add(-2);
            RbTree <int> .Node subroot = t1.Add(-3);
            t1.Add(-4);
            t1.Add(-5);

            t1.Print();

            WriteLine(Environment.NewLine);

            var t2 = t1.Subtree(subroot);

            t2.Print();

            t1.Remove(-5);

            WriteLine(Environment.NewLine);

            t2.Print();
        }
Example #2
0
        static void JoinExample()
        {
            var t1 = new RbTree <int>();

            t1.Add(3);
            t1.Add(2);
            t1.Add(4);
            t1.Add(1);
            t1.Add(5);
            t1.Add(0);
            t1.Add(-1);
            t1.Add(-2);
            t1.Add(-3);
            t1.Add(-4);
            t1.Add(-5);

            var t2 = new RbTree <int>();

            t2.Add(9);
            t2.Add(8);
            t2.Add(10);
            t2.Add(7);
            t2.Add(11);

            WriteLine($"Note: t1's black height > t2's black height in this example." + Environment.NewLine);
            var t3 = RbTree <int> .Join(t1, 6, t2);

            t3.Print();
        }
 private void AllocateDataStructures(int i)
 {
     tangents            = new List <Tangent>();
     diagonals           = new List <Diagonal>();
     activeDiagonalTree  = new RbTree <Diagonal>(this.activeDiagonalComparer);
     this.currentPolygon = polygons[i];
 }
Example #4
0
 internal ScanSegmentTree(ScanDirection scanDir)
 {
     ScanDirection            = scanDir;
     this.segmentTree         = new RbTree <ScanSegment>(this);
     this.findIntersectorPred = new Func <ScanSegment, bool>(this.CompareIntersector);
     this.findPointPred       = new Func <ScanSegment, bool>(this.CompareToPoint);
 }
Example #5
0
 public EdgeInserter(CdtEdge edge, Set <CdtTriangle> triangles, RbTree <CdtFrontElement> front, Func <CdtSite, CdtSite, CdtEdge> createEdgeDelegate)
 {
     this.edge               = edge;
     this.triangles          = triangles;
     this.front              = front;
     this.createEdgeDelegate = createEdgeDelegate;
 }
Example #6
0
 void InitActiveEdgesAndActiveEdgesComparer()
 {
     activeEdgeComparer = new ActiveEdgeComparerWithRay {
         Pivot = q
     };
     activeSidesTree = new RbTree <PolylinePoint>(activeEdgeComparer);
 }
        void CloseConesCoveredBySegment(Point leftPoint, Point rightPoint, RbTree <ConeSide> tree)
        {
            RBNode <ConeSide> node = tree.FindFirst(
                s => Point.GetTriangleOrientation(s.Start, s.Start + s.Direction, leftPoint) ==
                TriangleOrientation.Counterclockwise);

            Point x;

            if (node == null || !Point.IntervalIntersectsRay(leftPoint, rightPoint,
                                                             node.Item.Start, node.Item.Direction, out x))
            {
                return;
            }
            var conesToRemove = new List <Cone>();

            do
            {
                conesToRemove.Add(node.Item.Cone);
                node = tree.Next(node);
            } while (node != null && Point.IntervalIntersectsRay(leftPoint, rightPoint,
                                                                 node.Item.Start, node.Item.Direction, out x));


            foreach (Cone cone in conesToRemove)
            {
                RemoveCone(cone);
            }
        }
Example #8
0
        Dictionary <VisibilityEdge, VisibilityVertex> GetEdgeSnapAtVertexMap(LgNodeInfo nodeInfo)
        {
            var ret    = new Dictionary <VisibilityEdge, VisibilityVertex>();
            var center = nodeInfo.Center;
            RbTree <VisibilityVertex> nodeBoundaryRbTree =
                new RbTree <VisibilityVertex>((a, b) => CompareByAngleFromNodeCenter(a, b, center));

            foreach (var p in nodeInfo.BoundaryOnLayer)
            {
                nodeBoundaryRbTree.Insert(_visGraph.AddVertex(p));
            }

            var nodeInfoCenterV = VisGraph.FindVertex(center);

            if (nodeInfoCenterV == null)
            {
                return(ret);
            }
            foreach (var e in nodeInfoCenterV.OutEdges)
            {
                SnapToAfterBefore(e.Target, nodeBoundaryRbTree, center, ret, e);
            }
            foreach (var e in nodeInfoCenterV.InEdges)
            {
                SnapToAfterBefore(e.Source, nodeBoundaryRbTree, center, ret, e);
            }
            return(ret);
        }
Example #9
0
 private static void VerifyProperties(RbTree t)
 {
     VerifyProperty1(t.Root);
     VerifyProperty2(t.Root);
     // Property 3 is implicit
     VerifyProperty4(t.Root);
     VerifyProperty5(t.Root);
 }
 public EdgeTracer(CdtEdge edge, Set<CdtTriangle> triangles, RbTree<CdtFrontElement> front, List<CdtSite> leftPolygon, List<CdtSite> rightPolygon) {
     this.edge = edge;
     this.triangles = triangles;
     this.front = front;
     this.leftPolygon = leftPolygon;
     this.rightPolygon = rightPolygon;
     a = edge.upperSite;
     b = edge.lowerSite;
 }
Example #11
0
 public LineSweeperBase(IEnumerable <Polyline> obstacles, Point sweepDirection)
 {
     Obstacles             = obstacles;
     SweepDirection        = sweepDirection;
     DirectionPerp         = sweepDirection.Rotate(-Math.PI / 2);
     EventQueue            = new BinaryHeapWithComparer <SweepEvent>(this);
     ObstacleSideComparer  = new ObstacleSideComparer(this);
     LeftObstacleSideTree  = new RbTree <SegmentBase>(ObstacleSideComparer);
     RightObstacleSideTree = new RbTree <SegmentBase>(ObstacleSideComparer);
 }
Example #12
0
        private void ExtendPathAlongOutEdges(VertexEntry bestEntry, RbTree <VisibilityEdge> edges, Directions preferredBendDir)
        {
            // Avoid GetEnumerator overhead.
            var outEdgeNode = edges.IsEmpty() ? null : edges.TreeMinimum();

            for (; outEdgeNode != null; outEdgeNode = edges.Next(outEdgeNode))
            {
                ExtendPathAlongEdge(bestEntry, outEdgeNode.Item, false, preferredBendDir);
            }
        }
 public EdgeTracer(CdtEdge edge, Set <CdtTriangle> triangles, RbTree <CdtFrontElement> front, List <CdtSite> leftPolygon, List <CdtSite> rightPolygon)
 {
     this.edge         = edge;
     this.triangles    = triangles;
     this.front        = front;
     this.leftPolygon  = leftPolygon;
     this.rightPolygon = rightPolygon;
     a = edge.upperSite;
     b = edge.lowerSite;
 }
Example #14
0
        void SnapToAfterBefore(VisibilityVertex v, RbTree <VisibilityVertex> nodeBoundaryRbTree, Point center, Dictionary <VisibilityEdge, VisibilityVertex> ret, VisibilityEdge e)
        {
            VisibilityVertex beforeV, afterV;

            FindBeforeAfterV(v, nodeBoundaryRbTree, out beforeV, out afterV, center);
            var beforeAngle = Point.Angle(beforeV.Point - center, v.Point - center);
            var afterAngle  = Point.Angle(v.Point - center, afterV.Point - center);

            ret[e] = beforeAngle <= afterAngle ? beforeV : afterV;
        }
Example #15
0
        public override IEnumerable <TElement> Sort(IEnumerable <TElement> collection, IComparer <TElement> comparer, bool parallel)
        {
            var tree = new RbTree(comparer);

            foreach (var element in collection)
            {
                tree.Add(element);
            }

            return(tree);
        }
Example #16
0
        void FindBeforeAfterV(VisibilityVertex v, RbTree <VisibilityVertex> nodeBoundaryRbTree,
                              out VisibilityVertex beforeV, out VisibilityVertex afterV, Point center)
        {
            Point xDir   = new Point(1, 0);
            var   vAngle = Point.Angle(xDir, v.Point - center);
            var   rNode  = nodeBoundaryRbTree.FindLast(w => Point.Angle(xDir, w.Point - center) <= vAngle);

            beforeV = rNode != null ? rNode.Item : nodeBoundaryRbTree.TreeMaximum().Item;
            rNode   = nodeBoundaryRbTree.FindFirst(w => Point.Angle(xDir, w.Point - center) >= vAngle);
            afterV  = rNode != null ? rNode.Item : nodeBoundaryRbTree.TreeMinimum().Item;
        }
 LineSweeperForPortLocations(IEnumerable<Polyline> obstacles, Point direction, Point coneRsDir, Point coneLsDir,
                             VisibilityGraph visibilityGraph, IEnumerable<Point> portLocations)
     : base(obstacles, direction) {
     this.visibilityGraph = visibilityGraph;
     ConeRightSideDirection = coneRsDir;
     ConeLeftSideDirection = coneLsDir;
     coneSideComparer = new ConeSideComparer(this);
     leftConeSides = new RbTree<ConeSide>(coneSideComparer);
     rightConeSides = new RbTree<ConeSide>(coneSideComparer);
     PortLocations = portLocations;
 }
 LineSweeperForPortLocations(IEnumerable <Polyline> obstacles, Point direction, Point coneRsDir, Point coneLsDir,
                             VisibilityGraph visibilityGraph, IEnumerable <Point> portLocations)
     : base(obstacles, direction)
 {
     this.visibilityGraph   = visibilityGraph;
     ConeRightSideDirection = coneRsDir;
     ConeLeftSideDirection  = coneLsDir;
     coneSideComparer       = new ConeSideComparer(this);
     leftConeSides          = new RbTree <ConeSide>(coneSideComparer);
     rightConeSides         = new RbTree <ConeSide>(coneSideComparer);
     PortLocations          = portLocations;
 }
        public override IEnumerable <TElement> Sort(IEnumerable <TElement> collection, IComparer <TElement> comparer, bool parallel)
        {
            var array = collection.ToArray();
            var tree  = new RbTree(comparer, array);

            for (int i = 0; i < array.Length; i++)
            {
                tree.Add(i);
            }

            return(tree);
        }
 LineSweeper(IEnumerable<Polyline> obstacles, Point direction, Point coneRsDir, Point coneLsDir,
             VisibilityGraph visibilityGraph, Set<Point> ports, Polyline borderPolyline)
     : base(obstacles, direction) {
     this.visibilityGraph = visibilityGraph;
     ConeRightSideDirection = coneRsDir;
     ConeLeftSideDirection = coneLsDir;
     coneSideComparer = new ConeSideComparer(this);
     leftConeSides = new RbTree<ConeSide>(coneSideComparer);
     rightConeSides = new RbTree<ConeSide>(coneSideComparer);
     Ports = ports;
     BorderPolyline = borderPolyline;
     PortEdgesCreator = (a, b) => new TollFreeVisibilityEdge(a, b);
 }
Example #21
0
 public void Setup()
 {
     tree = new RbTree <int>();
     tree.Add(8);
     tree.Add(5);
     tree.Add(9);
     tree.Add(2);
     tree.Add(6);
     tree.Add(11);
     tree.Add(1);
     tree.Add(0);
     tree.Add(-1);
     tree.Add(-2);
 }
Example #22
0
        public void Test_RBTREE_ENUMERATE()
        {
            RbTree rbTree = new RbTree();

            System.Collections.Generic.IList <IDirectoryEntry> repo = GetDirectoryRepository(10000);

            foreach (var item in repo)
            {
                rbTree.Insert(item);
            }

            VerifyProperties(rbTree);
            //rbTree.Print();
        }
Example #23
0
        public void Delete_TryDeleteNoExistElement_Exception()
        {
            var comparer = new StringComparator();

            var map = new RbTree <string, int>(comparer);

            map.Insert("kgj", 45);

            map.Insert("a", 21);

            map.Insert("vba", 90);

            Assert.Throws <Exception>(() => map.Delete("b"));
        }
Example #24
0
 public void Setup()
 {
     tree = new RbTree <int>();
     tree.Add(3);
     tree.Add(2);
     tree.Add(4);
     tree.Add(1);
     tree.Add(5);
     tree.Add(0);
     tree.Add(-1);
     tree.Add(-2);
     tree.Add(-3);
     tree.Add(-4);
     tree.Add(-5);
 }
Example #25
0
        static RBNode <VisibilityEdge> FindFirst(RBNode <VisibilityEdge> n, RbTree <VisibilityEdge> tree, Point targetPoint)
        {
            if (n == tree.Nil)
            {
                return(null);
            }
            RBNode <VisibilityEdge> good = null;

            while (n != tree.Nil)
            {
                n = n.Item.TargetPoint >= targetPoint ? (good = n).left : n.right;
            }

            return(good);
        }
Example #26
0
        public void Find_TryFindNoExistElement_Exception()
        {
            var comparer = new StringComparator();

            var map = new RbTree <string, int>(comparer);

            map.Insert("ffg", 45);

            map.Insert("acb", 21);

            map.Insert("hyred", 90);

            map.Delete("acb");

            Assert.Throws <Exception>(() => map.Find("acb"));
        }
Example #27
0
        static void GenerateFile(string filename)
        {
            using var writer = File.CreateText(filename);

            var tree = new RbTree <string, string>((one, two) => one.CompareTo(two));

            var generator = new StringGenerator();

            for (int i = 0; i < 100_000; i++)
            {
                var str = generator.GenerateString();

                tree.Add(str, str);
                writer.WriteLine(str);
            }
        }
Example #28
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="direction"></param>
        /// <param name="obstacles"></param>
        /// <param name="axisEdgesToObstaclesTheyOriginatedFrom"></param>
        /// <param name="pathOrders"></param>
        /// <param name="axisEdges">edges to find the empty space around</param>
        internal FreeSpaceFinder(Directions direction, IEnumerable <Polyline> obstacles, Dictionary <AxisEdge, Polyline> axisEdgesToObstaclesTheyOriginatedFrom, Dictionary <AxisEdge, List <PathEdge> > pathOrders,
                                 IEnumerable <AxisEdge> axisEdges) : base(obstacles, new CompassVector(direction).ToPoint())
        {
            DirectionPerp = new CompassVector(direction).Right.ToPoint();
            PathOrders    = pathOrders;
            xProjection   = direction == Directions.North ? (PointProjection)X : MinusY;
#if SHARPKIT //https://code.google.com/p/sharpkit/issues/detail?id=301
            edgeContainersTree = new RbTree <AxisEdgesContainer>(new FreeSpaceFinderComparer(this));
#else
            edgeContainersTree = new RbTree <AxisEdgesContainer>(this);
#endif
            SweepPole = CompassVector.VectorDirection(SweepDirection);
            Debug.Assert(CompassVector.IsPureDirection(SweepPole));
            AxisEdges = axisEdges;
            AxisEdgesToObstaclesTheyOriginatedFrom = axisEdgesToObstaclesTheyOriginatedFrom;
        }
Example #29
0
        private static void Main()
        {
            var rbtree = new RbTree <int>();

            rbtree.Insert(3);
            rbtree.Insert(2);
            rbtree.Insert(5);
            rbtree.Insert(7);
            rbtree.Insert(6);
            rbtree.Insert(11);
            rbtree.Insert(13);
            rbtree.Insert(1);
            rbtree.Insert(4);
            rbtree.Preorder();
            Console.WriteLine(rbtree.Find(2));
            Console.ReadLine();
        }
Example #30
0
        public void Insert_InsertThreeElements_CorrectInsert()
        {
            var comparer = new StringComparator();

            var map = new RbTree <string, int>(comparer);

            map.Insert("dog", 44);

            map.Insert("cat", 63);

            map.Insert("sun", 81);

            var actualCount = map.Count;

            var expectedCount = 3;

            Assert.AreEqual(expectedCount, actualCount);
        }
Example #31
0
        public void Print_InsertThreeElements_CorrectPrint()
        {
            var comparer = new StringComparator();

            var map = new RbTree <string, int>(comparer);

            map.Insert("tt", 145);

            map.Insert("bb", 323);

            map.Insert("gg", 476);

            var actualOutput = map.ToString();

            var expectedOutput = "key = bb value = 323\nkey = gg value = 476\nkey = tt value = 145\n";

            Assert.AreEqual(expectedOutput, actualOutput);
        }
Example #32
0
        public void Clear_InsertTwoElementsAndClear_EmptyMap()
        {
            var comparer = new StringComparator();

            var map = new RbTree <string, int>(comparer);

            map.Insert("me", 55);

            map.Insert("you", 43);

            map.Clear();

            var actualCount = map.Count;

            var expectedCount = 0;

            Assert.AreEqual(expectedCount, actualCount);
        }
Example #33
0
        public void Setup()
        {
            t1 = new RbTree <int>();
            t1.Add(3);
            t1.Add(2);
            t1.Add(4);
            t1.Add(1);
            t1.Add(5);
            t1.Add(0);
            t1.Add(-1);
            t1.Add(-2);

            t2 = new RbTree <int>();
            t2.Add(9);
            t2.Add(8);
            t2.Add(10);
            t2.Add(7);
            t2.Add(11);
        }
 void SnapToAfterBefore(VisibilityVertex v, RbTree<VisibilityVertex> nodeBoundaryRbTree, Point center, Dictionary<VisibilityEdge, VisibilityVertex> ret, VisibilityEdge e) {
     VisibilityVertex beforeV, afterV;
     FindBeforeAfterV(v, nodeBoundaryRbTree, out beforeV, out afterV, center);
     var beforeAngle = Point.Angle(beforeV.Point - center, v.Point - center);
     var afterAngle = Point.Angle(v.Point - center, afterV.Point - center);
     ret[e] = beforeAngle <= afterAngle ? beforeV : afterV;
 }
        Dictionary<VisibilityEdge, VisibilityVertex> GetEdgeSnapAtVertexMap(LgNodeInfo nodeInfo) {
            var ret = new Dictionary<VisibilityEdge, VisibilityVertex>();
            var center = nodeInfo.Center;
            RbTree<VisibilityVertex> nodeBoundaryRbTree =
                new RbTree<VisibilityVertex>((a, b) => CompareByAngleFromNodeCenter(a, b, center));
            foreach (var p in nodeInfo.BoundaryOnLayer)
                nodeBoundaryRbTree.Insert(_visGraph.AddVertex(p));

            var nodeInfoCenterV = VisGraph.FindVertex(center);
            if (nodeInfoCenterV == null) return ret;
            foreach (var e in nodeInfoCenterV.OutEdges)
                SnapToAfterBefore(e.Target, nodeBoundaryRbTree, center, ret, e);
            foreach (var e in nodeInfoCenterV.InEdges)
                SnapToAfterBefore(e.Source, nodeBoundaryRbTree, center, ret, e);
            return ret;
        }
        void CloseConesCoveredBySegment(Point leftPoint, Point rightPoint, RbTree<ConeSide> tree) {
            var node = tree.FindFirst(
                s => Point.GetTriangleOrientation(s.Start, s.Start + s.Direction, leftPoint) ==
                     TriangleOrientation.Counterclockwise);

            Point x;
            if (node == null || !Point.IntervalIntersectsRay(leftPoint, rightPoint,
                                                             node.Item.Start, node.Item.Direction, out x))
                return;
            var conesToRemove = new List<Cone>();
            do {
                conesToRemove.Add(node.Item.Cone);
                node = tree.Next(node);
            } while (node != null && Point.IntervalIntersectsRay(leftPoint, rightPoint,
                                                                 node.Item.Start, node.Item.Direction, out x));


            foreach (var cone in conesToRemove)
                RemoveCone(cone);
        }
 RBNode<ConeSide> InsertToTree(RbTree<ConeSide> tree, ConeSide coneSide) {
     Debug.Assert(coneSide.Direction * SweepDirection > 0);
     coneSideComparer.SetOperand(coneSide);
     return tree.Insert(coneSide);
 }
 private void AllocateDataStructures(int i) {
     tangents = new List<Tangent>();
     diagonals = new List<Diagonal>();
     activeDiagonalTree = new RbTree<Diagonal>(this.activeDiagonalComparer);
     this.currentPolygon = polygons[i];
 }
Example #39
0
		public ReversedTree(RbTree tree) {
			_tree = tree;
		}
 public EdgeInserter(CdtEdge edge, Set<CdtTriangle> triangles, RbTree<CdtFrontElement> front, Func<CdtSite, CdtSite, CdtEdge> createEdgeDelegate) {
     this.edge = edge;
     this.triangles = triangles;
     this.front = front;
     this.createEdgeDelegate = createEdgeDelegate;
 }
 RBNode<ConeSide> InsertToTree(RbTree<ConeSide> tree, ConeSide coneSide) {
     Debug.Assert(coneSide.Direction*SweepDirection > ApproximateComparer.DistanceEpsilon);
     coneSideComparer.SetOperand(coneSide);
     return tree.Insert(coneSide);
 }
 private void AllocateDataStructures() {
     tangents = new List<Tangent>();
     diagonals = new List<Diagonal>();
     activeDiagonalTree = new RbTree<Diagonal>(this.activeDiagonalComparer);
 }
 void InitActiveEdgesAndActiveEdgesComparer() {
     activeEdgeComparer = new ActiveEdgeComparerWithRay {Pivot = q};
     activeSidesTree = new RbTree<PolylinePoint>(activeEdgeComparer);
 }
        internal static void ShowFront(IEnumerable<CdtTriangle> cdtTriangles, RbTree<CdtFrontElement> cdtFrontElements, IEnumerable<ICurve> redCurves, IEnumerable<ICurve> blueCurves) {
            List<DebugCurve> ls = new List<DebugCurve>();
            if (redCurves != null)
                foreach (var c in redCurves)
                    ls.Add(new DebugCurve(100, 0.5, "red", c));
            if (blueCurves != null)
                foreach (var c in blueCurves)
                    ls.Add(new DebugCurve(100, 2, "blue", c));

            if (cdtFrontElements != null)
                foreach (var frontElement in cdtFrontElements)
                    ls.Add(new DebugCurve(100, 0.001, "green",
                                          new LineSegment(frontElement.Edge.upperSite.Point,
                                                          frontElement.Edge.lowerSite.Point)));
            foreach (var t in cdtTriangles) {
                for (int i = 0; i < 3; i++) {
                    var e = t.Edges[i];
                    ls.Add(GetDebugCurveOfCdtEdge(e));
                }
            }
            LayoutAlgorithmSettings.ShowDebugCurvesEnumeration(ls);
        }
 void FindBeforeAfterV(VisibilityVertex v, RbTree<VisibilityVertex> nodeBoundaryRbTree,
     out VisibilityVertex beforeV, out VisibilityVertex afterV, Point center) {
     Point xDir=new Point(1,0);
     var vAngle = Point.Angle(xDir, v.Point - center);
     var rNode = nodeBoundaryRbTree.FindLast(w => Point.Angle(xDir, w.Point - center) <= vAngle);
     beforeV = rNode!=null? rNode.Item : nodeBoundaryRbTree.TreeMaximum().Item;
     rNode = nodeBoundaryRbTree.FindFirst(w => Point.Angle(xDir, w.Point - center) >= vAngle);
     afterV = rNode != null ? rNode.Item : nodeBoundaryRbTree.TreeMinimum().Item;
 }
 private void ExtendPathAlongOutEdges(VertexEntry bestEntry, RbTree<VisibilityEdge> edges, Directions preferredBendDir) {
     // Avoid GetEnumerator overhead.
     var outEdgeNode = edges.IsEmpty() ? null : edges.TreeMinimum();
     for (; outEdgeNode != null; outEdgeNode = edges.Next(outEdgeNode)) {
         ExtendPathAlongEdge(bestEntry, outEdgeNode.Item, false, preferredBendDir);
     }
 }
 internal static RBNode<CdtFrontElement> FindNodeInFrontBySite(RbTree<CdtFrontElement> cdtFrontElements, CdtSite piSite) {
     return  cdtFrontElements.FindLast(x => x.LeftSite.Point.X <= piSite.Point.X);
 }
Example #48
0
			public Enumerator(RbTree tree) {
				_tree = tree;
			}