Lists > Sets in this situation

This commit is contained in:
Aidan Brady 2013-08-05 02:08:37 -04:00
parent 0d052c8090
commit b44ea67b46
5 changed files with 205 additions and 236 deletions

View file

@ -0,0 +1,167 @@
package resonantinduction.base;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
public class ListUtil
{
public static <V> List<V> inverse(List<V> list)
{
List<V> toReturn = new ArrayList<V>();
for(int i = list.size(); i >= 0; i--)
{
toReturn.add((V)list.get(i));
}
return toReturn;
}
public static <V> List<V> cap(List<V> list, int cap)
{
List<V> toReturn = new ArrayList<V>();
if(list.size() <= cap)
{
toReturn = copy(list);
}
else {
int count = 0;
for(V obj : list)
{
count++;
toReturn.add(obj);
if(count == cap)
{
break;
}
}
}
return toReturn;
}
public static <V> List<V> copy(List<V> list)
{
List<V> toReturn = new ArrayList<V>();
for(V obj : list)
{
toReturn.add(obj);
}
return toReturn;
}
public static <V> List<V> merge(List<V> listOne, List<V> listTwo)
{
List<V> newList = new ArrayList<V>();
for(V obj : listOne)
{
newList.add(obj);
}
for(V obj : listTwo)
{
newList.add(obj);
}
return newList;
}
public static <V> List<V> capRemains(List<V> list, int cap)
{
List<V> toReturn = new ArrayList<V>();
if(list.size() <= cap)
{
return toReturn;
}
else {
List<V> inverse = inverse(list);
int iterNeeded = list.size()-cap;
int count = 0;
for(V obj : list)
{
count++;
toReturn.add(obj);
if(count == iterNeeded)
{
break;
}
}
return toReturn;
}
}
public static <V> ArrayList<List<V>> split(List<V> list, int divide)
{
int remain = list.size()%divide;
int size = (list.size()/divide)-remain;
ArrayList<List<V>> toReturn = new ArrayList<List<V>>();
for(int i = 0; i < divide; i++)
{
toReturn.add(i, new ArrayList<V>());
}
for(List<V> iterSet : toReturn)
{
List<V> removed = new ArrayList<V>();
int toAdd = size;
if(remain > 0)
{
remain--;
toAdd++;
}
for(V obj : list)
{
if(toAdd == 0)
{
break;
}
iterSet.add(obj);
removed.add(obj);
toAdd--;
}
for(V obj : removed)
{
list.remove(obj);
}
}
return toReturn;
}
public static <V> V getTop(List<V> list)
{
for(V obj : list)
{
return obj;
}
return null;
}
public static <V> List<V> asList(Set<V> set)
{
return (List<V>)Arrays.asList(set.toArray());
}
}

View file

@ -1,210 +0,0 @@
package resonantinduction.base;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class SetUtil
{
/**
* Returns a new set with the reverse of the provided set's order.
* @param set - set to perform the operation on
* @return new set with the previous set's order reversed
*/
public static <V> Set<V> inverse(Set<V> set)
{
Set<V> toReturn = new HashSet<V>();
List list = Arrays.asList(set.toArray());
for(int i = list.size(); i >= 0; i--)
{
toReturn.add((V)list.get(i));
}
return toReturn;
}
/**
* Returns a copy of the provided set, removing all objects after the defined cap index.
* @param set - set to perform the operation on
* @param cap - maximum amount of objects this set can store
* @return new set with previous set's objects other than those after the cap
*/
public static <V> Set<V> cap(Set<V> set, int cap)
{
Set<V> toReturn = new HashSet<V>();
if(set.size() <= cap)
{
toReturn = copy(set);
}
else {
int count = 0;
for(V obj : set)
{
count++;
toReturn.add(obj);
if(count == cap)
{
break;
}
}
}
return toReturn;
}
/**
* Copies a set.
* @param set - set to copy
* @return copied set
*/
public static <V> Set<V> copy(Set<V> set)
{
Set<V> toReturn = new HashSet<V>();
for(V obj : set)
{
toReturn.add(obj);
}
return toReturn;
}
/**
* Returns a new set created by merging two others together.
* @param setOne - a set to be used in the merge
* @param setTwo - a set to be used in the merge
* @return new merged set
*/
public static <V> Set<V> merge(Set<V> setOne, Set<V> setTwo)
{
Set<V> newSet = new HashSet<V>();
for(V obj : setOne)
{
newSet.add(obj);
}
for(V obj : setTwo)
{
newSet.add(obj);
}
return newSet;
}
public static <V> Set<V> capRemains(Set<V> set, int cap)
{
Set<V> toReturn = new HashSet<V>();
if(set.size() <= cap)
{
return toReturn;
}
else {
Set<V> inverse = inverse(set);
int iterNeeded = set.size()-cap;
int count = 0;
for(V obj : set)
{
count++;
toReturn.add(obj);
if(count == iterNeeded)
{
break;
}
}
return toReturn;
}
}
/**
* Splits a set into the defined amount of new sets, and returns them in an ArrayList.
* @param set - set to split
* @param divide - how many new sets should be created
* @return sets split by the defined amount
*/
public static <V> ArrayList<Set<V>> split(Set<V> set, int divide)
{
int remain = set.size()%divide;
int size = (set.size()/divide)-remain;
ArrayList<Set<V>> toReturn = new ArrayList<Set<V>>();
for(int i = 0; i < divide; i++)
{
toReturn.add(i, new HashSet<V>());
}
for(Set<V> iterSet : toReturn)
{
Set<V> removed = new HashSet<V>();
int toAdd = size;
if(remain > 0)
{
remain--;
toAdd++;
}
for(V obj : set)
{
if(toAdd == 0)
{
break;
}
iterSet.add(obj);
removed.add(obj);
toAdd--;
}
for(V obj : removed)
{
set.remove(obj);
}
}
return toReturn;
}
/**
* Gets the top object in a set.
* @param set - set to get the object from
* @return top object in the set
*/
public static <V> V getTop(Set<V> set)
{
for(V obj : set)
{
if(obj != null)
{
return obj;
}
}
return null;
}
/**
* Returns a List of a Set, while maintaining all it's objects.
* @param set - set to turn into a list
* @return list with defined set's objects
*/
public static <V> List<V> asList(Set<V> set)
{
return (List<V>)Arrays.asList(set.toArray());
}
}

View file

@ -9,7 +9,7 @@ import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection;
import resonantinduction.base.SetUtil;
import resonantinduction.base.ListUtil;
import resonantinduction.base.Vector3;
public class BatteryUpdateProtocol
@ -220,10 +220,10 @@ public class BatteryUpdateProtocol
int maxCells = iteratedNodes.size()*BatteryManager.CELLS_PER_BATTERY;
//TODO eject these
Set<ItemStack> rejected = SetUtil.capRemains(oldStructure.inventory, maxCells);
List<ItemStack> rejected = ListUtil.capRemains(oldStructure.inventory, maxCells);
ArrayList<Set<ItemStack>> inventories = SetUtil.split(SetUtil.cap(oldStructure.inventory, maxCells), iteratedNodes.size());
List<TileEntityBattery> iterList = SetUtil.asList(iteratedNodes);
ArrayList<List<ItemStack>> inventories = ListUtil.split(ListUtil.cap(oldStructure.inventory, maxCells), iteratedNodes.size());
List<TileEntityBattery> iterList = ListUtil.asList(iteratedNodes);
boolean didVisibleInventory = false;
@ -270,7 +270,7 @@ public class BatteryUpdateProtocol
{
TileEntityBattery tileEntity = (TileEntityBattery)obj.getTileEntity(pointer.worldObj);
structureFound.inventory = SetUtil.merge(structureFound.inventory, tileEntity.structure.inventory);
structureFound.inventory = ListUtil.merge(structureFound.inventory, tileEntity.structure.inventory);
if(tileEntity.structure.hasVisibleInventory())
{
@ -280,7 +280,7 @@ public class BatteryUpdateProtocol
tileEntity.structure = structureFound;
}
structureFound.inventory = SetUtil.cap(structureFound.inventory, structureFound.getMaxCells());
structureFound.inventory = ListUtil.cap(structureFound.inventory, structureFound.getMaxCells());
}
else {
disperseCells();

View file

@ -1,18 +1,20 @@
package resonantinduction.battery;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import net.minecraft.item.ItemStack;
import resonantinduction.api.IBattery;
import resonantinduction.base.SetUtil;
import resonantinduction.base.ListUtil;
import resonantinduction.base.Vector3;
public class SynchronizedBatteryData
{
public Set<Vector3> locations = new HashSet<Vector3>();
public Set<ItemStack> inventory = new HashSet<ItemStack>();
public List<ItemStack> inventory = new ArrayList<ItemStack>();
/**
* Slot 0: Cell input slot
@ -47,7 +49,7 @@ public class SynchronizedBatteryData
public void sortInventory()
{
Object[] array = SetUtil.copy(inventory).toArray();
Object[] array = ListUtil.copy(inventory).toArray();
ItemStack[] toSort = new ItemStack[array.length];
@ -63,19 +65,19 @@ public class SynchronizedBatteryData
{
cont = false;
for(int j = 0; j < toSort.length-1; j++)
for(int i = 0; i < toSort.length-1; i++)
{
if(((IBattery)toSort[j].getItem()).getEnergyStored(toSort[j]) < ((IBattery)toSort[j+1].getItem()).getEnergyStored(toSort[j+1]))
if(((IBattery)toSort[i].getItem()).getEnergyStored(toSort[i]) < ((IBattery)toSort[i+1].getItem()).getEnergyStored(toSort[i+1]))
{
temp = toSort[j];
toSort[j] = toSort[j+1];
toSort[j+1] = temp;
temp = toSort[i];
toSort[i] = toSort[i+1];
toSort[i+1] = temp;
cont = true;
}
}
}
inventory = new HashSet<ItemStack>();
inventory = new ArrayList<ItemStack>();
for(ItemStack itemStack : toSort)
{
@ -96,7 +98,7 @@ public class SynchronizedBatteryData
return false;
}
public static SynchronizedBatteryData getBase(TileEntityBattery tileEntity, Set<ItemStack> inventory)
public static SynchronizedBatteryData getBase(TileEntityBattery tileEntity, List<ItemStack> inventory)
{
SynchronizedBatteryData structure = getBase(tileEntity);
structure.inventory = inventory;

View file

@ -4,8 +4,7 @@
package resonantinduction.battery;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.List;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
@ -15,7 +14,7 @@ import net.minecraft.nbt.NBTTagList;
import resonantinduction.PacketHandler;
import resonantinduction.api.IBattery;
import resonantinduction.base.IPacketReceiver;
import resonantinduction.base.SetUtil;
import resonantinduction.base.ListUtil;
import resonantinduction.base.TileEntityBase;
import com.google.common.io.ByteArrayDataInput;
@ -38,8 +37,6 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver
@Override
public void updateEntity()
{
//DO NOT SUPER, CALCLAVIA!
ticks++;
if(!worldObj.isRemote)
@ -49,6 +46,18 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver
PacketHandler.sendTileEntityPacketToClients(this, getNetworkedData(new ArrayList()).toArray());
}
if(ListUtil.getTop(structure.inventory) != null)
{
System.out.println("-----");
for(ItemStack stack : structure.inventory)
{
System.out.println(((IBattery)stack.getItem()).getEnergyStored(stack));
}
System.out.println("----");
}
if(ticks == 5 && !structure.isMultiblock)
{
update();
@ -92,12 +101,12 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver
if(nbtTags.hasKey("Items"))
{
NBTTagList tagList = nbtTags.getTagList("Items");
structure.inventory = new HashSet<ItemStack>();
structure.inventory = new ArrayList<ItemStack>();
for(int tagCount = 0; tagCount < tagList.tagCount(); tagCount++)
{
NBTTagCompound tagCompound = (NBTTagCompound)tagList.tagAt(tagCount);
System.out.println("Yup!");
structure.inventory.add(ItemStack.loadItemStackFromNBT(tagCompound));
}
}
@ -135,6 +144,7 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver
{
if(itemStack != null)
{
System.out.println("YESTYSET");
NBTTagCompound tagCompound = new NBTTagCompound();
itemStack.writeToNBT(tagCompound);
tagList.appendTag(tagCompound);
@ -190,7 +200,7 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver
*/
public float removeEnergy(float amount)
{
Set inverse = SetUtil.inverse(structure.inventory);
List inverse = ListUtil.inverse(structure.inventory);
//go from bottom to top
return 0;
}
@ -292,7 +302,7 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver
{
if(!worldObj.isRemote)
{
return SetUtil.getTop(structure.inventory);
return ListUtil.getTop(structure.inventory);
}
else {
return structure.tempStack;
@ -351,7 +361,7 @@ public class TileEntityBattery extends TileEntityBase implements IPacketReceiver
{
if(!worldObj.isRemote)
{
structure.inventory.remove(SetUtil.getTop(structure.inventory));
structure.inventory.remove(ListUtil.getTop(structure.inventory));
}
else {
structure.tempStack = null;