Remove stack cleaning cruft

This commit is contained in:
raoulvdberge
2017-12-08 10:11:56 +01:00
parent 0546d227c5
commit fc7d81a632
5 changed files with 14 additions and 92 deletions

View File

@@ -111,11 +111,6 @@ public interface IStackList<T> {
*/
void clear();
/**
* Removes all empty stacks.
*/
void clean();
/**
* @return true if the list is empty, false otherwise
*/

View File

@@ -89,9 +89,7 @@ public class CraftingTask implements ICraftingTask {
@Override
public void calculate() {
// Copy here might be expensive but since it is only executed once it isn't a big impact
IStackList<ItemStack> networkList = network.getItemStorageCache().getList().copy();
networkList.clean(); // Remove the zero stacks
networkList = networkList.getOredicted();
IStackList<ItemStack> networkList = network.getItemStorageCache().getList().copy().getOredicted();
IStackList<FluidStack> networkFluidList = network.getFluidStorageCache().getList().copy();

View File

@@ -11,7 +11,6 @@ import javax.annotation.Nullable;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
public class StackListFluid implements IStackList<FluidStack> {
private ArrayListMultimap<Fluid, FluidStack> stacks = ArrayListMultimap.create();
@@ -109,15 +108,6 @@ public class StackListFluid implements IStackList<FluidStack> {
stacks.clear();
}
@Override
public void clean() {
List<FluidStack> toRemove = stacks.values().stream()
.filter(stack -> stack.amount <= 0)
.collect(Collectors.toList());
toRemove.forEach(stack -> stacks.remove(stack.getFluid(), stack));
}
@Override
public boolean isEmpty() {
return stacks.isEmpty();

View File

@@ -17,7 +17,6 @@ import java.util.List;
public class StackListItem implements IStackList<ItemStack> {
private ArrayListMultimap<Item, ItemStack> stacks = ArrayListMultimap.create();
private List<ItemStack> removeTracker = new LinkedList<>();
protected boolean needsCleanup = false;
@Override
public void add(@Nonnull ItemStack stack, int size) {
@@ -45,10 +44,11 @@ public class StackListItem implements IStackList<ItemStack> {
for (ItemStack otherStack : stacks.get(stack.getItem())) {
if (API.instance().getComparer().isEqualNoQuantity(otherStack, stack)) {
boolean success = otherStack.getCount() - size >= 0;
otherStack.shrink(size);
if (otherStack.isEmpty()) {
needsCleanup = true;
if (otherStack.getCount() - size <= 0) {
stacks.remove(otherStack.getItem(), otherStack);
} else {
otherStack.shrink(size);
}
return success;
@@ -66,10 +66,11 @@ public class StackListItem implements IStackList<ItemStack> {
this.removeTracker.add(removed);
boolean success = otherStack.getCount() - size >= 0;
otherStack.shrink(size);
if (otherStack.isEmpty()) {
needsCleanup = true;
if (otherStack.getCount() - size <= 0) {
stacks.remove(otherStack.getItem(), otherStack);
} else {
otherStack.shrink(size);
}
return success;
@@ -106,10 +107,6 @@ public class StackListItem implements IStackList<ItemStack> {
@Override
@Nullable
public ItemStack get(int hash) {
if (needsCleanup) {
clean();
}
for (ItemStack stack : this.stacks.values()) {
if (API.instance().getItemStackHashCode(stack) == hash) {
return stack;
@@ -124,13 +121,6 @@ public class StackListItem implements IStackList<ItemStack> {
stacks.clear();
}
@Override
public void clean() {
stacks.values().removeIf(ItemStack::isEmpty);
needsCleanup = false;
}
@Override
public boolean isEmpty() {
return stacks.isEmpty();
@@ -144,10 +134,6 @@ public class StackListItem implements IStackList<ItemStack> {
@Nonnull
@Override
public Collection<ItemStack> getStacks() {
if (needsCleanup) {
clean();
}
return stacks.values();
}
@@ -156,10 +142,6 @@ public class StackListItem implements IStackList<ItemStack> {
public IStackList<ItemStack> copy() {
StackListItem list = new StackListItem();
if (needsCleanup) {
clean();
}
for (ItemStack stack : stacks.values()) {
list.stacks.put(stack.getItem(), stack.copy());
}
@@ -169,10 +151,6 @@ public class StackListItem implements IStackList<ItemStack> {
@Nonnull
public StackListItemOredicted getOredicted() {
if (needsCleanup) {
clean();
}
return new StackListItemOredicted(this);
}

View File

@@ -5,17 +5,16 @@ import com.raoulvdberge.refinedstorage.api.util.IComparer;
import com.raoulvdberge.refinedstorage.api.util.IStackList;
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;
import org.apache.commons.lang3.tuple.Pair;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.*;
import java.util.stream.Collectors;
import java.util.Collection;
import java.util.List;
import java.util.Map;
public class StackListItemOredicted implements IStackList<ItemStack> {
private StackListItem underlyingList;
private ArrayListMultimap<Integer, ItemStack> stacks = ArrayListMultimap.create();
private Set<Integer> touchedIds = new HashSet<>();
private StackListItemOredicted() {
}
@@ -50,30 +49,12 @@ public class StackListItemOredicted implements IStackList<ItemStack> {
@Override
public boolean remove(@Nonnull ItemStack stack, int size) {
boolean rvalue = underlyingList.remove(stack, size);
if (underlyingList.needsCleanup) {
touchedIds.addAll(Arrays.stream(OreDictionary.getOreIDs(stack)).boxed().collect(Collectors.toList()));
clean();
}
return rvalue;
return underlyingList.remove(stack, size);
}
@Override
public boolean trackedRemove(@Nonnull ItemStack stack, int size) {
// Calling tracked remove with a stack from get causes the reference to be the same
// When the underlying list empties the stack the reference will become empty, thus we need to copy before hand
ItemStack original = stack.copy();
boolean rvalue = underlyingList.trackedRemove(stack, size);
if (underlyingList.needsCleanup) {
touchedIds.addAll(Arrays.stream(OreDictionary.getOreIDs(original)).boxed().collect(Collectors.toList()));
clean();
}
return rvalue;
return underlyingList.trackedRemove(stack, size);
}
@Override
@@ -123,10 +104,6 @@ public class StackListItemOredicted implements IStackList<ItemStack> {
@Nullable
@Override
public ItemStack get(int hash) {
if (underlyingList.needsCleanup) {
clean();
}
return underlyingList.get(hash);
}
@@ -136,22 +113,6 @@ public class StackListItemOredicted implements IStackList<ItemStack> {
underlyingList.clear();
}
private void localClean() {
List<Map.Entry<Integer, ItemStack>> toRemove = touchedIds.stream()
.flatMap(id -> stacks.get(id).stream().map(stack -> Pair.of(id, stack)))
.filter(entry -> entry.getValue().isEmpty())
.collect(Collectors.toList());
toRemove.forEach(entry -> stacks.remove(entry.getKey(), entry.getValue()));
touchedIds.clear();
}
@Override
public void clean() {
localClean();
underlyingList.clean();
}
@Override
public boolean isEmpty() {
return underlyingList.isEmpty();