index.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = diffSequence;
  6. /**
  7. * Copyright (c) Meta Platforms, Inc. and affiliates.
  8. *
  9. * This source code is licensed under the MIT license found in the
  10. * LICENSE file in the root directory of this source tree.
  11. *
  12. */
  13. // This diff-sequences package implements the linear space variation in
  14. // An O(ND) Difference Algorithm and Its Variations by Eugene W. Myers
  15. // Relationship in notation between Myers paper and this package:
  16. // A is a
  17. // N is aLength, aEnd - aStart, and so on
  18. // x is aIndex, aFirst, aLast, and so on
  19. // B is b
  20. // M is bLength, bEnd - bStart, and so on
  21. // y is bIndex, bFirst, bLast, and so on
  22. // Δ = N - M is negative of baDeltaLength = bLength - aLength
  23. // D is d
  24. // k is kF
  25. // k + Δ is kF = kR - baDeltaLength
  26. // V is aIndexesF or aIndexesR (see comment below about Indexes type)
  27. // index intervals [1, N] and [1, M] are [0, aLength) and [0, bLength)
  28. // starting point in forward direction (0, 0) is (-1, -1)
  29. // starting point in reverse direction (N + 1, M + 1) is (aLength, bLength)
  30. // The “edit graph” for sequences a and b corresponds to items:
  31. // in a on the horizontal axis
  32. // in b on the vertical axis
  33. //
  34. // Given a-coordinate of a point in a diagonal, you can compute b-coordinate.
  35. //
  36. // Forward diagonals kF:
  37. // zero diagonal intersects top left corner
  38. // positive diagonals intersect top edge
  39. // negative diagonals insersect left edge
  40. //
  41. // Reverse diagonals kR:
  42. // zero diagonal intersects bottom right corner
  43. // positive diagonals intersect right edge
  44. // negative diagonals intersect bottom edge
  45. // The graph contains a directed acyclic graph of edges:
  46. // horizontal: delete an item from a
  47. // vertical: insert an item from b
  48. // diagonal: common item in a and b
  49. //
  50. // The algorithm solves dual problems in the graph analogy:
  51. // Find longest common subsequence: path with maximum number of diagonal edges
  52. // Find shortest edit script: path with minimum number of non-diagonal edges
  53. // Input callback function compares items at indexes in the sequences.
  54. // Output callback function receives the number of adjacent items
  55. // and starting indexes of each common subsequence.
  56. // Either original functions or wrapped to swap indexes if graph is transposed.
  57. // Indexes in sequence a of last point of forward or reverse paths in graph.
  58. // Myers algorithm indexes by diagonal k which for negative is bad deopt in V8.
  59. // This package indexes by iF and iR which are greater than or equal to zero.
  60. // and also updates the index arrays in place to cut memory in half.
  61. // kF = 2 * iF - d
  62. // kR = d - 2 * iR
  63. // Division of index intervals in sequences a and b at the middle change.
  64. // Invariant: intervals do not have common items at the start or end.
  65. const pkg = 'diff-sequences'; // for error messages
  66. const NOT_YET_SET = 0; // small int instead of undefined to avoid deopt in V8
  67. // Return the number of common items that follow in forward direction.
  68. // The length of what Myers paper calls a “snake” in a forward path.
  69. const countCommonItemsF = (aIndex, aEnd, bIndex, bEnd, isCommon) => {
  70. let nCommon = 0;
  71. while (aIndex < aEnd && bIndex < bEnd && isCommon(aIndex, bIndex)) {
  72. aIndex += 1;
  73. bIndex += 1;
  74. nCommon += 1;
  75. }
  76. return nCommon;
  77. };
  78. // Return the number of common items that precede in reverse direction.
  79. // The length of what Myers paper calls a “snake” in a reverse path.
  80. const countCommonItemsR = (aStart, aIndex, bStart, bIndex, isCommon) => {
  81. let nCommon = 0;
  82. while (aStart <= aIndex && bStart <= bIndex && isCommon(aIndex, bIndex)) {
  83. aIndex -= 1;
  84. bIndex -= 1;
  85. nCommon += 1;
  86. }
  87. return nCommon;
  88. };
  89. // A simple function to extend forward paths from (d - 1) to d changes
  90. // when forward and reverse paths cannot yet overlap.
  91. const extendPathsF = (
  92. d,
  93. aEnd,
  94. bEnd,
  95. bF,
  96. isCommon,
  97. aIndexesF,
  98. iMaxF // return the value because optimization might decrease it
  99. ) => {
  100. // Unroll the first iteration.
  101. let iF = 0;
  102. let kF = -d; // kF = 2 * iF - d
  103. let aFirst = aIndexesF[iF]; // in first iteration always insert
  104. let aIndexPrev1 = aFirst; // prev value of [iF - 1] in next iteration
  105. aIndexesF[iF] += countCommonItemsF(
  106. aFirst + 1,
  107. aEnd,
  108. bF + aFirst - kF + 1,
  109. bEnd,
  110. isCommon
  111. );
  112. // Optimization: skip diagonals in which paths cannot ever overlap.
  113. const nF = d < iMaxF ? d : iMaxF;
  114. // The diagonals kF are odd when d is odd and even when d is even.
  115. for (iF += 1, kF += 2; iF <= nF; iF += 1, kF += 2) {
  116. // To get first point of path segment, move one change in forward direction
  117. // from last point of previous path segment in an adjacent diagonal.
  118. // In last possible iteration when iF === d and kF === d always delete.
  119. if (iF !== d && aIndexPrev1 < aIndexesF[iF]) {
  120. aFirst = aIndexesF[iF]; // vertical to insert from b
  121. } else {
  122. aFirst = aIndexPrev1 + 1; // horizontal to delete from a
  123. if (aEnd <= aFirst) {
  124. // Optimization: delete moved past right of graph.
  125. return iF - 1;
  126. }
  127. }
  128. // To get last point of path segment, move along diagonal of common items.
  129. aIndexPrev1 = aIndexesF[iF];
  130. aIndexesF[iF] =
  131. aFirst +
  132. countCommonItemsF(aFirst + 1, aEnd, bF + aFirst - kF + 1, bEnd, isCommon);
  133. }
  134. return iMaxF;
  135. };
  136. // A simple function to extend reverse paths from (d - 1) to d changes
  137. // when reverse and forward paths cannot yet overlap.
  138. const extendPathsR = (
  139. d,
  140. aStart,
  141. bStart,
  142. bR,
  143. isCommon,
  144. aIndexesR,
  145. iMaxR // return the value because optimization might decrease it
  146. ) => {
  147. // Unroll the first iteration.
  148. let iR = 0;
  149. let kR = d; // kR = d - 2 * iR
  150. let aFirst = aIndexesR[iR]; // in first iteration always insert
  151. let aIndexPrev1 = aFirst; // prev value of [iR - 1] in next iteration
  152. aIndexesR[iR] -= countCommonItemsR(
  153. aStart,
  154. aFirst - 1,
  155. bStart,
  156. bR + aFirst - kR - 1,
  157. isCommon
  158. );
  159. // Optimization: skip diagonals in which paths cannot ever overlap.
  160. const nR = d < iMaxR ? d : iMaxR;
  161. // The diagonals kR are odd when d is odd and even when d is even.
  162. for (iR += 1, kR -= 2; iR <= nR; iR += 1, kR -= 2) {
  163. // To get first point of path segment, move one change in reverse direction
  164. // from last point of previous path segment in an adjacent diagonal.
  165. // In last possible iteration when iR === d and kR === -d always delete.
  166. if (iR !== d && aIndexesR[iR] < aIndexPrev1) {
  167. aFirst = aIndexesR[iR]; // vertical to insert from b
  168. } else {
  169. aFirst = aIndexPrev1 - 1; // horizontal to delete from a
  170. if (aFirst < aStart) {
  171. // Optimization: delete moved past left of graph.
  172. return iR - 1;
  173. }
  174. }
  175. // To get last point of path segment, move along diagonal of common items.
  176. aIndexPrev1 = aIndexesR[iR];
  177. aIndexesR[iR] =
  178. aFirst -
  179. countCommonItemsR(
  180. aStart,
  181. aFirst - 1,
  182. bStart,
  183. bR + aFirst - kR - 1,
  184. isCommon
  185. );
  186. }
  187. return iMaxR;
  188. };
  189. // A complete function to extend forward paths from (d - 1) to d changes.
  190. // Return true if a path overlaps reverse path of (d - 1) changes in its diagonal.
  191. const extendOverlappablePathsF = (
  192. d,
  193. aStart,
  194. aEnd,
  195. bStart,
  196. bEnd,
  197. isCommon,
  198. aIndexesF,
  199. iMaxF,
  200. aIndexesR,
  201. iMaxR,
  202. division // update prop values if return true
  203. ) => {
  204. const bF = bStart - aStart; // bIndex = bF + aIndex - kF
  205. const aLength = aEnd - aStart;
  206. const bLength = bEnd - bStart;
  207. const baDeltaLength = bLength - aLength; // kF = kR - baDeltaLength
  208. // Range of diagonals in which forward and reverse paths might overlap.
  209. const kMinOverlapF = -baDeltaLength - (d - 1); // -(d - 1) <= kR
  210. const kMaxOverlapF = -baDeltaLength + (d - 1); // kR <= (d - 1)
  211. let aIndexPrev1 = NOT_YET_SET; // prev value of [iF - 1] in next iteration
  212. // Optimization: skip diagonals in which paths cannot ever overlap.
  213. const nF = d < iMaxF ? d : iMaxF;
  214. // The diagonals kF = 2 * iF - d are odd when d is odd and even when d is even.
  215. for (let iF = 0, kF = -d; iF <= nF; iF += 1, kF += 2) {
  216. // To get first point of path segment, move one change in forward direction
  217. // from last point of previous path segment in an adjacent diagonal.
  218. // In first iteration when iF === 0 and kF === -d always insert.
  219. // In last possible iteration when iF === d and kF === d always delete.
  220. const insert = iF === 0 || (iF !== d && aIndexPrev1 < aIndexesF[iF]);
  221. const aLastPrev = insert ? aIndexesF[iF] : aIndexPrev1;
  222. const aFirst = insert
  223. ? aLastPrev // vertical to insert from b
  224. : aLastPrev + 1; // horizontal to delete from a
  225. // To get last point of path segment, move along diagonal of common items.
  226. const bFirst = bF + aFirst - kF;
  227. const nCommonF = countCommonItemsF(
  228. aFirst + 1,
  229. aEnd,
  230. bFirst + 1,
  231. bEnd,
  232. isCommon
  233. );
  234. const aLast = aFirst + nCommonF;
  235. aIndexPrev1 = aIndexesF[iF];
  236. aIndexesF[iF] = aLast;
  237. if (kMinOverlapF <= kF && kF <= kMaxOverlapF) {
  238. // Solve for iR of reverse path with (d - 1) changes in diagonal kF:
  239. // kR = kF + baDeltaLength
  240. // kR = (d - 1) - 2 * iR
  241. const iR = (d - 1 - (kF + baDeltaLength)) / 2;
  242. // If this forward path overlaps the reverse path in this diagonal,
  243. // then this is the middle change of the index intervals.
  244. if (iR <= iMaxR && aIndexesR[iR] - 1 <= aLast) {
  245. // Unlike the Myers algorithm which finds only the middle “snake”
  246. // this package can find two common subsequences per division.
  247. // Last point of previous path segment is on an adjacent diagonal.
  248. const bLastPrev = bF + aLastPrev - (insert ? kF + 1 : kF - 1);
  249. // Because of invariant that intervals preceding the middle change
  250. // cannot have common items at the end,
  251. // move in reverse direction along a diagonal of common items.
  252. const nCommonR = countCommonItemsR(
  253. aStart,
  254. aLastPrev,
  255. bStart,
  256. bLastPrev,
  257. isCommon
  258. );
  259. const aIndexPrevFirst = aLastPrev - nCommonR;
  260. const bIndexPrevFirst = bLastPrev - nCommonR;
  261. const aEndPreceding = aIndexPrevFirst + 1;
  262. const bEndPreceding = bIndexPrevFirst + 1;
  263. division.nChangePreceding = d - 1;
  264. if (d - 1 === aEndPreceding + bEndPreceding - aStart - bStart) {
  265. // Optimization: number of preceding changes in forward direction
  266. // is equal to number of items in preceding interval,
  267. // therefore it cannot contain any common items.
  268. division.aEndPreceding = aStart;
  269. division.bEndPreceding = bStart;
  270. } else {
  271. division.aEndPreceding = aEndPreceding;
  272. division.bEndPreceding = bEndPreceding;
  273. }
  274. division.nCommonPreceding = nCommonR;
  275. if (nCommonR !== 0) {
  276. division.aCommonPreceding = aEndPreceding;
  277. division.bCommonPreceding = bEndPreceding;
  278. }
  279. division.nCommonFollowing = nCommonF;
  280. if (nCommonF !== 0) {
  281. division.aCommonFollowing = aFirst + 1;
  282. division.bCommonFollowing = bFirst + 1;
  283. }
  284. const aStartFollowing = aLast + 1;
  285. const bStartFollowing = bFirst + nCommonF + 1;
  286. division.nChangeFollowing = d - 1;
  287. if (d - 1 === aEnd + bEnd - aStartFollowing - bStartFollowing) {
  288. // Optimization: number of changes in reverse direction
  289. // is equal to number of items in following interval,
  290. // therefore it cannot contain any common items.
  291. division.aStartFollowing = aEnd;
  292. division.bStartFollowing = bEnd;
  293. } else {
  294. division.aStartFollowing = aStartFollowing;
  295. division.bStartFollowing = bStartFollowing;
  296. }
  297. return true;
  298. }
  299. }
  300. }
  301. return false;
  302. };
  303. // A complete function to extend reverse paths from (d - 1) to d changes.
  304. // Return true if a path overlaps forward path of d changes in its diagonal.
  305. const extendOverlappablePathsR = (
  306. d,
  307. aStart,
  308. aEnd,
  309. bStart,
  310. bEnd,
  311. isCommon,
  312. aIndexesF,
  313. iMaxF,
  314. aIndexesR,
  315. iMaxR,
  316. division // update prop values if return true
  317. ) => {
  318. const bR = bEnd - aEnd; // bIndex = bR + aIndex - kR
  319. const aLength = aEnd - aStart;
  320. const bLength = bEnd - bStart;
  321. const baDeltaLength = bLength - aLength; // kR = kF + baDeltaLength
  322. // Range of diagonals in which forward and reverse paths might overlap.
  323. const kMinOverlapR = baDeltaLength - d; // -d <= kF
  324. const kMaxOverlapR = baDeltaLength + d; // kF <= d
  325. let aIndexPrev1 = NOT_YET_SET; // prev value of [iR - 1] in next iteration
  326. // Optimization: skip diagonals in which paths cannot ever overlap.
  327. const nR = d < iMaxR ? d : iMaxR;
  328. // The diagonals kR = d - 2 * iR are odd when d is odd and even when d is even.
  329. for (let iR = 0, kR = d; iR <= nR; iR += 1, kR -= 2) {
  330. // To get first point of path segment, move one change in reverse direction
  331. // from last point of previous path segment in an adjacent diagonal.
  332. // In first iteration when iR === 0 and kR === d always insert.
  333. // In last possible iteration when iR === d and kR === -d always delete.
  334. const insert = iR === 0 || (iR !== d && aIndexesR[iR] < aIndexPrev1);
  335. const aLastPrev = insert ? aIndexesR[iR] : aIndexPrev1;
  336. const aFirst = insert
  337. ? aLastPrev // vertical to insert from b
  338. : aLastPrev - 1; // horizontal to delete from a
  339. // To get last point of path segment, move along diagonal of common items.
  340. const bFirst = bR + aFirst - kR;
  341. const nCommonR = countCommonItemsR(
  342. aStart,
  343. aFirst - 1,
  344. bStart,
  345. bFirst - 1,
  346. isCommon
  347. );
  348. const aLast = aFirst - nCommonR;
  349. aIndexPrev1 = aIndexesR[iR];
  350. aIndexesR[iR] = aLast;
  351. if (kMinOverlapR <= kR && kR <= kMaxOverlapR) {
  352. // Solve for iF of forward path with d changes in diagonal kR:
  353. // kF = kR - baDeltaLength
  354. // kF = 2 * iF - d
  355. const iF = (d + (kR - baDeltaLength)) / 2;
  356. // If this reverse path overlaps the forward path in this diagonal,
  357. // then this is a middle change of the index intervals.
  358. if (iF <= iMaxF && aLast - 1 <= aIndexesF[iF]) {
  359. const bLast = bFirst - nCommonR;
  360. division.nChangePreceding = d;
  361. if (d === aLast + bLast - aStart - bStart) {
  362. // Optimization: number of changes in reverse direction
  363. // is equal to number of items in preceding interval,
  364. // therefore it cannot contain any common items.
  365. division.aEndPreceding = aStart;
  366. division.bEndPreceding = bStart;
  367. } else {
  368. division.aEndPreceding = aLast;
  369. division.bEndPreceding = bLast;
  370. }
  371. division.nCommonPreceding = nCommonR;
  372. if (nCommonR !== 0) {
  373. // The last point of reverse path segment is start of common subsequence.
  374. division.aCommonPreceding = aLast;
  375. division.bCommonPreceding = bLast;
  376. }
  377. division.nChangeFollowing = d - 1;
  378. if (d === 1) {
  379. // There is no previous path segment.
  380. division.nCommonFollowing = 0;
  381. division.aStartFollowing = aEnd;
  382. division.bStartFollowing = bEnd;
  383. } else {
  384. // Unlike the Myers algorithm which finds only the middle “snake”
  385. // this package can find two common subsequences per division.
  386. // Last point of previous path segment is on an adjacent diagonal.
  387. const bLastPrev = bR + aLastPrev - (insert ? kR - 1 : kR + 1);
  388. // Because of invariant that intervals following the middle change
  389. // cannot have common items at the start,
  390. // move in forward direction along a diagonal of common items.
  391. const nCommonF = countCommonItemsF(
  392. aLastPrev,
  393. aEnd,
  394. bLastPrev,
  395. bEnd,
  396. isCommon
  397. );
  398. division.nCommonFollowing = nCommonF;
  399. if (nCommonF !== 0) {
  400. // The last point of reverse path segment is start of common subsequence.
  401. division.aCommonFollowing = aLastPrev;
  402. division.bCommonFollowing = bLastPrev;
  403. }
  404. const aStartFollowing = aLastPrev + nCommonF; // aFirstPrev
  405. const bStartFollowing = bLastPrev + nCommonF; // bFirstPrev
  406. if (d - 1 === aEnd + bEnd - aStartFollowing - bStartFollowing) {
  407. // Optimization: number of changes in forward direction
  408. // is equal to number of items in following interval,
  409. // therefore it cannot contain any common items.
  410. division.aStartFollowing = aEnd;
  411. division.bStartFollowing = bEnd;
  412. } else {
  413. division.aStartFollowing = aStartFollowing;
  414. division.bStartFollowing = bStartFollowing;
  415. }
  416. }
  417. return true;
  418. }
  419. }
  420. }
  421. return false;
  422. };
  423. // Given index intervals and input function to compare items at indexes,
  424. // divide at the middle change.
  425. //
  426. // DO NOT CALL if start === end, because interval cannot contain common items
  427. // and because this function will throw the “no overlap” error.
  428. const divide = (
  429. nChange,
  430. aStart,
  431. aEnd,
  432. bStart,
  433. bEnd,
  434. isCommon,
  435. aIndexesF,
  436. aIndexesR,
  437. division // output
  438. ) => {
  439. const bF = bStart - aStart; // bIndex = bF + aIndex - kF
  440. const bR = bEnd - aEnd; // bIndex = bR + aIndex - kR
  441. const aLength = aEnd - aStart;
  442. const bLength = bEnd - bStart;
  443. // Because graph has square or portrait orientation,
  444. // length difference is minimum number of items to insert from b.
  445. // Corresponding forward and reverse diagonals in graph
  446. // depend on length difference of the sequences:
  447. // kF = kR - baDeltaLength
  448. // kR = kF + baDeltaLength
  449. const baDeltaLength = bLength - aLength;
  450. // Optimization: max diagonal in graph intersects corner of shorter side.
  451. let iMaxF = aLength;
  452. let iMaxR = aLength;
  453. // Initialize no changes yet in forward or reverse direction:
  454. aIndexesF[0] = aStart - 1; // at open start of interval, outside closed start
  455. aIndexesR[0] = aEnd; // at open end of interval
  456. if (baDeltaLength % 2 === 0) {
  457. // The number of changes in paths is 2 * d if length difference is even.
  458. const dMin = (nChange || baDeltaLength) / 2;
  459. const dMax = (aLength + bLength) / 2;
  460. for (let d = 1; d <= dMax; d += 1) {
  461. iMaxF = extendPathsF(d, aEnd, bEnd, bF, isCommon, aIndexesF, iMaxF);
  462. if (d < dMin) {
  463. iMaxR = extendPathsR(d, aStart, bStart, bR, isCommon, aIndexesR, iMaxR);
  464. } else if (
  465. // If a reverse path overlaps a forward path in the same diagonal,
  466. // return a division of the index intervals at the middle change.
  467. extendOverlappablePathsR(
  468. d,
  469. aStart,
  470. aEnd,
  471. bStart,
  472. bEnd,
  473. isCommon,
  474. aIndexesF,
  475. iMaxF,
  476. aIndexesR,
  477. iMaxR,
  478. division
  479. )
  480. ) {
  481. return;
  482. }
  483. }
  484. } else {
  485. // The number of changes in paths is 2 * d - 1 if length difference is odd.
  486. const dMin = ((nChange || baDeltaLength) + 1) / 2;
  487. const dMax = (aLength + bLength + 1) / 2;
  488. // Unroll first half iteration so loop extends the relevant pairs of paths.
  489. // Because of invariant that intervals have no common items at start or end,
  490. // and limitation not to call divide with empty intervals,
  491. // therefore it cannot be called if a forward path with one change
  492. // would overlap a reverse path with no changes, even if dMin === 1.
  493. let d = 1;
  494. iMaxF = extendPathsF(d, aEnd, bEnd, bF, isCommon, aIndexesF, iMaxF);
  495. for (d += 1; d <= dMax; d += 1) {
  496. iMaxR = extendPathsR(
  497. d - 1,
  498. aStart,
  499. bStart,
  500. bR,
  501. isCommon,
  502. aIndexesR,
  503. iMaxR
  504. );
  505. if (d < dMin) {
  506. iMaxF = extendPathsF(d, aEnd, bEnd, bF, isCommon, aIndexesF, iMaxF);
  507. } else if (
  508. // If a forward path overlaps a reverse path in the same diagonal,
  509. // return a division of the index intervals at the middle change.
  510. extendOverlappablePathsF(
  511. d,
  512. aStart,
  513. aEnd,
  514. bStart,
  515. bEnd,
  516. isCommon,
  517. aIndexesF,
  518. iMaxF,
  519. aIndexesR,
  520. iMaxR,
  521. division
  522. )
  523. ) {
  524. return;
  525. }
  526. }
  527. }
  528. /* istanbul ignore next */
  529. throw new Error(
  530. `${pkg}: no overlap aStart=${aStart} aEnd=${aEnd} bStart=${bStart} bEnd=${bEnd}`
  531. );
  532. };
  533. // Given index intervals and input function to compare items at indexes,
  534. // return by output function the number of adjacent items and starting indexes
  535. // of each common subsequence. Divide and conquer with only linear space.
  536. //
  537. // The index intervals are half open [start, end) like array slice method.
  538. // DO NOT CALL if start === end, because interval cannot contain common items
  539. // and because divide function will throw the “no overlap” error.
  540. const findSubsequences = (
  541. nChange,
  542. aStart,
  543. aEnd,
  544. bStart,
  545. bEnd,
  546. transposed,
  547. callbacks,
  548. aIndexesF,
  549. aIndexesR,
  550. division // temporary memory, not input nor output
  551. ) => {
  552. if (bEnd - bStart < aEnd - aStart) {
  553. // Transpose graph so it has portrait instead of landscape orientation.
  554. // Always compare shorter to longer sequence for consistency and optimization.
  555. transposed = !transposed;
  556. if (transposed && callbacks.length === 1) {
  557. // Lazily wrap callback functions to swap args if graph is transposed.
  558. const {foundSubsequence, isCommon} = callbacks[0];
  559. callbacks[1] = {
  560. foundSubsequence: (nCommon, bCommon, aCommon) => {
  561. foundSubsequence(nCommon, aCommon, bCommon);
  562. },
  563. isCommon: (bIndex, aIndex) => isCommon(aIndex, bIndex)
  564. };
  565. }
  566. const tStart = aStart;
  567. const tEnd = aEnd;
  568. aStart = bStart;
  569. aEnd = bEnd;
  570. bStart = tStart;
  571. bEnd = tEnd;
  572. }
  573. const {foundSubsequence, isCommon} = callbacks[transposed ? 1 : 0];
  574. // Divide the index intervals at the middle change.
  575. divide(
  576. nChange,
  577. aStart,
  578. aEnd,
  579. bStart,
  580. bEnd,
  581. isCommon,
  582. aIndexesF,
  583. aIndexesR,
  584. division
  585. );
  586. const {
  587. nChangePreceding,
  588. aEndPreceding,
  589. bEndPreceding,
  590. nCommonPreceding,
  591. aCommonPreceding,
  592. bCommonPreceding,
  593. nCommonFollowing,
  594. aCommonFollowing,
  595. bCommonFollowing,
  596. nChangeFollowing,
  597. aStartFollowing,
  598. bStartFollowing
  599. } = division;
  600. // Unless either index interval is empty, they might contain common items.
  601. if (aStart < aEndPreceding && bStart < bEndPreceding) {
  602. // Recursely find and return common subsequences preceding the division.
  603. findSubsequences(
  604. nChangePreceding,
  605. aStart,
  606. aEndPreceding,
  607. bStart,
  608. bEndPreceding,
  609. transposed,
  610. callbacks,
  611. aIndexesF,
  612. aIndexesR,
  613. division
  614. );
  615. }
  616. // Return common subsequences that are adjacent to the middle change.
  617. if (nCommonPreceding !== 0) {
  618. foundSubsequence(nCommonPreceding, aCommonPreceding, bCommonPreceding);
  619. }
  620. if (nCommonFollowing !== 0) {
  621. foundSubsequence(nCommonFollowing, aCommonFollowing, bCommonFollowing);
  622. }
  623. // Unless either index interval is empty, they might contain common items.
  624. if (aStartFollowing < aEnd && bStartFollowing < bEnd) {
  625. // Recursely find and return common subsequences following the division.
  626. findSubsequences(
  627. nChangeFollowing,
  628. aStartFollowing,
  629. aEnd,
  630. bStartFollowing,
  631. bEnd,
  632. transposed,
  633. callbacks,
  634. aIndexesF,
  635. aIndexesR,
  636. division
  637. );
  638. }
  639. };
  640. const validateLength = (name, arg) => {
  641. if (typeof arg !== 'number') {
  642. throw new TypeError(`${pkg}: ${name} typeof ${typeof arg} is not a number`);
  643. }
  644. if (!Number.isSafeInteger(arg)) {
  645. throw new RangeError(`${pkg}: ${name} value ${arg} is not a safe integer`);
  646. }
  647. if (arg < 0) {
  648. throw new RangeError(`${pkg}: ${name} value ${arg} is a negative integer`);
  649. }
  650. };
  651. const validateCallback = (name, arg) => {
  652. const type = typeof arg;
  653. if (type !== 'function') {
  654. throw new TypeError(`${pkg}: ${name} typeof ${type} is not a function`);
  655. }
  656. };
  657. // Compare items in two sequences to find a longest common subsequence.
  658. // Given lengths of sequences and input function to compare items at indexes,
  659. // return by output function the number of adjacent items and starting indexes
  660. // of each common subsequence.
  661. function diffSequence(aLength, bLength, isCommon, foundSubsequence) {
  662. validateLength('aLength', aLength);
  663. validateLength('bLength', bLength);
  664. validateCallback('isCommon', isCommon);
  665. validateCallback('foundSubsequence', foundSubsequence);
  666. // Count common items from the start in the forward direction.
  667. const nCommonF = countCommonItemsF(0, aLength, 0, bLength, isCommon);
  668. if (nCommonF !== 0) {
  669. foundSubsequence(nCommonF, 0, 0);
  670. }
  671. // Unless both sequences consist of common items only,
  672. // find common items in the half-trimmed index intervals.
  673. if (aLength !== nCommonF || bLength !== nCommonF) {
  674. // Invariant: intervals do not have common items at the start.
  675. // The start of an index interval is closed like array slice method.
  676. const aStart = nCommonF;
  677. const bStart = nCommonF;
  678. // Count common items from the end in the reverse direction.
  679. const nCommonR = countCommonItemsR(
  680. aStart,
  681. aLength - 1,
  682. bStart,
  683. bLength - 1,
  684. isCommon
  685. );
  686. // Invariant: intervals do not have common items at the end.
  687. // The end of an index interval is open like array slice method.
  688. const aEnd = aLength - nCommonR;
  689. const bEnd = bLength - nCommonR;
  690. // Unless one sequence consists of common items only,
  691. // therefore the other trimmed index interval consists of changes only,
  692. // find common items in the trimmed index intervals.
  693. const nCommonFR = nCommonF + nCommonR;
  694. if (aLength !== nCommonFR && bLength !== nCommonFR) {
  695. const nChange = 0; // number of change items is not yet known
  696. const transposed = false; // call the original unwrapped functions
  697. const callbacks = [
  698. {
  699. foundSubsequence,
  700. isCommon
  701. }
  702. ];
  703. // Indexes in sequence a of last points in furthest reaching paths
  704. // from outside the start at top left in the forward direction:
  705. const aIndexesF = [NOT_YET_SET];
  706. // from the end at bottom right in the reverse direction:
  707. const aIndexesR = [NOT_YET_SET];
  708. // Initialize one object as output of all calls to divide function.
  709. const division = {
  710. aCommonFollowing: NOT_YET_SET,
  711. aCommonPreceding: NOT_YET_SET,
  712. aEndPreceding: NOT_YET_SET,
  713. aStartFollowing: NOT_YET_SET,
  714. bCommonFollowing: NOT_YET_SET,
  715. bCommonPreceding: NOT_YET_SET,
  716. bEndPreceding: NOT_YET_SET,
  717. bStartFollowing: NOT_YET_SET,
  718. nChangeFollowing: NOT_YET_SET,
  719. nChangePreceding: NOT_YET_SET,
  720. nCommonFollowing: NOT_YET_SET,
  721. nCommonPreceding: NOT_YET_SET
  722. };
  723. // Find and return common subsequences in the trimmed index intervals.
  724. findSubsequences(
  725. nChange,
  726. aStart,
  727. aEnd,
  728. bStart,
  729. bEnd,
  730. transposed,
  731. callbacks,
  732. aIndexesF,
  733. aIndexesR,
  734. division
  735. );
  736. }
  737. if (nCommonR !== 0) {
  738. foundSubsequence(nCommonR, aEnd, bEnd);
  739. }
  740. }
  741. }