Repository navigation
StringEncodedValues - #2204
StringEncodedValues#2204
Conversation
otbutz
commented
Dec 8, 2020
- minor code cleanup: add generic parameters to Enum references
- add StringEncodedValue which encodes Strings based on their index within a predefined list
- StringDetails for retrieval via PathDetails
|
Would it make sense to make this dynamic? I.e. only provide a maximum number of strings at construction time and feed strings as they come while import? This way we could easier introduce new tags. Hmmh, but it would also mean that a smaller area is fine and the larger area requires a higher number and breaks. |
I though a bit about dynamic approaches but encountered a few problems:
I dodged the problem in my custom area PR by providing two filter mechanisms(bbox+id) which should allow to conveniently limit the number of areas without having to deal with GeoJSON file itself. |
3ecd60b to
48267b3
Compare
|
Anything missing? |
If bits are full we could either throw an exception or everything goes into a special "OTHER" string (?)
Yes, it would be necessary to serialize this into the StorableProperties. But let us do this later in a separate issue (if required at all). Can you add an integration test where you collect e.g. "surface" or some other string tag via one of our test OSM PBFs? Because it might be useful to introduce some automatic normalization features like white space removal or lower case conversion etc? Hmmh, maybe we don't add such a test here and have a real integration test using the new Janino scripting (an important optimization there would be to do the comparison based on the index not on the String). |
From my custom area PR: custom_area_files:
- location: core/files/spatialrules/countries.geojson
id_field: "ISO3166-1:alpha3"
encoded_value: country
filter: deu,aut
max_bbox: -180,180,-90,90Would you add an extra option to define the maximum number of encoded entries?
I fear that this might cause erratic behavior and problems which are hard to reproduce. e.g if we encode countries and reach our maximum number of entries, the resulting EV would completely depend on the order of the processed ways
Don't we store the toString representation of the EncodedValues? Maybe we should extend this to a load/store mechanic.
The first usecase would be custom areas and i think it's ok to expect the IDs in the GeoJSON to be already normalized.
Not sure if feasible but you could lookup the String within the EncodedValue array once and rely on reference checks later on if you want to avoid String.equals() |
|
We would also need to switch to a hashmap based approach as the Strings passed in via setString() are unordered, so we can't rely on binary search. |
|
Something like this? public final class StringEncodedValue extends UnsignedIntEncodedValue {
private final List<String> values;
private final ObjectIntMap<String> indexMap;
private final int maxEntries;
public StringEncodedValue(String name, int maxEntries) {
this(name, maxEntries, false);
}
public StringEncodedValue(String name, int maxEntries, List<String> values, boolean storeTwoDirections) {
super(name, 32 - Integer.numberOfLeadingZeros(maxEntries), storeTwoDirections);
this.values = new ArrayList<>(values);
this.indexMap = new ObjectIntHashMap<>(values.size());
int index = 1;
for (String value : values) {
indexMap.put(value, index++);
}
this.maxEntries = maxEntries;
}
public StringEncodedValue(String name, int maxEntries, boolean storeTwoDirections) {
super(name, 32 - Integer.numberOfLeadingZeros(maxEntries), storeTwoDirections);
this.values = new ArrayList<>(maxEntries);
this.indexMap = new ObjectIntHashMap<>(maxEntries);
this.maxEntries = maxEntries;
}
public final void setString(boolean reverse, IntsRef ref, String value) {
int index = indexMap.get(value);
if (index == 0) {
if (values.size() == maxEntries) {
throw new IllegalStateException("Maximum number of entries reached for " + getName() + ": " + maxEntries);
}
values.add(value);
index = values.size();
indexMap.put(value, index);
}
super.setInt(reverse, ref, index);
}
public final String getString(boolean reverse, IntsRef ref) {
int value = super.getInt(reverse, ref);
if (value == 0) {
return null;
}
return values.get(value);
}
public List<String> getValues() {
return Collections.unmodifiableList(values);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof StringEncodedValue)) {
return false;
}
StringEncodedValue other = (StringEncodedValue) obj;
return Objects.equals(values, other.values);
}
@Override
public int getVersion() {
return 31 * super.getVersion() + staticHashCode(values);
}
} |
|
Yes, thanks :) It might also be that for small arrays a simple List is faster for querying. An additional indexOf method would be nice, so we can easier get the int (for fast usage inside Weighting.calc) |
332360a to
05716bc
Compare
05716bc to
b4b0197
Compare
|
Changed my implementation and added a few unittests |