var path = require('path');
var save = require('ngraph.tobinary');
var createLayout = require('hlayout');
var fs = require('fs');
var fileName = path.join(__dirname, '..', 'byField');

console.log('Parsing graph file...');
require('../lib/loadGraph.js')(performLayout, fileName);

function performLayout(graph) {
  console.log('Graph parsed. Found ' + graph.getNodesCount() + ' nodes and ' + graph.getLinksCount() + ' edges');
  var layout = createLayout(graph);
  layout.run();

  console.log('Layout completed. Saving to binary format');

  saveIteration('positions2d');
  save(graph, { outDir: './data' });
  console.log('Done.');

  function saveIteration(name) {
    var fname = path.join('data', name + '.bin');

    console.log('Saving: ', fname);
    var nodesLength = graph.getNodesCount();
    var buf = new Buffer(nodesLength * 4 * 3);
    var i = 0;
    var missed = 0;

    graph.forEachNode(function(node) {
      var idx = i * 4 * 3;
      var pos = layout.getNodePosition(node.id);
      var size = 0;
      var links = graph.getLinks(node.id);
      if (links) {
        links.forEach(function(link) {
          if (link.toId === node.id) size += 1;
        });
      }

      if (!pos) {
        missed += 1;
        console.log('missing position for ' + node.id);
        pos = {x : 0, y: 0};
      }

      buf.writeInt32LE(pos.x, idx);
      buf.writeInt32LE(pos.y, idx + 4);
      buf.writeInt32LE(size, idx + 8);
      i++;
    });

    fs.writeFileSync(fname, buf);
    console.log('missed: ', missed);
  }
}


@@ -534,16 +534,97 @@
 mat');%0A%0A
+  var topLevelGroups = layout.getGroupsAtLevel(0);%0A  saveGroups(topLevelGroups);%0A
   saveIt
@@ -709,16 +709,532 @@
 ne.');%0A%0A
+  function saveGroups(topLevelGroups) %7B%0A    var fname = path.join('data', 'groups.bin');%0A    var nodesLength = graph.getNodesCount();%0A    var buf = new Buffer(nodesLength * 2);%0A    var max = Math.pow(2, 16) - 1;%0A    var i = 0;%0A%0A    graph.forEachNode(function(node) %7B%0A      var idx = i * 2;%0A      var groupId = topLevelGroups%5Bnode.id%5D;%0A      if (groupId %3E= max) %7B%0A        throw new Error('Too many groups');%0A      %7D%0A%0A      buf.writeInt16LE(groupId, idx);%0A      i += 1;%0A    %7D);%0A%0A    fs.writeFileSync(fname, buf);%0A  %7D%0A%0A
   functi

Save top level groups instead of hierarchy