Skip to content

Commit

Permalink
Merge pull request #15 from RomualdRousseau/bug-fix/fix-row-index
Browse files Browse the repository at this point in the history
Bug fix/fix row index
  • Loading branch information
RomualdRousseau authored Sep 22, 2024
2 parents ccea90b + 8b973af commit c25b078
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ public class BaseRow implements Row {
public BaseRow(final BaseTable table, final int rowIndex) {
this.table = table;
this.rowIndex = rowIndex;
this.cachedCells = new BaseCell[table.getNumberOfColumns()];

this.ignored = table.ignoreRows().stream().anyMatch(x -> table.getFirstRowOffset() + x == rowIndex);
this.cellCount = 0;
this.emptyCellCount = 0;
this.islandCellCount = 0;
this.cellCountUpdated = false;
this.cachedCells = new BaseCell[table.getNumberOfColumns()];
this.ignored = false;
this.rowNum = 0;
}

Expand Down Expand Up @@ -58,9 +59,9 @@ public BaseCell getCellAt(final int colIndex) {
if (colIndex < 0 || colIndex >= this.table.getNumberOfColumns()) {
throw new IndexOutOfBoundsException();
}
BaseCell result = cachedCells[colIndex];
var result = cachedCells[colIndex];
if (result == null) {
final String v = this.getCellValueAt(colIndex);
final var v = this.getCellValueAt(colIndex);
result = new BaseCell(v, colIndex, this.getNumberOfMergedCellsAt(colIndex), this.table.getSheet());
cachedCells[colIndex] = result;
}
Expand Down Expand Up @@ -104,7 +105,7 @@ protected int getNumberOfMergedCellsAt(final int colIndex) {
private void updateCellCount() {
int n = 0;
for (int i = 0; i < this.table.getNumberOfColumns();) {
final BaseCell cell = this.getCellAt(i);
final var cell = this.getCellAt(i);
if (!cell.hasValue()) {
this.emptyCellCount++;
n = 0;
Expand All @@ -121,11 +122,12 @@ private void updateCellCount() {

private final BaseTable table;
private final int rowIndex;
private final BaseCell[] cachedCells;

private boolean ignored;
private int cellCount;
private int emptyCellCount;
private int islandCellCount;
private boolean cellCountUpdated;
private final BaseCell[] cachedCells;
private boolean ignored;
private int rowNum;
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,8 @@ public BaseRow getRowAt(final int rowIndex) {
if (rowIndex < 0 || rowIndex >= this.getNumberOfRows()) {
throw new ArrayIndexOutOfBoundsException(rowIndex);
}
return this.cachedRows.computeIfAbsent(this.firstRow + this.firstRowOffset + rowIndex, (x) -> {
final var result = new BaseRow(this, x);
// Retrieve ignore status possibly lost in cache removal
for (final var i : this.ignoreRows()) {
if (i == rowIndex) {
result.setIgnored(true);
}
}
return result;
});
final var rowGID = this.firstRow + this.firstRowOffset + rowIndex;
return this.cachedRows.computeIfAbsent(rowGID, x -> new BaseRow(this, x - this.firstRow));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class DropColumnsWhenEntropyLessThan {

public static void Apply(final BaseSheet sheet, final float minEntropy) {
for(int j = 0; j <= sheet.getLastColumnNum(); j++) {
final HashMap<String, Double> x = new HashMap<>();
final var x = new HashMap<String, Double>();
var n = 0;
for(int i = 0; i <= sheet.getLastRowNum(); i++) {
if(sheet.hasCellDataAt(j, i)) {
Expand All @@ -31,7 +31,7 @@ public static void Apply(final BaseSheet sheet, final float minEntropy) {

public static void Apply(final BaseSheet sheet, final float minEntropy, final int start, final int stop) {
for(int j = 0; j <= sheet.getLastColumnNum(); j++) {
final HashMap<String, Double> x = new HashMap<>();
final var x = new HashMap<String, Double>();
var n = 0;
for(int i = start; i <= stop; i++) {
if(sheet.hasCellDataAt(j, i)) {
Expand All @@ -42,7 +42,7 @@ public static void Apply(final BaseSheet sheet, final float minEntropy, final in
}
}
}
final float e = (float) computeEntropy(x, n);
final var e = (float) computeEntropy(x, n);
if (e <= minEntropy) {
sheet.markColumnAsNull(j);
}
Expand All @@ -51,7 +51,7 @@ public static void Apply(final BaseSheet sheet, final float minEntropy, final in
}

private static double computeEntropy(HashMap<String, Double> x, double n) {
var result = 0.0f;
var result = 0.0;
for (final Entry<String, Double> e: x.entrySet()) {
double p = e.getValue() / n;
result += p * Math.log(p) / Math.log(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static void Apply(final BaseSheet sheet, final float minEntropy) {
for(int i = 0; i <= sheet.getLastRowNum(); i++) {
final var x = new HashMap<String, Double>();
final var lastColumnNum = sheet.getLastColumnNum(i);
var n = 0;
int n = 0;
for(int j = 0; j <= lastColumnNum; j++) {
if(sheet.hasCellDataAt(j, i)) {
final var value = sheet.getCellDataAt(j, i);
Expand All @@ -22,7 +22,7 @@ public static void Apply(final BaseSheet sheet, final float minEntropy) {
}
}
}
final float e = (float) computeEntropy(x, n);
final var e = (float) computeEntropy(x, n);
if (e <= minEntropy) {
sheet.markRowAsNull(i);
}
Expand All @@ -32,8 +32,8 @@ public static void Apply(final BaseSheet sheet, final float minEntropy) {

public static void Apply(final BaseSheet sheet, final float minEntropy, final int start, final int stop) {
for(int i = 0; i <= sheet.getLastRowNum(); i++) {
final HashMap<String, Double> x = new HashMap<>();
var n = 0;
final var x = new HashMap<String, Double>();
int n = 0;
for(int j = start; j <= stop; j++) {
if(sheet.hasCellDataAt(j, i)) {
final var value = sheet.getCellDataAt(j, i);
Expand All @@ -52,7 +52,7 @@ public static void Apply(final BaseSheet sheet, final float minEntropy, final in
}

private static double computeEntropy(final HashMap<String, Double> x, final double n) {
var result = 0.0f;
var result = 0.0;
for (final Entry<String, Double> e: x.entrySet()) {
final double p = e.getValue() / n;
result += p * Math.log(p) / Math.log(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ public static void deleteDir(final Path dir) throws IOException {
}

public static void zipDir(final Path sourceDirPath, final File zipFilePath) throws IOException {
try (final ZipOutputStream zs = new ZipOutputStream(new FileOutputStream(zipFilePath))) {
try (final var zs = new ZipOutputStream(new FileOutputStream(zipFilePath))) {
Files.walk(sourceDirPath)
.filter(path -> !Files.isDirectory(path))
.forEach(path -> {
final ZipEntry zipEntry = new ZipEntry(sourceDirPath.relativize(path).toString().replace("\\", "/"));
final var zipEntry = new ZipEntry(sourceDirPath.relativize(path).toString().replace("\\", "/"));
try {
zs.putNextEntry(zipEntry);
Files.copy(path, zs);
Expand All @@ -43,16 +43,19 @@ public static void zipDir(final Path sourceDirPath, final File zipFilePath) thro

public static void unzipDir(final Path zipFile, final Path folder) throws IOException {
final byte[] buffer = new byte[4096];
try (final ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile.toFile()))) {
try (final var zis = new ZipInputStream(new FileInputStream(zipFile.toFile()))) {
ZipEntry ze = zis.getNextEntry();
while (ze != null) {
final Path newFile = folder.resolve(ze.getName());
final var newFile = folder.resolve(ze.getName()).normalize();
if (!newFile.startsWith(folder)) {
throw new IOException("Bad zip entry: " + ze.getName());
}

// Ensure parent directory exists
newFile.getParent().toFile().mkdirs();

if (!ze.isDirectory()) {
try (final FileOutputStream fos = new FileOutputStream(newFile.toFile())) {
try (final var fos = new FileOutputStream(newFile.toFile())) {
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
Expand All @@ -75,8 +78,8 @@ public static void copyFile(Path src, Path dest) {
}

public static void removeFileName(final Path filename1, final Path filename2) {
final File file1 = filename1.toFile();
final File file2 = filename2.toFile();
final var file1 = filename1.toFile();
final var file2 = filename2.toFile();
file1.renameTo(file2);
}

Expand Down

0 comments on commit c25b078

Please sign in to comment.