index.js 26 KB

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