diff options
author | Jon Bratseth <bratseth@oath.com> | 2018-11-25 20:07:56 +0100 |
---|---|---|
committer | Jon Bratseth <bratseth@oath.com> | 2018-11-25 20:07:56 +0100 |
commit | 1d88554bd513783715425120e76fc5f2a86f439f (patch) | |
tree | 166c86107d3620014cc7e26d85118c311e1b8cf0 /vespajlib | |
parent | a01bc21d9bcbc417a9fb2591079561f59f76865e (diff) |
Java type only interface between imported-models and config models
This avoids class incompatibility problems when passing an
imported model across bundle boundaries to a config model.
Tensor string parsing has been sped up as this relies on it more.
Diffstat (limited to 'vespajlib')
3 files changed, 24 insertions, 15 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/Tensor.java b/vespajlib/src/main/java/com/yahoo/tensor/Tensor.java index 483ccd330e0..1ee22c69c23 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/Tensor.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/Tensor.java @@ -230,7 +230,7 @@ public interface Tensor { * @return the tensor on the standard string format */ static String toStandardString(Tensor tensor) { - if (tensor.isEmpty() && ! tensor.type().dimensions().isEmpty()) // explicitly output type TODO: Never do that? + if (tensor.isEmpty() && ! tensor.type().dimensions().isEmpty()) // explicitly output type TODO: Always do that return tensor.type() + ":" + contentToString(tensor); else return contentToString(tensor); diff --git a/vespajlib/src/main/java/com/yahoo/tensor/TensorParser.java b/vespajlib/src/main/java/com/yahoo/tensor/TensorParser.java index 000f33696f2..fa32d385004 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/TensorParser.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/TensorParser.java @@ -76,32 +76,41 @@ class TensorParser { } private static Tensor fromCellString(Tensor.Builder builder, String s) { - s = s.trim().substring(1).trim(); - while (s.length() > 1) { - int keyOrTensorEnd = s.indexOf('}'); + int index = 1; + index = skipSpace(index, s); + while (index + 1 < s.length()) { + int keyOrTensorEnd = s.indexOf('}', index); TensorAddress.Builder addressBuilder = new TensorAddress.Builder(builder.type()); - if (keyOrTensorEnd < s.length() - 1) { // Key end: This has a key - otherwise TensorAdress is empty - addLabels(s.substring(0, keyOrTensorEnd + 1), addressBuilder); - s = s.substring(keyOrTensorEnd + 1).trim(); - if ( ! s.startsWith(":")) - throw new IllegalArgumentException("Expecting a ':' after " + s + ", got '" + s + "'"); - s = s.substring(1); + if (keyOrTensorEnd < s.length() - 1) { // Key end: This has a key - otherwise TensorAddress is empty + addLabels(s.substring(index, keyOrTensorEnd + 1), addressBuilder); + index = keyOrTensorEnd + 1; + index = skipSpace(index, s); + if ( s.charAt(index) != ':') + throw new IllegalArgumentException("Expecting a ':' after " + s.substring(index) + ", got '" + s + "'"); + index++; } - int valueEnd = s.indexOf(','); + int valueEnd = s.indexOf(',', index); if (valueEnd < 0) { // last value - valueEnd = s.indexOf("}"); + valueEnd = s.indexOf('}', index); if (valueEnd < 0) throw new IllegalArgumentException("A tensor string must end by '}'"); } TensorAddress address = addressBuilder.build(); - Double value = asDouble(address, s.substring(0, valueEnd).trim()); + Double value = asDouble(address, s.substring(index, valueEnd).trim()); builder.cell(address, value); - s = s.substring(valueEnd+1).trim(); + index = valueEnd+1; + index = skipSpace(index, s); } return builder.build(); } + private static int skipSpace(int index, String s) { + while (index < s.length() && s.charAt(index) == ' ') + index++; + return index; + } + /** Creates a tenor address from a string on the form {dimension1:label1,dimension2:label2,...} */ private static void addLabels(String mapAddressString, TensorAddress.Builder builder) { mapAddressString = mapAddressString.trim(); diff --git a/vespajlib/src/test/java/com/yahoo/tensor/TensorTestCase.java b/vespajlib/src/test/java/com/yahoo/tensor/TensorTestCase.java index 38a8329bff1..122b6019884 100644 --- a/vespajlib/src/test/java/com/yahoo/tensor/TensorTestCase.java +++ b/vespajlib/src/test/java/com/yahoo/tensor/TensorTestCase.java @@ -101,7 +101,7 @@ public class TensorTestCase { " {x:0,y:0,z:1}:1, {x:0,y:1,z:1}:2, {x:1,y:0,z:1}:2, {x:1,y:1,z:1}:3, {x:2,y:0,z:1}:3, {x:2,y:1,z:1}:4 }"), Tensor.range(new TensorType.Builder().indexed("x", 3).indexed("y", 2).indexed("z", 2).build())); assertEquals(Tensor.from("{ {x:0,y:0,z:0}:1, {x:0,y:1,z:0}:0, {x:1,y:0,z:0}:0, {x:1,y:1,z:0}:0, {x:2,y:0,z:0}:0, {x:2,y:1,z:0}:0, "+ - " {x:0,y:0,z:1}:0, {x:0,y:1,z:1}:0, {x:1,y:0,z:1}:0, {x:1,y:1,z:1}:1, {x:2,y:0,z:1}:0, {x:2,y:1,z:1}:00 }"), + " {x:0,y:0,z:1}:0, {x:0,y:1,z:1}:0, {x:1,y:0,z:1}:0, {x:1,y:1,z:1}:1, {x:2,y:0,z:1}:0, {x:2,y:1,z:1}:00 } "), Tensor.diag(new TensorType.Builder().indexed("x", 3).indexed("y", 2).indexed("z", 2).build())); assertEquals(Tensor.from("{ {x:1}:0, {x:3}:1, {x:9}:0 }"), Tensor.from("{ {x:1}:1, {x:3}:5, {x:9}:3 }").argmax("x")); } |