コード例 #1
0
  /**
   * Performs a binary search on the dataSet
   *
   * @param element is the value in to be searched for in dataSet
   * @return the index of dataSet that contains the element, or -1 if element does not exist in the
   *     dataSet.
   */
  public int binarySearch(E element) {
    if (element == null) return -1;

    Comparator<? super E> binaryComparator = comparator();

    if (binaryComparator == null)
      binaryComparator = (Comparator<? super E>) Comparator.naturalOrder();

    int lowerBound = 0;
    int upperBound = size - 1;
    int index = (lowerBound + upperBound) / 2;
    while (lowerBound <= upperBound) {

      int comparisonInt = binaryComparator.compare(element, dataSet[index]);
      if (comparisonInt == 0) {
        return index;
      } else if (comparisonInt > 0) {
        lowerBound = index + 1;
        index = (lowerBound + upperBound) / 2;
      } else {
        upperBound = index - 1;
        index = (lowerBound + upperBound) / 2;
      }
    }
    return -1;
  }
コード例 #2
0
  /**
   * Compares the two elements and returns an integer for ordering.
   *
   * @param lhs left hand element to compare to the right hand
   * @param rhs right hand element to be compared against
   * @return positive integer if the lhs is greater than the right hand side, 0 if equal, and
   *     negative if it is less than the rhs
   */
  private int elementCompare(E lhs, E rhs) {
    if (rhs == null) return -1;
    if (lhs == null) return 1;

    Comparator<? super E> binaryComparator = comparator();

    if (binaryComparator == null)
      binaryComparator = (Comparator<? super E>) Comparator.naturalOrder();

    return binaryComparator.compare(lhs, rhs);
  }
コード例 #3
0
  @Test
  public void testSorted() {
    final Random r = new Random(0);
    final Integer[] test = new Integer[1024];
    final Integer[] expected;

    for (int i = 0; i < test.length; i++) {
      test[i] = r.nextInt(100);
    }

    expected = Arrays.copyOf(test, test.length);

    sort.sort(test, Comparator.naturalOrder());
    Arrays.sort(test);

    assertArrayEquals(expected, test);
  }
コード例 #4
0
ファイル: ShellSort.java プロジェクト: joshimoo/JLib
 /**
  * ShellSort runs multiple InsertionSorts with a custom gap between elements With a a final pass
  * with a gap size of 1 This takes advantage of InsertionSorts adaptive nature on partial sorted
  * data
  *
  * @param gaps must be descendingly sorted and contain the element 1
  */
 public static <T extends Comparable<? super T>> void sort(T[] data, Integer[] gaps) {
   sort(data, gaps, Comparator.<T>naturalOrder());
 }
コード例 #5
0
ファイル: ShellSort.java プロジェクト: joshimoo/JLib
 /**
  * ShellSort runs multiple InsertionSorts with a custom gap between elements With a a final pass
  * with a gap size of 1 This takes advantage of InsertionSorts adaptive nature on partial sorted
  * data Marcin Ciura's gap sequence is used by default
  */
 public static <T extends Comparable<? super T>> void sort(T[] data) {
   sort(data, defaultGapSequence, Comparator.<T>naturalOrder());
 }
コード例 #6
0
  @Test
  public void testMaxAll() {
    List<String> input = Arrays.asList("a", "bb", "c", "", "cc", "eee", "bb", "ddd");
    checkCollector(
        "maxAll",
        Arrays.asList("eee", "ddd"),
        input::stream,
        MoreCollectors.maxAll(Comparator.comparingInt(String::length)));
    Collector<String, ?, String> maxAllJoin =
        MoreCollectors.maxAll(Comparator.comparingInt(String::length), Collectors.joining(","));
    checkCollector("maxAllJoin", "eee,ddd", input::stream, maxAllJoin);
    checkCollector(
        "minAll",
        1L,
        input::stream,
        MoreCollectors.minAll(Comparator.comparingInt(String::length), Collectors.counting()));
    checkCollector(
        "minAllEmpty",
        Arrays.asList(""),
        input::stream,
        MoreCollectors.minAll(Comparator.comparingInt(String::length)));
    checkCollectorEmpty(
        "maxAll",
        Collections.emptyList(),
        MoreCollectors.maxAll(Comparator.comparingInt(String::length)));
    checkCollectorEmpty("maxAllJoin", "", maxAllJoin);

    List<Integer> ints = IntStreamEx.of(new Random(1), 10000, 1, 1000).boxed().toList();
    List<Integer> expectedMax = getMaxAll(ints, Comparator.naturalOrder());
    List<Integer> expectedMin = getMaxAll(ints, Comparator.reverseOrder());
    Collector<Integer, ?, SimpleEntry<Integer, Long>> downstream =
        MoreCollectors.pairing(
            MoreCollectors.first(),
            Collectors.counting(),
            (opt, cnt) -> new AbstractMap.SimpleEntry<>(opt.get(), cnt));

    checkCollector("maxAll", expectedMax, ints::stream, MoreCollectors.maxAll(Integer::compare));
    checkCollector("minAll", expectedMin, ints::stream, MoreCollectors.minAll());
    checkCollector(
        "entry",
        new SimpleEntry<>(expectedMax.get(0), (long) expectedMax.size()),
        ints::stream,
        MoreCollectors.maxAll(downstream));
    checkCollector(
        "entry",
        new SimpleEntry<>(expectedMin.get(0), (long) expectedMin.size()),
        ints::stream,
        MoreCollectors.minAll(downstream));

    Integer a = new Integer(1), b = new Integer(1), c = new Integer(1000), d = new Integer(1000);
    ints = IntStreamEx.range(10, 100).boxed().append(a, c).prepend(b, d).toList();
    for (StreamExSupplier<Integer> supplier : streamEx(ints::stream)) {
      List<Integer> list = supplier.get().collect(MoreCollectors.maxAll());
      assertEquals(2, list.size());
      assertSame(d, list.get(0));
      assertSame(c, list.get(1));

      list = supplier.get().collect(MoreCollectors.minAll());
      assertEquals(2, list.size());
      assertSame(b, list.get(0));
      assertSame(a, list.get(1));
    }
  }
コード例 #7
0
 public BinarySearchSet() {
   wayToCompare = (Comparator<? super E>) Comparator.naturalOrder();
   data = (E[]) new Object[8]; // Creates the array of size 8 as initial size
 }