<commit_before>/**
 */

import { sum } from 'd3-array'
import assignRanks from './assignRanks/index.js'
import sortNodes from './sortNodes/index.js'
import { addDummyNodes, removeDummyNodes } from './sortNodes/dummy-nodes.js'
import nestGraph from './sankeyLayout/nest-graph.js'
import positionHorizontally from './sankeyLayout/horizontal.js'
import positionVertically from './sankeyLayout/verticalJustified.js'
import orderLinks from './sankeyLayout/link-ordering.js'
import layoutLinks from './sankeyLayout/layout-links.js'
import { buildGraph } from './util.js'

function defaultNodes (graph) {
  return graph.nodes
}

function defaultLinks (graph) {
  return graph.links
}

function defaultNodeId (d) {
  return d.id
}

function defaultNodeBackwards (d) {
  return d.direction && d.direction.toLowerCase() === 'l'
}

function defaultSourceId (d) {
  return typeof d.source === 'object' ? d.source.id : d.source
}

function defaultTargetId (d) {
  return typeof d.target === 'object' ? d.target.id : d.target
}

function defaultLinkType (d) {
  return d.type
}

export default function sankeyLayout () {
  var nodes = defaultNodes
  var links = defaultLinks
  var nodeId = defaultNodeId
  var nodeBackwards = defaultNodeBackwards
  var sourceId = defaultSourceId
  var targetId = defaultTargetId
  var linkType = defaultLinkType
  var ordering = null
  var rankSets = [] // XXX setter/getter
  var maxIterations = 25 // XXX setter/getter

  // extent
  var x0 = 0
  var y0 = 0
  var x1 = 1
  var y1 = 1

  var scale = null
  var linkValue = function (e) { return e.value }
  var whitespace = 0.5
  var verticalLayout = positionVertically()
  var alignLinkTypes = false

  function sankey () {
    var graph = {nodes: nodes.apply(null, arguments), links: links.apply(null, arguments)}
    var G = buildGraph(graph, nodeId, nodeBackwards, sourceId, targetId, linkType, linkValue)

    setNodeValues(G, linkValue)

    if (ordering !== null) {
      applyOrdering(G, ordering)
    } else {
      assignRanks(G, rankSets)
      sortNodes(G, maxIterations)
    }

    addDummyNodes(G)
    setNodeValues(G, linkValue)
    sortNodes(G, maxIterations)
    // XXX sort nodes?

    const nested = nestGraph(G.nodes().map(u => G.node(u)))
    maybeScaleToFit(G, nested)
    setWidths(G, scale)

    // position nodes
    verticalLayout(nested, y1 - y0, whitespace)
    positionHorizontally(G, x1 - x0)

    // adjust origin
    G.nodes().forEach(u => {
      const node = G.node(u)
      node.x += x0
      node.y += y0
    })

    // // sort & position links
    orderLinks(G, { alignLinkTypes: alignLinkTypes })
    layoutLinks(G)
<commit_msg>Only sort nodes if ordering not specficied<commit_after>/**
 */

import { sum } from 'd3-array'
import assignRanks from './assignRanks/index.js'
import sortNodes from './sortNodes/index.js'
import { addDummyNodes, removeDummyNodes } from './sortNodes/dummy-nodes.js'
import nestGraph from './sankeyLayout/nest-graph.js'
import positionHorizontally from './sankeyLayout/horizontal.js'
import positionVertically from './sankeyLayout/verticalJustified.js'
import orderLinks from './sankeyLayout/link-ordering.js'
import layoutLinks from './sankeyLayout/layout-links.js'
import { buildGraph } from './util.js'

function defaultNodes (graph) {
  return graph.nodes
}

function defaultLinks (graph) {
  return graph.links
}

function defaultNodeId (d) {
  return d.id
}

function defaultNodeBackwards (d) {
  return d.direction && d.direction.toLowerCase() === 'l'
}

function defaultSourceId (d) {
  return typeof d.source === 'object' ? d.source.id : d.source
}

function defaultTargetId (d) {
  return typeof d.target === 'object' ? d.target.id : d.target
}

function defaultLinkType (d) {
  return d.type
}

export default function sankeyLayout () {
  var nodes = defaultNodes
  var links = defaultLinks
  var nodeId = defaultNodeId
  var nodeBackwards = defaultNodeBackwards
  var sourceId = defaultSourceId
  var targetId = defaultTargetId
  var linkType = defaultLinkType
  var ordering = null
  var rankSets = [] // XXX setter/getter
  var maxIterations = 25 // XXX setter/getter

  // extent
  var x0 = 0
  var y0 = 0
  var x1 = 1
  var y1 = 1

  var scale = null
  var linkValue = function (e) { return e.value }
  var whitespace = 0.5
  var verticalLayout = positionVertically()
  var alignLinkTypes = false

  function sankey () {
    var graph = {nodes: nodes.apply(null, arguments), links: links.apply(null, arguments)}
    var G = buildGraph(graph, nodeId, nodeBackwards, sourceId, targetId, linkType, linkValue)

    setNodeValues(G, linkValue)

    if (ordering !== null) {
      applyOrdering(G, ordering)
    } else {
      assignRanks(G, rankSets)
      sortNodes(G, maxIterations)
    }

    addDummyNodes(G)
    setNodeValues(G, linkValue)
    if (ordering === null) {
      // XXX sort nodes?
      sortNodes(G, maxIterations)
    }

    const nested = nestGraph(G.nodes().map(u => G.node(u)))
    maybeScaleToFit(G, nested)
    setWidths(G, scale)

    // position nodes
    verticalLayout(nested, y1 - y0, whitespace)
    positionHorizontally(G, x1 - x0)

    // adjust origin
    G.nodes().forEach(u => {
      const node = G.node(u)
      node.x += x0
      node.y += y0
    })

    // // sort & position links
    orderLinks(G, { alignLinkTypes: alignLinkTypes })
    layoutLinks(G)

{'fraction_non_alphanumeric': 0.11549981280419319, 'fraction_numerical': 0.00711344065892924, 'mean_word_length': 3.3616326530612244, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5724511', 'n_tokens_mistral': 1742, 'n_tokens_neox': 1703, 'n_words': 465}