Searches.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.statistics.inference;

  18. import java.util.function.IntToDoubleFunction;

  19. /**
  20.  * Search utility methods.
  21.  *
  22.  * @since 1.1
  23.  */
  24. final class Searches {
  25.     /** Range threshold to use a binary search.
  26.      * The binary search takes O(log(n)) so is used when n is large and a sequential
  27.      * search is slower. */
  28.     private static final int BINARY_SEARCH = 8;

  29.     /** No instances. */
  30.     private Searches() {}

  31.     /**
  32.      * Conduct a search between {@code a} inclusive and {@code b} inclusive
  33.      * to find the lowest index where {@code value <= x}. The values must be
  34.      * in <em>descending</em> order. The method is functionally equivalent to:
  35.      * <pre>
  36.      * {@code
  37.      * i = b + 1
  38.      * while (i > a AND value(i - 1) <= x)
  39.      *    i = i - 1
  40.      * return i
  41.      * }</pre>
  42.      *
  43.      * <p>The function is only evaluated between the closed interval {@code [a, b]}.
  44.      * Special cases:
  45.      * <ul>
  46.      * <li>If {@code value(a) <= x} the returned index is {@code a}.
  47.      * <li>If {@code value(b) > x} the returned index is {@code b + 1}.
  48.      * </ul>
  49.      *
  50.      * @param a Lower limit (inclusive).
  51.      * @param b Upper limit (inclusive).
  52.      * @param x Target value.
  53.      * @param value Function to evaluate the value at an index.
  54.      * @return the minimum index where {@code value(i) <= x}.
  55.      */
  56.     static int searchDescending(int a, int b, double x, IntToDoubleFunction value) {
  57.         // Re-use the search for ascending order.
  58.         // Invert the index to find the lowest for the descending order.
  59.         final int offset = a + b;
  60.         return offset - searchAscending(a, b, x, i -> value.applyAsDouble(offset - i));
  61.     }

  62.     /**
  63.      * Conduct a search between {@code a} inclusive and {@code b} inclusive
  64.      * to find the highest index where {@code value <= x}. The values must be
  65.      * in <em>ascending</em> order. The method is functionally equivalent to:
  66.      * <pre>
  67.      * {@code
  68.      * i = a - 1
  69.      * while (i < b AND value(i + 1) <= x)
  70.      *    i = i + 1
  71.      * return i
  72.      * }</pre>
  73.      *
  74.      * <p>The function is only evaluated between the closed interval {@code [a, b]}.
  75.      * Special cases:
  76.      * <ul>
  77.      * <li>If {@code value(a) > x} the returned index is {@code a - 1}.
  78.      * <li>If {@code value(b) <= x} the returned index is {@code b}.
  79.      * </ul>
  80.      *
  81.      * @param a Lower limit (inclusive).
  82.      * @param b Upper limit (inclusive).
  83.      * @param x Target value.
  84.      * @param value Function to evaluate the value at an index.
  85.      * @return the maximum index where {@code value(i) <= x}.
  86.      */
  87.     static int searchAscending(int a, int b, double x, IntToDoubleFunction value) {
  88.         // Use a binary search for a large range.
  89.         if (b - a > BINARY_SEARCH) {
  90.             // Edge case as the search never evaluates the end points.
  91.             if (value.applyAsDouble(a) > x) {
  92.                 return a - 1;
  93.             }
  94.             if (value.applyAsDouble(b) <= x) {
  95.                 return b;
  96.             }

  97.             // value(lo) is always <= x
  98.             // value(hi) is always > x
  99.             int lo = a;
  100.             int hi = b;
  101.             while (lo + 1 < hi) {
  102.                 final int mid = (lo + hi) >>> 1;
  103.                 if (value.applyAsDouble(mid) <= x) {
  104.                     lo = mid;
  105.                 } else {
  106.                     hi = mid;
  107.                 }
  108.             }
  109.             return lo;
  110.         }

  111.         // Sequential search
  112.         int i = a - 1;
  113.         // Evaluate between [a, b]
  114.         while (i < b && value.applyAsDouble(i + 1) <= x) {
  115.             i++;
  116.         }
  117.         return i;
  118.     }
  119. }