2014-01-03 03:58:14 +01:00
|
|
|
package mekanism.api;
|
2013-11-16 16:21:37 +01:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2013-11-17 00:56:01 +01:00
|
|
|
public class ListUtils
|
2013-11-16 16:21:37 +01:00
|
|
|
{
|
|
|
|
public static <V> List<V> inverse(List<V> list)
|
|
|
|
{
|
|
|
|
List<V> toReturn = new ArrayList<V>();
|
|
|
|
|
2013-11-17 00:56:01 +01:00
|
|
|
for(int i = list.size() - 1; i >= 0; i--)
|
2013-11-16 16:21:37 +01:00
|
|
|
{
|
|
|
|
toReturn.add(list.get(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <V> List<V> cap(List<V> list, int cap)
|
|
|
|
{
|
|
|
|
List<V> toReturn = new ArrayList<V>();
|
|
|
|
|
2013-11-17 00:56:01 +01:00
|
|
|
if(list.size() <= cap)
|
2013-11-16 16:21:37 +01:00
|
|
|
{
|
|
|
|
toReturn = copy(list);
|
|
|
|
}
|
2013-11-17 00:56:01 +01:00
|
|
|
else {
|
2013-11-16 16:21:37 +01:00
|
|
|
int count = 0;
|
|
|
|
|
2013-11-17 00:56:01 +01:00
|
|
|
for(V obj : list)
|
2013-11-16 16:21:37 +01:00
|
|
|
{
|
|
|
|
count++;
|
|
|
|
|
|
|
|
toReturn.add(obj);
|
|
|
|
|
2013-11-17 00:56:01 +01:00
|
|
|
if(count == cap)
|
2013-11-16 16:21:37 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <V> List<V> copy(List<V> list)
|
|
|
|
{
|
|
|
|
List<V> toReturn = new ArrayList<V>();
|
|
|
|
|
2013-11-17 00:56:01 +01:00
|
|
|
for(V obj : list)
|
2013-11-16 16:21:37 +01:00
|
|
|
{
|
|
|
|
toReturn.add(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <V> List<V> merge(List<V> listOne, List<V> listTwo)
|
|
|
|
{
|
|
|
|
List<V> newList = new ArrayList<V>();
|
|
|
|
|
2013-11-17 00:56:01 +01:00
|
|
|
for(V obj : listOne)
|
2013-11-16 16:21:37 +01:00
|
|
|
{
|
|
|
|
newList.add(obj);
|
|
|
|
}
|
|
|
|
|
2013-11-17 00:56:01 +01:00
|
|
|
for(V obj : listTwo)
|
2013-11-16 16:21:37 +01:00
|
|
|
{
|
|
|
|
newList.add(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
return newList;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <V> List<V> capRemains(List<V> list, int cap)
|
|
|
|
{
|
|
|
|
List<V> toReturn = new ArrayList<V>();
|
|
|
|
|
2013-11-17 00:56:01 +01:00
|
|
|
if(list.size() <= cap)
|
2013-11-16 16:21:37 +01:00
|
|
|
{
|
|
|
|
return toReturn;
|
|
|
|
}
|
2013-11-17 00:56:01 +01:00
|
|
|
else {
|
2013-11-16 16:21:37 +01:00
|
|
|
List<V> inverse = inverse(list);
|
|
|
|
|
|
|
|
int iterNeeded = list.size() - cap;
|
|
|
|
int count = 0;
|
|
|
|
|
2013-11-17 00:56:01 +01:00
|
|
|
for(V obj : list)
|
2013-11-16 16:21:37 +01:00
|
|
|
{
|
|
|
|
count++;
|
|
|
|
|
|
|
|
toReturn.add(obj);
|
|
|
|
|
2013-11-17 00:56:01 +01:00
|
|
|
if(count == iterNeeded)
|
2013-11-16 16:21:37 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <V> ArrayList<List<V>> split(List<V> list, int divide)
|
|
|
|
{
|
|
|
|
int remain = list.size() % divide;
|
|
|
|
int size = (list.size() - remain) / divide;
|
|
|
|
|
|
|
|
ArrayList<List<V>> toReturn = new ArrayList<List<V>>();
|
|
|
|
|
2013-11-17 00:56:01 +01:00
|
|
|
for(int i = 0; i < divide; i++)
|
2013-11-16 16:21:37 +01:00
|
|
|
{
|
|
|
|
toReturn.add(i, new ArrayList<V>());
|
|
|
|
}
|
|
|
|
|
2013-11-17 00:56:01 +01:00
|
|
|
for(List<V> iterSet : toReturn)
|
2013-11-16 16:21:37 +01:00
|
|
|
{
|
|
|
|
List<V> removed = new ArrayList<V>();
|
|
|
|
|
|
|
|
int toAdd = size;
|
|
|
|
|
2013-11-17 00:56:01 +01:00
|
|
|
if(remain > 0)
|
2013-11-16 16:21:37 +01:00
|
|
|
{
|
|
|
|
remain--;
|
|
|
|
toAdd++;
|
|
|
|
}
|
|
|
|
|
2013-11-17 00:56:01 +01:00
|
|
|
for(V obj : list)
|
2013-11-16 16:21:37 +01:00
|
|
|
{
|
2013-11-17 00:56:01 +01:00
|
|
|
if(toAdd == 0)
|
2013-11-16 16:21:37 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
iterSet.add(obj);
|
|
|
|
removed.add(obj);
|
|
|
|
toAdd--;
|
|
|
|
}
|
|
|
|
|
2013-11-17 00:56:01 +01:00
|
|
|
for(V obj : removed)
|
2013-11-16 16:21:37 +01:00
|
|
|
{
|
|
|
|
list.remove(obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <V> V getTop(List<V> list)
|
|
|
|
{
|
2013-11-17 00:56:01 +01:00
|
|
|
for(V obj : list)
|
2013-11-16 16:21:37 +01:00
|
|
|
{
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <V> List<V> asList(Set<V> set)
|
|
|
|
{
|
2013-11-27 02:11:26 +01:00
|
|
|
return (List<V>)Arrays.asList(set.toArray());
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-11-27 02:11:26 +01:00
|
|
|
public static <V> List<V> asList(V... values)
|
|
|
|
{
|
|
|
|
return (List<V>)Arrays.asList(values);
|
2013-11-16 16:21:37 +01:00
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-16 21:00:57 +01:00
|
|
|
public static double[] splitDouble(int size, double num)
|
2013-12-16 17:16:10 +01:00
|
|
|
{
|
|
|
|
double[] split = new double[size];
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-16 17:16:10 +01:00
|
|
|
for(int i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
double remain = num%size;
|
|
|
|
double ret = (num-remain)/size;
|
|
|
|
ret += remain;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-16 17:16:10 +01:00
|
|
|
split[i] = ret;
|
|
|
|
num -= remain;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-16 17:16:10 +01:00
|
|
|
return split;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-16 23:54:27 +01:00
|
|
|
public static double[] percent(double[] values)
|
|
|
|
{
|
|
|
|
double[] ret = new double[values.length];
|
|
|
|
double total = 0;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-16 23:54:27 +01:00
|
|
|
for(double d : values) total += d;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-16 23:54:27 +01:00
|
|
|
for(int i = 0; i < values.length; i++)
|
|
|
|
{
|
|
|
|
ret[i] = values[i]/total;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-16 23:54:27 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-17 18:53:33 +01:00
|
|
|
public static int[] calcPercentInt(double[] percent, int val)
|
|
|
|
{
|
|
|
|
int[] ret = new int[percent.length];
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-17 18:53:33 +01:00
|
|
|
for(int i = 0; i < percent.length; i++)
|
|
|
|
{
|
|
|
|
ret[i] = (int)Math.round(val*percent[i]);
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-18 04:37:21 +01:00
|
|
|
int newTotal = 0;
|
|
|
|
for(int i : ret) newTotal += i;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-18 04:37:21 +01:00
|
|
|
int diff = val-newTotal;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-18 04:37:21 +01:00
|
|
|
if(diff != val)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < ret.length; i++)
|
|
|
|
{
|
|
|
|
int num = ret[i];
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-18 04:37:21 +01:00
|
|
|
if(diff < 0 && num == 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-18 04:37:21 +01:00
|
|
|
if(diff > 0)
|
|
|
|
{
|
|
|
|
ret[i]++;
|
|
|
|
diff--;
|
|
|
|
}
|
|
|
|
else if(diff < 0)
|
|
|
|
{
|
|
|
|
ret[i]--;
|
|
|
|
diff++;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-18 04:37:21 +01:00
|
|
|
if(diff == 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-17 18:53:33 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-16 21:00:57 +01:00
|
|
|
public static int[] splitInt(int size, int num)
|
|
|
|
{
|
|
|
|
int[] split = new int[size];
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-16 21:00:57 +01:00
|
|
|
for(int i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
int remain = num%size;
|
|
|
|
int ret = (num-remain)/size;
|
|
|
|
ret += remain;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-16 21:00:57 +01:00
|
|
|
split[i] = ret;
|
|
|
|
num -= remain;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-16 21:00:57 +01:00
|
|
|
return split;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-17 18:53:33 +01:00
|
|
|
public static double[] percent(int[] values)
|
|
|
|
{
|
|
|
|
double[] ret = new double[values.length];
|
|
|
|
double total = 0;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-17 18:53:33 +01:00
|
|
|
for(double d : values) total += d;
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-17 18:53:33 +01:00
|
|
|
for(int i = 0; i < values.length; i++)
|
|
|
|
{
|
|
|
|
ret[i] = values[i]/total;
|
|
|
|
}
|
2014-03-08 02:00:25 +01:00
|
|
|
|
2013-12-17 18:53:33 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2013-11-16 16:21:37 +01:00
|
|
|
}
|