SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
SubsetOfPointSet.java
1/*
2 * Class: SubsetOfPointSet
3 * Description: Subset of a point set
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 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 */
25package umontreal.ssj.hups;
26
27import umontreal.ssj.rng.RandomStream;
28import umontreal.ssj.util.PrintfFormat;
29
30/*Attention: No array index range tests neither for the dimension
31 nor for the number of points is performed. This is left to JAVA. */
32
57public class SubsetOfPointSet extends PointSet {
58 protected PointSet P; // Source points
59 protected int i_from, i_to, i_index[]; // Limits or lookup for row
60 protected int j_from, j_to, j_index[]; // Limits or lookup for column
61
70 this.P = p;
72 dim = p.getDimension();
73 i_from = 0;
74 i_to = p.getNumPoints();
75 j_from = 0;
76 j_to = p.getDimension();
77 }
78
87 public void selectPointsRange(int from, int to) {
88 if (0 > from || from >= to || to > P.getNumPoints())
89 throw new IllegalArgumentException("Invalid range for points");
90
91 i_index = null;
92 i_from = from;
93 i_to = to;
94 numPoints = to - from;
95 }
96
104 public void selectPoints(int[] pointIndices, int numPoints) {
105 if (numPoints > P.getNumPoints() || numPoints > pointIndices.length)
106 throw new IllegalArgumentException("Number of indices too large");
107 i_index = pointIndices;
108 this.numPoints = numPoints;
109 }
110
120 public void selectCoordinatesRange(int from, int to) {
121 if (0 > from || from >= to || to > P.getDimension())
122 throw new IllegalArgumentException("Invalid column range");
123 j_index = null;
124 j_from = from;
125 j_to = to;
126 dim = to - from;
127 }
128
137 public void selectCoordinates(int[] coordIndices, int numCoord) {
138 if (numCoord > P.getDimension() || numCoord > coordIndices.length)
139 throw new IllegalArgumentException("Number of indices too large");
140
141 j_index = coordIndices;
142 this.dim = numCoord;
143 }
144
145 public double getCoordinate(int i, int j) {
146 int access_i, access_j;
147
148 // if no range check done: left to JAVA array index check
149
150 if (i_index == null) {
151 if (i < 0 || i >= numPoints)
152 throw new IllegalArgumentException("Row out of range");
153
154 access_i = i + i_from;
155 } else
156 access_i = i_index[i];
157
158 if (j_index == null) {
159 if (j < 0 || j > dim)
160 throw new IllegalArgumentException("Column out of range");
161
162 access_j = j + j_from;
163 } else
164 access_j = j_index[j];
165
166 return P.getCoordinate(access_i, access_j);
167 }
168
170 return new SubsetIterator();
171 }
172
173 public String toString() {
174 StringBuffer sb = new StringBuffer("Subset of point set" + PrintfFormat.NEWLINE);
175 sb.append("Inner point set information {" + PrintfFormat.NEWLINE);
176 sb.append(P.toString());
177 sb.append(PrintfFormat.NEWLINE + "}" + PrintfFormat.NEWLINE);
178
179 if (i_index == null)
180 sb.append("Points range from " + i_from + " to " + i_to + "." + PrintfFormat.NEWLINE);
181 else {
182 sb.append("Point indices: [");
183 boolean first = true;
184 for (int i = 0; i < numPoints; i++) {
185 if (first)
186 first = false;
187 else
188 sb.append(", ");
189 sb.append(i_index[i]);
190 }
191 sb.append("]" + PrintfFormat.NEWLINE);
192 }
193
194 if (j_index == null)
195 sb.append("Coordinates range from " + j_from + " to " + j_to + ".");
196 else {
197 sb.append("Coordinate indices: [");
198 boolean first = true;
199 for (int i = 0; i < dim; i++) {
200 if (first)
201 first = false;
202 else
203 sb.append(", ");
204 sb.append(j_index[i]);
205 }
206 sb.append("]");
207 }
208
209 return sb.toString();
210 }
211
212 // ***********************************************************
213
214 private class SubsetIterator extends DefaultPointSetIterator {
215
216 private PointSetIterator innerIterator;
217 /*
218 * private int i_from; private int i_to; private int j_from; private int j_to;
219 * private int[] i_index; private int[] j_index;
220 */
221
222 SubsetIterator() {
223 // Since one can change range after construction, we
224 // must save the current one.
225 // this.i_from = SubsetOfPointSet.this.i_from;
226 // this.i_to = SubsetOfPointSet.this.i_to;
227 // this.j_from = SubsetOfPointSet.this.j_from;
228 // this.j_to = SubsetOfPointSet.this.j_to;
229
230 // Also recopy indices in case one has set the indices,
231 // kept the array and modified it after construction.
232 // if (SubsetOfPointSet.this.i_index == null)
233 // this.i_index = null;
234 // else {
235 // this.i_index = new int[SubsetOfPointSet.this.i_index.length];
236 // System.arraycopy (SubsetOfPointSet.this.i_index, 0,
237 // this.i_index, 0, numPoints);
238 // }
239
240 // if (SubsetOfPointSet.this.j_index == null)
241 // this.j_index = null;
242 // else {
243 // this.j_index = new int[SubsetOfPointSet.this.j_index.length];
244 // System.arraycopy (SubsetOfPointSet.this.j_index, 0, this.j_index, 0, dim);
245 // }
246
247 // Create the inner iterator and set its state according to the subset.
248 innerIterator = P.iterator();
249 if (i_index == null) {
250 if (i_from != 0)
251 innerIterator.setCurPointIndex(i_from);
252 } else {
253 if (i_index[0] != 0)
254 innerIterator.setCurPointIndex(i_index[0]);
255 }
256
257 if (j_index == null) {
258 if (j_from != 0)
259 innerIterator.setCurCoordIndex(j_from);
260 } else {
261 if (j_index[0] != 0)
262 innerIterator.setCurCoordIndex(j_index[0]);
263 }
264 }
265
266 public void setCurCoordIndex(int j) {
267 if (j_index == null)
268 innerIterator.setCurCoordIndex(j + j_from);
269 else
270 innerIterator.setCurCoordIndex(j_index[j]);
271 curCoordIndex = j;
272 }
273
274 public void resetCurCoordIndex() {
275 if (j_index == null) {
276 if (j_from == 0)
277 innerIterator.resetCurCoordIndex();
278 else
279 innerIterator.setCurCoordIndex(j_from);
280 } else {
281 if (j_index[0] == 0)
282 innerIterator.resetCurCoordIndex();
283 else
284 innerIterator.setCurCoordIndex(j_index[0]);
285 }
286 curCoordIndex = 0;
287 }
288
289 public double nextCoordinate() {
290 if ((curPointIndex >= numPoints) || (curCoordIndex >= dim))
291 outOfBounds();
292 // The inner iterator could throw an exception.
293 // If that happens, e must not alter the current coordinate.
294 double coord = 0.0;
295
296 if (j_index == null)
297 coord = innerIterator.nextCoordinate();
298 else {
299 int currentIndex = j_index[curCoordIndex];
300 int futureIndex = (curCoordIndex + 1) == dim ? currentIndex + 1 : j_index[curCoordIndex + 1];
301 coord = innerIterator.nextCoordinate();
302 if (futureIndex != (currentIndex + 1))
303 innerIterator.setCurCoordIndex(futureIndex);
304 }
306 return coord;
307 }
308
309 public void nextCoordinates(double[] p, int d) {
311 outOfBounds();
312 if (j_index != null) {
313 super.nextCoordinates(p, d);
314 return;
315 }
316 innerIterator.nextCoordinates(p, d);
317 curCoordIndex += d;
318 }
319
320 public void setCurPointIndex(int i) {
321 if (i_index == null)
322 innerIterator.setCurPointIndex(i + i_from);
323 else
324 innerIterator.setCurPointIndex(i_index[i]);
325 curPointIndex = i;
327 }
328
329 public void resetCurPointIndex() {
330 if (i_index == null) {
331 if (i_from == 0)
332 innerIterator.resetCurPointIndex();
333 else
334 innerIterator.setCurPointIndex(i_from);
335 } else {
336 if (i_index[0] == 0)
337 innerIterator.resetCurPointIndex();
338 else
339 innerIterator.setCurPointIndex(i_index[0]);
340 }
341 curPointIndex = 0;
343 }
344
345 public int resetToNextPoint() {
346 if (i_index == null)
347 innerIterator.resetToNextPoint();
348 else if (curPointIndex < (numPoints - 1))
349 innerIterator.setCurPointIndex(i_index[curPointIndex + 1]);
352 return curPointIndex;
353 }
354 }
355}
int curPointIndex
Index of the current point.
void resetCurPointIndex()
Resets both the current point index and the current coordinate to 0.
int curCoordIndex
Index of the current coordinate.
int resetToNextPoint()
Resets the current point index to the next one and current coordinate to 0, and returns the new curre...
void resetCurCoordIndex()
Set current coordinate to 0.
void outOfBounds()
Error message for index out of bounds.
This abstract class represents a general point set.
Definition PointSet.java:99
PointSetIterator iterator()
Constructs and returns a point set iterator.
int numPoints
Number of points.
int getNumPoints()
Returns the number of points.
int getDimension()
Returns the dimension (number of available coordinates) of the points.
int dim
Dimension of the points.
SubsetOfPointSet(PointSet p)
Constructs a new PointSet object, initially identical to p, and from which a subset of the points and...
String toString()
Formats a string that contains information about the point set.
double getCoordinate(int i, int j)
Returns , the coordinate of the point .
void selectPointsRange(int from, int to)
Selects the points numbered from "<tt>from</tt>" to "<tt>to -1</tt>" from the original point set.
void selectCoordinatesRange(int from, int to)
Selects the coordinates from "<tt>from</tt>" to "<tt>to - 1</tt>" from the original point set.
PointSetIterator iterator()
Constructs and returns a point set iterator.
void selectPoints(int[] pointIndices, int numPoints)
Selects the numPoints points whose numbers are provided in the array pointIndices.
void selectCoordinates(int[] coordIndices, int numCoord)
Selects the numCoord coordinates whose numbers are provided in the array coordIndices.
This class acts like a StringBuffer which defines new types of append methods.
static final String NEWLINE
End-of-line symbol or line separator.
This is the interface for iterators that permit one to go through the points of a PointSet and the su...
void setCurPointIndex(int i)
Resets the current point index to and the current coordinate index to zero.
void setCurCoordIndex(int j)
Sets the current coordinate index to , so that the next calls to nextCoordinate or nextCoordinates wi...