package org.paxml.util;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class TraversalUtils {
	
	public static <K, V> List<Map<K, V>> combination(Map<K, List<V>> map) {
		List<K> keys = new ArrayList<K>(map.size());
		List<List<V>> grid = new ArrayList<List<V>>(map.size());
		for (Map.Entry entry : map.entrySet()) {
			keys.add((K) entry.getKey());
			List<V> row = (List<V>) entry.getValue();
			if (row == null) {
				row = new ArrayList<V>();
				row.add(null);
			}
			grid.add(row);
		}
		return combine(keys.size()-1, keys, grid);
	}

	private static <K, V> List<Map<K, V>> combine(int rowIndex, List<K> keys, List<List<V>> grid) {

		List<V> row = grid.get(rowIndex);
		K key = keys.get(rowIndex);
		
		List<Map<K, V>> result = new ArrayList();
		if (rowIndex <= 0) {
			// reached the 1st row, create new maps
			for (V val : row) {
				Map<K, V> map = new LinkedHashMap();
				map.put(key, val);
				result.add(map);
			}

		} else {
			// not to the 1st row yet
			for (V val : row) {
				List<Map<K, V>> created = combine(rowIndex - 1, keys, grid);
				for (Map<K, V> map : created) {
					map.put(key, val);
				}
				result.addAll(created);
			}

		}
		return result;
	}
}

@@ -155,17 +155,500 @@
 tils %7B%0A%09
+/**%0A%09 * Make a list of maps out of a map of lists. The given map is treated as a%0A%09 * grid, where the row index is the map key. The combination algorithm is to%0A%09 * grab an item from each row of the grid to make a map, where each result%0A%09 * map is contains that chosen item associated with the key. For instance,%0A%09 * map: %7Ba:%5B1,2%5D,b:%5B3,4%5D%7D becomes list%0A%09 * %5B%7Ba:1,b:3%7D,%7Ba:1,b:4%7D,%7Ba:2,b:3%7D,%7Ba:2,b:4%7D%5D.%0A%09 * %0A%09 * @param map%0A%09 *            the map of lists%0A%09 * @return the list of maps%0A%09 */
 %0A
-
 %09public 
@@ -1065,17 +1065,19 @@
 s.size()
--
+ - 
 1, keys,
@@ -1252,18 +1252,16 @@
 Index);%0A
-%09%09
 %0A%09%09List%3C

add java doc