package mekanism.api.util; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Set; public class ListUtils { public static List inverse(List list) { List toReturn = new ArrayList(); for(int i = list.size() - 1; i >= 0; i--) { toReturn.add(list.get(i)); } return toReturn; } public static List cap(List list, int cap) { List toReturn = new ArrayList(); 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 List copy(List list) { List toReturn = new ArrayList(); for(V obj : list) { toReturn.add(obj); } return toReturn; } public static List merge(List listOne, List listTwo) { List newList = new ArrayList(); for(V obj : listOne) { newList.add(obj); } for(V obj : listTwo) { newList.add(obj); } return newList; } public static List capRemains(List list, int cap) { List toReturn = new ArrayList(); if(list.size() <= cap) { return toReturn; } else { List 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 ArrayList> split(List list, int divide) { int remain = list.size() % divide; int size = (list.size() - remain) / divide; ArrayList> toReturn = new ArrayList>(); for(int i = 0; i < divide; i++) { toReturn.add(i, new ArrayList()); } for(List iterSet : toReturn) { List removed = new ArrayList(); 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 getTop(List list) { for(V obj : list) { return obj; } return null; } public static List asList(Set set) { return (List)Arrays.asList(set.toArray()); } public static List asList(V... values) { return (List)Arrays.asList(values); } public static double[] splitDouble(int size, double num) { double[] split = new double[size]; for(int i = 0; i < size; i++) { double remain = num%size; double ret = (num-remain)/size; ret += remain; split[i] = ret; num -= remain; } return split; } public static double[] percent(double[] values) { double[] ret = new double[values.length]; double total = 0; for(double d : values) total += d; for(int i = 0; i < values.length; i++) { ret[i] = values[i]/total; } return ret; } public static int[] calcPercentInt(double[] percent, int val) { int[] ret = new int[percent.length]; for(int i = 0; i < percent.length; i++) { ret[i] = (int)Math.round(val*percent[i]); } int newTotal = 0; for(int i : ret) newTotal += i; int diff = val-newTotal; if(diff != val) { for(int i = 0; i < ret.length; i++) { int num = ret[i]; if(diff < 0 && num == 0) { continue; } if(diff > 0) { ret[i]++; diff--; } else if(diff < 0) { ret[i]--; diff++; } if(diff == 0) { return ret; } } } return ret; } public static int[] splitInt(int size, int num) { int[] split = new int[size]; for(int i = 0; i < size; i++) { int remain = num%size; int ret = (num-remain)/size; ret += remain; split[i] = ret; num -= remain; } return split; } public static double[] percent(int[] values) { double[] ret = new double[values.length]; double total = 0; for(double d : values) total += d; for(int i = 0; i < values.length; i++) { ret[i] = values[i]/total; } return ret; } }