Skip to content

Commit

Permalink
Add double[] and boolean[] versions of Util.expandArray
Browse files Browse the repository at this point in the history
  • Loading branch information
tpietzsch committed Jul 23, 2024
1 parent 39411b0 commit 606fc18
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/main/java/net/imglib2/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,32 @@ public static long[] getArrayFromValue( final long value, final int numDimension
return values;
}

/**
* Expand or truncate the provided array of {@code values} to length {@code
* newLength}.
* <p>
* If {@code values.length < newLength} then the last value is repeated.
* That is, the remaining elements are filled with {@code
* values[values.length - 1]}.
* <p>
* If {@code values.length == newLength} then {@code values} is returned,
* otherwise a new array is created.
*
* @param values
* values to copy
* @param newLength
* length of expanded array
*
* @return an array where {@code array.length == newLength} and {@code array[i] == values[Math.max(i, values.length)]}
*/
public static double[] expandArray( final double[] values, final int newLength )
{
final double[] expandedValues = ( values.length == newLength ) ? values : Arrays.copyOf( values, newLength );
if ( values.length < newLength )
Arrays.fill( expandedValues, values.length, newLength, values[ values.length - 1 ] );
return expandedValues;
}

/**
* Expand or truncate the provided array of {@code values} to length {@code
* newLength}.
Expand Down Expand Up @@ -181,6 +207,32 @@ public static int[] expandArray( final int[] values, final int newLength )
return expandedValues;
}

/**
* Expand or truncate the provided array of {@code values} to length {@code
* newLength}.
* <p>
* If {@code values.length < newLength} then the last value is repeated.
* That is, the remaining elements are filled with {@code
* values[values.length - 1]}.
* <p>
* If {@code values.length == newLength} then {@code values} is returned,
* otherwise a new array is created.
*
* @param values
* values to copy
* @param newLength
* length of expanded array
*
* @return an array where {@code array.length == newLength} and {@code array[i] == values[Math.max(i, values.length)]}
*/
public static boolean[] expandArray( final boolean[] values, final int newLength )
{
final boolean[] expandedValues = ( values.length == newLength ) ? values : Arrays.copyOf( values, newLength );
if ( values.length < newLength )
Arrays.fill( expandedValues, values.length, newLength, values[ values.length - 1 ] );
return expandedValues;
}

public static double distance( final RealLocalizable position1, final RealLocalizable position2 )
{
double dist = 0;
Expand Down

0 comments on commit 606fc18

Please sign in to comment.