package com.bbn.bue.common.math;

import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Ordering;

import static com.google.common.base.Preconditions.checkArgument;

public final class MathUtils {

  private MathUtils() {
    throw new UnsupportedOperationException();
  }

  public static int max(int[] arr) {
    checkArgument(arr.length > 0);
    int mx = Integer.MIN_VALUE;

    for (final int x : arr) {
      if (x > mx) {
        mx = x;
      }
    }
    return mx;
  }

  public static int sum(int[] permutation) {
    int ret = 0;
    for (final int x : permutation) {
      ret += x;
    }
    return ret;
  }

  public static Optional<Double> medianOfIntegers(Iterable<Integer> sizes) {
    final ImmutableList<Integer> sorted = Ordering.natural().immutableSortedCopy(sizes);
    if(sorted.size() == 0) {
      return Optional.absent();
    }
    if (sorted.size() % 2 == 0) {
      return Optional.of(0.5 * (sorted.get(sorted.size() / 2) + sorted.get(sorted.size() / 2 - 1)));
    } else {
      return Optional.of((double) sorted.get(sorted.size() / 2));
    }
  }

  public static Optional<Double> medianOfDoubles(Iterable<Double> sizes) {
    final ImmutableList<Double> sorted = Ordering.natural().immutableSortedCopy(sizes);
    if(sorted.size() == 0) {
      return Optional.absent();
    }
    if (sorted.size() % 2 == 0) {
      return Optional.of(0.5 * (sorted.get(sorted.size() / 2) + sorted.get(sorted.size() / 2 - 1)));
    } else {
      return Optional.of(sorted.get(sorted.size() / 2));
    }
  }


  public static double xLogX(double d) {
    if (d == 0.0) {
      return 0.0;
    } else {
      return d * Math.log(d);
    }
  }
}

@@ -25,24 +25,65 @@
 on.math;%0D%0A%0D%0A
+import com.google.common.base.Function;%0D%0A
 import com.g
@@ -1685,16 +1685,321 @@
 %0A  %7D%0D%0A%0D%0A
+  public static Function%3CIterable%3CDouble%3E, Optional%3CDouble%3E%3E medianOfDoublesFunction() %7B%0D%0A    return new Function%3CIterable%3CDouble%3E, Optional%3CDouble%3E%3E() %7B%0D%0A      @Override%0D%0A      public Optional%3CDouble%3E apply(final Iterable%3CDouble%3E input) %7B%0D%0A        return medianOfDoubles(input);%0D%0A      %7D%0D%0A    %7D;%0D%0A  %7D%0D%0A%0D%0A
 %0D%0A  publ

add medianOfDoublesFunction()