4 Copyright (c) 2018, Mungo Carstairs
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
10 * Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
13 * Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
17 * Neither the name of the copyright holder nor the names of its
18 contributors may be used to endorse or promote products derived from
19 this software without specific prior written permission.
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 package intervalstore.impl;
34 import java.util.List;
35 import java.util.function.ToIntFunction;
37 import intervalstore.api.IntervalI;
40 * Provides a method to perform binary search of an ordered list for the first
41 * entry that satisfies a supplied condition
45 public final class BinarySearcher
48 public static ToIntFunction<IntervalI> fbegin = new ToIntFunction<IntervalI>()
52 public int applyAsInt(IntervalI value)
54 return value.getBegin();
59 public static ToIntFunction<IntervalI> fend = new ToIntFunction<IntervalI>()
63 public int applyAsInt(IntervalI value)
65 return value.getEnd();
70 private BinarySearcher()
75 * Performs a binary search of the list to find the index of the first entry
76 * for which the test returns true. Answers the length of the list if there is
79 * For correct behaviour, the provided list must be ordered consistent with
80 * the test, that is, any entries returning false must precede any entries
81 * returning true. Note that this means that this method is <em>not</em>
82 * usable to search for equality (to find a specific entry), as all unequal
83 * entries will answer false to the test. To do that, use
84 * <code>Collections.binarySearch</code> instead.
89 * @see java.util.Collections#binarySearch(List, Object)
91 public static <T> int findFirst(List<? extends T> list, int pos,
92 ToIntFunction<T> test)
95 int end = list.size() - 1;
96 int matched = list.size();
100 int mid = (start + end) / 2;
101 T entry = list.get(mid);
102 boolean itsTrue = test.applyAsInt(entry) >= pos;