Skip to content

Commit

Permalink
* Simplify bitwise
Browse files Browse the repository at this point in the history
* Simplify boolean expression
* Simplify conditional expression
* Redundant 'if' statement
  • Loading branch information
arturobernalg committed Aug 5, 2021
1 parent 771a218 commit b71e2ab
Show file tree
Hide file tree
Showing 14 changed files with 18 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ public static void checkNonNegative(final long[][] in) {
*/
public static boolean equals(float[] x, float[] y) {
if (x == null || y == null) {
return !((x == null) ^ (y == null));
return (x == null) == (y == null);
}
if (x.length != y.length) {
return false;
Expand All @@ -643,7 +643,7 @@ public static boolean equals(float[] x, float[] y) {
*/
public static boolean equalsIncludingNaN(float[] x, float[] y) {
if (x == null || y == null) {
return !((x == null) ^ (y == null));
return (x == null) == (y == null);
}
if (x.length != y.length) {
return false;
Expand All @@ -668,7 +668,7 @@ public static boolean equalsIncludingNaN(float[] x, float[] y) {
*/
public static boolean equals(double[] x, double[] y) {
if (x == null || y == null) {
return !((x == null) ^ (y == null));
return (x == null) == (y == null);
}
if (x.length != y.length) {
return false;
Expand All @@ -694,7 +694,7 @@ public static boolean equals(double[] x, double[] y) {
*/
public static boolean equalsIncludingNaN(double[] x, double[] y) {
if (x == null || y == null) {
return !((x == null) ^ (y == null));
return (x == null) == (y == null);
}
if (x.length != y.length) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2351,10 +2351,7 @@ public Dfp nextAfter(final Dfp x) {
}

// if this is greater than x
boolean up = false;
if (this.lessThan(x)) {
up = true;
}
boolean up = this.lessThan(x);

if (compare(this, x) == 0) {
return newInstance(x);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -941,11 +941,7 @@ public static Dfp asin(final Dfp a) {
*/
public static Dfp acos(Dfp a) {
Dfp result;
boolean negative = false;

if (a.lessThan(a.getZero())) {
negative = true;
}
boolean negative = a.lessThan(a.getZero());

a = Dfp.copySign(a, a.getOne()); // absolute value

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,7 @@ public boolean equals(Object obj) {
return false;
}
PolynomialFunction other = (PolynomialFunction) obj;
if (!Arrays.equals(coefficients, other.coefficients)) {
return false;
}
return true;
return Arrays.equals(coefficients, other.coefficients);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,7 @@ public int compare(WeightedObservedPoint p1,
if (comp != 0) {
return comp;
}
comp = Double.compare(p1.getWeight(), p2.getWeight());
if (comp != 0) {
return comp;
}
return 0;
return Double.compare(p1.getWeight(), p2.getWeight());
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ public void stepAccepted(final double t, final double[] y) {
// force the sign to its value "just after the event"
previousEventTime = t;
g0Positive = increasing;
nextAction = handler.eventOccurred(t, y, !(increasing ^ forward));
nextAction = handler.eventOccurred(t, y, increasing == forward);
} else {
g0Positive = g0 >= 0;
nextAction = EventHandler.Action.CONTINUE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public void stepAccepted(final FieldODEStateAndDerivative<T> state) {
// force the sign to its value "just after the event"
previousEventTime = state.getTime();
g0Positive = increasing;
nextAction = handler.eventOccurred(state, !(increasing ^ forward));
nextAction = handler.eventOccurred(state, increasing == forward);
} else {
g0Positive = g0.getReal() >= 0;
nextAction = Action.CONTINUE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ public PointValuePair getSolution() {
* @return {@code true} if the solution is optimal, {@code false} otherwise
*/
public boolean isSolutionOptimal() {
return tableau != null ? tableau.isOptimal() : false;
return tableau != null && tableau.isOptimal();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ public boolean equals(Object other) {
@Override
public int hashCode() {
long bits = Double.doubleToLongBits(value);
return (int) ((1438542 ^ (bits >>> 32) ^ bits) & 0xffffffff);
return (int) ((1438542 ^ (bits >>> 32) ^ bits));
}
}
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,14 +376,7 @@ public boolean equals(Object obj) {
return false;
}
Frequency<?> other = (Frequency<?>) obj;
if (freqTable == null) {
if (other.freqTable != null) {
return false;
}
} else if (!freqTable.equals(other.freqTable)) {
return false;
}
return true;
return freqTable.equals(other.freqTable);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,7 @@ public boolean equals(Object obj) {
if (!Arrays.equals(productsSums, other.productsSums)) {
return false;
}
if (!Arrays.equals(sums, other.sums)) {
return false;
}
return true;
return Arrays.equals(sums, other.sums);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,7 @@ public boolean equals(Object obj) {
return false;
}
VectorialMean other = (VectorialMean) obj;
if (!Arrays.equals(means, other.means)) {
return false;
}
return true;
return Arrays.equals(means, other.means);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ private static class FixedCapacityList<E> extends ArrayList<E> implements Serial
*/
@Override
public boolean add(final E e) {
return size() < capacity ? super.add(e) : false;
return size() < capacity && super.add(e);
}

/**
Expand All @@ -924,7 +924,7 @@ public boolean addAll(Collection<? extends E> collection) {
boolean isCollectionLess =
collection != null &&
collection.size() + size() <= capacity;
return isCollectionLess ? super.addAll(collection) : false;
return isCollectionLess && super.addAll(collection);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1132,11 +1132,7 @@ private static boolean hasTies(double[] x, double[] y) {
throw new InsufficientDataException();
}

if (values.length == x.length + y.length) {
return false; // There are no ties.
}

return true;
return values.length != x.length + y.length; // There are no ties.
}

/**
Expand Down

0 comments on commit b71e2ab

Please sign in to comment.