@@ -260,7 +260,7 @@ public void foreachNonzero(int startColumn, int endColumn, MatrixElementConsumer
260
260
* Provides a stream over all of the non-zero elements of a sparse matrix.
261
261
*/
262
262
public Stream <Entry > nonzeros () {
263
- return StreamSupport .stream (new SparseMatrixSpliterator (this , 0 , ncols ), false );
263
+ return StreamSupport .stream (new SparseMatrixSpliterator (0 , ncols ), false );
264
264
}
265
265
266
266
/**
@@ -270,42 +270,40 @@ public Stream<Entry> nonzeros() {
270
270
* @param endColumn The first column after the ones that we should scan.
271
271
*/
272
272
public Stream <Entry > nonzeros (int startColumn , int endColumn ) {
273
- return StreamSupport .stream (new SparseMatrixSpliterator (this , startColumn , endColumn ), false );
273
+ return StreamSupport .stream (new SparseMatrixSpliterator (startColumn , endColumn ), false );
274
274
}
275
275
276
276
/**
277
277
* Provides a spliterator for access to a sparse matrix in column major order.
278
278
* <p>
279
279
* This is exposed to facilitate lower level access to the stream API for a matrix.
280
280
*/
281
- private static class SparseMatrixSpliterator implements Spliterator <Entry > {
282
- private final SparseMatrix m ;
281
+ private class SparseMatrixSpliterator implements Spliterator <Entry > {
283
282
private int col ; // current column, advanced on split or traversal
284
283
private int index ; // current element within column
285
284
private final int fence ; // one past the last column to process
286
285
287
- SparseMatrixSpliterator (SparseMatrix matrix , int col , int fence ) {
288
- this .m = matrix ;
286
+ SparseMatrixSpliterator (int col , int fence ) {
289
287
this .col = col ;
290
- this .index = m . colIndex [col ];
288
+ this .index = colIndex [col ];
291
289
this .fence = fence ;
292
290
}
293
291
294
292
public void forEachRemaining (Consumer <? super Entry > action ) {
295
293
for (; col < fence ; col ++) {
296
- for (; index < m . colIndex [col + 1 ]; index ++) {
297
- action .accept (new Entry (m . rowIndex [index ], col , m . x [index ], index , m . x ));
294
+ for (; index < colIndex [col + 1 ]; index ++) {
295
+ action .accept (new Entry (rowIndex [index ], col , x [index ], index ));
298
296
}
299
297
}
300
298
}
301
299
302
300
public boolean tryAdvance (Consumer <? super Entry > action ) {
303
301
if (col < fence ) {
304
- while (col < fence && index >= m . colIndex [col + 1 ]) {
302
+ while (col < fence && index >= colIndex [col + 1 ]) {
305
303
col ++;
306
304
}
307
305
if (col < fence ) {
308
- action .accept (new Entry (m . rowIndex [index ], col , m . x [index ], index , m . x ));
306
+ action .accept (new Entry (rowIndex [index ], col , x [index ], index ));
309
307
index ++;
310
308
return true ;
311
309
} else {
@@ -322,15 +320,15 @@ public Spliterator<Entry> trySplit() {
322
320
int mid = ((lo + fence ) >>> 1 ) & ~1 ; // force midpoint to be even
323
321
if (lo < mid ) { // split out left half
324
322
col = mid ; // reset this Spliterator's origin
325
- return new SparseMatrixSpliterator (m , lo , mid );
323
+ return new SparseMatrixSpliterator (lo , mid );
326
324
} else {
327
325
// too small to split
328
326
return null ;
329
327
}
330
328
}
331
329
332
330
public long estimateSize () {
333
- return (long ) (m . colIndex [fence ] - m . colIndex [col ]);
331
+ return (long ) (colIndex [fence ] - colIndex [col ]);
334
332
}
335
333
336
334
public int characteristics () {
@@ -339,32 +337,37 @@ public int characteristics() {
339
337
}
340
338
341
339
/**
342
- * Encapsulates important information about an entry in a matrix for use
343
- * in streaming, including a string that leads back to the original cell
344
- * so that in-place updates are possible.
340
+ * Encapsulates an entry in a matrix for use in streaming. As typical stream object,
341
+ * this object is immutable. But we can update the corresponding value in the matrix
342
+ * through <code>update</code> method. This provides an efficient way to update the
343
+ * non-zero entries of a sparse matrix.
345
344
*/
346
- public static class Entry {
345
+ public class Entry {
347
346
// these fields are exposed for direct access to simplify in-lining by the JVM
348
347
public final int row ;
349
348
public final int col ;
350
- public double x ;
349
+ public final double value ;
351
350
352
351
// these are hidden due to internal dependency
353
352
private final int index ;
354
- private double [] values ;
355
353
356
-
357
- public Entry (int row , int col , double x , int index , double [] values ) {
354
+ /**
355
+ * Private constructor. Only the enclosure matrix can creates
356
+ * the instances of entry.
357
+ */
358
+ private Entry (int row , int col , double value , int index ) {
358
359
this .row = row ;
359
360
this .col = col ;
360
- this .x = x ;
361
+ this .value = value ;
361
362
this .index = index ;
362
- this .values = values ;
363
363
}
364
364
365
- public void set (double value ) {
366
- this .x = value ;
367
- values [index ] = value ;
365
+ /**
366
+ * Update the value of entry in the matrix. Note that the field <code>value</code>
367
+ * is final and thus not updated.
368
+ */
369
+ public void update (double value ) {
370
+ x [index ] = value ;
368
371
}
369
372
}
370
373
0 commit comments