private static long loadWeightedGraph()

in src/main/java/com/twitter/sbf/graph/Graph.java [392:462]


  private static long loadWeightedGraph(SimpleIterator<String> lines, int[][] nbrs, float[][] wts,
                                       float[] wtedOutDegrees, long expectedNumEdges) {
    Optional<String> lineOpt;
    int nodeId = 0;
    long edgesSoFar = 0;
    assert nbrs != null && wts != null;
    while (true) {
      lineOpt = lines.next();
      if (!lineOpt.isPresent()) {
        break;
      } else {
        String line = lineOpt.get();
        if (line.startsWith("%")) {
          continue;
        }
        line = line.trim();
        // Fields are separated by spaces
        if (nodeId >= nbrs.length) {
          throw new RuntimeException("More adjacency lists in input file than specified on "
              + "first line (" + nbrs.length + ")");
        }
        if (line.isEmpty()) { // vertices with no neighbor
          nbrs[nodeId] = new int[0];
          wts[nodeId++] = new float[0];
        } else { // The rest are adjacency lists
          String[] tokens = line.split("\\s+");
          assert tokens.length % 2 == 0;
          int numNeighbors = tokens.length / 2;
          String[] combinedTokensForSorting = new String[numNeighbors];
          for (int i = 0; i < numNeighbors; i++) {
            combinedTokensForSorting[i] = tokens[2 * i] + " " + tokens[2 * i + 1];
          }

          int[] myNbrs = new int[numNeighbors];
          float[] myWts = new float[numNeighbors];
          wtedOutDegrees[nodeId] = 0;
          Arrays.sort(combinedTokensForSorting, ID_WEIGHT_STRING_COMPARATOR);
          for (int i = 0; i < numNeighbors; i++) {
            int nId = getIdFromIdWeightString(combinedTokensForSorting[i]) - 1; // 0-based indexing
            if (nId < 0) {
              throw new IllegalStateException(
                  String.format(
                      "Line %d: neighbor %d is less than 1 (all vertex ids are positive)\n",
                      nodeId + 1, nId + 1
                  )
              );
            }
            myNbrs[i] = nId;
            myWts[i] = getWeightFromIdWeightString(combinedTokensForSorting[i]);
            wtedOutDegrees[nodeId] += myWts[i];
          }
          nbrs[nodeId] = myNbrs;
          wts[nodeId++] = myWts;
          edgesSoFar += numNeighbors;

          if (nodeId > nbrs.length) {
            throw new RuntimeException("Expected number of nodes " + nbrs.length
                + " is inconsistent with number of adjacency lists");
          }
          if (nodeId % 100000 == 0) {
            System.err.print("\rDone loading " + nodeId + " lines.");
          }
        }
      }
    }
    if (edgesSoFar != expectedNumEdges * 2) {
      System.err.println("Expected " + 2 * expectedNumEdges
          + " (counting both directions), but got " + edgesSoFar + " many edges.");
    }
    return edgesSoFar / 2;
  }