SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
HilbertCurveBatchSort.java
1/*
2 * Class: HilbertCurveBatchSort
3 * Description: performs a batch sort on the arrays
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2015 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 */
24package umontreal.ssj.util.multidimsort;
25
70public class HilbertCurveBatchSort<T extends MultiDimComparable<? super T>> implements MultiDimSortComparable<T> {
71 int dimension; // Dimension d of the points used for the sort.
72 int m; // Number of bit retained for each coordinate. Must be <= 31.
73 HilbertCurveMap hcMap;
74
75 BatchSortPow2<T> bsort; // Batch sort used.
76 long[][] indexH; // Table whose each line gives the number of an object after the
77 // batch sort in first coord. and its Hilbert index in second coord.
78 // Here, the second coordinates are all distinct!
79 // For a given n, this index is computed once for all and saved.
80 int nSavedIndex = 0; // Value of n for which indexH is now computed.
81
96 public HilbertCurveBatchSort(double[] batchExponents, int m) {
97 hcMap = new HilbertCurveMap(batchExponents.length, m);
98 dimension = batchExponents.length;
99 this.m = m;
100 bsort = new BatchSortPow2<T>(batchExponents);
101 }
102
115 public HilbertCurveBatchSort(double[] batchExponents, int m, HilbertCurveMap map) {
116 dimension = batchExponents.length;
117 this.m = m;
118 if (map.dimension() != dimension)
119 throw new IllegalArgumentException("HilbertCurveMap has a different dimension! Expecting: " + dimension);
120 this.hcMap = map;
121 bsort = new BatchSortPow2<T>(batchExponents);
122 }
123
134 public void computeIndexH(int n) {
135 if (n == nSavedIndex)
136 return;
137 nSavedIndex = n;
138 indexH = new long[n][2]; // To put Hilbert indexes of points.
139 // For the following, we need e_0, ..., e_{d-1}.
140 int twom = (1 << m); // 2^m.
141 int mdim = dimension - 1;
142 int[] ej = bsort.getBitNumbers();
143 int stepj[] = new int[dimension]; // The integers 2^{m-e_j}.
144 int[] icoord = new int[dimension]; // The integer coordinates of current point.
145 for (int j = 0; j < dimension; ++j) {
146 if (ej[j] > m)
147 throw new RuntimeException("ej[j] is larger than m");
148 icoord[j] = 0;
149 stepj[j] = 1 << (m - ej[j]);
150 }
151 indexH[0][0] = 0;
152 indexH[0][1] = 0;
153 for (int i = 1; i < n; ++i) {
154 icoord[mdim] += stepj[mdim]; // Increment last coordinate.
155 if (icoord[mdim] == twom) { // Must propagate the carry.
156 for (int j = mdim; j >= 0; --j) {
157 if (icoord[j] < twom)
158 break; // Exit for loop if no carry.
159 icoord[j] = 0;
160 ++icoord[j - 1];
161 }
162 }
163 indexH[i][0] = i;
164 indexH[i][1] = hcMap.coordinatesToIndex(icoord);
165 }
166
167 // use static method of HilbertCurveSort to sort
168 HilbertCurveSort.sortIndexOfLong2(indexH, 0, n); // First coordinate of indexH gives the ordering
169 // that must be applied.
170 }
171
176 public void sort(T[] a, int iMin, int iMax) {
177 if (iMin + 1 == iMax)
178 return;
179 if (iMax - iMin != nSavedIndex)
180 computeIndexH(iMax - iMin); // This changes nSavedIndex.
181 bsort.sort(a, iMin, iMax); // This changes nSaved if needed.
182 // Now use the sorted indexH to sort a.
183 // First make a copy, and pick the elements from this unmodified copy.
184 T[] aclone = a.clone(); // We should not clone all the objects in a,
185 // but only the array of pointers.
186 // T[] aclone = new Object[iMax];
187
188 for (int i = iMin; i < iMax; ++i) {
189 a[i] = aclone[(int) indexH[i][0]];
190 }
191 }
192
196 public void sort(T[] a) {
197 sort(a, 0, a.length);
198 }
199
200 public void sort(double[][] a, int iMin, int iMax) {
201 if (iMin + 1 == iMax)
202 return;
203 if (iMax - iMin != nSavedIndex)
204 computeIndexH(iMax - iMin); // This changes nSavedIndex.
205 bsort.sort(a, iMin, iMax); // This changes nSaved if needed.
206 // Now use the sorted indexH to sort a.
207 // First make a copy, and pick the elements from this unmodified copy.
208 double[][] aclone = a.clone();
209 for (int i = iMin; i < iMax; ++i) {
210 a[i] = aclone[(int) indexH[i][0]];
211 }
212 }
213
214 public void sort(double[][] a) {
215 sort(a, 0, a.length);
216 }
217
221 public int dimension() {
222 return dimension;
223 }
224
229 return hcMap;
230 }
231
232}
This is a subclass of BatchSort for which the batch numbers.
void sort(T[] a, int iMin, int iMax)
Sorts the subarray a[iMin..iMax-1] using this batch sort.
void sort(T[] a, int iMin, int iMax)
Sorts the subarray a[iMin..iMax-1] of MultiDimComparable<T> objects with this sort.
HilbertCurveBatchSort(double[] batchExponents, int m, HilbertCurveMap map)
This constructor is similar to HilbertCurveBatchSort(double[],int) except that the mapping object is ...
HilbertCurveBatchSort(double[] batchExponents, int m)
Constructs a HilbertCurveBatchSort that will use the.
HilbertCurveMap getHilbertCurveMap()
Returns the HilbertCurveMap used for the mapping.
void computeIndexH(int n)
For the given , ’s, and , computes and sort the table that gives the Hilbert index of each object fr...
int dimension()
Returns the dimension of the unit hypercube.
This class implements the mapping of a Hilbert curve in the.
This class implements a MultiDimSort01<T extends MultiDim01> that can sort an array of points in the ...
static void sortIndexOfLong2(long[][] index, int iMin, int iMax)
Sorts the index table by its second coordinate.
This interface is an extension (or variant) of the Comparable interface in Java.
This interface extends MultiDimSort<T> to implement multivariate sorting algorithms that sort objects...