Skip to content

Commit

Permalink
renamed implementations of IntEncodedValue and DecimalEncodedValue (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
karussell committed Jun 19, 2019
1 parent 57767a8 commit a8c654b
Show file tree
Hide file tree
Showing 21 changed files with 71 additions and 54 deletions.
Expand Up @@ -8,7 +8,7 @@
* so that the storable part of it fits into the specified number of bits (maximum 32 at the moment
* for all implementations) and 2. the default value is always 0.
*
* @see FactorizedDecimalEncodedValue
* @see UnsignedDecimalEncodedValue
*/
public interface DecimalEncodedValue extends EncodedValue {

Expand Down
Expand Up @@ -24,7 +24,7 @@
/**
* This class allows to store distinct values via an enum. I.e. it stores just the indices
*/
public final class EnumEncodedValue<E extends Enum> extends SimpleIntEncodedValue {
public final class EnumEncodedValue<E extends Enum> extends UnsignedIntEncodedValue {
private final E[] arr;

public EnumEncodedValue(String name, Class<E> enumType) {
Expand Down
Expand Up @@ -7,7 +7,7 @@
* integer is highly limited (unlike the Java 32bit integer values) so that the storable part of it fits into the
* specified number of bits (maximum 32) and 2. the default value is always 0.
*
* @see SimpleIntEncodedValue
* @see UnsignedIntEncodedValue
*/
public interface IntEncodedValue extends EncodedValue {

Expand Down
Expand Up @@ -31,6 +31,6 @@ public class MaxHeight {
* it is assumed to use the maximum value.
*/
public static DecimalEncodedValue create() {
return new FactorizedDecimalEncodedValue(KEY, 7, 0.1, Double.POSITIVE_INFINITY, false);
return new UnsignedDecimalEncodedValue(KEY, 7, 0.1, Double.POSITIVE_INFINITY, false);
}
}
Expand Up @@ -35,6 +35,6 @@ public class MaxSpeed {
public static final double UNSET_SPEED = Double.POSITIVE_INFINITY;

public static DecimalEncodedValue create() {
return new FactorizedDecimalEncodedValue(KEY, 5, 5, UNSET_SPEED, true);
return new UnsignedDecimalEncodedValue(KEY, 5, 5, UNSET_SPEED, true);
}
}
Expand Up @@ -32,6 +32,6 @@ public class MaxWeight {
* it was done with the MappedDecimalEncodedValue still handling (or rounding) of unknown values is unclear.
*/
public static DecimalEncodedValue create() {
return new FactorizedDecimalEncodedValue(KEY, 8, 0.1, Double.POSITIVE_INFINITY, false);
return new UnsignedDecimalEncodedValue(KEY, 8, 0.1, Double.POSITIVE_INFINITY, false);
}
}
Expand Up @@ -31,6 +31,6 @@ public class MaxWidth {
* it is assumed to use the maximum value.
*/
public static DecimalEncodedValue create() {
return new FactorizedDecimalEncodedValue(KEY, 7, 0.1, Double.POSITIVE_INFINITY, false);
return new UnsignedDecimalEncodedValue(KEY, 7, 0.1, Double.POSITIVE_INFINITY, false);
}
}
Expand Up @@ -20,9 +20,9 @@
import com.graphhopper.storage.IntsRef;

/**
* This class implements a simple Boolean storage via a FactoredIntEncodedValue with 1 bit.
* This class implements a simple boolean storage via an UnsignedIntEncodedValue with 1 bit.
*/
public final class SimpleBooleanEncodedValue extends SimpleIntEncodedValue implements BooleanEncodedValue {
public final class SimpleBooleanEncodedValue extends UnsignedIntEncodedValue implements BooleanEncodedValue {

public SimpleBooleanEncodedValue(String name) {
this(name, false);
Expand Down
Expand Up @@ -22,14 +22,14 @@
import java.util.Objects;

/**
* This class holds a decimal value and stores it as an integer value via a conversion factor and a maximum number
* This class holds a decimal value and stores it as an unsigned integer value via a conversion factor and a maximum number
* of bits.
*/
public final class FactorizedDecimalEncodedValue extends SimpleIntEncodedValue implements DecimalEncodedValue {
public final class UnsignedDecimalEncodedValue extends UnsignedIntEncodedValue implements DecimalEncodedValue {
private final double factor;
private final double defaultValue;

public FactorizedDecimalEncodedValue(String name, int bits, double factor, boolean storeTwoDirections) {
public UnsignedDecimalEncodedValue(String name, int bits, double factor, boolean storeTwoDirections) {
this(name, bits, factor, 0, storeTwoDirections);
}

Expand All @@ -40,7 +40,7 @@ public FactorizedDecimalEncodedValue(String name, int bits, double factor, boole
* @param defaultValue the value that should be returned if the stored value is 0.
* @param storeTwoDirections true if forward and backward direction of the edge should get two independent values.
*/
public FactorizedDecimalEncodedValue(String name, int bits, double factor, double defaultValue, boolean storeTwoDirections) {
public UnsignedDecimalEncodedValue(String name, int bits, double factor, double defaultValue, boolean storeTwoDirections) {
super(name, bits, storeTwoDirections);
this.factor = factor;
this.defaultValue = defaultValue;
Expand Down Expand Up @@ -82,7 +82,7 @@ public double getMaxDecimal() {
@Override
public boolean equals(Object o) {
if (!super.equals(o)) return false;
FactorizedDecimalEncodedValue that = (FactorizedDecimalEncodedValue) o;
UnsignedDecimalEncodedValue that = (UnsignedDecimalEncodedValue) o;
return Double.compare(that.factor, factor) == 0;
}

Expand Down
Expand Up @@ -23,10 +23,10 @@
import java.util.Objects;

/**
* Implementation of the IntEncodedValue via a limited number of bits. It introduces simple handling of "backward"- and
* "forward"-edge information.
* Implementation of the IntEncodedValue via a limited number of bits and without a sign. It introduces handling
* of "backward"- and "forward"-edge information.
*/
public class SimpleIntEncodedValue implements IntEncodedValue {
public class UnsignedIntEncodedValue implements IntEncodedValue {

private final String name;

Expand All @@ -50,13 +50,13 @@ public class SimpleIntEncodedValue implements IntEncodedValue {
* @param storeTwoDirections if true this EncodedValue can store different values for the forward and backward
* direction.
*/
public SimpleIntEncodedValue(String name, int bits, boolean storeTwoDirections) {
public UnsignedIntEncodedValue(String name, int bits, boolean storeTwoDirections) {
if (!name.toLowerCase(Locale.ROOT).equals(name))
throw new IllegalArgumentException("EncodedValue name must be lower case but was " + name);
if (bits <= 0)
throw new IllegalArgumentException(name + ": bits cannot be zero or negative");
if (bits > 31)
throw new IllegalArgumentException(name + ": at the moment bits cannot be >32");
throw new IllegalArgumentException(name + ": at the moment the number of reserved bits cannot be more than 31");
this.bits = bits;
this.name = name;
this.storeTwoDirections = storeTwoDirections;
Expand Down Expand Up @@ -157,7 +157,7 @@ public final String toString() {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SimpleIntEncodedValue that = (SimpleIntEncodedValue) o;
UnsignedIntEncodedValue that = (UnsignedIntEncodedValue) o;
return fwdDataIndex == that.fwdDataIndex &&
bwdDataIndex == that.bwdDataIndex &&
bits == that.bits &&
Expand Down
Expand Up @@ -214,10 +214,10 @@ public int getVersion() {
public void createEncodedValues(List<EncodedValue> registerNewEncodedValue, String prefix, int index) {
// first two bits are reserved for route handling in superclass
super.createEncodedValues(registerNewEncodedValue, prefix, index);
registerNewEncodedValue.add(speedEncoder = new FactorizedDecimalEncodedValue(getKey(prefix, "average_speed"), speedBits, speedFactor, speedTwoDirections));
registerNewEncodedValue.add(speedEncoder = new UnsignedDecimalEncodedValue(getKey(prefix, "average_speed"), speedBits, speedFactor, speedTwoDirections));
registerNewEncodedValue.add(unpavedEncoder = new SimpleBooleanEncodedValue(getKey(prefix, "paved"), false));
registerNewEncodedValue.add(wayTypeEncoder = new SimpleIntEncodedValue(getKey(prefix, "waytype"), 2, false));
registerNewEncodedValue.add(priorityWayEncoder = new FactorizedDecimalEncodedValue(getKey(prefix, "priority"), 3, PriorityCode.getFactor(1), false));
registerNewEncodedValue.add(wayTypeEncoder = new UnsignedIntEncodedValue(getKey(prefix, "waytype"), 2, false));
registerNewEncodedValue.add(priorityWayEncoder = new UnsignedDecimalEncodedValue(getKey(prefix, "priority"), 3, PriorityCode.getFactor(1), false));
}

@Override
Expand Down
Expand Up @@ -20,7 +20,7 @@
import com.graphhopper.reader.ReaderRelation;
import com.graphhopper.reader.ReaderWay;
import com.graphhopper.routing.profiles.EncodedValue;
import com.graphhopper.routing.profiles.FactorizedDecimalEncodedValue;
import com.graphhopper.routing.profiles.UnsignedDecimalEncodedValue;
import com.graphhopper.storage.IntsRef;
import com.graphhopper.util.Helper;
import com.graphhopper.util.PMap;
Expand Down Expand Up @@ -161,7 +161,7 @@ public int getVersion() {
public void createEncodedValues(List<EncodedValue> registerNewEncodedValue, String prefix, int index) {
// first two bits are reserved for route handling in superclass
super.createEncodedValues(registerNewEncodedValue, prefix, index);
registerNewEncodedValue.add(speedEncoder = new FactorizedDecimalEncodedValue(EncodingManager.getKey(prefix, "average_speed"), speedBits, speedFactor, speedTwoDirections));
registerNewEncodedValue.add(speedEncoder = new UnsignedDecimalEncodedValue(EncodingManager.getKey(prefix, "average_speed"), speedBits, speedFactor, speedTwoDirections));
}

protected double getSpeed(ReaderWay way) {
Expand Down
Expand Up @@ -20,7 +20,6 @@
import com.graphhopper.reader.ReaderRelation;
import com.graphhopper.reader.ReaderWay;
import com.graphhopper.routing.profiles.*;
import com.graphhopper.routing.util.parsers.OSMMaxSpeedParser;
import com.graphhopper.routing.weighting.GenericWeighting;
import com.graphhopper.storage.IntsRef;
import com.graphhopper.util.EdgeIteratorState;
Expand Down Expand Up @@ -102,7 +101,7 @@ public void createEncodedValues(List<EncodedValue> registerNewEncodedValue, Stri
}

// workaround to init AbstractWeighting.avSpeedEnc variable that GenericWeighting does not need
speedEncoder = new FactorizedDecimalEncodedValue("fake", 1, 1, false);
speedEncoder = new UnsignedDecimalEncodedValue("fake", 1, 1, false);
roadEnvironmentEnc = getEnumEncodedValue(RoadEnvironment.KEY, RoadEnvironment.class);
}

Expand Down
Expand Up @@ -21,7 +21,7 @@
import com.graphhopper.reader.ReaderWay;
import com.graphhopper.routing.profiles.DecimalEncodedValue;
import com.graphhopper.routing.profiles.EncodedValue;
import com.graphhopper.routing.profiles.FactorizedDecimalEncodedValue;
import com.graphhopper.routing.profiles.UnsignedDecimalEncodedValue;
import com.graphhopper.routing.weighting.PriorityWeighting;
import com.graphhopper.storage.IntsRef;
import com.graphhopper.util.PMap;
Expand Down Expand Up @@ -151,8 +151,8 @@ public void createEncodedValues(List<EncodedValue> registerNewEncodedValue, Stri
// first two bits are reserved for route handling in superclass
super.createEncodedValues(registerNewEncodedValue, prefix, index);
// larger value required - ferries are faster than pedestrians
registerNewEncodedValue.add(speedEncoder = new FactorizedDecimalEncodedValue(getKey(prefix, "average_speed"), speedBits, speedFactor, false));
registerNewEncodedValue.add(priorityWayEncoder = new FactorizedDecimalEncodedValue(getKey(prefix, "priority"), 3, PriorityCode.getFactor(1), false));
registerNewEncodedValue.add(speedEncoder = new UnsignedDecimalEncodedValue(getKey(prefix, "average_speed"), speedBits, speedFactor, false));
registerNewEncodedValue.add(priorityWayEncoder = new UnsignedDecimalEncodedValue(getKey(prefix, "priority"), 3, PriorityCode.getFactor(1), false));
}

@Override
Expand Down
Expand Up @@ -20,7 +20,7 @@
import com.graphhopper.reader.ReaderWay;
import com.graphhopper.routing.profiles.DecimalEncodedValue;
import com.graphhopper.routing.profiles.EncodedValue;
import com.graphhopper.routing.profiles.FactorizedDecimalEncodedValue;
import com.graphhopper.routing.profiles.UnsignedDecimalEncodedValue;
import com.graphhopper.routing.weighting.CurvatureWeighting;
import com.graphhopper.routing.weighting.PriorityWeighting;
import com.graphhopper.storage.IntsRef;
Expand Down Expand Up @@ -134,8 +134,8 @@ public void createEncodedValues(List<EncodedValue> registerNewEncodedValue, Stri
// first two bits are reserved for route handling in superclass
super.createEncodedValues(registerNewEncodedValue, prefix, index);

registerNewEncodedValue.add(priorityWayEncoder = new FactorizedDecimalEncodedValue(getKey(prefix, "priority"), 3, PriorityCode.getFactor(1), false));
registerNewEncodedValue.add(curvatureEncoder = new FactorizedDecimalEncodedValue(getKey(prefix, "curvature"), 4, 0.1, false));
registerNewEncodedValue.add(priorityWayEncoder = new UnsignedDecimalEncodedValue(getKey(prefix, "priority"), 3, PriorityCode.getFactor(1), false));
registerNewEncodedValue.add(curvatureEncoder = new UnsignedDecimalEncodedValue(getKey(prefix, "curvature"), 4, 0.1, false));
}

@Override
Expand Down
Expand Up @@ -12,7 +12,7 @@ public class BooleanEncodedValueTest {
@Test
public void testBit() {
EncodedValue.InitializerConfig config = new EncodedValue.InitializerConfig();
IntEncodedValue intProp = new SimpleIntEncodedValue("somevalue", 5, false);
IntEncodedValue intProp = new UnsignedIntEncodedValue("somevalue", 5, false);
intProp.init(config);

BooleanEncodedValue bool = new SimpleBooleanEncodedValue("access", false);
Expand Down
Expand Up @@ -13,7 +13,7 @@ public class DecimalEncodedValueTest {

@Test
public void testInit() {
DecimalEncodedValue prop = new FactorizedDecimalEncodedValue("test", 10, 2, false);
DecimalEncodedValue prop = new UnsignedDecimalEncodedValue("test", 10, 2, false);
prop.init(new EncodedValue.InitializerConfig());
IntsRef ref = new IntsRef(1);
prop.setDecimal(false, ref, 10d);
Expand All @@ -32,7 +32,7 @@ public void testMaxValue() {
IntsRef flags = carEncoder.handleWayTags(em.createEdgeFlags(), way, carEncoder.getAccess(way), 0);
assertEquals(101.5, carAverageSpeedEnc.getDecimal(true, flags), 1e-1);

DecimalEncodedValue instance1 = new FactorizedDecimalEncodedValue("test1", 8, 0.5, false);
DecimalEncodedValue instance1 = new UnsignedDecimalEncodedValue("test1", 8, 0.5, false);
instance1.init(new EncodedValue.InitializerConfig());
flags = em.createEdgeFlags();
instance1.setDecimal(false, flags, 100d);
Expand All @@ -41,7 +41,7 @@ public void testMaxValue() {

@Test
public void testNegativeBounds() {
DecimalEncodedValue prop = new FactorizedDecimalEncodedValue("test", 10, 5, false);
DecimalEncodedValue prop = new UnsignedDecimalEncodedValue("test", 10, 5, false);
prop.init(new EncodedValue.InitializerConfig());
try {
prop.setDecimal(false, new IntsRef(1), -1);
Expand Down
Expand Up @@ -18,7 +18,7 @@ public void loadRoadClass() {
@Test
public void loadCarMaxSpeed() {
EncodedValue enc = MaxSpeed.create();
FactorizedDecimalEncodedValue loadedEnc = (FactorizedDecimalEncodedValue) factory.create(enc.toString());
UnsignedDecimalEncodedValue loadedEnc = (UnsignedDecimalEncodedValue) factory.create(enc.toString());
assertEquals(loadedEnc, enc);
}

Expand Down
@@ -1,17 +1,15 @@
package com.graphhopper.routing.profiles;

import com.graphhopper.storage.IntsRef;
import com.graphhopper.util.PMap;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;

public class IntEncodedValueTest {

@Test
public void testInvalidReverseAccess() {
IntEncodedValue prop = new SimpleIntEncodedValue("test", 10, false);
IntEncodedValue prop = new UnsignedIntEncodedValue("test", 10, false);
prop.init(new EncodedValue.InitializerConfig());
try {
prop.setInt(true, new IntsRef(1), -1);
Expand All @@ -22,7 +20,7 @@ public void testInvalidReverseAccess() {

@Test
public void testDirectedValue() {
IntEncodedValue prop = new SimpleIntEncodedValue("test", 10, true);
IntEncodedValue prop = new UnsignedIntEncodedValue("test", 10, true);
prop.init(new EncodedValue.InitializerConfig());
IntsRef ref = new IntsRef(1);
prop.setInt(false, ref, 10);
Expand All @@ -33,7 +31,7 @@ public void testDirectedValue() {

@Test
public void multiIntsUsage() {
IntEncodedValue prop = new SimpleIntEncodedValue("test", 31, true);
IntEncodedValue prop = new UnsignedIntEncodedValue("test", 31, true);
prop.init(new EncodedValue.InitializerConfig());
IntsRef ref = new IntsRef(2);
prop.setInt(false, ref, 10);
Expand All @@ -44,12 +42,33 @@ public void multiIntsUsage() {

@Test
public void padding() {
IntEncodedValue prop = new SimpleIntEncodedValue("test", 30, true);
IntEncodedValue prop = new UnsignedIntEncodedValue("test", 30, true);
prop.init(new EncodedValue.InitializerConfig());
IntsRef ref = new IntsRef(2);
prop.setInt(false, ref, 10);
prop.setInt(true, ref, 20);
assertEquals(10, prop.getInt(false, ref));
assertEquals(20, prop.getInt(true, ref));
}

@Test
public void testSignedInt() {
IntEncodedValue prop = new UnsignedIntEncodedValue("test", 31, false);
BooleanEncodedValue sign = new SimpleBooleanEncodedValue("a");
EncodedValue.InitializerConfig config = new EncodedValue.InitializerConfig();
prop.init(config);
sign.init(config);

IntsRef ref = new IntsRef(1);

prop.setInt(false, ref, Integer.MAX_VALUE);
sign.setBool(false, ref, true);
assertEquals(Integer.MAX_VALUE, prop.getInt(false, ref));
assertTrue(sign.getBool(false, ref));

prop.setInt(false, ref, Integer.MAX_VALUE);
sign.setBool(false, ref, false);
assertEquals(Integer.MAX_VALUE, prop.getInt(false, ref));
assertFalse(sign.getBool(false, ref));
}
}
Expand Up @@ -6,11 +6,11 @@
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertEquals;

public class FactorizedDecimalEncodedValueTest {
public class UnsignedDecimalEncodedValueTest {

@Test
public void getDecimal() {
FactorizedDecimalEncodedValue testEnc = new FactorizedDecimalEncodedValue("test", 3, 1, 100, false);
UnsignedDecimalEncodedValue testEnc = new UnsignedDecimalEncodedValue("test", 3, 1, 100, false);
testEnc.init(new EncodedValue.InitializerConfig());

IntsRef intsRef = new IntsRef(1);
Expand All @@ -21,14 +21,14 @@ public void getDecimal() {
@Test
public void testDefault() {
// default value 100
FactorizedDecimalEncodedValue testEnc = new FactorizedDecimalEncodedValue("test", 3, 1, 100, false);
UnsignedDecimalEncodedValue testEnc = new UnsignedDecimalEncodedValue("test", 3, 1, 100, false);
testEnc.init(new EncodedValue.InitializerConfig());
IntsRef intsRef = new IntsRef(1);
testEnc.setDecimal(false, intsRef, 0);
assertEquals(100, testEnc.getDecimal(false, intsRef), .1);

// try positive infinity
testEnc = new FactorizedDecimalEncodedValue("test", 3, 1, Double.POSITIVE_INFINITY, false);
testEnc = new UnsignedDecimalEncodedValue("test", 3, 1, Double.POSITIVE_INFINITY, false);
testEnc.init(new EncodedValue.InitializerConfig());
testEnc.setDecimal(false, intsRef, 0);
assertTrue(Double.isInfinite(testEnc.getDecimal(false, intsRef)));
Expand Down

0 comments on commit a8c654b

Please sign in to comment.