From 29af6f3f7589200f562e0a25ab3af38d68487e95 Mon Sep 17 00:00:00 2001 From: brinn <brinn> Date: Sun, 21 Sep 2008 20:44:06 +0000 Subject: [PATCH] change: rename getValue() -> get(), setValue() -> set() add: specific getters and setters for arrays of rank 1, 2 and 3 SVN: 8400 --- .../systemsx/cisd/common/array/MDArray.java | 21 +++++ .../cisd/common/array/MDByteArray.java | 77 +++++++++++++++++-- .../cisd/common/array/MDDoubleArray.java | 73 +++++++++++++++++- .../cisd/common/array/MDFloatArray.java | 73 +++++++++++++++++- .../cisd/common/array/MDIntArray.java | 73 +++++++++++++++++- .../cisd/common/array/MDLongArray.java | 77 +++++++++++++++++-- .../cisd/common/array/MDShortArray.java | 73 +++++++++++++++++- .../cisd/common/array/MDArraytest.java | 25 +++++- 8 files changed, 463 insertions(+), 29 deletions(-) diff --git a/common/source/java/ch/systemsx/cisd/common/array/MDArray.java b/common/source/java/ch/systemsx/cisd/common/array/MDArray.java index 29eacad2110..b83c7e38462 100644 --- a/common/source/java/ch/systemsx/cisd/common/array/MDArray.java +++ b/common/source/java/ch/systemsx/cisd/common/array/MDArray.java @@ -107,6 +107,27 @@ public abstract class MDArray<T> return index; } + /** + * Computes the linear index for the two-dimensional (<var>indexX, indexY</var>) provided. + */ + protected int computeIndex(int indexX, int indexY) + { + assert 2 == shape.length; + + return shape[1] * indexX + indexY; + } + + /** + * Computes the linear index for the three-dimensional (<var>indexX, indexY, indexZ</var>) + * provided. + */ + protected int computeIndex(int indexX, int indexY, int indexZ) + { + assert 3 == shape.length; + + return shape[2] * (shape[1] * indexX + indexY) + indexZ; + } + /** * Converts the <var>shape</var> from <code>long[]</code> to <code>int[]</code>. */ diff --git a/common/source/java/ch/systemsx/cisd/common/array/MDByteArray.java b/common/source/java/ch/systemsx/cisd/common/array/MDByteArray.java index 10a3868aa8d..493bf33d79a 100644 --- a/common/source/java/ch/systemsx/cisd/common/array/MDByteArray.java +++ b/common/source/java/ch/systemsx/cisd/common/array/MDByteArray.java @@ -78,13 +78,13 @@ public final class MDByteArray extends MDArray<Byte> @Override public Byte getAsObject(int[] indices) { - return getValue(indices); + return get(indices); } @Override public void setToObject(int[] indices, Byte value) { - setValue(indices, value); + set(indices, value); } /** @@ -99,23 +99,88 @@ public final class MDByteArray extends MDArray<Byte> /** * Returns the value of array at the position defined by <var>indices</var>. */ - public byte getValue(int[] indices) + public byte get(int[] indices) { return flattenedArray[computeIndex(indices)]; } - + + /** + * Returns the value of a one-dimensional array at the position defined by <var>index</var>. + * <p> + * <b>Do not call for arrays other than one-dimensional!</b> + */ + public byte get(int index) + { + return flattenedArray[index]; + } + + /** + * Returns the value of a two-dimensional array at the position defined by <var>indexX</var> and + * <var>indexY</var>. + * <p> + * <b>Do not call for arrays other than two-dimensional!</b> + */ + public byte get(int indexX, int indexY) + { + return flattenedArray[computeIndex(indexX, indexY)]; + } + + /** + * Returns the value of a three-dimensional array at the position defined by <var>indexX</var>, + * <var>indexY</var> and <var>indexZ</var>. + * <p> + * <b>Do not call for arrays other than three-dimensional!</b> + */ + public byte get(int indexX, int indexY, int indexZ) + { + return flattenedArray[computeIndex(indexX, indexY, indexZ)]; + } + /** * Sets the <var>value</var> of array at the position defined by <var>indices</var>. */ - public void setValue(int[] indices, byte value) + public void set(int[] indices, byte value) { flattenedArray[computeIndex(indices)] = value; } + /** + * Sets the <var>value</var> of a one-dimension array at the position defined by + * <var>index</var>. + * <p> + * <b>Do not call for arrays other than one-dimensional!</b> + */ + public void set(int index, byte value) + { + flattenedArray[index] = value; + } + + /** + * Sets the <var>value</var> of a two-dimensional array at the position defined by + * <var>indexX</var> and <var>indexY</var>. + * <p> + * <b>Do not call for arrays other than two-dimensional!</b> + */ + public void set(int indexX, int indexY, byte value) + { + flattenedArray[computeIndex(indexX, indexY)] = value; + } + + /** + * Sets the <var>value</var> of a three-dimensional array at the position defined by + * <var>indexX</var>, <var>indexY</var> and <var>indexZ</var>. + * <p> + * <b>Do not call for arrays other than three-dimensional!</b> + */ + public void set(int indexX, int indexY, int indexZ, byte value) + { + flattenedArray[computeIndex(indexX, indexY, indexZ)] = value; + } + // // Object // - + @Override public int hashCode() { diff --git a/common/source/java/ch/systemsx/cisd/common/array/MDDoubleArray.java b/common/source/java/ch/systemsx/cisd/common/array/MDDoubleArray.java index c8b826f0fa8..fb4bdd49c29 100644 --- a/common/source/java/ch/systemsx/cisd/common/array/MDDoubleArray.java +++ b/common/source/java/ch/systemsx/cisd/common/array/MDDoubleArray.java @@ -78,13 +78,13 @@ public final class MDDoubleArray extends MDArray<Double> @Override public Double getAsObject(int[] indices) { - return getValue(indices); + return get(indices); } @Override public void setToObject(int[] indices, Double value) { - setValue(indices, value); + set(indices, value); } /** @@ -99,19 +99,84 @@ public final class MDDoubleArray extends MDArray<Double> /** * Returns the value of array at the position defined by <var>indices</var>. */ - public double getValue(int[] indices) + public double get(int[] indices) { return flattenedArray[computeIndex(indices)]; } + /** + * Returns the value of a one-dimensional array at the position defined by <var>index</var>. + * <p> + * <b>Do not call for arrays other than one-dimensional!</b> + */ + public double get(int index) + { + return flattenedArray[index]; + } + + /** + * Returns the value of a two-dimensional array at the position defined by <var>indexX</var> and + * <var>indexY</var>. + * <p> + * <b>Do not call for arrays other than two-dimensional!</b> + */ + public double get(int indexX, int indexY) + { + return flattenedArray[computeIndex(indexX, indexY)]; + } + + /** + * Returns the value of a three-dimensional array at the position defined by <var>indexX</var>, + * <var>indexY</var> and <var>indexZ</var>. + * <p> + * <b>Do not call for arrays other than three-dimensional!</b> + */ + public double get(int indexX, int indexY, int indexZ) + { + return flattenedArray[computeIndex(indexX, indexY, indexZ)]; + } + /** * Sets the <var>value</var> of array at the position defined by <var>indices</var>. */ - public void setValue(int[] indices, double value) + public void set(int[] indices, double value) { flattenedArray[computeIndex(indices)] = value; } + /** + * Sets the <var>value</var> of a one-dimension array at the position defined by + * <var>index</var>. + * <p> + * <b>Do not call for arrays other than one-dimensional!</b> + */ + public void set(int index, double value) + { + flattenedArray[index] = value; + } + + /** + * Sets the <var>value</var> of a two-dimensional array at the position defined by + * <var>indexX</var> and <var>indexY</var>. + * <p> + * <b>Do not call for arrays other than two-dimensional!</b> + */ + public void set(int indexX, int indexY, double value) + { + flattenedArray[computeIndex(indexX, indexY)] = value; + } + + /** + * Sets the <var>value</var> of a three-dimensional array at the position defined by + * <var>indexX</var>, <var>indexY</var> and <var>indexZ</var>. + * <p> + * <b>Do not call for arrays other than three-dimensional!</b> + */ + public void set(int indexX, int indexY, int indexZ, double value) + { + flattenedArray[computeIndex(indexX, indexY, indexZ)] = value; + } + // // Object // diff --git a/common/source/java/ch/systemsx/cisd/common/array/MDFloatArray.java b/common/source/java/ch/systemsx/cisd/common/array/MDFloatArray.java index 4bf9c845197..a6f310fc8dd 100644 --- a/common/source/java/ch/systemsx/cisd/common/array/MDFloatArray.java +++ b/common/source/java/ch/systemsx/cisd/common/array/MDFloatArray.java @@ -78,13 +78,13 @@ public final class MDFloatArray extends MDArray<Float> @Override public Float getAsObject(int[] indices) { - return getValue(indices); + return get(indices); } @Override public void setToObject(int[] indices, Float value) { - setValue(indices, value); + set(indices, value); } /** @@ -99,19 +99,84 @@ public final class MDFloatArray extends MDArray<Float> /** * Returns the value of array at the position defined by <var>indices</var>. */ - public float getValue(int[] indices) + public float get(int[] indices) { return flattenedArray[computeIndex(indices)]; } + /** + * Returns the value of a one-dimensional array at the position defined by <var>index</var>. + * <p> + * <b>Do not call for arrays other than one-dimensional!</b> + */ + public float get(int index) + { + return flattenedArray[index]; + } + + /** + * Returns the value of a two-dimensional array at the position defined by <var>indexX</var> and + * <var>indexY</var>. + * <p> + * <b>Do not call for arrays other than two-dimensional!</b> + */ + public float get(int indexX, int indexY) + { + return flattenedArray[computeIndex(indexX, indexY)]; + } + + /** + * Returns the value of a three-dimensional array at the position defined by <var>indexX</var>, + * <var>indexY</var> and <var>indexZ</var>. + * <p> + * <b>Do not call for arrays other than three-dimensional!</b> + */ + public float get(int indexX, int indexY, int indexZ) + { + return flattenedArray[computeIndex(indexX, indexY, indexZ)]; + } + /** * Sets the <var>value</var> of array at the position defined by <var>indices</var>. */ - public void setValue(int[] indices, float value) + public void set(int[] indices, float value) { flattenedArray[computeIndex(indices)] = value; } + /** + * Sets the <var>value</var> of a one-dimension array at the position defined by + * <var>index</var>. + * <p> + * <b>Do not call for arrays other than one-dimensional!</b> + */ + public void set(int index, float value) + { + flattenedArray[index] = value; + } + + /** + * Sets the <var>value</var> of a two-dimensional array at the position defined by + * <var>indexX</var> and <var>indexY</var>. + * <p> + * <b>Do not call for arrays other than two-dimensional!</b> + */ + public void set(int indexX, int indexY, float value) + { + flattenedArray[computeIndex(indexX, indexY)] = value; + } + + /** + * Sets the <var>value</var> of a three-dimensional array at the position defined by + * <var>indexX</var>, <var>indexY</var> and <var>indexZ</var>. + * <p> + * <b>Do not call for arrays other than three-dimensional!</b> + */ + public void set(int indexX, int indexY, int indexZ, float value) + { + flattenedArray[computeIndex(indexX, indexY, indexZ)] = value; + } + // // Object // diff --git a/common/source/java/ch/systemsx/cisd/common/array/MDIntArray.java b/common/source/java/ch/systemsx/cisd/common/array/MDIntArray.java index 21fcc658284..802355a671c 100644 --- a/common/source/java/ch/systemsx/cisd/common/array/MDIntArray.java +++ b/common/source/java/ch/systemsx/cisd/common/array/MDIntArray.java @@ -78,13 +78,13 @@ public final class MDIntArray extends MDArray<Integer> @Override public Integer getAsObject(int[] indices) { - return getValue(indices); + return get(indices); } @Override public void setToObject(int[] indices, Integer value) { - setValue(indices, value); + set(indices, value); } /** @@ -99,19 +99,84 @@ public final class MDIntArray extends MDArray<Integer> /** * Returns the value of array at the position defined by <var>indices</var>. */ - public int getValue(int[] indices) + public int get(int[] indices) { return flattenedArray[computeIndex(indices)]; } + /** + * Returns the value of a one-dimensional array at the position defined by <var>index</var>. + * <p> + * <b>Do not call for arrays other than one-dimensional!</b> + */ + public int get(int index) + { + return flattenedArray[index]; + } + + /** + * Returns the value of a two-dimensional array at the position defined by <var>indexX</var> and + * <var>indexY</var>. + * <p> + * <b>Do not call for arrays other than two-dimensional!</b> + */ + public int get(int indexX, int indexY) + { + return flattenedArray[computeIndex(indexX, indexY)]; + } + + /** + * Returns the value of a three-dimensional array at the position defined by <var>indexX</var>, + * <var>indexY</var> and <var>indexZ</var>. + * <p> + * <b>Do not call for arrays other than three-dimensional!</b> + */ + public int get(int indexX, int indexY, int indexZ) + { + return flattenedArray[computeIndex(indexX, indexY, indexZ)]; + } + /** * Sets the <var>value</var> of array at the position defined by <var>indices</var>. */ - public void setValue(int[] indices, int value) + public void set(int[] indices, int value) { flattenedArray[computeIndex(indices)] = value; } + /** + * Sets the <var>value</var> of a one-dimension array at the position defined by + * <var>index</var>. + * <p> + * <b>Do not call for arrays other than one-dimensional!</b> + */ + public void set(int index, int value) + { + flattenedArray[index] = value; + } + + /** + * Sets the <var>value</var> of a two-dimensional array at the position defined by + * <var>indexX</var> and <var>indexY</var>. + * <p> + * <b>Do not call for arrays other than two-dimensional!</b> + */ + public void set(int indexX, int indexY, int value) + { + flattenedArray[computeIndex(indexX, indexY)] = value; + } + + /** + * Sets the <var>value</var> of a three-dimensional array at the position defined by + * <var>indexX</var>, <var>indexY</var> and <var>indexZ</var>. + * <p> + * <b>Do not call for arrays other than three-dimensional!</b> + */ + public void set(int indexX, int indexY, int indexZ, int value) + { + flattenedArray[computeIndex(indexX, indexY, indexZ)] = value; + } + // // Object // diff --git a/common/source/java/ch/systemsx/cisd/common/array/MDLongArray.java b/common/source/java/ch/systemsx/cisd/common/array/MDLongArray.java index 7ac5c777294..35b6f8b5cb5 100644 --- a/common/source/java/ch/systemsx/cisd/common/array/MDLongArray.java +++ b/common/source/java/ch/systemsx/cisd/common/array/MDLongArray.java @@ -78,13 +78,13 @@ public final class MDLongArray extends MDArray<Long> @Override public Long getAsObject(int[] indices) { - return getValue(indices); + return get(indices); } @Override public void setToObject(int[] indices, Long value) { - setValue(indices, value); + set(indices, value); } /** @@ -99,23 +99,88 @@ public final class MDLongArray extends MDArray<Long> /** * Returns the value of array at the position defined by <var>indices</var>. */ - public long getValue(int[] indices) + public long get(int[] indices) { return flattenedArray[computeIndex(indices)]; } - + + /** + * Returns the value of a one-dimensional array at the position defined by <var>index</var>. + * <p> + * <b>Do not call for arrays other than one-dimensional!</b> + */ + public long get(int index) + { + return flattenedArray[index]; + } + + /** + * Returns the value of a two-dimensional array at the position defined by <var>indexX</var> and + * <var>indexY</var>. + * <p> + * <b>Do not call for arrays other than two-dimensional!</b> + */ + public long get(int indexX, int indexY) + { + return flattenedArray[computeIndex(indexX, indexY)]; + } + + /** + * Returns the value of a three-dimensional array at the position defined by <var>indexX</var>, + * <var>indexY</var> and <var>indexZ</var>. + * <p> + * <b>Do not call for arrays other than three-dimensional!</b> + */ + public long get(int indexX, int indexY, int indexZ) + { + return flattenedArray[computeIndex(indexX, indexY, indexZ)]; + } + /** * Sets the <var>value</var> of array at the position defined by <var>indices</var>. */ - public void setValue(int[] indices, long value) + public void set(int[] indices, long value) { flattenedArray[computeIndex(indices)] = value; } + /** + * Sets the <var>value</var> of a one-dimension array at the position defined by + * <var>index</var>. + * <p> + * <b>Do not call for arrays other than one-dimensional!</b> + */ + public void set(int index, long value) + { + flattenedArray[index] = value; + } + + /** + * Sets the <var>value</var> of a two-dimensional array at the position defined by + * <var>indexX</var> and <var>indexY</var>. + * <p> + * <b>Do not call for arrays other than two-dimensional!</b> + */ + public void set(int indexX, int indexY, long value) + { + flattenedArray[computeIndex(indexX, indexY)] = value; + } + + /** + * Sets the <var>value</var> of a three-dimensional array at the position defined by + * <var>indexX</var>, <var>indexY</var> and <var>indexZ</var>. + * <p> + * <b>Do not call for arrays other than three-dimensional!</b> + */ + public void set(int indexX, int indexY, int indexZ, long value) + { + flattenedArray[computeIndex(indexX, indexY, indexZ)] = value; + } + // // Object // - + @Override public int hashCode() { diff --git a/common/source/java/ch/systemsx/cisd/common/array/MDShortArray.java b/common/source/java/ch/systemsx/cisd/common/array/MDShortArray.java index 6504bf9ce12..852b0c89992 100644 --- a/common/source/java/ch/systemsx/cisd/common/array/MDShortArray.java +++ b/common/source/java/ch/systemsx/cisd/common/array/MDShortArray.java @@ -78,13 +78,13 @@ public final class MDShortArray extends MDArray<Short> @Override public Short getAsObject(int[] indices) { - return getValue(indices); + return get(indices); } @Override public void setToObject(int[] indices, Short value) { - setValue(indices, value); + set(indices, value); } /** @@ -99,19 +99,84 @@ public final class MDShortArray extends MDArray<Short> /** * Returns the value of array at the position defined by <var>indices</var>. */ - public short getValue(int[] indices) + public short get(int[] indices) { return flattenedArray[computeIndex(indices)]; } + /** + * Returns the value of a one-dimensional array at the position defined by <var>index</var>. + * <p> + * <b>Do not call for arrays other than one-dimensional!</b> + */ + public short get(int index) + { + return flattenedArray[index]; + } + + /** + * Returns the value of a two-dimensional array at the position defined by <var>indexX</var> and + * <var>indexY</var>. + * <p> + * <b>Do not call for arrays other than two-dimensional!</b> + */ + public short get(int indexX, int indexY) + { + return flattenedArray[computeIndex(indexX, indexY)]; + } + + /** + * Returns the value of a three-dimensional array at the position defined by <var>indexX</var>, + * <var>indexY</var> and <var>indexZ</var>. + * <p> + * <b>Do not call for arrays other than three-dimensional!</b> + */ + public short get(int indexX, int indexY, int indexZ) + { + return flattenedArray[computeIndex(indexX, indexY, indexZ)]; + } + /** * Sets the <var>value</var> of array at the position defined by <var>indices</var>. */ - public void setValue(int[] indices, short value) + public void set(int[] indices, short value) { flattenedArray[computeIndex(indices)] = value; } + /** + * Sets the <var>value</var> of a one-dimension array at the position defined by + * <var>index</var>. + * <p> + * <b>Do not call for arrays other than one-dimensional!</b> + */ + public void set(int index, short value) + { + flattenedArray[index] = value; + } + + /** + * Sets the <var>value</var> of a two-dimensional array at the position defined by + * <var>indexX</var> and <var>indexY</var>. + * <p> + * <b>Do not call for arrays other than two-dimensional!</b> + */ + public void set(int indexX, int indexY, short value) + { + flattenedArray[computeIndex(indexX, indexY)] = value; + } + + /** + * Sets the <var>value</var> of a three-dimensional array at the position defined by + * <var>indexX</var>, <var>indexY</var> and <var>indexZ</var>. + * <p> + * <b>Do not call for arrays other than three-dimensional!</b> + */ + public void set(int indexX, int indexY, int indexZ, short value) + { + flattenedArray[computeIndex(indexX, indexY, indexZ)] = value; + } + // // Object // diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/array/MDArraytest.java b/common/sourceTest/java/ch/systemsx/cisd/common/array/MDArraytest.java index bf4d685fec8..663d51c9b66 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/array/MDArraytest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/array/MDArraytest.java @@ -35,7 +35,6 @@ public class MDArraytest protected TestMDArray(int[] shape) { super(shape); - // TODO Auto-generated constructor stub } @Override @@ -89,4 +88,28 @@ public class MDArraytest array = new TestMDArray(new int[] { 2, 7, 3 }); assertEquals(3 * 7 * 1 + 3 * 2 + 3, array.computeIndex(new int[] { 1, 2, 3 })); } + + @Test + public void testComputeIndex2D() + { + TestMDArray array; + array = new TestMDArray(new int[] { 100, 10 }); + assertEquals(array.computeIndex(new int[] { 5, 8, }), array.computeIndex(5, 8)); + assertEquals(array.computeIndex(new int[] { 9, 1, }), array.computeIndex(9, 1)); + array = new TestMDArray(new int[] { 101, 11 }); + assertEquals(array.computeIndex(new int[] { 5, 8, }), array.computeIndex(5, 8)); + assertEquals(array.computeIndex(new int[] { 9, 1, }), array.computeIndex(9, 1)); + } + + @Test + public void testComputeIndex3() + { + TestMDArray array; + array = new TestMDArray(new int[] { 100, 10, 17 }); + assertEquals(array.computeIndex(new int[] { 5, 8, 16 }), array.computeIndex(5, 8, 16)); + assertEquals(array.computeIndex(new int[] { 9, 1, 5 }), array.computeIndex(9, 1, 5)); + array = new TestMDArray(new int[] { 101, 11, 3 }); + assertEquals(array.computeIndex(new int[] { 5, 8, 0 }), array.computeIndex(5, 8, 0)); + assertEquals(array.computeIndex(new int[] { 9, 1, 2 }), array.computeIndex(9, 1, 2)); + } } -- GitLab