package org.psjava.dp;

import org.psjava.ds.map.MutableMap;
import org.psjava.ds.map.MutableMapFactory;
import org.psjava.ds.set.MutableSet;
import org.psjava.ds.set.MutableSetFactory;

public class MemoizationFactory {

	private MutableMapFactory mapFactory;
	private MutableSetFactory setFactory;

	public MemoizationFactory(MutableMapFactory mapFactory, MutableSetFactory setFactory) {
		this.mapFactory = mapFactory;
		this.setFactory = setFactory;
	}

	public <I, V> Memoization<I, V> create(final MemoizationFunction<I, V> f) {
		final MutableMap<I, V> table = mapFactory.create();
		final MutableSet<I> inStack = setFactory.create();
		return new Memoization<I, V>() {
			@Override
			public V get(I input) {
				if (inStack.contains(input))
					throw new IllegalArgumentException("The function calls infinite recursion. check the logic.");
				inStack.insert(input);
				V v = table.get(input, null);
				if (v == null) {
					v = f.get(input, this);
					table.put(input, v);
				}
				inStack.remove(input);
				return v;
			}
		};
	}

}

@@ -463,17 +463,17 @@
 lic %3CI, 
-V
+O
 %3E Memoiz
@@ -481,17 +481,17 @@
 tion%3CI, 
-V
+O
 %3E create
@@ -520,17 +520,17 @@
 tion%3CI, 
-V
+O
 %3E f) %7B%0A%09
@@ -550,17 +550,17 @@
 eMap%3CI, 
-V
+O
 %3E table 
@@ -663,17 +663,17 @@
 tion%3CI, 
-V
+O
 %3E() %7B%0A%09%09
@@ -693,17 +693,17 @@
 %09public 
-V
+O
  get(I i
@@ -878,9 +878,9 @@
 %09%09%09%09
-V
+O
  v =

refactor. rename