coupling method

List<Coupling> coupling(
  1. int depth
)

Every pair of features at depth joined by at least one edge, with the number of imports from one into the other — heaviest first.

Implementation

List<Coupling> coupling(int depth) {
  final counts = <String, Map<String, int>>{};
  for (final entry in edges.entries) {
    final from = featureOf(entry.key, depth);
    if (from == null) continue;
    for (final target in entry.value) {
      final to = featureOf(target, depth);
      if (to == null || to == from) continue;
      final row = counts.putIfAbsent(from, () => {});
      row[to] = (row[to] ?? 0) + 1;
    }
  }
  return [
    for (final from in counts.entries)
      for (final to in from.value.entries)
        Coupling(from.key, to.key, to.value),
  ]..sort((a, b) {
      final byEdges = b.edges.compareTo(a.edges);
      if (byEdges != 0) return byEdges;
      final byFrom = a.from.compareTo(b.from);
      return byFrom != 0 ? byFrom : a.to.compareTo(b.to);
    });
}