From 6ca6027167209d2a008a565a2b51890aebb52959 Mon Sep 17 00:00:00 2001 From: Enkidu93 Date: Fri, 15 Nov 2024 12:46:08 -0500 Subject: [PATCH] Move same ref rows out of range info --- .../Corpora/NParallelTextCorpus.cs | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/src/SIL.Machine/Corpora/NParallelTextCorpus.cs b/src/SIL.Machine/Corpora/NParallelTextCorpus.cs index 9b5bf777..899a9cf9 100644 --- a/src/SIL.Machine/Corpora/NParallelTextCorpus.cs +++ b/src/SIL.Machine/Corpora/NParallelTextCorpus.cs @@ -105,6 +105,11 @@ private IEnumerable GetRows(IList> enumer Versifications = Corpora.Select(c => c.Versification).ToArray(), RowRefComparer = RowRefComparer }; + List> sameRefRows = new List>(); + for (int i = 0; i < N; i++) + { + sameRefRows.Add(new List()); + } bool[] completed = new bool[N]; int numCompleted = 0; @@ -162,7 +167,7 @@ private IEnumerable GetRows(IList> enumer foreach (int i in minRefIndexes) rangeInfo.AddTextRow(enumerators[i].Current, i); foreach (int i in nonMinRefIndexes) - rangeInfo.Rows[i].SameRefRows.Clear(); + sameRefRows[i].Clear(); } else { @@ -175,6 +180,7 @@ NParallelTextRow row in CreateMinRefRows( currentRows.ToArray(), minRefIndexes.ToArray(), nonMinRefIndexes.ToArray(), + sameRefRows, forceInRange: minRefIndexes .Select(i => anyNonMinEnumeratorsMidRange @@ -191,7 +197,7 @@ NParallelTextRow row in CreateMinRefRows( } foreach (int i in minRefIndexes) { - rangeInfo.Rows[i].SameRefRows.Add(enumerators[i].Current); + sameRefRows[i].Add(enumerators[i].Current); bool isCompleted = !enumerators[i].MoveNext(); completed[i] = isCompleted; if (isCompleted) @@ -221,12 +227,14 @@ NParallelTextRow row in CreateMinRefRows( for (int i = 0; i < rangeInfo.Rows.Count; i++) { rangeInfo.AddTextRow(currentRows[i], i); - rangeInfo.Rows[i].SameRefRows.Clear(); + sameRefRows[i].Clear(); } } else { - foreach (NParallelTextRow row in CreateSameRefRows(rangeInfo, completed, currentRows)) + foreach ( + NParallelTextRow row in CreateSameRefRows(rangeInfo, completed, currentRows, sameRefRows) + ) { yield return row; } @@ -244,7 +252,7 @@ NParallelTextRow row in CreateRows( for (int i = 0; i < rangeInfo.Rows.Count; i++) { - rangeInfo.Rows[i].SameRefRows.Add(currentRows[i]); + sameRefRows[i].Add(currentRows[i]); bool isCompleted = !enumerators[i].MoveNext(); completed[i] = isCompleted; if (isCompleted) @@ -323,21 +331,18 @@ private IEnumerable CreateMinRefRows( IReadOnlyList currentRows, IReadOnlyList minRefIndexes, IReadOnlyList nonMinRefIndexes, + IReadOnlyList> sameRefRowsPerIndex, IReadOnlyList forceInRange = null ) { - List<(List Rows, int Index)> sameRefRowsPerIndex = nonMinRefIndexes - .Select(i => (rangeInfo.Rows[i], i)) - .Select(pair => (pair.Item1.SameRefRows.ToList(), pair.Item2)) - .ToList(); - HashSet alreadyYielded = new HashSet(); TextRow[] textRows; foreach (int i in minRefIndexes) { TextRow textRow = currentRows[i]; - foreach ((List sameRefRows, int j) in sameRefRowsPerIndex) + foreach (int j in nonMinRefIndexes) { + IList sameRefRows = sameRefRowsPerIndex[j]; if (CheckSameRefRows(sameRefRows, textRow)) { alreadyYielded.Add(i); @@ -392,22 +397,23 @@ private bool CheckSameRefRows(IList sameRefRows, TextRow otherRow) private IEnumerable CreateSameRefRows( NRangeInfo rangeInfo, IList completed, - IList currentRows + IList currentRows, + IReadOnlyList> sameRefRows ) { - for (int i = 0; i < rangeInfo.Rows.Count; i++) + for (int i = 0; i < N; i++) { if (completed[i]) continue; - for (int j = 0; j < rangeInfo.Rows.Count; j++) + for (int j = 0; j < N; j++) { if (i == j || completed[j]) continue; - if (CheckSameRefRows(rangeInfo.Rows[i].SameRefRows, currentRows[j])) + if (CheckSameRefRows(sameRefRows[i], currentRows[j])) { - foreach (TextRow tr in rangeInfo.Rows[i].SameRefRows) + foreach (TextRow tr in sameRefRows[i]) { var textRows = new TextRow[N]; textRows[i] = tr; @@ -426,7 +432,6 @@ private class RangeRow { public IList Refs { get; } = new List(); public IList Segment { get; } = new List(); - public IList SameRefRows { get; } = new List(); public bool IsSentenceStart { get; set; } = false; public bool IsInRange => Refs.Count > 0; public bool IsEmpty => Segment.Count == 0;