Skip to content

Commit

Permalink
Add transformation methods to table classes
Browse files Browse the repository at this point in the history
  • Loading branch information
p-kovacs committed Dec 15, 2021
1 parent f416b77 commit b6bb7f8
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/main/java/pkovacs/util/data/AbstractTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public int size() {

abstract void set0(int row, int col, T value);

abstract AbstractTable<T> newInstance(int rowCount, int colCount, BiFunction<Integer, Integer, T> function);

/**
* Returns true if this table contains the given cell.
*/
Expand Down Expand Up @@ -119,4 +121,48 @@ public void updateAll(Function<? super T, ? extends T> function) {
}
}

/**
* Mirrors this table horizontally: the row indices remain the same, while column indices are flipped.
* A new table is generated and returned, this table is not changed.
*/
public AbstractTable<T> mirrorHorizontally() {
int colCount = colCount();
return newInstance(rowCount(), colCount, (i, j) -> get0(i, colCount - 1 - j));
}

/**
* Mirrors this table vertically: the column indices remain the same, while the row indices are flipped.
* A new table is generated and returned, this table is not changed.
*/
public AbstractTable<T> mirrorVertically() {
int rowCount = rowCount();
return newInstance(rowCount, colCount(), (i, j) -> get0(rowCount - 1 - i, j));
}

/**
* Rotates this table to the right (clockwise).
* A new table is generated and returned, this table is not changed.
*/
public AbstractTable<T> rotateRight() {
int rowCount = rowCount();
return newInstance(colCount(), rowCount(), (i, j) -> get0(rowCount - 1 - j, i));
}

/**
* Rotates this table to the left (counter-clockwise).
* A new table is generated and returned, this table is not changed.
*/
public AbstractTable<T> rotateLeft() {
int colCount = colCount();
return newInstance(colCount(), rowCount(), (i, j) -> get0(j, colCount - 1 - i));
}

/**
* Transposes this table: turns rows into columns and vice versa.
* A new table is generated and returned, this table is not changed.
*/
public AbstractTable<T> transpose() {
return newInstance(colCount(), rowCount(), (i, j) -> get0(j, i));
}

}
30 changes: 30 additions & 0 deletions src/main/java/pkovacs/util/data/CharTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ void set0(int row, int col, Character value) {
data[row][col] = value;
}

@Override
CharTable newInstance(int rowCount, int colCount, BiFunction<Integer, Integer, Character> function) {
return new CharTable(rowCount, colCount, function);
}

/**
* Returns the {@code char[][]} array that backs this table. Changes to the returned array are reflected in the
* table, and vice-versa.
Expand Down Expand Up @@ -160,6 +165,31 @@ public Stream<Character> values(int startRow, int startCol, int endRow, int endC
return cells(startRow, startCol, endRow, endCol).map(this::get);
}

@Override
public CharTable mirrorHorizontally() {
return (CharTable) super.mirrorHorizontally();
}

@Override
public CharTable mirrorVertically() {
return (CharTable) super.mirrorVertically();
}

@Override
public CharTable rotateRight() {
return (CharTable) super.rotateRight();
}

@Override
public CharTable rotateLeft() {
return (CharTable) super.rotateLeft();
}

@Override
public CharTable transpose() {
return (CharTable) super.transpose();
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/pkovacs/util/data/IntTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ void set0(int row, int col, Integer value) {
data[row][col] = value;
}

@Override
IntTable newInstance(int rowCount, int colCount, BiFunction<Integer, Integer, Integer> function) {
return new IntTable(rowCount, colCount, function);
}

/**
* Returns the {@code int[][]} array that backs this table. Changes to the returned array are reflected in the
* table, and vice-versa.
Expand Down Expand Up @@ -193,6 +198,31 @@ public int max() {
return values().max().orElseThrow();
}

@Override
public IntTable mirrorHorizontally() {
return (IntTable) super.mirrorHorizontally();
}

@Override
public IntTable mirrorVertically() {
return (IntTable) super.mirrorVertically();
}

@Override
public IntTable rotateRight() {
return (IntTable) super.rotateRight();
}

@Override
public IntTable rotateLeft() {
return (IntTable) super.rotateLeft();
}

@Override
public IntTable transpose() {
return (IntTable) super.transpose();
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/pkovacs/util/data/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ void set0(int row, int col, T value) {
data[row][col] = value;
}

@Override
Table<T> newInstance(int rowCount, int colCount, BiFunction<Integer, Integer, T> function) {
return new Table<T>(rowCount, colCount, function);
}

/**
* Returns the value associated with the specified cell.
*/
Expand Down Expand Up @@ -148,6 +153,31 @@ public Stream<T> values(int startRow, int startCol, int endRow, int endCol) {
return cells(startRow, startCol, endRow, endCol).map(this::get);
}

@Override
public Table<T> mirrorHorizontally() {
return (Table<T>) super.mirrorHorizontally();
}

@Override
public Table<T> mirrorVertically() {
return (Table<T>) super.mirrorVertically();
}

@Override
public Table<T> rotateRight() {
return (Table<T>) super.rotateRight();
}

@Override
public Table<T> rotateLeft() {
return (Table<T>) super.rotateLeft();
}

@Override
public Table<T> transpose() {
return (Table<T>) super.transpose();
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/pkovacs/util/data/AbstractTableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,40 @@ void testCellNeighbors() {
table.extendedNeighborCells(new Tile(2, 1)).toList());
}

@Test
void testTransformations() {
var table = createTestTable(3, 4);
var transposed = table.transpose();
var right = table.rotateRight();
var left = table.rotateLeft();

assertEquals(table.rowCount(), transposed.colCount());
assertEquals(table.colCount(), transposed.rowCount());
assertEquals(table.rowCount(), right.colCount());
assertEquals(table.colCount(), right.rowCount());
assertEquals(table.rowCount(), left.colCount());
assertEquals(table.colCount(), left.rowCount());

assertNotEquals(transposed, right);
assertNotEquals(transposed, left);
assertNotEquals(right, left);

assertEquals(table, transposed.transpose());
assertEquals(table, right.rotateLeft());
assertEquals(table, left.rotateRight());
assertEquals(table, right.rotateRight().rotateRight().rotateRight());
assertEquals(table, left.rotateLeft().rotateLeft().rotateLeft());
assertEquals(table, right.rotateRight().mirrorVertically().mirrorHorizontally());
assertEquals(table, left.rotateLeft().mirrorVertically().mirrorHorizontally());

assertEquals(right.rotateRight(), left.rotateLeft());

assertEquals(transposed, right.mirrorHorizontally());
assertEquals(transposed, left.mirrorVertically());
assertEquals(transposed, table.mirrorVertically().rotateRight());
assertEquals(transposed, table.mirrorHorizontally().rotateLeft());
}

@Test
void testEqualsAndHashCode() {
var table1 = createTestTable(3, 4);
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/pkovacs/util/data/IntTableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ void testToString() {
table3.set(1, 1, 424242);
assertEquals(" 0 -1 2 -3\n -100 424242 -102 103\n 200 -201 202 -203\n",
table3.toString());

var table4 = createTestTable(3, 4).transpose();
var table5 = createTestTable(3, 4).rotateRight();

assertEquals(" 0 100 200\n 1 101 201\n 2 102 202\n 3 103 203\n", table4.toString());
assertEquals("200 100 0\n201 101 1\n202 102 2\n203 103 3\n", table5.toString());
}

private static void assertContentEquals(int[][] expected, IntTable table) {
Expand Down

0 comments on commit b6bb7f8

Please sign in to comment.