All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.archive.util.CrossProduct Maven / Gradle / Ivy

There is a newer version: 1.1.9
Show newest version
package org.archive.util;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import java.util.Stack;

public class CrossProduct  {
	public List> crossProduct(List> listOfLists) {

		ArrayList> results = new ArrayList>();
		
		Stack current = new Stack();
		Deque> remainder = new ArrayDeque>(listOfLists); 
		recurse(remainder,current,results);
		return results;
	}
	private void recurse(Deque> remainder,
			Stack current, ArrayList> accumulation) {
		if(remainder.isEmpty()) {
			// all done:
			dump(new ArrayList(current));
			accumulation.add(new ArrayList(current));
			
		} else {
			List cur = remainder.removeFirst();
			for(T o : cur) {
				current.push(o);
				recurse(remainder,current,accumulation);
				current.pop();
			}
			remainder.addFirst(cur);
		}
	}
	private void dump(ArrayList a) {
		StringBuilder sb = new StringBuilder();
		boolean first = false;
		for(T o : a) {
			if(first) {
				first = false;
			} else {
				sb.append(",");
			}
			sb.append(o.toString());
		}
		System.out.println("CrossOutput:" + sb.toString());
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy