SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
BatchSort.java
1/*
2 * Class: BatchSort
3 * Description: performs a batch sort on the arrays
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001 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
26import java.util.Arrays;
27
71
72public class BatchSort<T extends MultiDimComparable<? super T>> implements MultiDimSortComparable<T> {
73
74 int dimension; // Number of coordinates.
75 boolean useExponents; // True if we use proportion exponents.
76 int[] batchNumbers; // Number of batches n_j for each dimension.
77 double[] batchExponents; // The alpha_j.
78 int batchProduct = 1; // Product p of numbers in batchNumbers.
79 int nSaved = 0; // Number n of objects last time sort was called.
80
90 public BatchSort(int[] batchNumbers) {
91 if (batchNumbers == null)
92 throw new NullPointerException("batchNumbers is null");
93 useExponents = false;
94 this.batchNumbers = batchNumbers;
95 dimension = batchNumbers.length;
96 batchProduct = 1;
97 for (int i = 0; i < batchNumbers.length; ++i)
98 batchProduct *= batchNumbers[i];
99 }
100
107 public BatchSort(double[] batchExponents) {
108 if (batchExponents == null)
109 throw new NullPointerException("batchExponents is null");
110 this.batchExponents = batchExponents;
111 useExponents = true;
112 dimension = batchExponents.length;
113 batchNumbers = new int[dimension];
114 double sum = 0.0;
115 for (int j = 0; j < dimension; ++j)
116 sum += batchExponents[j];
117 if (Math.abs(sum - 1) > 1.0e-10)
118 throw new IllegalArgumentException("Sum of batchExponents not equal to 1");
119 }
120
128 public void setBatchNumbers(int n) {
129 if (batchExponents == null)
130 throw new NullPointerException("batchExponents is null");
131 if (useExponents == false)
132 throw new IllegalArgumentException("method allowed only when using proportion exponents");
133 nSaved = n;
134 batchProduct = 1; // Product of batch numbers.
135 for (int dim = 0; dim < dimension; ++dim) {
136 batchNumbers[dim] = (int) Math.ceil(Math.pow(n, batchExponents[dim])); // Round up.
137 batchProduct *= batchNumbers[dim];
138 }
139 }
140
149 public void setBatchExponents(double[] batchExponents) {
150 if (batchExponents == null)
151 throw new NullPointerException("batchExponents is null");
152 if (batchExponents.length != dimension)
153 throw new IllegalArgumentException("batchExponents has wrong dimension");
154 nSaved = 0;
155 useExponents = true;
156 this.batchExponents = batchExponents;
157 double sumExponents = 0.0; // Sum of exponents.
158 for (int dim = 0; dim < dimension; ++dim) {
159 sumExponents += batchExponents[dim];
160 }
161 if (Math.abs(sumExponents - 1.0) > 1.0e-10)
162 throw new IllegalArgumentException("Sum of batchExponents not equal to 1");
163 }
164
168 public int[] getBatchNumbers() {
169 return batchNumbers;
170 }
171
175 public int getBatchProduct() {
176 return batchProduct;
177 }
178
182 public double[] getBatchExponents() {
183 return batchExponents;
184 }
185
186 public int dimension() {
187 return dimension;
188 }
189
193 public void sort(T[] a, int iMin, int iMax) {
194 if (iMin + 1 == iMax)
195 return;
196 if (useExponents && (nSaved != iMax - iMin))
197 setBatchNumbers(iMax - iMin);
198 int i1, i2;
199 int bsize = iMax - iMin; // Current batch size. At level 0: single batch.
200 for (int j = 0; (j < dimension) && (bsize > 1); ++j) {
202 // Sort all the batches (entire array) according to coordinate j.
203 if (batchNumbers[j] == 1)
204 continue; // Can skip the sort for this dim j.
205 // if (bsize <= 1) break;
206 i1 = iMin;
207 while (i1 < iMax) {
208 i2 = i1 + bsize;
209 if (i2 > iMax)
210 i2 = iMax;
211 Arrays.sort(a, i1, i2, compar);
212 i1 += bsize;
213 }
214 bsize = (int) Math.ceil(bsize / batchNumbers[j]); // New batch size.
215 }
216 }
217
221 public void sort(T[] a) {
222 sort(a, 0, a.length);
223 }
224
228 public void sort(double[][] a, int iMin, int iMax) {
229 if (iMin + 1 == iMax)
230 return;
231 if (useExponents && (nSaved != iMax - iMin))
232 setBatchNumbers(iMax - iMin);
233 int i1, i2;
234 int bsize = iMax - iMin; // Current batch size. At level 0: single batch.
235 for (int j = 0; (j < dimension) && (bsize > 1); ++j) {
237 // Sort all the batches (entire array) according to coordinate j.
238 if (batchNumbers[j] == 1)
239 continue; // Can skip the sort for this dim j.
240 i1 = iMin;
241 while (i1 < iMax) {
242 i2 = i1 + bsize;
243 if (i2 > iMax)
244 i2 = iMax;
245 Arrays.sort(a, i1, i2, compar);
246 i1 += bsize;
247 }
248 bsize = (int) Math.ceil(bsize / batchNumbers[j]); // New batch size.
249 }
250 }
251
255 public void sort(double[][] a) {
256 sort(a, 0, a.length);
257 }
258
259}
int getBatchProduct()
Returns the product of current batch numbers.
void sort(T[] a)
Sorts the entire array.
int[] getBatchNumbers()
Returns the current vector of batch numbers .
void setBatchExponents(double[] batchExponents)
Sets the vector of proportion exponents to the values in batchExponents.
BatchSort(double[] batchExponents)
Constructs a BatchSort that will use the proportion exponents in batchExponents, which must contain n...
void sort(double[][] a)
Sorts the entire array.
void setBatchNumbers(int n)
Resets the corresponding vector of batch numbers for the given number of objects.
double[] getBatchExponents()
Returns the current vector of batch exponents .
void sort(double[][] 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] using this batch sort.
BatchSort(int[] batchNumbers)
Constructs a BatchSort that will always use the (fixed) batch numbers given in batchNumbers.
This provides an implementation of Comparator in which arrays of double in dimensions are compared b...
This class is useful if one wishes to perform an ordinary one-dimensional sort on MultiDimComparable<...
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...