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(); void clear();
/**
* Removes all empty stacks.
*/
void clean();
/** /**
* @return true if the list is empty, false otherwise * @return true if the list is empty, false otherwise
*/ */

View File

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

View File

@@ -11,7 +11,6 @@ import javax.annotation.Nullable;
import java.util.Collection; import java.util.Collection;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
public class StackListFluid implements IStackList<FluidStack> { public class StackListFluid implements IStackList<FluidStack> {
private ArrayListMultimap<Fluid, FluidStack> stacks = ArrayListMultimap.create(); private ArrayListMultimap<Fluid, FluidStack> stacks = ArrayListMultimap.create();
@@ -109,15 +108,6 @@ public class StackListFluid implements IStackList<FluidStack> {
stacks.clear(); 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 @Override
public boolean isEmpty() { public boolean isEmpty() {
return stacks.isEmpty(); return stacks.isEmpty();

View File

@@ -17,7 +17,6 @@ import java.util.List;
public class StackListItem implements IStackList<ItemStack> { public class StackListItem implements IStackList<ItemStack> {
private ArrayListMultimap<Item, ItemStack> stacks = ArrayListMultimap.create(); private ArrayListMultimap<Item, ItemStack> stacks = ArrayListMultimap.create();
private List<ItemStack> removeTracker = new LinkedList<>(); private List<ItemStack> removeTracker = new LinkedList<>();
protected boolean needsCleanup = false;
@Override @Override
public void add(@Nonnull ItemStack stack, int size) { 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())) { for (ItemStack otherStack : stacks.get(stack.getItem())) {
if (API.instance().getComparer().isEqualNoQuantity(otherStack, stack)) { if (API.instance().getComparer().isEqualNoQuantity(otherStack, stack)) {
boolean success = otherStack.getCount() - size >= 0; boolean success = otherStack.getCount() - size >= 0;
otherStack.shrink(size);
if (otherStack.isEmpty()) { if (otherStack.getCount() - size <= 0) {
needsCleanup = true; stacks.remove(otherStack.getItem(), otherStack);
} else {
otherStack.shrink(size);
} }
return success; return success;
@@ -66,10 +66,11 @@ public class StackListItem implements IStackList<ItemStack> {
this.removeTracker.add(removed); this.removeTracker.add(removed);
boolean success = otherStack.getCount() - size >= 0; boolean success = otherStack.getCount() - size >= 0;
otherStack.shrink(size);
if (otherStack.isEmpty()) { if (otherStack.getCount() - size <= 0) {
needsCleanup = true; stacks.remove(otherStack.getItem(), otherStack);
} else {
otherStack.shrink(size);
} }
return success; return success;
@@ -106,10 +107,6 @@ public class StackListItem implements IStackList<ItemStack> {
@Override @Override
@Nullable @Nullable
public ItemStack get(int hash) { public ItemStack get(int hash) {
if (needsCleanup) {
clean();
}
for (ItemStack stack : this.stacks.values()) { for (ItemStack stack : this.stacks.values()) {
if (API.instance().getItemStackHashCode(stack) == hash) { if (API.instance().getItemStackHashCode(stack) == hash) {
return stack; return stack;
@@ -124,13 +121,6 @@ public class StackListItem implements IStackList<ItemStack> {
stacks.clear(); stacks.clear();
} }
@Override
public void clean() {
stacks.values().removeIf(ItemStack::isEmpty);
needsCleanup = false;
}
@Override @Override
public boolean isEmpty() { public boolean isEmpty() {
return stacks.isEmpty(); return stacks.isEmpty();
@@ -144,10 +134,6 @@ public class StackListItem implements IStackList<ItemStack> {
@Nonnull @Nonnull
@Override @Override
public Collection<ItemStack> getStacks() { public Collection<ItemStack> getStacks() {
if (needsCleanup) {
clean();
}
return stacks.values(); return stacks.values();
} }
@@ -156,10 +142,6 @@ public class StackListItem implements IStackList<ItemStack> {
public IStackList<ItemStack> copy() { public IStackList<ItemStack> copy() {
StackListItem list = new StackListItem(); StackListItem list = new StackListItem();
if (needsCleanup) {
clean();
}
for (ItemStack stack : stacks.values()) { for (ItemStack stack : stacks.values()) {
list.stacks.put(stack.getItem(), stack.copy()); list.stacks.put(stack.getItem(), stack.copy());
} }
@@ -169,10 +151,6 @@ public class StackListItem implements IStackList<ItemStack> {
@Nonnull @Nonnull
public StackListItemOredicted getOredicted() { public StackListItemOredicted getOredicted() {
if (needsCleanup) {
clean();
}
return new StackListItemOredicted(this); 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 com.raoulvdberge.refinedstorage.api.util.IStackList;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary; import net.minecraftforge.oredict.OreDictionary;
import org.apache.commons.lang3.tuple.Pair;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.*; import java.util.Collection;
import java.util.stream.Collectors; import java.util.List;
import java.util.Map;
public class StackListItemOredicted implements IStackList<ItemStack> { public class StackListItemOredicted implements IStackList<ItemStack> {
private StackListItem underlyingList; private StackListItem underlyingList;
private ArrayListMultimap<Integer, ItemStack> stacks = ArrayListMultimap.create(); private ArrayListMultimap<Integer, ItemStack> stacks = ArrayListMultimap.create();
private Set<Integer> touchedIds = new HashSet<>();
private StackListItemOredicted() { private StackListItemOredicted() {
} }
@@ -50,30 +49,12 @@ public class StackListItemOredicted implements IStackList<ItemStack> {
@Override @Override
public boolean remove(@Nonnull ItemStack stack, int size) { public boolean remove(@Nonnull ItemStack stack, int size) {
boolean rvalue = underlyingList.remove(stack, size); return underlyingList.remove(stack, size);
if (underlyingList.needsCleanup) {
touchedIds.addAll(Arrays.stream(OreDictionary.getOreIDs(stack)).boxed().collect(Collectors.toList()));
clean();
}
return rvalue;
} }
@Override @Override
public boolean trackedRemove(@Nonnull ItemStack stack, int size) { public boolean trackedRemove(@Nonnull ItemStack stack, int size) {
// Calling tracked remove with a stack from get causes the reference to be the same return underlyingList.trackedRemove(stack, size);
// 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;
} }
@Override @Override
@@ -123,10 +104,6 @@ public class StackListItemOredicted implements IStackList<ItemStack> {
@Nullable @Nullable
@Override @Override
public ItemStack get(int hash) { public ItemStack get(int hash) {
if (underlyingList.needsCleanup) {
clean();
}
return underlyingList.get(hash); return underlyingList.get(hash);
} }
@@ -136,22 +113,6 @@ public class StackListItemOredicted implements IStackList<ItemStack> {
underlyingList.clear(); 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 @Override
public boolean isEmpty() { public boolean isEmpty() {
return underlyingList.isEmpty(); return underlyingList.isEmpty();