Skip to content

Commit

Permalink
Minor code cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
kno10 committed Feb 17, 2024
1 parent 6e50897 commit 2c11055
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 60 deletions.
95 changes: 44 additions & 51 deletions elki-core-util/src/main/java/elki/utilities/io/ParseUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ public static double parseDouble(final byte[] str, final int start, final int en
return Double.NaN;
}
// Match sign
boolean isNegative = (cur == '-');
boolean isNegative = cur == '-';
// Carefully consume the - character, update c and i:
if((isNegative || (cur == '+')) && (++pos < end)) {
if((isNegative || cur == '+') && ++pos < end) {
cur = str[pos];
}
if(matchInf(str, cur, pos, end)) {
Expand All @@ -158,7 +158,7 @@ public static double parseDouble(final byte[] str, final int start, final int en

final int realstart = pos;
// Begin parsing real numbers!
if(((cur < '0') || (cur > '9')) && (cur != '.')) {
if((cur < '0' || cur > '9') && cur != '.') {
throw LOG.isDebuggingFine() ? new NumberFormatException(NOT_A_NUMBER.getMessage()) : NOT_A_NUMBER;
}

Expand All @@ -167,7 +167,7 @@ public static double parseDouble(final byte[] str, final int start, final int en
int decimalPoint = -1;
while(true) {
final int digit = cur - '0';
if((digit >= 0) && (digit <= 9)) {
if(digit >= 0 && digit <= 9) {
final long tmp = (decimal << 3) + (decimal << 1) + digit;
if(tmp >= decimal) {
decimal = tmp; // Otherwise, silently ignore the extra digits.
Expand All @@ -176,7 +176,7 @@ else if(++decimalPoint == 0) { // Because we ignored the digit
throw LOG.isDebuggingFine() ? new NumberFormatException(PRECISION_OVERFLOW.getMessage()) : PRECISION_OVERFLOW;
}
}
else if((cur == '.') && (decimalPoint < 0)) {
else if(cur == '.' && decimalPoint < 0) {
decimalPoint = pos;
}
else { // No more digits, or a second dot.
Expand All @@ -197,13 +197,13 @@ else if((cur == '.') && (decimalPoint < 0)) {

// Reads exponent.
int exp = 0;
if((pos + 1 < end) && ((cur == 'E') || (cur == 'e'))) {
if(pos + 1 < end && (cur == 'E' || cur == 'e')) {
cur = str[++pos];
final boolean isNegativeExp = (cur == '-');
if((isNegativeExp || (cur == '+')) && (++pos < end)) {
if((isNegativeExp || cur == '+') && ++pos < end) {
cur = str[pos];
}
if((cur < '0') || (cur > '9')) { // At least one digit required.
if(cur < '0' || cur > '9') { // At least one digit required.
throw LOG.isDebuggingFine() ? new NumberFormatException(INVALID_EXPONENT.getMessage()) : INVALID_EXPONENT;
}
while(true) {
Expand Down Expand Up @@ -272,9 +272,9 @@ public static double parseDouble(final CharSequence str, final int start, final
return Double.NaN;
}
// Match sign
boolean isNegative = (cur == '-');
boolean isNegative = cur == '-';
// Carefully consume the - character, update c and i:
if((isNegative || (cur == '+')) && (++pos < end)) {
if((isNegative || cur == '+') && ++pos < end) {
cur = str.charAt(pos);
}
if(matchInf(str, cur, pos, end)) {
Expand All @@ -283,7 +283,7 @@ public static double parseDouble(final CharSequence str, final int start, final

final int realstart = pos;
// Begin parsing real numbers!
if(((cur < '0') || (cur > '9')) && (cur != '.')) {
if((cur < '0' || cur > '9') && cur != '.') {
throw LOG.isDebuggingFine() ? new NumberFormatException(NOT_A_NUMBER.getMessage()) : NOT_A_NUMBER;
}

Expand All @@ -301,7 +301,7 @@ else if(++decimalPoint == 0) { // Because we ignored the digit
throw LOG.isDebuggingFine() ? new NumberFormatException(PRECISION_OVERFLOW.getMessage()) : PRECISION_OVERFLOW;
}
}
else if((cur == '.') && (decimalPoint < 0)) {
else if(cur == '.' && decimalPoint < 0) {
decimalPoint = pos;
}
else { // No more digits, or a second dot.
Expand All @@ -322,13 +322,13 @@ else if((cur == '.') && (decimalPoint < 0)) {

// Reads exponent.
int exp = 0;
if((pos + 1 < end) && ((cur == 'E') || (cur == 'e'))) {
if(pos + 1 < end && (cur == 'E' || cur == 'e')) {
cur = str.charAt(++pos);
final boolean isNegativeExp = (cur == '-');
if((isNegativeExp || (cur == '+')) && (++pos < end)) {
final boolean isNegativeExp = cur == '-';
if((isNegativeExp || cur == '+') && ++pos < end) {
cur = str.charAt(pos);
}
if((cur < '0') || (cur > '9')) { // At least one digit required.
if(cur < '0' || cur > '9') { // At least one digit required.
throw LOG.isDebuggingFine() ? new NumberFormatException(INVALID_EXPONENT.getMessage()) : INVALID_EXPONENT;
}
while(true) {
Expand Down Expand Up @@ -383,35 +383,33 @@ public static long parseLongBase10(final CharSequence str, final int start, fina
char cur = str.charAt(pos);

// Match sign
boolean isNegative = (cur == '-');
boolean isNegative = cur == '-';
// Carefully consume the - character, update c and i:
if((isNegative || (cur == '+')) && (++pos < end)) {
if((isNegative || cur == '+') && ++pos < end) {
cur = str.charAt(pos);
}

// Begin parsing real numbers!
if((cur < '0') || (cur > '9')) {
if(cur < '0' || cur > '9') {
throw LOG.isDebuggingFine() ? new NumberFormatException(NOT_A_NUMBER.getMessage()) : NOT_A_NUMBER;
}

// Parse digits into a long, remember offset of decimal point.
long decimal = 0;
while(true) {
final int digit = cur - '0';
if((digit >= 0) && (digit <= 9)) {
final long tmp = (decimal << 3) + (decimal << 1) + digit;
if(tmp < decimal) {
// Special case, Long.MIN_VALUE only.
if(isNegative && tmp == 0x8000000000000000L && pos + 1 == end) {
return Long.MIN_VALUE;
}
throw LOG.isDebuggingFine() ? new NumberFormatException(PRECISION_OVERFLOW.getMessage()) : PRECISION_OVERFLOW;
}
decimal = tmp;
if(digit < 0 || digit > 9) {
break; // No more digits
}
else { // No more digits
break;
final long tmp = (decimal << 3) + (decimal << 1) + digit;
if(tmp < decimal) {
// Special case, Long.MIN_VALUE only.
if(isNegative && tmp == Long.MIN_VALUE && pos + 1 == end) {
return Long.MIN_VALUE;
}
throw LOG.isDebuggingFine() ? new NumberFormatException(PRECISION_OVERFLOW.getMessage()) : PRECISION_OVERFLOW;
}
decimal = tmp;
if(++pos >= end) {
break;
}
Expand All @@ -420,7 +418,6 @@ public static long parseLongBase10(final CharSequence str, final int start, fina
if(pos != end) {
throw LOG.isDebuggingFine() ? new NumberFormatException(TRAILING_CHARACTERS.getMessage()) : TRAILING_CHARACTERS;
}

return isNegative ? -decimal : decimal;
}

Expand Down Expand Up @@ -451,35 +448,33 @@ public static int parseIntBase10(final CharSequence str, final int start, final
char cur = str.charAt(pos);

// Match sign
boolean isNegative = (cur == '-');
boolean isNegative = cur == '-';
// Carefully consume the - character, update c and i:
if((isNegative || (cur == '+')) && (++pos < end)) {
if((isNegative || cur == '+') && ++pos < end) {
cur = str.charAt(pos);
}

// Begin parsing real numbers!
if((cur < '0') || (cur > '9')) {
if(cur < '0' || cur > '9') {
throw LOG.isDebuggingFine() ? new NumberFormatException(NOT_A_NUMBER.getMessage()) : NOT_A_NUMBER;
}

// Parse digits into a int, remember offset of decimal point.
int decimal = 0;
while(true) {
final int digit = cur - '0';
if((digit >= 0) && (digit <= 9)) {
final int tmp = (decimal << 3) + (decimal << 1) + digit;
if(tmp < decimal) {
// Special case, Integer.MIN_VALUE only.
if(isNegative && tmp == 0x80000000 && pos + 1 == end) {
return Integer.MIN_VALUE;
}
throw LOG.isDebuggingFine() ? new NumberFormatException(PRECISION_OVERFLOW.getMessage()) : PRECISION_OVERFLOW;
}
decimal = tmp;
if(digit < 0 || digit > 9) {
break; // No more digits
}
else { // No more digits
break;
final int tmp = (decimal << 3) + (decimal << 1) + digit;
if(tmp < decimal) {
// Special case, Integer.MIN_VALUE only.
if(isNegative && tmp == Integer.MIN_VALUE && pos + 1 == end) {
return Integer.MIN_VALUE;
}
throw LOG.isDebuggingFine() ? new NumberFormatException(PRECISION_OVERFLOW.getMessage()) : PRECISION_OVERFLOW;
}
decimal = tmp;
if(++pos >= end) {
break;
}
Expand Down Expand Up @@ -507,8 +502,7 @@ private static boolean matchInf(byte[] str, byte firstchar, int start, int end)
if(len == 3 && firstchar == -0x1E && str[start + 1] == -0x78 && str[start + 2] == -0x62) {
return true;
}
if((len != 3 && len != INFINITY_LENGTH) //
|| (firstchar != 'I' && firstchar != 'i')) {
if((len != 3 && len != INFINITY_LENGTH) || (firstchar != 'I' && firstchar != 'i')) {
return false;
}
for(int i = 1, j = INFINITY_LENGTH + 1; i < INFINITY_LENGTH; i++, j++) {
Expand Down Expand Up @@ -538,8 +532,7 @@ private static boolean matchInf(CharSequence str, char firstchar, int start, int
if(len == 1 && firstchar == '\u221E') {
return true;
}
if((len != 3 && len != INFINITY_LENGTH) //
|| (firstchar != 'I' && firstchar != 'i')) {
if((len != 3 && len != INFINITY_LENGTH) || (firstchar != 'I' && firstchar != 'i')) {
return false;
}
for(int i = 1, j = INFINITY_LENGTH + 1; i < INFINITY_LENGTH; i++, j++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
import elki.database.datastore.DataStoreFactory;
import elki.database.datastore.WritableDataStore;
import elki.database.ids.*;
import elki.database.query.QueryBuilder;
import elki.database.query.distance.DistanceQuery;
import elki.database.query.knn.KNNSearcher;
import elki.database.relation.Relation;
import elki.distance.Distance;
import elki.logging.Logging;
Expand Down Expand Up @@ -467,12 +464,6 @@ protected Logging getLogger() {
return LOG;
}

@Override
public KNNSearcher<O> kNNByObject(DistanceQuery<O> distanceQuery, int maxk, int flags) {
return (flags & QueryBuilder.FLAG_EXACT_ONLY) != 0 ? null : // approximate
super.kNNByObject(distanceQuery, maxk, flags);
}

/**
* Index factory.
*
Expand Down

0 comments on commit 2c11055

Please sign in to comment.