6c598ceac51b52d036cdcdfd525260652f44139d
[jalview.git] / src / intervalstore / impl / BinarySearcher.java
1 /*
2 BSD 3-Clause License
3
4 Copyright (c) 2018, Mungo Carstairs
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice, this
11   list of conditions and the following disclaimer.
12
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.
16
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.
20
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.
31 */
32 package intervalstore.impl;
33
34 import java.util.List;
35 import java.util.function.ToIntFunction;
36
37 import intervalstore.api.IntervalI;
38
39 /**
40  * Provides a method to perform binary search of an ordered list for the first
41  * entry that satisfies a supplied condition
42  * 
43  * @author gmcarstairs
44  */
45 public final class BinarySearcher
46 {
47
48   public static ToIntFunction<IntervalI> fbegin = new ToIntFunction<IntervalI>()
49   {
50
51     @Override
52     public int applyAsInt(IntervalI value)
53     {
54       return value.getBegin();
55     }
56
57   };
58
59   public static ToIntFunction<IntervalI> fend = new ToIntFunction<IntervalI>()
60   {
61
62     @Override
63     public int applyAsInt(IntervalI value)
64     {
65       return value.getEnd();
66     }
67
68   };
69
70   private BinarySearcher()
71   {
72   }
73
74   /**
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
77    * no such entry.
78    * <p>
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.
85    * 
86    * @param list
87    * @param test
88    * @return
89    * @see java.util.Collections#binarySearch(List, Object)
90    */
91   public static <T> int findFirst(List<? extends T> list, int pos,
92           ToIntFunction<T> test)
93   {
94     int start = 0;
95     int end = list.size() - 1;
96     int matched = list.size();
97   
98     while (start <= end)
99     {
100       int mid = (start + end) / 2;
101       T entry = list.get(mid);
102       boolean itsTrue = test.applyAsInt(entry) >= pos;
103       if (itsTrue)
104       {
105         matched = mid;
106         end = mid - 1;
107       }
108       else
109       {
110         start = mid + 1;
111       }
112     }
113   
114     return matched;
115   }
116 }