private static TechnicalIndex LCA(TechnicalIndex u, TechnicalIndex v) { int du = u.Depth; int dv = v.Depth; while (du < dv) { v = v.Source; dv--; } while (dv < du) { u = u.Source; du--; } while (u != v) { v = v.Source; u = u.Source; } return(u); }
public TechnicalIndex Bind(TechnicalIndex index, int time = 1) { if (Source != null) { if (Source.Root != index.Root) { throw new Exception("LCA invalid"); } var lca = LCA(Source, index); // equivalent source // rebind Source.Update -= OnSourceOnUpdate; lca.Update += OnSourceOnUpdate; // re-assign member TimeScalar = Math.Max(RelativeTime(Source, lca) * TimeScalar, RelativeTime(index, lca) * time); Source = lca; } else { Source = index; TimeScalar = time; Source.Update += OnSourceOnUpdate; } return(this); }
private static int RelativeTime(TechnicalIndex thisIndex, TechnicalIndex thatIndex) => thisIndex.Time / thatIndex.Time;