/// <summary> /// Partions the vertices based on the pivot value. /// </summary> /// <typeparam name="TEdgeData"></typeparam> /// <param name="graph">The graph to sort.</param> /// <param name="n">The hilbert accuracy.</param> /// <param name="left">The first vertex to consider.</param> /// <param name="right">The last vertex to consider.</param> /// <return>The new left.</return> private static uint SortHilbertPartition <TEdgeData>(GraphBase <TEdgeData> graph, int n, uint left, uint right) where TEdgeData : struct, IGraphEdgeData { // get the pivot value. uint start = left; ulong pivotValue = graph.HilbertDistance(n, left); left++; while (true) { ulong leftValue = graph.HilbertDistance(n, left); while (left <= right && leftValue <= pivotValue) { // move the left to the first value bigger than pivot. left++; leftValue = graph.HilbertDistance(n, left); } ulong rightValue = graph.HilbertDistance(n, right); while (left <= right && rightValue > pivotValue) { // move the right to the first value smaller than pivot. right--; rightValue = graph.HilbertDistance(n, right); } if (left > right) { // we are done searching, left is to the right of right. // make sure the pivot value is where it is supposed to be. graph.Switch(start, left - 1); return(left); } // swith left<->right. graph.Switch(left, right); } }
/// <summary> /// Sorts the vertices in the given graph based on a hilbert curve. /// </summary> /// <typeparam name="TEdgeData"></typeparam> public static void BuildHilbertRank <TEdgeData>(this GraphBase <TEdgeData> graph, int n, HugeArrayBase <uint> ranks) where TEdgeData : struct, IGraphEdgeData { if (graph == null) { throw new ArgumentNullException("graph"); } if (graph.VertexCount != ranks.Length - 1) { throw new ArgumentException("Graph and ranks array must have equal sizes."); } for (uint i = 0; i <= graph.VertexCount; i++) { // fill with default data. ranks[i] = i; } if (graph.VertexCount == 1) { // just return the rank for the one vertex. ranks[0] = 0; ranks[1] = 1; } // sort the complete graph and keep the transformations. QuickSort.Sort((i) => graph.HilbertDistance(n, ranks[i]), (i, j) => { var temp = ranks[i]; ranks[i] = ranks[j]; ranks[j] = temp; }, 1, graph.VertexCount); }