package org.psjava.example.algo;

import junit.framework.Assert;

import org.junit.Test;
import org.psjava.algo.sequence.search.BinarySearchFirst;
import org.psjava.algo.sequence.search.BinarySearchFirstInArray;
import org.psjava.ds.array.Array;
import org.psjava.ds.array.ArrayFromVarargs;
import org.psjava.ds.math.Function;
import org.psjava.ds.numbersystrem.IntegerNumberSystem;
import org.psjava.util.DefaultComparator;
import org.psjava.util.ReversedComparator;

/**
 * @implementation {@link BinarySearchFirst}
 *
 * @see {@link BinarySearchFirst}
 * @see {@link org.psjava.algo.sequence.search.BinarySearchFirstTrue}
 * @see {@link org.psjava.algo.sequence.search.BinarySearchFirstFalse}
 * @see {@link BinarySearchFirstInArray}
 * @see {@link org.psjava.algo.sequence.search.BinarySearchLast}
 * @see {@link org.psjava.algo.sequence.search.BinarySearchLastInArray}
 * @see {@link org.psjava.algo.sequence.search.BinarySearchLastTrue}
 * @see {@link org.psjava.algo.sequence.search.BinarySearchLastFalse}
 */
public class BinarySearchExample {

	@Test
	public void example() {

		// Let's search 5 in a increasingly sorted array.

		Array<Integer> inc = ArrayFromVarargs.create(1, 3, 5, 7, 9);
		int res1 = BinarySearchFirstInArray.search(inc, new DefaultComparator<Integer>(), 5, -1); // must be 2
		int res2 = BinarySearchFirstInArray.search(inc, new DefaultComparator<Integer>(), 6, -1); // must be -1, given by argument.
		Assert.assertEquals(2, res1);
		Assert.assertEquals(-1, res2);

		// Following is an example for decreasing array.
		// In this example, you can use a reversed comparator.

		Array<Integer> dec = ArrayFromVarargs.create(9, 7, 5, 3, 1);
		int res3 = BinarySearchFirstInArray.search(dec, ReversedComparator.wrap(new DefaultComparator<Integer>()), 3, -1);
		Assert.assertEquals(3, res3);

		// You don't have to prepare an array. Any function is enough.
		// Create a function 'y=8x', and let's search 'x' for 'y'=800.

		Function<Integer, Integer> f = new Function<Integer, Integer>() {
			@Override
			public Integer get(Integer x) {
				return x * 8;
			}
		};

		// x must be 100.
		int x = BinarySearchFirst.search(IntegerNumberSystem.getInstance(), f, new DefaultComparator<Integer>(), 0, 999999, 800, -1);
		Assert.assertEquals(100, x);
	}

}

@@ -533,52 +533,24 @@
 %0D%0A *
-%0D%0A * @see %7B@link BinarySearchFirst%7D%0D%0A * @see
+ @implementation
  %7B@l
@@ -605,35 +605,46 @@
 FirstTrue%7D%0D%0A * @
-see
+implementation
  %7B@link org.psja
@@ -688,35 +688,46 @@
 irstFalse%7D%0D%0A * @
-see
+implementation
  %7B@link BinarySe
@@ -741,35 +741,46 @@
 stInArray%7D%0D%0A * @
-see
+implementation
  %7B@link org.psja
@@ -822,27 +822,38 @@
 hLast%7D%0D%0A * @
-see
+implementation
  %7B@link org.
@@ -906,27 +906,38 @@
 Array%7D%0D%0A * @
-see
+implementation
  %7B@link org.
@@ -995,11 +995,22 @@
  * @
-see
+implementation
  %7B@l

fix wrong tag in example