Skip to content

Commit 7f53c13

Browse files
committed
minor tweak to EncodedValue implementation
1 parent d1b287a commit 7f53c13

File tree

4 files changed

+24
-54
lines changed

4 files changed

+24
-54
lines changed

core/src/main/java/com/graphhopper/routing/ev/SimpleBooleanEncodedValue.java

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,32 +34,11 @@ public SimpleBooleanEncodedValue(String name, boolean storeBothDirections) {
3434

3535
@Override
3636
public final void setBool(boolean reverse, IntsRef ref, boolean value) {
37-
if (storeTwoDirections && reverse) {
38-
int flags = ref.ints[bwdDataIndex + ref.offset];
39-
flags &= ~bwdMask;
40-
// set value
41-
if (value)
42-
flags = flags | (1 << bwdShift);
43-
ref.ints[bwdDataIndex + ref.offset] = flags;
44-
45-
} else {
46-
int flags = ref.ints[fwdDataIndex + ref.offset];
47-
flags &= ~fwdMask;
48-
if (value)
49-
flags = flags | (1 << fwdShift);
50-
ref.ints[fwdDataIndex + ref.offset] = flags;
51-
}
37+
setInt(reverse, ref, value ? 1 : 0);
5238
}
5339

5440
@Override
5541
public final boolean getBool(boolean reverse, IntsRef ref) {
56-
int flags;
57-
if (storeTwoDirections && reverse) {
58-
flags = ref.ints[bwdDataIndex + ref.offset];
59-
return (flags & bwdMask) >>> bwdShift == 1;
60-
}
61-
62-
flags = ref.ints[fwdDataIndex + ref.offset];
63-
return (flags & fwdMask) >>> fwdShift == 1;
42+
return getInt(reverse, ref) == 1;
6443
}
6544
}

core/src/main/java/com/graphhopper/routing/ev/StringEncodedValue.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package com.graphhopper.routing.ev;
22

3+
import com.carrotsearch.hppc.ObjectIntHashMap;
4+
import com.carrotsearch.hppc.ObjectIntMap;
5+
import com.graphhopper.storage.IntsRef;
6+
37
import java.util.ArrayList;
48
import java.util.Collections;
59
import java.util.List;
610
import java.util.Objects;
711

8-
import com.carrotsearch.hppc.ObjectIntHashMap;
9-
import com.carrotsearch.hppc.ObjectIntMap;
10-
import com.graphhopper.storage.IntsRef;
11-
1212
/**
1313
* This class holds a List of up to {@link #maxValues} encountered Strings and stores
1414
* <i>index+1</i> to indicate a string is set or <i>0</i> if no value is assigned
15-
*
15+
*
1616
* @author Peter Karich
1717
* @author Thomas Butz
1818
*/
@@ -37,11 +37,10 @@ public StringEncodedValue(String name, int bits, List<String> values, boolean st
3737
super(name, bits, storeTwoDirections);
3838

3939
this.maxValues = (1 << bits) - 1;
40-
if (values.size() > maxValues) {
41-
throw new IllegalArgumentException(
42-
"Number of values is higher than the maximum value count: "
43-
+ values.size() + " > " + maxValues);
44-
}
40+
if (values.size() > maxValues)
41+
throw new IllegalArgumentException("Number of values is higher than the maximum value count: "
42+
+ values.size() + " > " + maxValues);
43+
4544
this.values = new ArrayList<>(values);
4645
this.indexMap = new ObjectIntHashMap<>(values.size());
4746
int index = 1;
@@ -57,10 +56,9 @@ public final void setString(boolean reverse, IntsRef ref, String value) {
5756
}
5857
int index = indexMap.get(value);
5958
if (index == 0) {
60-
if (values.size() == maxValues) {
61-
throw new IllegalStateException("Maximum number of values reached for " + getName()
62-
+ ": " + maxValues);
63-
}
59+
if (values.size() == maxValues)
60+
throw new IllegalStateException("Maximum number of values reached for " + getName() + ": " + maxValues);
61+
6462
values.add(value);
6563
index = values.size();
6664
indexMap.put(value, index);
@@ -77,17 +75,15 @@ public final String getString(boolean reverse, IntsRef ref) {
7775
}
7876

7977
/**
80-
* @param value
81-
* the value to be rounded
78+
* @param value the value to be rounded
8279
* @return the value rounded to the highest integer with the same number of leading zeros
8380
*/
8481
private static int roundUp(int value) {
8582
return -1 >>> Integer.numberOfLeadingZeros(value);
8683
}
8784

8885
/**
89-
* @param value
90-
* the String to retrieve the index
86+
* @param value the String to retrieve the index
9187
* @return the non-zero index of the String or <i>0</i> if it couldn't be found
9288
*/
9389
public int indexOf(String value) {

core/src/main/java/com/graphhopper/routing/ev/UnsignedDecimalEncodedValue.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ public final void setDecimal(boolean reverse, IntsRef ints, double value) {
6666
}
6767
if (value == defaultValue)
6868
value = 0;
69-
if (value > maxValue * factor)
69+
else if (value > maxValue * factor)
7070
throw new IllegalArgumentException(getName() + " value " + value + " too large for encoding. maxValue:" + maxValue * factor);
71-
if (value < 0)
71+
else if (value < 0)
7272
throw new IllegalArgumentException("Negative value for " + getName() + " not allowed! " + value);
73-
if (Double.isNaN(value))
73+
else if (Double.isNaN(value))
7474
throw new IllegalArgumentException("NaN value for " + getName() + " not allowed!");
7575

7676
super.setInt(reverse, ints, toInt(value));
@@ -81,9 +81,7 @@ public final double getDecimal(boolean reverse, IntsRef ref) {
8181
int value = getInt(reverse, ref);
8282
if (useMaximumAsInfinity && value == maxValue)
8383
return Double.POSITIVE_INFINITY;
84-
if (value == 0)
85-
return defaultValue;
86-
return value * factor;
84+
return value == 0 ? defaultValue : value * factor;
8785
}
8886

8987
@Override

core/src/main/java/com/graphhopper/routing/ev/UnsignedIntEncodedValue.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,19 @@ final void uncheckedSet(boolean reverse, IntsRef ref, int value) {
112112
int flags = ref.ints[bwdDataIndex + ref.offset];
113113
// clear value bits
114114
flags &= ~bwdMask;
115-
value <<= bwdShift;
116-
// set value
117-
ref.ints[bwdDataIndex + ref.offset] = flags | value;
115+
ref.ints[bwdDataIndex + ref.offset] = flags | (value << bwdShift);
118116
} else {
119117
int flags = ref.ints[fwdDataIndex + ref.offset];
120118
flags &= ~fwdMask;
121-
value <<= fwdShift;
122-
ref.ints[fwdDataIndex + ref.offset] = flags | value;
119+
ref.ints[fwdDataIndex + ref.offset] = flags | (value << fwdShift);
123120
}
124121
}
125122

126123
@Override
127124
public final int getInt(boolean reverse, IntsRef ref) {
128125
int flags;
129126
// if we do not store both directions ignore reverse == true for convenient reading
130-
if (reverse && storeTwoDirections) {
127+
if (storeTwoDirections && reverse) {
131128
flags = ref.ints[bwdDataIndex + ref.offset];
132129
return (flags & bwdMask) >>> bwdShift;
133130
} else {
@@ -234,7 +231,7 @@ static int staticHashCode(String... vals) {
234231

235232
return val;
236233
}
237-
234+
238235
/**
239236
* Produces a static hashcode for a collection of Strings that is platform independent and still compatible to the default
240237
* of openjdk.

0 commit comments

Comments
 (0)