SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
HilbertCurveSort.java
1/*
2 * Class: HilbertCurveSort
3 * Description: Sorts d-dimensional points in [0,1)^d based on Hilbert curve.
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2014 Pierre L'Ecuyer and Universite de Montreal
7 * Organization: DIRO, Universite de Montreal
8 * @author
9 * @since
10
11 * SSJ is free software: you can redistribute it and/or modify it under
12 * the terms of the GNU General Public License (GPL) as published by the
13 * Free Software Foundation, either version 3 of the License, or
14 * any later version.
15
16 * SSJ is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20
21 * A copy of the GNU General Public License is available at
22 <a href="http://www.gnu.org/licenses">GPL licence site</a>.
23 */
24
25/* IMPORTANT NOTE:
26* Much of this code has been taken (with adaptations) from
27* the hilbert.c code
28* Author: Spencer W. Thomas
29* EECS Dept.
30* University of Michigan
31* Date: Thu Feb 7 1991
32* Copyright (c) 1991, University of Michigan
33*/
34package umontreal.ssj.util.multidimsort;
35
36import java.util.Comparator;
37import java.util.Arrays;
38
119public class HilbertCurveSort implements MultiDimSort01<MultiDim01> {
120
121 int dimension; // Dimension d of the points used for the sort.
122 long[][] indexForSort; // This is the index computed by the last Hilbert sort,
123 // sorted by the second coordinate. The order of the first coordinates
124 // gives the permutation made by that sort.
125 // This index is recomputed each time we sort.
126 HilbertCurveMap hcMap; // The map used for sorting
127
137 public HilbertCurveSort(int d, int m) {
138 dimension = d;
139 hcMap = new HilbertCurveMap(d, m);
140 }
141
149 this.hcMap = map;
150 this.dimension = map.dimension();
151 }
152
157 public void sort(MultiDim01[] a, int iMin, int iMax) {
158 // Copy the (0,1)^d transformations of the states in array b.
159 double b[][] = new double[iMax][dimension];
160 for (int i = iMin; i < iMax; ++i)
161 b[i] = a[i].getPoint();
162 // Sort this array b by Hilbert sort. The index
163 // indexForSort will contain the permutation made by that sort.
164 sort(b, iMin, iMax);
165 // Now use indexForSort to sort a.
166 // We do not want to clone all the objects in a,
167 // but only the array of pointers.
168 MultiDim01[] aclone = a.clone(); // new Object[iMax];
169 for (int i = iMin; i < iMax; ++i)
170 a[i] = aclone[(int) indexForSort[i][0]];
171 }
172
176 public void sort(MultiDim01[] a) {
177 sort(a, 0, a.length);
178 }
179
183 public void sort(double[][] a, int iMin, int iMax) {
184 if (iMin + 1 == iMax)
185 return;
186 indexForSort = new long[iMax][2]; // Index used for sort.
187 int[] icoord = new int[dimension]; // To store integer coordinates.
188 for (int i = 0; i < a.length; ++i) {
189 // Transform to integer coordinates.
190 hcMap.pointToCoordinates(a[i], icoord);
191 indexForSort[i][0] = i;
192 indexForSort[i][1] = hcMap.coordinatesToIndex(icoord); // Hilbert index of this point.
193 }
194 // Sort the index based on the positions on the Hilbert curve
195 sortIndexOfLong2(indexForSort, iMin, iMax);
196 // Now use the sorted index to sort a.
197 double[][] aclone = a.clone(); // Save copy of a before the sort.
198 for (int i = iMin; i < iMax; ++i) {
199 a[i] = aclone[(int) indexForSort[i][0]];
200 }
201 }
202
203 public void sort(double[][] a) {
204 sort(a, 0, a.length);
205 }
206
212 public long[][] getIndexAfterSort() {
213 return indexForSort;
214 }
215
219 public int dimension() {
220 return dimension;
221 }
222
227 return hcMap;
228 }
229
230 // Compares two arrays of long according to their second coordinate.
231 // This is used to sort an index of type long[][2] by the second coordinate.
232 // The permutation can be recovered in the first coordinate.
233
237 public static class LongIndexComparator2 implements Comparator<long[]> {
238 public int compare(long[] p1, long[] p2) {
239 if (p1[1] > p2[1])
240 return 1;
241 else if (p1[1] < p2[1])
242 return -1;
243 else
244 return 0;
245 }
246 }
247
251 public static void sortIndexOfLong2(long[][] index, int iMin, int iMax) {
252 // if (iMin==(iMax-1)) return;
253 Arrays.sort(index, iMin, iMax, new LongIndexComparator2());
254 }
255
256}
This class implements the mapping of a Hilbert curve in the.
HilbertCurveSort(int d, int m)
Constructs a HilbertCurveSort object that will use the first.
void sort(MultiDim01[] a)
Sorts the entire array: same as sort (a, 0, a.length).
static void sortIndexOfLong2(long[][] index, int iMin, int iMax)
Sorts the index table by its second coordinate.
long[][] getIndexAfterSort()
Returns the index computed by the last sort, which is sorted by the second coordinate.
HilbertCurveMap getHilbertCurveMap()
Returns the HilbertCurveMap used for the mapping.
void sort(double[][] a, int iMin, int iMax)
Sort the array with Hilbert sort.
int dimension()
Returns the dimension of the unit hypercube.
HilbertCurveSort(HilbertCurveMap map)
Constructs a HilbertCurveSort object with a given mapping of the Hilbert curve in the unit hypercube.
void sort(MultiDim01[] a, int iMin, int iMax)
Sorts the subarray a[iMin..iMax-1] with this Hilbert curve sort.
This interface represents a point or array of dimensions in a unit hypercube .
This interface extends MultiDimSort<T> to implement multivariate sorting algorithms that sort points ...