index.js 28 KB

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