Permuted1DConfiguration.java

  1. package neureka.ndim.config.types.permuted;

  2. import neureka.ndim.config.types.D1C;

  3. public class Permuted1DConfiguration extends D1C {
  4.     /**
  5.      *  The shape of the NDArray.
  6.      */
  7.     protected final int _shape;
  8.     /**
  9.      *  The translation from a shape index (indices) to the index of the underlying data array.
  10.      */
  11.     private final int _stride;
  12.     /**
  13.      *  The mapping of the indices array.
  14.      */
  15.     private final int _indicesMap; // Maps index integer to array like translation. Used to avoid distortion when slicing!


  16.     public static Permuted1DConfiguration construct(
  17.             int[] shape,
  18.             int[] strides,
  19.             int[] indicesMap
  20.     ) {
  21.         return _cached( new Permuted1DConfiguration(shape[ 0 ], strides[ 0 ],  indicesMap[ 0 ]) );
  22.     }

  23.     protected Permuted1DConfiguration(
  24.             int shape,
  25.             int stride,
  26.             int indicesMap
  27.     ) {
  28.         _shape      = shape;
  29.         _stride     = stride;
  30.         _indicesMap = indicesMap;
  31.         assert stride != 0;
  32.         assert indicesMap != 0;
  33.     }

  34.     /** {@inheritDoc} */
  35.     @Override public final int rank() { return 1; }

  36.     /** {@inheritDoc} */
  37.     @Override public final int[] shape() { return new int[]{_shape}; }

  38.     /** {@inheritDoc} */
  39.     @Override public final int shape( int i ) { return _shape; }

  40.     /** {@inheritDoc} */
  41.     @Override public final int[] indicesMap() { return new int[]{_indicesMap}; }

  42.     /** {@inheritDoc} */
  43.     @Override public final int indicesMap( int i ) { return _indicesMap; }

  44.     /** {@inheritDoc} */
  45.     @Override public final int[] strides() { return new int[]{_stride}; }

  46.     /** {@inheritDoc} */
  47.     @Override public final int strides(int i ) { return _stride; }

  48.     /** {@inheritDoc} */
  49.     @Override public final int[] spread() { return new int[]{1}; }

  50.     /** {@inheritDoc} */
  51.     @Override public final int spread( int i ) { return 1; }

  52.     /** {@inheritDoc} */
  53.     @Override public final int[] offset() { return new int[]{0}; }

  54.     /** {@inheritDoc} */
  55.     @Override public final int offset( int i ) { return 0; }

  56.     /** {@inheritDoc} */
  57.     @Override public final int indexOfIndex( int index ) { return ( index / _indicesMap ) * _stride; }

  58.     /** {@inheritDoc} */
  59.     @Override public final int[] indicesOfIndex( int index ) { return new int[]{index / _indicesMap}; }

  60.     /** {@inheritDoc} */
  61.     @Override public final int indexOfIndices( int[] indices ) { return indices[ 0 ] * _stride; }

  62.     /** {@inheritDoc} */
  63.     @Override public final int indexOfIndices( int d1 ) { return d1 * _stride; }

  64. }