Example #1
0
  /**
   * Searches the specified region of chars for the specified value using the binary search
   * algorithm. The region <strong>must</strong> be sorted (as by the {@link #sort} method) prior to
   * making this call. If it is not sorted, the results are undefined. If the region contains
   * multiple elements with the specified value, there is no guarantee which one will be found.
   *
   * @param a the region to be searched.
   * @param key the value to be searched for.
   * @return index of the search key, if it is contained in the region; otherwise,
   *     <tt>(-(<i>insertion point</i>) - 1)</tt>. The <i>insertion point</i> is defined as the
   *     point at which the key would be inserted into the region: the index of the first element
   *     greater than the key, or <tt>region.length()</tt>, if all elements in the region are less
   *     than the specified key. Note that this guarantees that the return value will be &gt;= 0 if
   *     and only if the key is found.
   * @see #sort(CharRegion)
   */
  public static int binarySearch(CharRegion a, char key) {
    char[] arr = a.baseArray();
    int low = a.offset();
    int high = a.length() - 1;

    while (low <= high) {
      int mid = (low + high) / 2;
      char midVal = arr[mid];

      if (midVal < key) low = mid + 1;
      else if (midVal > key) high = mid - 1;
      else return mid; // key found
    }
    return -(low + 1); // key not found.
  }
Example #2
0
  /**
   * Returns <tt>true</tt> if the two specified regions of chars are <i>equal</i> to one another.
   * Two regions are considered equal if both regions contain the same number of elements, and all
   * corresponding pairs of elements in the two regions are equal. In other words, two regions are
   * equal if they contain the same elements in the same order. Also, two region references are
   * considered equal if both are <tt>null</tt>.
   *
   * <p>
   *
   * @param a one region to be tested for equality.
   * @param a2 the other region to be tested for equality.
   * @return <tt>true</tt> if the two regions are equal.
   */
  public static boolean equals(CharRegion a, CharRegion a2) {
    if (a == a2) return true;
    if (a == null || a2 == null) return false;
    char[] arr1 = a.baseArray();
    char[] arr2 = a2.baseArray();
    if (arr1 == arr2) return true;

    int length = a.length();
    if (a2.length() != length) return false;

    for (int i = a.offset(), j = a2.offset(); length-- > 0; i++, j++)
      if (arr1[i] != arr2[i]) return false;

    return true;
  }
Example #3
0
 /**
  * Sorts the specified region of chars into ascending numerical order, using {@link
  * Arrays#sort(char[], int, int)} method.
  *
  * @param a the region to be sorted.
  */
 public static void sort(CharRegion a) {
   Arrays.sort(a.baseArray(), a.startIdx(), a.endIdx());
 }
Example #4
0
 /**
  * Fills the specified region of chars with a specific value, using {@link Arrays#fill(char[],
  * int, int, char)} method.
  *
  * @param a the region to be filled.
  */
 public static void fill(CharRegion a, char value) {
   Arrays.fill(a.baseArray(), a.startIdx(), a.endIdx(), value);
 }