@Override public Cell< A > get( final Long key ) { final long index = key; final int n = grid.numDimensions(); final long[] cellMin = new long[ n ]; final int[] cellDims = new int[ n ]; grid.getCellDimensions( index, cellMin, cellDims ); final long[] cellMax = IntStream.range( 0, n ).mapToLong( d -> cellMin[ d ] + cellDims[ d ] - 1 ).toArray(); final A result = intervalKeyLoader.apply( new FinalInterval( cellMin, cellMax ) ); return new Cell<>( cellDims, cellMin, result ); } }
@Override public void onRemoval( final Long key, final Cell< A > value ) { final long index = key; final String filename = blockname( index ); final int[] cellDims = new int[ n ]; value.dimensions( cellDims ); final long blocksize = entitiesPerPixel.mulCeil( Intervals.numElements( cellDims ) ); final long bytesize = blocksize * accessIo.getBytesPerElement(); try ( final RandomAccessFile mmFile = new RandomAccessFile( filename, "rw" ); ) { final MappedByteBuffer out = mmFile.getChannel().map( MapMode.READ_WRITE, 0, bytesize ); accessIo.save( value.getData(), out, ( int ) blocksize ); } catch ( final IOException e ) { throw new RuntimeException( e ); } }
@Override public long getLongPosition( final int dim ) { return getCell().indexToGlobalPosition( index, dim ); }
@Test public void testGlobalPositionCalculation() { final Cell< FloatArray > cell = new Cell<>( new int[] {20, 8, 10}, new long[] { 0, 9876543210l, 222 }, new FloatArray( 1 ) ); final int[] index = { 883, 492, 43 }; final long[][] expectedPosition = { { 3, 9876543214l, 227 }, { 12, 9876543210l, 225 }, { 3, 9876543212l, 222 } }; for ( int i = 0; i < index.length; ++i ) { final long[] position = new long[ 3 ]; cell.indexToGlobalPosition( index[ i ], position ); assertArrayEquals( expectedPosition[ i ], position ); for ( int d = 0; d < position.length; ++d ) assertTrue( cell.indexToGlobalPosition( index[ i ], d ) == expectedPosition[ i ][ d ] ); } } }
@Test public void testLocalIndexCalculation() { final long[] min = new long[] { 0, 9876543210l, 222 }; final Cell< FloatArray > cell = new Cell<>( new int[] {20, 8, 10}, min, new FloatArray( 1 ) ); final long[][] position = { {3, 4, 5}, {12, 0, 3}, {3, 2, 0} }; final int[] expectedIndex = { 883, 492, 43 }; for ( int i = 0; i < position.length; ++i ) { for ( int d =0; d <min.length; ++d) position[ i ][ d ] += min[ d ]; assertTrue( cell.globalPositionToIndex( position[ i ] ) == expectedIndex[ i ] ); } }
final short[] dataBlock = cell.getData().getCurrentStorageArray(); cell.dimensions( dimsInt ); cell.min( min ); try
@Override @SuppressWarnings( "unchecked" ) public A update( final Object cursor ) { // directly get data? return ( ( CellImgSampler< C > ) cursor ).getCell().getData(); }
@Override public void jumpFwd( final long steps ) { long newIndex = index + steps; while ( newIndex > lastIndexInCell ) { newIndex -= lastIndexInCell + 1; cursorOnCells.fwd(); isNotLastCell = cursorOnCells.hasNext(); lastIndexInCell = ( int ) ( getCell().size() - 1 ); } final C cell = getCell(); currentCellMin = cell.min; currentCellMax = cell.max; index = ( int ) newIndex; cell.indexToGlobalPosition( index, position ); type.updateIndex( index ); type.updateContainer( this ); }
currentCellMax = cell.max; index = cell.globalPositionToIndex( position ); type.updateContainer( this );
/** * Move cursor right before the first element of the next cell. Update type * and index variables. */ private void moveToNextCell() { cursorOnCells.fwd(); isNotLastCell = cursorOnCells.hasNext(); lastIndexInCell = ( int ) ( getCell().size() - 1 ); index = -1; type.updateContainer( this ); } }
@Override @SuppressWarnings( "unchecked" ) public A update( final Object cursor ) { // directly get data? return ( ( CellImgSampler< C > ) cursor ).getCell().getData(); }
@Override public void jumpFwd( final long steps ) { long newIndex = index + steps; while ( newIndex > lastIndexInCell ) { newIndex -= lastIndexInCell + 1; cursorOnCells.fwd(); isNotLastCell = cursorOnCells.hasNext(); lastIndexInCell = ( int ) ( getCell().size() - 1 ); } final C cell = getCell(); currentCellMin = cell.min; currentCellMax = cell.max; index = ( int ) newIndex; cell.indexToGlobalPosition( index, position ); type.updateIndex( index ); type.updateContainer( this ); }
currentCellMax = cell.max; index = cell.globalPositionToIndex( position ); type.updateContainer( this );
/** * Move cursor right before the first element of the next cell. Update type * and index variables. */ private void moveToNextCell() { cursorOnCells.fwd(); isNotLastCell = cursorOnCells.hasNext(); lastIndexInCell = ( int ) ( getCell().size() - 1 ); index = -1; type.updateContainer( this ); } }
@Override public Cell< A > get( final Long key ) throws Exception { final int n = grid.numDimensions(); final long[] cellMin = new long[ n ]; final int[] cellDims = new int[ n ]; grid.getCellDimensions( key, cellMin, cellDims ); return new Cell<>( cellDims, cellMin, cacheArrayLoader.loadArray( timepoint, setup, level, cellDims, cellMin ) ); } };
@Override public void onRemoval( final Long key, final Cell< A > value ) { if ( value.getData().isDirty() ) super.onRemoval( key, value ); } }
@Override public long getLongPosition( final int dim ) { return getCell().indexToGlobalPosition( index, dim ); }
/** * Move cursor right before the first element of the next cell. Update type, * position, and index variables. */ private void moveToNextCell() { cursorOnCells.fwd(); isNotLastCell = cursorOnCells.hasNext(); final C cell = getCell(); lastIndexInCell = ( int ) ( cell.size() - 1 ); currentCellMin = cell.min; currentCellMax = cell.max; position[ 0 ] = currentCellMin[ 0 ] - 1; for ( int d = 1; d < n; ++d ) position[ d ] = currentCellMin[ d ]; type.updateContainer( this ); } }
@Override public Cell< A > get( final Long key ) throws Exception { final long index = key; final String filename = blockname( index ); if ( new File( filename ).exists() ) { final long[] cellMin = new long[ n ]; final int[] cellDims = new int[ n ]; grid.getCellDimensions( index, cellMin, cellDims ); final long numEntities = entitiesPerPixel.mulCeil( Intervals.numElements( cellDims ) ); final long bytesize = numEntities * accessIo.getBytesPerElement(); try ( final RandomAccessFile mmFile = new RandomAccessFile( filename, "r" ); ) { final MappedByteBuffer in = mmFile.getChannel().map( MapMode.READ_ONLY, 0, bytesize ); final A access = accessIo.load( in, ( int ) numEntities ); return new Cell<>( cellDims, cellMin, access ); } } else { return backingLoader.get( key ); } }
@Override public void localize( final long[] position ) { getCell().indexToGlobalPosition( index, position ); }