* Takes a map from the unique common lines between two arrays and determines * the longest common subsequence. * * @see uniqueCommon * * @param abMap - The map of unique common lines between two arrays. * @returns An array of objects containing the indices of the longest common
(
abMap: Map<string, Subsequence>,
)
| 118 | * subsequence. |
| 119 | */ |
| 120 | function longestCommonSubsequence( |
| 121 | abMap: Map<string, Subsequence>, |
| 122 | ): Subsequence[] { |
| 123 | const jagged: [Subsequence][] = []; |
| 124 | |
| 125 | abMap.forEach(value => { |
| 126 | let i = 0; |
| 127 | while (jagged[i] && jagged[i].at(-1)!.bIndex < value.bIndex) { |
| 128 | i++; |
| 129 | } |
| 130 | |
| 131 | if (i > 0) { |
| 132 | value.prev = jagged[i - 1].at(-1); |
| 133 | } |
| 134 | |
| 135 | if (!jagged[i]) { |
| 136 | jagged[i] = [value]; |
| 137 | } else { |
| 138 | jagged[i].push(value); |
| 139 | } |
| 140 | }); |
| 141 | |
| 142 | // Pull out the longest common subsequence |
| 143 | let lcs: Subsequence[] = []; |
| 144 | |
| 145 | if (jagged.length > 0) { |
| 146 | lcs = [jagged.at(-1)!.at(-1)!]; |
| 147 | let cursor = lcs.at(-1); |
| 148 | while (cursor?.prev) { |
| 149 | cursor = cursor.prev; |
| 150 | lcs.push(cursor); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | return lcs.reverse(); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Keeps track of the aLines that have been deleted, are shared between aLines |