/*
 * Zorbage: an algebraic data hierarchy for use in numeric processing.
 *
 * Copyright (C) 2016-2019 Barry DeZonia
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package nom.bdezonia.zorbage.algorithm;

import nom.bdezonia.zorbage.function.Function2;
import nom.bdezonia.zorbage.procedure.Procedure2;
import nom.bdezonia.zorbage.type.algebra.Algebra;

/**
 * 
 * @author Barry DeZonia
 *
 */
public class FindFixedPoint<T extends Algebra<T,U>, U>
	implements Function2<Long,U,U>
{
	private final Algebra<T,U> alg;
	private final Procedure2<U,U> proc;
	private final Function2<Boolean,U,U> closeEnough;
	private long maxIters;
	
	/**
	 * 
	 * @param alg
	 * @param proc
	 * @param closeEnough
	 * @param maxIters
	 */
	public FindFixedPoint(T alg, Procedure2<U,U> proc, Function2<Boolean,U,U> closeEnough, long maxIters) {
		this.alg = alg;
		this.proc = proc;
		this.closeEnough = closeEnough;
		this.maxIters = maxIters;
		if (maxIters < 1)
			throw new IllegalArgumentException("maxIters must be > 0");
	}
	
	public long getMaxIters() {
		return maxIters;
	}
	
	public void setMaxIters(long maxIters) {
		if (maxIters < 1)
			throw new IllegalArgumentException("maxIters must be > 0");
		this.maxIters = maxIters;
	}

	@Override
	public Long call(U firstGuess, U result)
	{
		U guess = alg.construct(firstGuess);
		U tmp = alg.construct();
		return iterate(guess, tmp, maxIters, result);
	}
	
	private long iterate(U guess, U tmp, long iters, U result)
	{
		if (iters == 0)
			return -1;
		proc.call(guess, tmp);
		if (closeEnough.call(guess, tmp)) {
			alg.assign().call(guess, result);
			return iters;
		}
		else {
			alg.assign().call(tmp, guess);
			return iterate(guess, tmp, iters-1, result);
		}			
	}
}

@@ -2726,38 +2726,44 @@
 %7B%0A%09%09
-if (iters == 0)%0A%09%09%09return -1;%0A
+for (long i = 0; i %3C iters; i++) %7B%0A%09
 %09%09pr
@@ -2783,16 +2783,17 @@
 , tmp);%0A
+%09
 %09%09if (cl
@@ -2821,24 +2821,25 @@
  tmp)) %7B%0A%09%09%09
+%09
 alg.assign()
@@ -2863,24 +2863,25 @@
 lt);%0A%09%09%09
+%09
 return i
 ters;%0A%09%09
@@ -2876,18 +2876,16 @@
 rn i
-ters
 ;%0A%09%09
+%09
 %7D%0A
+%09
 %09%09el
@@ -2889,16 +2889,17 @@
 %09else %7B%0A
+%09
 %09%09%09alg.a
@@ -2931,59 +2931,29 @@
 %0A%09%09%09
-return iterate(guess, tmp, iters-1, result);%0A%09%09%7D%09%09%09
+%7D%09%09%09%0A%09%09%7D%0A%09%09return -1;
 %0A%09%7D%0A

Improve FindFixedPoint's iteration code: do not blow the stack