Skip to content

Commit

Permalink
#159 MappedColumn and MappedRow bug
Browse files Browse the repository at this point in the history
  • Loading branch information
vssekorin committed Mar 29, 2018
1 parent f3fa198 commit 5eb7fc0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
17 changes: 14 additions & 3 deletions src/main/java/com/vssekorin/cactoosmath/matrix/MappedColumn.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,22 @@ public final class MappedColumn<T> extends MatrixEnvelope<T> {
public MappedColumn(final Matrix<T> src, final int number,
final Func<T, T> map) {
super(() -> () -> {
final int rows = new NmbRows<>(src).value();
final int cols = new NmbColumns<>(src).value();
final T[][] result = (T[][]) new Object[rows][cols];
final T[][] array = src.asArray();
for (int row = 0; row < new NmbRows<>(src).value(); ++row) {
array[row][number] = map.apply(array[row][number]);
for (int col = 0; col < cols; ++col) {
if (col == number) {
for (int row = 0; row < rows; ++row) {
result[row][col] = map.apply(array[row][col]);
}
} else {
for (int row = 0; row < rows; ++row) {
result[row][col] = array[row][col];
}
}
}
return array;
return result;
});
}
}
15 changes: 12 additions & 3 deletions src/main/java/com/vssekorin/cactoosmath/matrix/MappedRow.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,20 @@ public final class MappedRow<T> extends MatrixEnvelope<T> {
public MappedRow(final Matrix<T> src, final int number,
final Func<T, T> map) {
super(() -> () -> {
final int rows = new NmbRows<>(src).value();
final int cols = new NmbColumns<>(src).value();
final T[][] result = (T[][]) new Object[rows][cols];
final T[][] array = src.asArray();
for (int col = 0; col < new NmbColumns<>(src).value(); ++col) {
array[number][col] = map.apply(array[number][col]);
for (int row = 0; row < rows; ++row) {
if (row == number) {
for (int col = 0; col < cols; ++col) {
result[row][col] = map.apply(array[row][col]);
}
} else {
System.arraycopy(array[row], 0, result[row], 0, cols);
}
}
return array;
return result;
});
}
}

0 comments on commit 5eb7fc0

Please sign in to comment.