Skip to content

Commit

Permalink
small edits
Browse files Browse the repository at this point in the history
  • Loading branch information
akiezun committed Jun 10, 2015
1 parent e5460eb commit cd4db74
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 77 deletions.
23 changes: 13 additions & 10 deletions src/main/java/org/broadinstitute/hellbender/utils/MathUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,20 @@ private static final class JacobianLogTable {
public static final double MAX_TOLERANCE = 8.0;

public static double get(final double difference) {
if (cache == null)
if (cache == null) {
initialize();
}
final int index = fastRound(difference * INV_STEP);
return cache[index];
}

private static synchronized void initialize() {
private static void initialize() {
if (cache == null) {
final int tableSize = (int) (MAX_TOLERANCE / TABLE_STEP) + 1;
cache = new double[tableSize];
for (int k = 0; k < cache.length; k++)
for (int k = 0; k < cache.length; k++) {
cache[k] = Math.log10(1.0 + Math.pow(10.0, -((double) k) * TABLE_STEP));
}
}
}

Expand All @@ -117,8 +119,9 @@ public static double approximateLog10SumLog10(final double[] vals, final int end
double approxSum = vals[maxElementIndex];

for (int i = 0; i < endIndex; i++) {
if (i == maxElementIndex || vals[i] == Double.NEGATIVE_INFINITY)
if (i == maxElementIndex || vals[i] == Double.NEGATIVE_INFINITY) {
continue;
}

final double diff = approxSum - vals[i];
if (diff < JacobianLogTable.MAX_TOLERANCE) {
Expand All @@ -134,20 +137,20 @@ public static double approximateLog10SumLog10(final double a, final double b, fi
return approximateLog10SumLog10(a, approximateLog10SumLog10(b, c));
}

public static double approximateLog10SumLog10(double small, double big) {
public static double approximateLog10SumLog10(final double small, final double big) {
// make sure small is really the smaller value
if (small > big) {
final double t = big;
big = small;
small = t;
return approximateLog10SumLog10(big, small);
}

if (small == Double.NEGATIVE_INFINITY || big == Double.NEGATIVE_INFINITY)
if (small == Double.NEGATIVE_INFINITY || big == Double.NEGATIVE_INFINITY) {
return big;
}

final double diff = big - small;
if (diff >= JacobianLogTable.MAX_TOLERANCE)
if (diff >= JacobianLogTable.MAX_TOLERANCE) {
return big;
}

// OK, so |y-x| < tol: we use the following identity then:
// we need to compute log10(10^x + 10^y)
Expand Down
38 changes: 21 additions & 17 deletions src/main/java/org/broadinstitute/hellbender/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,18 +270,18 @@ public static <T> List<T> append(final List<T> left, T ... elts) {
* @param nCopies how many copies?
* @return a string
*/
public static String dupString(final String s, int nCopies) {
public static String dupString(final String s, final int nCopies) {
return Strings.repeat(s, nCopies);
}

public static String dupString(char c, int nCopies) {
char[] chars = new char[nCopies];
public static String dupString(final char c, final int nCopies) {
final char[] chars = new char[nCopies];
Arrays.fill(chars, c);
return new String(chars);
}

public static byte[] dupBytes(byte b, int nCopies) {
byte[] bytes = new byte[nCopies];
public static byte[] dupBytes(final byte b, final int nCopies) {
final byte[] bytes = new byte[nCopies];
Arrays.fill(bytes, b);
return bytes;
}
Expand All @@ -292,7 +292,7 @@ public static byte[] dupBytes(byte b, int nCopies) {
* @param args Arguments to parse.
* @return Parsed expressions.
*/
public static String[] escapeExpressions(String args) {
public static String[] escapeExpressions(final String args) {
// special case for ' and " so we can allow expressions
if (args.indexOf('\'') != -1)
return escapeExpressions(args, "'");
Expand All @@ -308,20 +308,23 @@ else if (args.indexOf('\"') != -1)
* @param delimiter Delimiter for grouping expressions.
* @return Parsed expressions.
*/
private static String[] escapeExpressions(String args, String delimiter) {
private static String[] escapeExpressions(final String args, final String delimiter) {
String[] command = {};
String[] split = args.split(delimiter);
final String[] split = args.split(delimiter);
String arg;
for (int i = 0; i < split.length - 1; i += 2) {
arg = split[i].trim();
if (arg.length() > 0) // if the unescaped arg has a size
if (arg.length() > 0) { // if the unescaped arg has a size
command = Utils.concatArrays(command, arg.split(" +"));
}
command = Utils.concatArrays(command, new String[]{split[i + 1]});
}
arg = split[split.length - 1].trim();
if (split.length % 2 == 1) // if the command ends with a delimiter
if (arg.length() > 0) // if the last unescaped arg has a size
if (split.length % 2 == 1) { // if the command ends with a delimiter
if (arg.length() > 0) { // if the last unescaped arg has a size
command = Utils.concatArrays(command, arg.split(" +"));
}
}
return command;
}

Expand All @@ -331,8 +334,8 @@ private static String[] escapeExpressions(String args, String delimiter) {
* @param B Second array.
* @return Concatenation of A then B.
*/
public static String[] concatArrays(String[] A, String[] B) {
String[] C = new String[A.length + B.length];
public static String[] concatArrays(final String[] A, final String[] B) {
final String[] C = new String[A.length + B.length];
System.arraycopy(A, 0, C, 0, A.length);
System.arraycopy(B, 0, C, A.length, B.length);
return C;
Expand All @@ -345,14 +348,14 @@ static public <T> List<T> reverse(final List<T> l) {
return newL;
}

public static byte [] arrayFromArrayWithLength(byte[] array, int length) {
byte [] output = new byte[length];
public static byte [] arrayFromArrayWithLength(final byte[] array, final int length) {
final byte [] output = new byte[length];
for (int j = 0; j < length; j++)
output[j] = array[(j % array.length)];
return output;
}

public static void fillArrayWithByte(byte[] array, byte value) {
public static void fillArrayWithByte(final byte[] array, final byte value) {
for (int i=0; i<array.length; i++) {
array[i] = value;
}
Expand All @@ -374,8 +377,9 @@ public static <T> List<List<T>> makePermutations(final List<T> objects, final in
final List<List<T>> combinations = new ArrayList<>();

if ( n == 1 ) {
for ( final T o : objects )
for ( final T o : objects ) {
combinations.add(Collections.singletonList(o));
}
} else if (n > 1) {
final List<List<T>> sub = makePermutations(objects, n - 1, withReplacement);
for ( List<T> subI : sub ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,21 @@ public IndexedSet(final int initialCapacity) {
* if {@code values} array is {@code null} itself, or it contains {@code null}.
*/
public IndexedSet(final Collection<E> values) {
if (values == null)
if (values == null) {
throw new IllegalArgumentException("input values cannot be null");
}

final int initialCapacity = values.size();
elements = new ArrayList<>(initialCapacity);
indexByElement = new HashMap<>(initialCapacity);
int nextIndex = 0;
for (final E value : values) {
if (value == null)
if (value == null) {
throw new IllegalArgumentException("null element not allowed: index == " + nextIndex);
if (indexByElement.containsKey(value))
}
if (indexByElement.containsKey(value)) {
continue;
}
indexByElement.put(value, nextIndex++);
elements.add(value);
}
Expand All @@ -83,18 +86,21 @@ public IndexedSet(final Collection<E> values) {
* if {@code values} collection is {@code null} itself, or it contains {@code null}.
*/
public IndexedSet(final E... values) {
if (values == null)
if (values == null) {
throw new IllegalArgumentException("input values cannot be null");
}

final int initialCapacity = values.length;
elements = new ArrayList<>(initialCapacity);
indexByElement = new HashMap<>(initialCapacity);
int nextIndex = 0;
for (final E value : values) {
if (value == null)
if (value == null) {
throw new IllegalArgumentException("null element not allowed: index == " + nextIndex);
if (indexByElement.containsKey(value))
}
if (indexByElement.containsKey(value)) {
continue;
}
indexByElement.put(value, nextIndex++);
elements.add(value);
}
Expand All @@ -116,8 +122,9 @@ public IndexedSet(final E... values) {
* @return never {@code null}.
*/
public List<E> asList() {
if (unmodifiableElementsListView == null)
if (unmodifiableElementsListView == null) {
unmodifiableElementsListView = Collections.unmodifiableList(elements);
}
return unmodifiableElementsListView;
}

Expand All @@ -133,10 +140,12 @@ public List<E> asList() {
* @throws IllegalArgumentException {@code index} is out of bounds.
*/
protected void checkIndex(final int index) {
if (index < 0)
if (index < 0) {
throw new IllegalArgumentException("the index cannot be negative: " + index);
if (index >= size())
}
if (index >= size()) {
throw new IllegalArgumentException("the index is equal or larger than the list length: " + index + " >= " + size());
}
}

@Override
Expand Down Expand Up @@ -181,10 +190,12 @@ public boolean contains(final Object o) {
*/
@Override
public boolean add(final E o) {
if (o == null)
if (o == null) {
throw new IllegalArgumentException("the input argument cannot be null");
if (contains(o))
}
if (contains(o)) {
return false;
}
final int nextIndex = size();
elements.add(o);
indexByElement.put(o, nextIndex);
Expand Down Expand Up @@ -217,8 +228,9 @@ public boolean remove(final Object o) {
indexByElement.remove(o);
final ListIterator<E> it = elements.listIterator(index);
int nextIndex = index;
while (it.hasNext())
indexByElement.put(it.next(),nextIndex++);
while (it.hasNext()) {
indexByElement.put(it.next(), nextIndex++);
}
return true;
}

Expand All @@ -238,12 +250,15 @@ public void clear() {
*/
@Override
public boolean equals(final Object o) {
if (o == this)
if (o == this) {
return true;
if (o == null)
}
if (o == null) {
return false;
if (!(o instanceof IndexedSet<?>))
}
if (!(o instanceof IndexedSet<?>)) {
return false;
}

final IndexedSet<?> other = (IndexedSet<?>)o;

Expand All @@ -261,13 +276,15 @@ public boolean equals(final Object o) {
* (as compared using {@link Object#equals} a this set with matching indices.
*/
public boolean equals(final IndexedSet<?> other) {
if (other == null)
if (other == null) {
throw new IllegalArgumentException("other cannot be null");
}
final ArrayList<?> otherElements = other.elements;

final int elementCount = elements.size();
if (otherElements.size() != elementCount)
if (otherElements.size() != elementCount) {
return false;
}
for (int i = 0; i < elementCount; i++) {
if (!elements.get(i).equals(otherElements.get(i))) {
return false;
Expand Down
Loading

0 comments on commit cd4db74

Please sign in to comment.