Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Commit

Permalink
Optimized size estimates
Browse files Browse the repository at this point in the history
  • Loading branch information
nibix committed Jun 15, 2024
1 parent 563e51c commit 59a36c0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
11 changes: 9 additions & 2 deletions src/main/java/com/selectivem/check/BackingCollections.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ static <E> IndexedUnmodifiableSet<E> of(Set<E> set) {
} else {
if (size <= 800) {
InternalBuilder<E> internalBuilder = new HashArrayBackedSet.Builder<E>(
size <= 8 ? 16 : size <= 40 ? 64 : size < 200 ? 256 : 1024);
size <= 8 ? 16 : size <= 40 ? 64 : size < 200 ? 256 : 1024, size);

for (E e : set) {
internalBuilder = internalBuilder.with(e);
Expand All @@ -126,7 +126,7 @@ static <E> IndexedUnmodifiableSet<E> of(Set<E> set) {

static <E> InternalBuilder<E> builder(int size) {
if (size <= 800) {
return new HashArrayBackedSet.Builder<E>(size <= 10 ? 16 : size <= 50 ? 64 : size < 200 ? 256 : 1024);
return new HashArrayBackedSet.Builder<E>(size <= 10 ? 16 : size <= 50 ? 64 : size < 200 ? 256 : 1024, size);
} else {
return new SetBackedSet.Builder<E>(size);
}
Expand Down Expand Up @@ -662,6 +662,13 @@ public Builder(int tableSize) {
this.tableSize = tableSize;
}

public Builder(int tableSize, int flatSize) {
this.tableSize = tableSize;
if (flatSize > 0) {
this.flat = createEArray(flatSize);
}
}

public InternalBuilder<E> with(E e) {
if (e == null) {
throw new IllegalArgumentException("Null elements are not supported");
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/com/selectivem/check/CheckTableImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1449,7 +1449,6 @@ public String toTableString(String checkedIndicator, String uncheckedIndicator)

i = 0;
for (C column : columns) {

String v = isChecked(row, column) ? checkedIndicator : uncheckedIndicator;

result.append(" ");
Expand All @@ -1458,7 +1457,6 @@ public String toTableString(String checkedIndicator, String uncheckedIndicator)
i++;
}
result.append("\n");

}

return result.toString();
Expand All @@ -1474,7 +1472,8 @@ public Set<R> getCompleteRows() {
return rows;
}

BackingCollections.IndexedUnmodifiableSet.InternalBuilder<R> builder = BackingCollections.IndexedUnmodifiableSet.builder(rowCount);
int estimatedRows = Math.min(checkedCount / columnCount + 12, rowCount - 1);
BackingCollections.IndexedUnmodifiableSet.InternalBuilder<R> builder = BackingCollections.IndexedUnmodifiableSet.builder(estimatedRows);

for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
if (isRowCompleted(rowIndex)) {
Expand All @@ -1496,7 +1495,8 @@ public Set<C> getCompleteColumns() {
return columns;
}

BackingCollections.IndexedUnmodifiableSet.InternalBuilder<C> builder = BackingCollections.IndexedUnmodifiableSet.builder(columnCount);
int estimatedColumns = Math.min(checkedCount / rowCount + 12, columnCount - 1);
BackingCollections.IndexedUnmodifiableSet.InternalBuilder<C> builder = BackingCollections.IndexedUnmodifiableSet.builder(estimatedColumns);

for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
if (isColumnCompleted(columnIndex)) {
Expand All @@ -1518,7 +1518,8 @@ public Set<R> getIncompleteRows() {
return rows;
}

BackingCollections.IndexedUnmodifiableSet.InternalBuilder<R> builder = BackingCollections.IndexedUnmodifiableSet.builder(rowCount);
int estimatedRows = Math.min(uncheckedCount / columnCount + 12, rowCount - 1);
BackingCollections.IndexedUnmodifiableSet.InternalBuilder<R> builder = BackingCollections.IndexedUnmodifiableSet.builder(estimatedRows);

for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
if (!isRowCompleted(rowIndex)) {
Expand All @@ -1540,7 +1541,8 @@ public Set<C> getIncompleteColumns() {
return columns;
}

BackingCollections.IndexedUnmodifiableSet.InternalBuilder<C> builder = BackingCollections.IndexedUnmodifiableSet.builder(columnCount);
int estimatedColumns = Math.min(uncheckedCount / rowCount + 12, columnCount - 1);
BackingCollections.IndexedUnmodifiableSet.InternalBuilder<C> builder = BackingCollections.IndexedUnmodifiableSet.builder(estimatedColumns);

for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
if (!isColumnCompleted(columnIndex)) {
Expand Down

0 comments on commit 59a36c0

Please sign in to comment.