-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
1,616 additions
and
240 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
...thub/skjolber/packing/iterator/AbstractStackableItemGroupPermutationRotationIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package com.github.skjolber.packing.iterator; | ||
|
||
import java.util.List; | ||
|
||
import com.github.skjolber.packing.api.StackableItem; | ||
import com.github.skjolber.packing.api.StackableItemGroup; | ||
|
||
public abstract class AbstractStackableItemGroupPermutationRotationIterator extends AbstractStackableItemPermutationRotationIterator { | ||
|
||
protected List<StackableItemGroup> groups; | ||
|
||
public AbstractStackableItemGroupPermutationRotationIterator(IndexedStackableItem[] matrix, List<StackableItemGroup> groups) { | ||
super(matrix); | ||
this.groups = groups; | ||
} | ||
|
||
/** | ||
* Return number of permutations for boxes which fit within this container. | ||
* | ||
* @return permutation count | ||
*/ | ||
|
||
public long countPermutations() { | ||
// 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 (StackableItemGroup loadableItemGroup : groups) { | ||
|
||
List<StackableItem> 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); | ||
|
||
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; | ||
} | ||
|
||
|
||
@Override | ||
public void removePermutations(List<Integer> removed) { | ||
|
||
for (Integer i : removed) { | ||
IndexedStackableItem loadableItem = stackableItems[i]; | ||
|
||
loadableItem.decrement(); | ||
|
||
if(loadableItem.isEmpty()) { | ||
stackableItems[i] = null; | ||
} | ||
} | ||
|
||
// go through all groups and clean up | ||
for(int i = 0; i < groups.size(); i++) { | ||
StackableItemGroup group = groups.get(i); | ||
|
||
group.removeEmpty(); | ||
if(group.isEmpty()) { | ||
groups.remove(i); | ||
i--; | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.