Skip to content

Instantly share code, notes, and snippets.

@hleinone
Created May 16, 2011 08:44
Show Gist options
  • Save hleinone/974108 to your computer and use it in GitHub Desktop.
Save hleinone/974108 to your computer and use it in GitHub Desktop.
Matrix interface, an array-backed implementation and a JUnit test verifying that it works
package matrix;
import java.util.Arrays;
public class ArrayMatrix<T> implements Matrix<T> {
private Object[][] array;
public ArrayMatrix(int height, int width) {
array = new Object[height][width];
}
@Override
public T getValue(int r, int c) {
@SuppressWarnings("unchecked")
T value = (T) array[r][c];
return value;
}
@Override
public T setValue(int r, int c, T value) {
array[r][c] = value;
return value;
}
@Override
public T removeValue(int r, int c) {
T value = getValue(r, c);
array[r][c] = null;
return value;
}
@Override
public void increaseHeight(int amount) {
array = Arrays.copyOf(array, array.length + amount);
}
@Override
public void removeRow(int r) {
Object[][] first = Arrays.copyOfRange(array, 0, r);
Object[][] second = Arrays.copyOfRange(array, r + 1, array.length);
Object[][] result = Arrays.copyOf(first, first.length + second.length);
System.arraycopy(second, 0, result, first.length, second.length);
array = result;
}
@Override
public void increaseWidth(int amount) {
for (int r = 0; r < array.length; r++) {
Object[] row = array[r];
array[r] = Arrays.copyOf(row, row.length + amount);
}
}
@Override
public void decreaseWidth(int amount) {
for (int r = 0; r < array.length; r++) {
Object[] row = array[r];
array[r] = Arrays.copyOf(row, row.length - amount);
}
}
@Override
public boolean[][] mask() {
boolean[][] mask = new boolean[height()][width()];
for (int r = 0; r < array.length; r++) {
Object[] row = array[r];
for (int c = 0; c < row.length; c++) {
if (array[r][c] != null)
mask[r][c] = true;
}
}
return mask;
}
@Override
public int width() {
return array.length > 0 ? array[0].length : 0;
}
@Override
public int height() {
return array.length;
}
}
package matrix;
public interface Matrix<T> {
T getValue(int r, int c);
T setValue(int r, int c, T value);
T removeValue(int r, int c);
void increaseHeight(int amount);
void removeRow(int r);
void increaseWidth(int amount);
void decreaseWidth(int amount);
boolean[][] mask();
int width();
int height();
}
package matrix;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Test;
public class MatrixTest {
@Test
public void testMask() {
Matrix<Cell> matrix = new ArrayMatrix<Cell>(1, 1);
assertArrayEquals(new boolean[][] { { false } }, matrix.mask());
}
@Test
public void testSetMask() {
Matrix<Cell> matrix = new ArrayMatrix<Cell>(1, 1);
matrix.setValue(0, 0, new Cell());
assertArrayEquals(new boolean[][] { { true } }, matrix.mask());
}
@Test
public void testSetGet() {
Matrix<Cell> matrix = new ArrayMatrix<Cell>(1, 1);
Cell expected = new Cell();
matrix.setValue(0, 0, expected);
assertEquals(expected, matrix.getValue(0, 0));
}
@Test
public void testSetRemove() {
Matrix<Cell> matrix = new ArrayMatrix<Cell>(1, 1);
Cell expected = new Cell();
matrix.setValue(0, 0, expected);
assertEquals(expected, matrix.removeValue(0, 0));
}
@Test
public void testSetRemoveGet() {
Matrix<Cell> matrix = new ArrayMatrix<Cell>(1, 1);
Cell expected = new Cell();
matrix.setValue(0, 0, expected);
matrix.removeValue(0, 0);
assertNull(matrix.getValue(0, 0));
}
@Test
public void testSetRemoveRowGet() {
Matrix<Cell> matrix = new ArrayMatrix<Cell>(2, 2);
Cell expected = new Cell();
matrix.setValue(1, 0, expected);
matrix.removeRow(0);
assertEquals(expected, matrix.getValue(0, 0));
}
@Test
public void testRemoveRowHeight() {
Matrix<Cell> matrix = new ArrayMatrix<Cell>(2, 2);
matrix.removeRow(0);
assertEquals(1, matrix.height());
}
@Test
public void testIncreaseHeightHeight() {
Matrix<Cell> matrix = new ArrayMatrix<Cell>(1, 1);
matrix.increaseHeight(1);
assertEquals(2, matrix.height());
}
@Test
public void testIncreaseWidthWidth() {
Matrix<Cell> matrix = new ArrayMatrix<Cell>(1, 1);
matrix.increaseWidth(1);
assertEquals(2, matrix.width());
}
@Test
public void testDecreaseWidthWidth() {
Matrix<Cell> matrix = new ArrayMatrix<Cell>(2, 2);
matrix.decreaseWidth(1);
assertEquals(1, matrix.width());
}
@Test
public void testIncreaseWidthMask() {
Matrix<Cell> matrix = new ArrayMatrix<Cell>(1, 1);
matrix.increaseWidth(1);
assertArrayEquals(new boolean[][] {{false, false}}, matrix.mask());
}
@Test
public void testDecreaseWidthMask() {
Matrix<Cell> matrix = new ArrayMatrix<Cell>(2, 2);
matrix.decreaseWidth(1);
assertArrayEquals(new boolean[][] {{false}, {false}}, matrix.mask());
}
public void testHeight() {
Matrix<Cell> matrix = new ArrayMatrix<Cell>(1, 1);
assertEquals(1, matrix.height());
}
public void testWidth() {
Matrix<Cell> matrix = new ArrayMatrix<Cell>(1, 1);
assertEquals(1, matrix.width());
}
public static class Cell {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment