index.js 27 KB

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