
edu.jas.util.ListUtil Maven / Gradle / Ivy
The newest version!
/*
* $Id: ListUtil.java 4065 2012-07-27 15:17:38Z kredel $
*/
package edu.jas.util;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import edu.jas.structure.Element;
import edu.jas.structure.UnaryFunctor;
/**
* List utilities. For example map functor on list elements.
* @author Heinz Kredel
*/
public class ListUtil {
//private static final Logger logger = Logger.getLogger(ListUtil.class);
// private static boolean debug = logger.isDebugEnabled();
/**
* Map a unary function to the list.
* @param f evaluation functor.
* @return new list elements f(list(i)).
*/
public static , D extends Element> List map(List list, UnaryFunctor f) {
if (list == null) {
return null;
}
List nl;
if (list instanceof ArrayList) {
nl = new ArrayList(list.size());
} else if (list instanceof LinkedList) {
nl = new LinkedList();
} else {
throw new RuntimeException("list type not implemented");
}
for (C c : list) {
D n = f.eval(c);
nl.add(n);
}
return nl;
}
/**
* Tuple from lists.
* @param A list of lists.
* @return new list with tuples (a_1,...,an) with ai in Ai,
* i=0,...,length(A)-1.
*/
public static List> tupleFromList(List> A) {
if (A == null) {
return null;
}
List> T = new ArrayList>(A.size());
if (A.size() == 0) {
return T;
}
if (A.size() == 1) {
List Ap = A.get(0);
for (C a : Ap) {
List Tp = new ArrayList(1);
Tp.add(a);
T.add(Tp);
}
return T;
}
List> Ap = new ArrayList>(A);
List f = Ap.remove(0);
List> Tp = tupleFromList(Ap);
//System.out.println("Tp = " + Tp);
for (C a : f) {
for (List tp : Tp) {
List ts = new ArrayList();
ts.add(a);
ts.addAll(tp);
T.add(ts);
}
}
return T;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy