package org.psjava.algo;

import org.psjava.ds.array.AddToLastAll;
import org.psjava.ds.array.Array;
import org.psjava.ds.array.DynamicArray;
import org.psjava.ds.array.MutableArray;
import org.psjava.ds.array.MutableArrayFromIterable;
import org.psjava.goods.GoodSortingAlgorithm;
import org.psjava.util.IterableUsingStatusUpdater;
import org.psjava.util.Updater;

import java.util.Comparator;


public class PermutationWithRepetitionIterable {

	// SRM464-1-250 is a good problem to solve using this.

	public static <T> Iterable<Array<T>> create(Iterable<T> items, final Comparator<T> comparator) {
		MutableArray<T> initial = MutableArrayFromIterable.create(items);
		GoodSortingAlgorithm.getInstance().sort(initial, comparator);
		return IterableUsingStatusUpdater.create(initial, new Updater<Array<T>>() {
			@Override
			public Array<T> getUpdatedOrNull(Array<T> current) {
				DynamicArray<T> next = DynamicArray.create();
				AddToLastAll.add(next, current);
				if (!NextPermutation.step(next, comparator))
					return null;
				return next;
			}
		});
	}

}

@@ -50,90 +50,8 @@
 ray.
-AddToLastAll;%0Aimport org.psjava.ds.array.Array;%0Aimport org.psjava.ds.array.Dynamic
 Arra
@@ -796,23 +796,23 @@
 ) %7B%0A%09%09%09%09
-Dynamic
+Mutable
 Array%3CT%3E
@@ -823,77 +823,72 @@
 t = 
-DynamicArray.create();%0A%09%09%09%09AddToLastAll.add(next, current);%0A%09%09%09%09if (!
+MutableArrayFromIterable.create(current);%0A%09%09%09%09boolean success = 
 Next
@@ -925,43 +925,42 @@
 tor)
-)
+;
 %0A%09%09%09%09
-%09
 return 
-null;%0A%09%09%09%09return next
+success ? next : null
 ;%0A%09%09

improve performance - PermutationWithRepetitionIterable