package org.psjava.example;

import junit.framework.Assert;

import org.junit.Test;
import org.psjava.algo.sequence.search.BinarySearchFirst;
import org.psjava.algo.sequence.search.BinarySearchFirstFalse;
import org.psjava.algo.sequence.search.BinarySearchFirstInArray;
import org.psjava.algo.sequence.search.BinarySearchFirstTrue;
import org.psjava.algo.sequence.search.BinarySearchLast;
import org.psjava.algo.sequence.search.BinarySearchLastFalse;
import org.psjava.algo.sequence.search.BinarySearchLastInArray;
import org.psjava.algo.sequence.search.BinarySearchLastTrue;
import org.psjava.ds.array.MutableArray;
import org.psjava.ds.array.MutableArrayFromValues;
import org.psjava.ds.math.Function;
import org.psjava.ds.numbersystrem.IntegerNumberSystem;
import org.psjava.util.DefaultComparator;
import org.psjava.util.ReversedComparator;

public class BinarySearchExample {

	@Test
	public void example() {

		// Search a value 5 in increasing int array. result is the position = 2

		MutableArray<Integer> array1 = MutableArrayFromValues.create(1, 3, 5, 7, 9);
		int res1 = BinarySearchFirstInArray.search(array1, new DefaultComparator<Integer>(), 5, -1);

		Assert.assertEquals(2, res1);


		// Following is an example for decresing array.
		// You can use the reversed comparator.

		MutableArray<Integer> array2 = MutableArrayFromValues.create(9, 7, 5, 3, 1);
		int res2 = BinarySearchFirstInArray.search(array2, ReversedComparator.wrap(new DefaultComparator<Integer>()), 3, -1);

		Assert.assertEquals(3, res2);


		// You don't have to prepare an array. Any function is enough.
		
		int res3 = BinarySearchFirst.search(IntegerNumberSystem.getInstance(), new Function<Integer, Integer>() {
			@Override
			public Integer get(Integer input) {
				return input * 8;
			}
		}, new DefaultComparator<Integer>(), 100, 200, 888, -1);

		Assert.assertEquals(111, res3);


		// And, there are many alternatives interfaces. check them

		BinarySearchFirst.class.getClass();
		BinarySearchFirstTrue.class.getClass();
		BinarySearchFirstFalse.class.getClass();
		BinarySearchFirstInArray.class.getClass();
		BinarySearchLast.class.getClass();
		BinarySearchLastInArray.class.getClass();
		BinarySearchLastTrue.class.getClass();
		BinarySearchLastFalse.class.getClass();
	}

}

@@ -990,16 +990,27 @@
 sult is 
+2. that is 
 the posi
@@ -1018,11 +1018,17 @@
 ion 
-= 2
+in array.
 %0D%0A%0D%0A
@@ -1298,17 +1298,31 @@
 .%0D%0A%09%09// 
-Y
+In this case, y
 ou can u
@@ -1324,19 +1324,17 @@
 can use 
-the
+a
  reverse

improve comment