addRouteEdge function

RouteGraphFile addRouteEdge(
  1. RouteGraphFile g,
  2. String from,
  3. String to,
  4. String ts,
)

Record a transition.

Implementation

RouteGraphFile addRouteEdge(
  RouteGraphFile g,
  String from,
  String to,
  String ts,
) {
  final f = from.trim();
  final t = to.trim();
  if (f.isEmpty || t.isEmpty || f == t) return g;

  final edges = List<RouteGraphEdge>.of(g.edges);
  final i = edges.indexWhere((e) => e.from == f && e.to == t);
  if (i >= 0) {
    edges[i] = edges[i].copyWith(count: edges[i].count + 1, lastSeen: ts);
  } else {
    edges.add(RouteGraphEdge(
      from: f,
      to: t,
      count: 1,
      firstSeen: ts,
      lastSeen: ts,
    ));
  }
  return RouteGraphFile(
    schemaVersion: g.schemaVersion,
    type: g.type,
    nodes: g.nodes,
    edges: edges,
    sync: g.sync.markDirty(ts),
  );
}