@Test public void testParseSparse() { SparseVector vec1 = VectorUtil.parseSparse("0:1 2:-3"); SparseVector vec3 = VectorUtil.parseSparse("$4$0:1 2:-3"); SparseVector vec4 = VectorUtil.parseSparse("$4$"); SparseVector vec5 = VectorUtil.parseSparse(""); Assert.assertEquals(vec1.get(0), 1., 0.); Assert.assertEquals(vec1.get(2), -3., 0.); Assert.assertArrayEquals(vec3.toDenseVector().getData(), new double[]{1, 0, -3, 0}, 0); Assert.assertEquals(vec3.size(), 4); Assert.assertArrayEquals(vec4.toDenseVector().getData(), new double[]{0, 0, 0, 0}, 0); Assert.assertEquals(vec4.size(), 4); Assert.assertEquals(vec5.size(), -1); }
public static SparseVector parseSparse(String str) { try { if (org.apache.flink.util.StringUtils.isNullOrWhitespaceOnly(str)) { return new SparseVector(); } int n = -1; int firstDollarPos = str.indexOf(HEADER_DELIMITER); int lastDollarPos = -1; if (firstDollarPos >= 0) { lastDollarPos = StringUtils.lastIndexOf(str, HEADER_DELIMITER); String sizeStr = StringUtils.substring(str, firstDollarPos + 1, lastDollarPos); n = Integer.valueOf(sizeStr); if (lastDollarPos == str.length() - 1) { return new SparseVector(n); } } int numValues = StringUtils.countMatches(str, String.valueOf(INDEX_VALUE_DELIMITER)); double[] data = new double[numValues]; int[] indices = new int[numValues]; int startPos = lastDollarPos + 1; int endPos; for (int i = 0; i < numValues; i++) { int colonPos = StringUtils.indexOf(str, INDEX_VALUE_DELIMITER, startPos); if (colonPos < 0) { throw new IllegalArgumentException("Format error."); } endPos = StringUtils.indexOf(str, ELEMENT_DELIMITER, colonPos); if (endPos == -1) { endPos = StringUtils.indexOf(str, ",", colonPos); } if (endPos == -1) { endPos = str.length(); } indices[i] = Integer.valueOf(str.substring(startPos, colonPos).trim()); data[i] = Double.valueOf(str.substring(colonPos + 1, endPos).trim()); startPos = endPos + 1; } return new SparseVector(n, indices, data); } catch (Exception e) { throw new IllegalArgumentException( String.format("Fail to getVector sparse vector from string: \"%s\".", str), e); } }
VectorUtil { public static SparseVector parseSparse(String str) { try { if (org.apache.flink.util.StringUtils.isNullOrWhitespaceOnly(str)) { return new SparseVector(); } int n = -1; int firstDollarPos = str.indexOf(HEADER_DELIMITER); int lastDollarPos = -1; if (firstDollarPos >= 0) { lastDollarPos = StringUtils.lastIndexOf(str, HEADER_DELIMITER); String sizeStr = StringUtils.substring(str, firstDollarPos + 1, lastDollarPos); n = Integer.valueOf(sizeStr); if (lastDollarPos == str.length() - 1) { return new SparseVector(n); } } int numValues = StringUtils.countMatches(str, String.valueOf(INDEX_VALUE_DELIMITER)); double[] data = new double[numValues]; int[] indices = new int[numValues]; int startPos = lastDollarPos + 1; int endPos; for (int i = 0; i < numValues; i++) { int colonPos = StringUtils.indexOf(str, INDEX_VALUE_DELIMITER, startPos); if (colonPos < 0) { throw new IllegalArgumentException("Format error."); } endPos = StringUtils.indexOf(str, ELEMENT_DELIMITER, colonPos); if (endPos == -1) { endPos = StringUtils.indexOf(str, ",", colonPos); } if (endPos == -1) { endPos = str.length(); } indices[i] = Integer.valueOf(str.substring(startPos, colonPos).trim()); data[i] = Double.valueOf(str.substring(colonPos + 1, endPos).trim()); startPos = endPos + 1; } return new SparseVector(n, indices, data); } catch (Exception e) { throw new IllegalArgumentException( String.format("Fail to getVector sparse vector from string: \"%s\".", str), e); } } }
VectorUtil { public static SparseVector parseSparse(String str) { try { if (org.apache.flink.util.StringUtils.isNullOrWhitespaceOnly(str)) { return new SparseVector(); } int n = -1; int firstDollarPos = str.indexOf(HEADER_DELIMITER); int lastDollarPos = -1; if (firstDollarPos >= 0) { lastDollarPos = StringUtils.lastIndexOf(str, HEADER_DELIMITER); String sizeStr = StringUtils.substring(str, firstDollarPos + 1, lastDollarPos); n = Integer.valueOf(sizeStr); if (lastDollarPos == str.length() - 1) { return new SparseVector(n); } } int numValues = StringUtils.countMatches(str, String.valueOf(INDEX_VALUE_DELIMITER)); double[] data = new double[numValues]; int[] indices = new int[numValues]; int startPos = lastDollarPos + 1; int endPos; for (int i = 0; i < numValues; i++) { int colonPos = StringUtils.indexOf(str, INDEX_VALUE_DELIMITER, startPos); if (colonPos < 0) { throw new IllegalArgumentException("Format error."); } endPos = StringUtils.indexOf(str, ELEMENT_DELIMITER, colonPos); if (endPos == -1) { endPos = StringUtils.indexOf(str, ",", colonPos); } if (endPos == -1) { endPos = str.length(); } indices[i] = Integer.valueOf(str.substring(startPos, colonPos).trim()); data[i] = Double.valueOf(str.substring(colonPos + 1, endPos).trim()); startPos = endPos + 1; } return new SparseVector(n, indices, data); } catch (Exception e) { throw new IllegalArgumentException( String.format("Fail to getVector sparse vector from string: \"%s\".", str), e); } }  }
VectorUtil { public static SparseVector parseSparse(String str) { try { if (org.apache.flink.util.StringUtils.isNullOrWhitespaceOnly(str)) { return new SparseVector(); } int n = -1; int firstDollarPos = str.indexOf(HEADER_DELIMITER); int lastDollarPos = -1; if (firstDollarPos >= 0) { lastDollarPos = StringUtils.lastIndexOf(str, HEADER_DELIMITER); String sizeStr = StringUtils.substring(str, firstDollarPos + 1, lastDollarPos); n = Integer.valueOf(sizeStr); if (lastDollarPos == str.length() - 1) { return new SparseVector(n); } } int numValues = StringUtils.countMatches(str, String.valueOf(INDEX_VALUE_DELIMITER)); double[] data = new double[numValues]; int[] indices = new int[numValues]; int startPos = lastDollarPos + 1; int endPos; for (int i = 0; i < numValues; i++) { int colonPos = StringUtils.indexOf(str, INDEX_VALUE_DELIMITER, startPos); if (colonPos < 0) { throw new IllegalArgumentException("Format error."); } endPos = StringUtils.indexOf(str, ELEMENT_DELIMITER, colonPos); if (endPos == -1) { endPos = StringUtils.indexOf(str, ",", colonPos); } if (endPos == -1) { endPos = str.length(); } indices[i] = Integer.valueOf(str.substring(startPos, colonPos).trim()); data[i] = Double.valueOf(str.substring(colonPos + 1, endPos).trim()); startPos = endPos + 1; } return new SparseVector(n, indices, data); } catch (Exception e) { throw new IllegalArgumentException( String.format("Fail to getVector sparse vector from string: \"%s\".", str), e); } }  static DenseVector parseDense(String str); static SparseVector parseSparse(String str); static String toString(Vector vector); static String toString(SparseVector sparseVector); static String toString(DenseVector denseVector); static Vector getVector(Object obj); static DenseVector getDenseVector(Object obj); static SparseVector getSparseVector(Object obj); }
VectorUtil { public static SparseVector parseSparse(String str) { try { if (org.apache.flink.util.StringUtils.isNullOrWhitespaceOnly(str)) { return new SparseVector(); } int n = -1; int firstDollarPos = str.indexOf(HEADER_DELIMITER); int lastDollarPos = -1; if (firstDollarPos >= 0) { lastDollarPos = StringUtils.lastIndexOf(str, HEADER_DELIMITER); String sizeStr = StringUtils.substring(str, firstDollarPos + 1, lastDollarPos); n = Integer.valueOf(sizeStr); if (lastDollarPos == str.length() - 1) { return new SparseVector(n); } } int numValues = StringUtils.countMatches(str, String.valueOf(INDEX_VALUE_DELIMITER)); double[] data = new double[numValues]; int[] indices = new int[numValues]; int startPos = lastDollarPos + 1; int endPos; for (int i = 0; i < numValues; i++) { int colonPos = StringUtils.indexOf(str, INDEX_VALUE_DELIMITER, startPos); if (colonPos < 0) { throw new IllegalArgumentException("Format error."); } endPos = StringUtils.indexOf(str, ELEMENT_DELIMITER, colonPos); if (endPos == -1) { endPos = StringUtils.indexOf(str, ",", colonPos); } if (endPos == -1) { endPos = str.length(); } indices[i] = Integer.valueOf(str.substring(startPos, colonPos).trim()); data[i] = Double.valueOf(str.substring(colonPos + 1, endPos).trim()); startPos = endPos + 1; } return new SparseVector(n, indices, data); } catch (Exception e) { throw new IllegalArgumentException( String.format("Fail to getVector sparse vector from string: \"%s\".", str), e); } }  static DenseVector parseDense(String str); static SparseVector parseSparse(String str); static String toString(Vector vector); static String toString(SparseVector sparseVector); static String toString(DenseVector denseVector); static Vector getVector(Object obj); static DenseVector getDenseVector(Object obj); static SparseVector getSparseVector(Object obj);  }