Пример #1
0
        public void SortHilbertTestSteps4()
        {
            var n = 4;

            // build locations.
            var locations = new List <Coordinate>();

            locations.Add(new Coordinate(-90, -180));
            locations.Add(new Coordinate(-90, -60));
            locations.Add(new Coordinate(-90, 60));
            locations.Add(new Coordinate(-90, 180));
            locations.Add(new Coordinate(-30, -180));
            locations.Add(new Coordinate(-30, -60));
            locations.Add(new Coordinate(-30, 60));
            locations.Add(new Coordinate(-30, 180));
            locations.Add(new Coordinate(30, -180));
            locations.Add(new Coordinate(30, -60));
            locations.Add(new Coordinate(30, 60));
            locations.Add(new Coordinate(30, 180));
            locations.Add(new Coordinate(90, -180));
            locations.Add(new Coordinate(90, -60));
            locations.Add(new Coordinate(90, 60));
            locations.Add(new Coordinate(90, 180));

            // build graph.
            var graph = new GeometricGraph(1);

            for (var vertex = 0; vertex < locations.Count; vertex++)
            {
                graph.AddVertex((uint)vertex, locations[vertex].Latitude,
                                locations[vertex].Longitude);
            }

            // build a sorted version in-place.
            graph.Sort(n);

            // test if sorted.
            for (uint vertex = 1; vertex < graph.VertexCount - 1; vertex++)
            {
                Assert.IsTrue(
                    graph.Distance(n, vertex) <=
                    graph.Distance(n, vertex + 1));
            }

            // sort locations.
            locations.Sort((x, y) =>
            {
                return(HilbertCurve.HilbertDistance(x.Latitude, x.Longitude, n).CompareTo(
                           HilbertCurve.HilbertDistance(y.Latitude, y.Longitude, n)));
            });

            // confirm sort.
            for (uint vertex = 0; vertex < graph.VertexCount; vertex++)
            {
                float latitude, longitude;
                graph.GetVertex(vertex, out latitude, out longitude);
                Assert.AreEqual(latitude, locations[(int)vertex].Latitude);
                Assert.AreEqual(longitude, locations[(int)vertex].Longitude);
            }
        }
Пример #2
0
 public static void Sort(this GeometricGraph graph, int n)
 {
     if (graph.VertexCount <= 0U)
     {
         return;
     }
     QuickSort.Sort((Func <long, long>)(vertex => graph.Distance(n, (uint)vertex)), (Action <long, long>)((vertex1, vertex2) =>
     {
         if (vertex1 == vertex2)
         {
             return;
         }
         graph.Switch((uint)vertex1, (uint)vertex2);
     }), 0L, (long)(graph.VertexCount - 1U));
 }
Пример #3
0
        public static bool SearchRange(this GeometricGraph graph, long minHilbert, long maxHilbert, int n, uint vertex1, uint vertex2, out uint vertex, out int count)
        {
            long num1 = graph.Distance(n, vertex1);
            long num2 = graph.Distance(n, vertex2);

            while (vertex1 <= vertex2)
            {
                uint vertex3 = vertex1 + (vertex2 - vertex1) / 2U;
                long num3    = graph.Distance(n, vertex3);
                if (maxHilbert <= num3)
                {
                    vertex2 = vertex3;
                    num2    = num3;
                }
                else if (minHilbert >= num3)
                {
                    vertex1 = vertex3;
                    num1    = num3;
                }
                else
                {
                    uint vertex4 = vertex1;
                    uint vertex5 = vertex3;
                    uint num4    = uint.MaxValue;
                    while ((int)num4 == -1)
                    {
                        if (graph.Distance(n, vertex4) == minHilbert)
                        {
                            num4 = vertex4;
                        }
                        else if (graph.Distance(n, vertex5) == minHilbert)
                        {
                            num4 = vertex5;
                        }
                        else
                        {
                            while (vertex4 <= vertex5)
                            {
                                uint vertex6 = vertex4 + (vertex5 - vertex4) / 2U;
                                long num5    = graph.Distance(n, vertex6);
                                if (num5 > minHilbert)
                                {
                                    vertex5 = vertex6;
                                }
                                else if (num5 < minHilbert)
                                {
                                    vertex4 = vertex6;
                                }
                                else
                                {
                                    num4 = vertex6;
                                    break;
                                }
                                if ((int)vertex4 == (int)vertex5 || (int)vertex4 == (int)vertex5 - 1)
                                {
                                    break;
                                }
                            }
                            if ((int)num4 == -1)
                            {
                                ++minHilbert;
                                if (minHilbert == maxHilbert)
                                {
                                    vertex = vertex1;
                                    count  = 0;
                                    return(true);
                                }
                            }
                        }
                    }
                    uint vertex7 = vertex3;
                    uint vertex8 = vertex2;
                    uint num6    = uint.MaxValue;
                    while ((int)num6 == -1)
                    {
                        if (graph.Distance(n, vertex7) == maxHilbert)
                        {
                            num6 = vertex7;
                        }
                        else if (graph.Distance(n, vertex8) == maxHilbert)
                        {
                            num6 = vertex8;
                        }
                        else
                        {
                            while (vertex7 <= vertex8)
                            {
                                uint vertex6 = vertex7 + (vertex8 - vertex7) / 2U;
                                long num5    = graph.Distance(n, vertex6);
                                if (num5 > maxHilbert)
                                {
                                    vertex8 = vertex6;
                                }
                                else if (num5 < maxHilbert)
                                {
                                    vertex7 = vertex6;
                                }
                                else
                                {
                                    num6 = vertex6;
                                    break;
                                }
                                if ((int)vertex7 == (int)vertex8 || (int)vertex7 == (int)vertex8 - 1)
                                {
                                    break;
                                }
                            }
                            if ((int)num6 == -1)
                            {
                                --maxHilbert;
                                if (minHilbert == maxHilbert)
                                {
                                    num6 = num4;
                                    break;
                                }
                            }
                        }
                    }
                    uint num7 = num4;
                    while ((int)num7 != 0 && graph.Distance(n, num7 - 1U) == minHilbert)
                    {
                        --num7;
                    }
                    uint num8 = num6;
                    while ((int)num8 != (int)graph.VertexCount - 1 && graph.Distance(n, num8 + 1U) == maxHilbert)
                    {
                        ++num8;
                    }
                    vertex = num7;
                    count  = (int)num8 - (int)num7 + 1;
                    return(true);
                }
                if (num1 > num2)
                {
                    throw new Exception("Graph not sorted: Binary search using hilbert distance not possible.");
                }
                if (num1 == num2 || (int)vertex1 == (int)vertex2 || (int)vertex1 == (int)vertex2 - 1)
                {
                    vertex = vertex1;
                    count  = 0;
                    return(true);
                }
            }
            vertex = vertex1;
            count  = 0;
            return(false);
        }
Пример #4
0
        public static bool Search(this GeometricGraph graph, long hilbert, int n, uint vertex1, uint vertex2, out uint vertex, out int count)
        {
            long num1 = graph.Distance(n, vertex1);
            long num2 = graph.Distance(n, vertex2);

            while (vertex1 <= vertex2)
            {
                if (num1 > num2)
                {
                    throw new Exception("Graph not sorted: Binary search using hilbert distance not possible.");
                }
                if (num1 == hilbert)
                {
                    uint vertex3;
                    for (vertex3 = vertex1; num1 == hilbert && (int)vertex3 != 0; num1 = graph.Distance(n, vertex3))
                    {
                        --vertex3;
                    }
                    uint vertex4 = vertex1;
                    for (long index = graph.Distance(n, vertex4); index == hilbert && vertex4 < graph.VertexCount - 1U; index = graph.Distance(n, vertex4))
                    {
                        ++vertex4;
                    }
                    vertex = vertex3;
                    count  = (int)vertex4 - (int)vertex3 + 1;
                    return(true);
                }
                if (num2 == hilbert)
                {
                    uint vertex3;
                    for (vertex3 = vertex2; num2 == hilbert && (int)vertex3 != 0; num2 = graph.Distance(n, vertex3))
                    {
                        --vertex3;
                    }
                    uint vertex4 = vertex2;
                    for (long index = graph.Distance(n, vertex4); index == hilbert && vertex4 < graph.VertexCount - 1U; index = graph.Distance(n, vertex4))
                    {
                        ++vertex4;
                    }
                    vertex = vertex3;
                    count  = (int)vertex4 - (int)vertex3 + 1;
                    return(true);
                }
                if (num1 == num2 || (int)vertex1 == (int)vertex2 || (int)vertex1 == (int)vertex2 - 1)
                {
                    vertex = vertex1;
                    count  = 0;
                    return(true);
                }
                uint vertex5 = vertex1 + (vertex2 - vertex1) / 2U;
                long num3    = graph.Distance(n, vertex5);
                if (hilbert <= num3)
                {
                    vertex2 = vertex5;
                    num2    = num3;
                }
                else
                {
                    vertex1 = vertex5;
                    num1    = num3;
                }
            }
            vertex = vertex1;
            count  = 0;
            return(false);
        }