Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Index object for NativeTypes instead of index fields #303

Merged
merged 10 commits into from
Apr 21, 2021
19 changes: 12 additions & 7 deletions src/main/java/net/imglib2/img/array/AbstractArrayCursor.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import net.imglib2.AbstractCursorInt;
import net.imglib2.Cursor;
import net.imglib2.type.Index;
import net.imglib2.type.NativeType;
import net.imglib2.util.IntervalIndexer;

Expand Down Expand Up @@ -68,6 +69,8 @@ public abstract class AbstractArrayCursor< T extends NativeType< T > > extends A
*/
protected final T type;

private final Index typeIndex;

/**
* Source image
*/
Expand All @@ -89,11 +92,12 @@ protected AbstractArrayCursor( final AbstractArrayCursor< T > cursor )

this.img = cursor.img;
this.type = img.createLinkedType();
this.typeIndex = type.index();
this.offset = cursor.offset;
this.size = cursor.size;
this.lastIndex = cursor.lastIndex;

type.updateIndex( cursor.type.getIndex() );
typeIndex.set( cursor.typeIndex.get() );
type.updateContainer( this );
}

Expand All @@ -109,6 +113,7 @@ public AbstractArrayCursor( final ArrayImg< T, ? > img, final int offset, final
super( img.numDimensions() );

this.type = img.createLinkedType();
this.typeIndex = type.index();
this.img = img;
this.lastIndex = offset + size - 1;
this.offset = offset;
Expand All @@ -126,25 +131,25 @@ public T get()
@Override
public boolean hasNext()
{
return type.getIndex() < lastIndex;
return typeIndex.get() < lastIndex;
}

@Override
public void jumpFwd( final long steps )
{
type.incIndex( ( int ) steps );
typeIndex.inc( ( int ) steps );
}

@Override
public void fwd()
{
type.incIndex();
typeIndex.inc();
}

@Override
public void reset()
{
type.updateIndex( offset - 1 );
typeIndex.set( offset - 1 );
type.updateContainer( this );
}

Expand All @@ -157,12 +162,12 @@ public String toString()
@Override
public int getIntPosition( final int dim )
{
return IntervalIndexer.indexToPosition( type.getIndex(), img.dim, dim );
return IntervalIndexer.indexToPosition( typeIndex.get(), img.dim, dim );
}

@Override
public void localize( final int[] position )
{
IntervalIndexer.indexToPosition( type.getIndex(), img.dim, position );
IntervalIndexer.indexToPosition( typeIndex.get(), img.dim, position );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import net.imglib2.AbstractLocalizingCursorInt;
import net.imglib2.Cursor;
import net.imglib2.type.Index;
import net.imglib2.type.NativeType;
import net.imglib2.util.IntervalIndexer;

Expand Down Expand Up @@ -69,6 +70,8 @@ public abstract class AbstractArrayLocalizingCursor< T extends NativeType< T > >
*/
protected final T type;

private final Index typeIndex;

/**
* The underlying source {@link ArrayImg}.
*/
Expand Down Expand Up @@ -96,6 +99,7 @@ protected AbstractArrayLocalizingCursor( final AbstractArrayLocalizingCursor< T

this.img = cursor.img;
this.type = img.createLinkedType();
this.typeIndex = type.index();
this.offset = cursor.offset;
this.size = cursor.size;

Expand All @@ -108,7 +112,7 @@ protected AbstractArrayLocalizingCursor( final AbstractArrayLocalizingCursor< T
max[ d ] = cursor.max[ d ];
}

type.updateIndex( cursor.type.getIndex() );
typeIndex.set( cursor.typeIndex.get() );
type.updateContainer( this );
}

Expand All @@ -128,6 +132,7 @@ public AbstractArrayLocalizingCursor( final ArrayImg< T, ? > img, final int offs
this.size = size;

this.type = img.createLinkedType();
this.typeIndex = type.index();
this.lastIndex = offset + size - 1;

max = new int[ n ];
Expand All @@ -152,7 +157,7 @@ public T get()
@Override
public boolean hasNext()
{
return type.getIndex() < lastIndex;
return typeIndex.get() < lastIndex;
}

/**
Expand All @@ -161,7 +166,7 @@ public boolean hasNext()
@Override
public void fwd()
{
type.incIndex();
typeIndex.inc();

// for ( int d = 0; d < n; ++d )
// {
Expand Down Expand Up @@ -202,8 +207,8 @@ public void fwd()
@Override
public void jumpFwd( final long steps )
{
type.incIndex( ( int ) steps );
IntervalIndexer.indexToPosition( type.getIndex(), img.dim, position );
typeIndex.inc( ( int ) steps );
IntervalIndexer.indexToPosition( typeIndex.get(), img.dim, position );
}

/**
Expand All @@ -212,11 +217,11 @@ public void jumpFwd( final long steps )
@Override
public void reset()
{
type.updateIndex( offset - 1 );
typeIndex.set( offset - 1 );

IntervalIndexer.indexToPosition( offset, img.dim, position );
position[ 0 ]--;

type.updateContainer( this );
type.updateContainer( this ); // TODO: This is unnecessary. Remove.
}
}
Loading