/*
 * Zorbage: an algebraic data hierarchy for use in numeric processing.
 *
 * Copyright (C) 2016-2020 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.algebra.G;
import nom.bdezonia.zorbage.algebra.RModuleMember;
import nom.bdezonia.zorbage.axis.CoordinateSpace;
import nom.bdezonia.zorbage.data.DimensionedDataSource;
import nom.bdezonia.zorbage.sampling.IntegerIndex;
import nom.bdezonia.zorbage.sampling.SamplingIterator;
import nom.bdezonia.zorbage.type.real.highprec.HighPrecisionMember;

/**
 * 
 * @author Barry DeZonia
 *
 */
public class ComputeRnBoundingBox {

	// do not instantiate
	
	private ComputeRnBoundingBox() { }
	
	/**
	 * 
	 * @param data
	 * @param min
	 * @param max
	 */
	public static
		void compute(DimensionedDataSource<?> data,
						RModuleMember<HighPrecisionMember> min,
						RModuleMember<HighPrecisionMember> max)
	{
		HighPrecisionMember mn = G.HP.construct();
		HighPrecisionMember mx = G.HP.construct();
		HighPrecisionMember tmp = G.HP.construct();
		int numD = data.numDimensions();
		min.alloc(numD);
		max.alloc(numD);
		CoordinateSpace cspace = data.getCoordinateSpace();
		IntegerIndex idx = new IntegerIndex(numD);
		SamplingIterator<IntegerIndex> iter = GridIterator.compute(data);
		while (iter.hasNext()) {
			iter.next(idx);
			for (int i = 0; i < numD; i++) {
				tmp.setV(cspace.toRn(idx, i));
				max.getV(i, mx);
				min.getV(i, mn);
				if (G.HP.isLess().call(mx,mn)) {
					max.setV(i, mx);
					min.setV(i, mn);
				}
				else {
					if (G.HP.isLess().call(tmp, mn)) {
						min.setV(i, tmp);
					}
					if (G.HP.isGreater().call(tmp, mx)) {
						max.setV(i, tmp);
					}
				}
			}
		}
	}
}

@@ -2142,16 +2142,17 @@
 ber%3E min
+s
 ,%0A%09%09%09%09%09%09
@@ -2189,16 +2189,18 @@
 ber%3E max
+es
 )%0A%09%7B%0A%09%09H
@@ -2230,32 +2230,35 @@
  G.HP.construct(
+%221%22
 );%0A%09%09HighPrecisi
@@ -2282,24 +2282,28 @@
 P.construct(
+%22-1%22
 );%0A%09%09HighPre
@@ -2379,16 +2379,17 @@
 );%0A%09%09min
+s
 .alloc(n
@@ -2399,16 +2399,18 @@
 );%0A%09%09max
+es
 .alloc(n
@@ -2415,16 +2415,98 @@
 (numD);%0A
+%09%09for (int i = 0; i %3C numD; i++) %7B%0A%09%09%09mins.setV(i, mn);%0A%09%09%09maxes.setV(i, mx);%0A%09%09%7D%0A
 %09%09Coordi
@@ -2788,16 +2788,18 @@
 %0A%09%09%09%09max
+es
 .getV(i,
@@ -2811,16 +2811,17 @@
 %0A%09%09%09%09min
+s
 .getV(i,
@@ -2856,16 +2856,17 @@
 call(mx,
+ 
 mn)) %7B%0A%09
@@ -2864,32 +2864,34 @@
  mn)) %7B%0A%09%09%09%09%09max
+es
 .setV(i, mx);%0A%09%09
@@ -2879,26 +2879,27 @@
 xes.setV(i, 
-mx
+tmp
 );%0A%09%09%09%09%09min.
@@ -2889,32 +2889,33 @@
 , tmp);%0A%09%09%09%09%09min
+s
 .setV(i, mn);%0A%09%09
@@ -2903,26 +2903,27 @@
 ins.setV(i, 
-mn
+tmp
 );%0A%09%09%09%09%7D%0A%09%09%09
@@ -2979,16 +2979,17 @@
 %09%09%09%09%09min
+s
 .setV(i,
@@ -3054,16 +3054,18 @@
 %09%09%09%09%09max
+es
 .setV(i,

fix a couple bugs