Skip to content

Commit

Permalink
Remove duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
skjolber committed Sep 19, 2024
1 parent d89f262 commit 53d7419
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ public AbstractStackableItemGroupPermutationRotationIterator(IndexedStackableIte
this.groups = groups;
}

protected int getCount() {
int index = 0;
for (IndexedStackableItemGroup loadableItemGroup : groups) {
index += loadableItemGroup.stackableItemsCount();
}
return index;
}

/**
* Return number of permutations for boxes which fit within this container.
*
Expand All @@ -31,6 +39,9 @@ public long countPermutations() {
List<IndexedStackableItem> items = loadableItemGroup.getItems();

int count = loadableItemGroup.stackableItemsCount();
if(count == 0) {
continue;
}

int maxCount = 0;
for (StackableItem value : items) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public List<StackValue> get(PermutationRotationState state, int length) {
public abstract StackValue getStackValue(int index);


protected int[] getFrequencies() {
protected int[] calculateFrequencies() {
int[] frequencies = new int[stackableItems.length];

for (int i = 0; i < stackableItems.length; i++) {
Expand All @@ -82,4 +82,21 @@ protected int[] getFrequencies() {
return frequencies;
}


public long countRotations() {
int[] permutations = getPermutations();

long n = 1;
for (int i = 0; i < permutations.length; i++) {
IndexedStackableItem value = stackableItems[permutations[i]];
if(Long.MAX_VALUE / value.getStackable().getStackValues().length <= n) {
return -1L;
}

n = n * value.getStackable().getStackValues().length;
}
return n;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ public int nextRotation(int maxIndex) {

return -1;
}


@Override
public int[] getPermutations() {
Expand All @@ -186,19 +185,6 @@ protected void resetRotations() {
System.arraycopy(reset, 0, rotations, 0, rotations.length);
}

public long countRotations() {
long n = 1;
for (int i = 0; i < permutations.length; i++) {
IndexedStackableItem value = stackableItems[permutations[i]];
if(Long.MAX_VALUE / value.getStackable().getStackValues().length <= n) {
return -1L;
}

n = n * value.getStackable().getStackValues().length;
}
return n;
}

public int nextPermutation(int maxIndex) {
int[] permutations = this.permutations;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,6 @@ protected void resetRotations() {
System.arraycopy(reset, 0, rotations, 0, rotations.length);
}

public long countRotations() {
long n = 1;
for (int i = 0; i < permutations.length; i++) {
IndexedStackableItem value = stackableItems[permutations[i]];
if(Long.MAX_VALUE / value.getStackable().getStackValues().length <= n) {
return -1L;
}

n = n * value.getStackable().getStackValues().length;
}
return n;
}

/**
* Return number of permutations for boxes which fit within this container.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,19 +362,6 @@ public int length() {
return permutations.length - ParallelPermutationRotationIteratorList.PADDING;
}

public long countRotations() {
long n = 1;
for (int i = 0; i < permutations.length; i++) {
IndexedStackableItem value = stackableItems[permutations[i]];
if(Long.MAX_VALUE / value.getStackable().getStackValues().length <= n) {
return -1L;
}

n = n * value.getStackable().getStackValues().length;
}
return n;
}

/**
* Remove permutations, if present.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,7 @@ public ParallelStackableItemGroupPermutationRotationIteratorList build() {
public ParallelStackableItemGroupPermutationRotationIteratorList(IndexedStackableItem[] matrix, List<IndexedStackableItemGroup> groups, int parallelizationCount) {
super(matrix, groups);

this.frequencies = new int[matrix.length];
for (int i = 0; i < matrix.length; i++) {
if(matrix[i] != null) {
frequencies[i] = matrix[i].getCount();
}
}

this.frequencies = calculateFrequencies();

workUnits = new ParallelStackableItemGroupPermutationRotationIterator[parallelizationCount];
for (int i = 0; i < parallelizationCount; i++) {
Expand Down Expand Up @@ -144,122 +138,6 @@ private void calculate() {
}
}

private int getCount() {
int count = 0;
for (int f : frequencies) {
count += f;
}
return count;
}

public long countPermutations() {
int first = firstDuplicate(frequencies);
if(first == -1) {
return getPermutationCount();
} else {
return getPermutationCountWithRepeatedItems();
}
}

private long getPermutationCount() {
long n = 1;

for (IndexedStackableItemGroup loadableItemGroup : groups) {
int count = loadableItemGroup.stackableItemsCount();

for (long i = 0; i < count; i++) {
if(Long.MAX_VALUE / (i + 1) <= n) {
return -1L;
}
n = n * (i + 1);
}
}
return n;
}

/**
* Return number of permutations for boxes which fit within this container.
*
* @return permutation count
*/

public long getPermutationCountWithRepeatedItems() {
// reduce permutations for boxes which are duplicated

// could be further bounded by looking at how many boxes (i.e. n x the smallest) which actually
// fit within the container volume
long n = 1;

for (IndexedStackableItemGroup loadableItemGroup : groups) {

List<IndexedStackableItem> items = loadableItemGroup.getItems();

int count = loadableItemGroup.stackableItemsCount();

int maxCount = 0;
for (StackableItem value : items) {
if(value != null) {
if(maxCount < value.getCount()) {
maxCount = value.getCount();
}
}
}

if(maxCount > 1) {
int[] factors = new int[maxCount];
for (StackableItem value : items) {
if(value != null) {
for (int k = 0; k < value.getCount(); k++) {
factors[k]++;
}
}
}

for (long i = 0; i < count; i++) {
if(Long.MAX_VALUE / (i + 1) <= n) {
return -1L;
}

n = n * (i + 1);

// reduce n if possible
for (int k = 1; k < maxCount; k++) {
while (factors[k] > 0 && n % (k + 1) == 0) {
n = n / (k + 1);

factors[k]--;
}
}
}

for (int k = 1; k < maxCount; k++) {
while (factors[k] > 0) {
n = n / (k + 1);

factors[k]--;
}
}
} else {
for (long i = 0; i < count; i++) {
if(Long.MAX_VALUE / (i + 1) <= n) {
return -1L;
}
n = n * (i + 1);
}
}
}
return n;
}

private static int firstDuplicate(int[] frequencies) {
for (int i = 0; i < frequencies.length; i++) {
if(frequencies[i] > 1) {
return i;
}
}
return -1;
}

protected static int[] unrank(int[] frequencies, int elementCount, long permutationCount, long rank, List<IndexedStackableItemGroup> groups) {
int[] result = new int[PADDING + elementCount];

Expand Down Expand Up @@ -305,10 +183,6 @@ public ParallelStackableItemGroupPermutationRotationIterator[] getIterators() {
public ParallelStackableItemGroupPermutationRotationIterator getIterator(int i) {
return workUnits[i];
}

public int[] getFrequencies() {
return frequencies;
}

public int length() {
return workUnits[workUnitIndex].length();
Expand Down

0 comments on commit 53d7419

Please sign in to comment.