Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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 {foundSubsequence, isCommon} = callbacks[0];
  520. callbacks[1] = {
  521. foundSubsequence: (nCommon, bCommon, aCommon) => {
  522. foundSubsequence(nCommon, aCommon, bCommon);
  523. },
  524. isCommon: (bIndex, aIndex) => isCommon(aIndex, bIndex)
  525. };
  526. }
  527. const tStart = aStart;
  528. const tEnd = aEnd;
  529. aStart = bStart;
  530. aEnd = bEnd;
  531. bStart = tStart;
  532. bEnd = tEnd;
  533. }
  534. const {foundSubsequence, isCommon} = callbacks[transposed ? 1 : 0]; // Divide the index intervals at the middle change.
  535. divide(
  536. nChange,
  537. aStart,
  538. aEnd,
  539. bStart,
  540. bEnd,
  541. isCommon,
  542. aIndexesF,
  543. aIndexesR,
  544. division
  545. );
  546. const {
  547. nChangePreceding,
  548. aEndPreceding,
  549. bEndPreceding,
  550. nCommonPreceding,
  551. aCommonPreceding,
  552. bCommonPreceding,
  553. nCommonFollowing,
  554. aCommonFollowing,
  555. bCommonFollowing,
  556. nChangeFollowing,
  557. aStartFollowing,
  558. bStartFollowing
  559. } = division; // Unless either index interval is empty, they might contain common items.
  560. if (aStart < aEndPreceding && bStart < bEndPreceding) {
  561. // Recursely find and return common subsequences preceding the division.
  562. findSubsequences(
  563. nChangePreceding,
  564. aStart,
  565. aEndPreceding,
  566. bStart,
  567. bEndPreceding,
  568. transposed,
  569. callbacks,
  570. aIndexesF,
  571. aIndexesR,
  572. division
  573. );
  574. } // Return common subsequences that are adjacent to the middle change.
  575. if (nCommonPreceding !== 0) {
  576. foundSubsequence(nCommonPreceding, aCommonPreceding, bCommonPreceding);
  577. }
  578. if (nCommonFollowing !== 0) {
  579. foundSubsequence(nCommonFollowing, aCommonFollowing, bCommonFollowing);
  580. } // Unless either index interval is empty, they might contain common items.
  581. if (aStartFollowing < aEnd && bStartFollowing < bEnd) {
  582. // Recursely find and return common subsequences following the division.
  583. findSubsequences(
  584. nChangeFollowing,
  585. aStartFollowing,
  586. aEnd,
  587. bStartFollowing,
  588. bEnd,
  589. transposed,
  590. callbacks,
  591. aIndexesF,
  592. aIndexesR,
  593. division
  594. );
  595. }
  596. };
  597. const validateLength = (name, arg) => {
  598. if (typeof arg !== 'number') {
  599. throw new TypeError(`${pkg}: ${name} typeof ${typeof arg} is not a number`);
  600. }
  601. if (!Number.isSafeInteger(arg)) {
  602. throw new RangeError(`${pkg}: ${name} value ${arg} is not a safe integer`);
  603. }
  604. if (arg < 0) {
  605. throw new RangeError(`${pkg}: ${name} value ${arg} is a negative integer`);
  606. }
  607. };
  608. const validateCallback = (name, arg) => {
  609. const type = typeof arg;
  610. if (type !== 'function') {
  611. throw new TypeError(`${pkg}: ${name} typeof ${type} is not a function`);
  612. }
  613. }; // Compare items in two sequences to find a longest common subsequence.
  614. // Given lengths of sequences and input function to compare items at indexes,
  615. // return by output function the number of adjacent items and starting indexes
  616. // of each common subsequence.
  617. var _default = (aLength, bLength, isCommon, foundSubsequence) => {
  618. validateLength('aLength', aLength);
  619. validateLength('bLength', bLength);
  620. validateCallback('isCommon', isCommon);
  621. validateCallback('foundSubsequence', foundSubsequence); // Count common items from the start in the forward direction.
  622. const nCommonF = countCommonItemsF(0, aLength, 0, bLength, isCommon);
  623. if (nCommonF !== 0) {
  624. foundSubsequence(nCommonF, 0, 0);
  625. } // Unless both sequences consist of common items only,
  626. // find common items in the half-trimmed index intervals.
  627. if (aLength !== nCommonF || bLength !== nCommonF) {
  628. // Invariant: intervals do not have common items at the start.
  629. // The start of an index interval is closed like array slice method.
  630. const aStart = nCommonF;
  631. const bStart = nCommonF; // Count common items from the end in the reverse direction.
  632. const nCommonR = countCommonItemsR(
  633. aStart,
  634. aLength - 1,
  635. bStart,
  636. bLength - 1,
  637. isCommon
  638. ); // Invariant: intervals do not have common items at the end.
  639. // The end of an index interval is open like array slice method.
  640. const aEnd = aLength - nCommonR;
  641. const bEnd = bLength - nCommonR; // Unless one sequence consists of common items only,
  642. // therefore the other trimmed index interval consists of changes only,
  643. // find common items in the trimmed index intervals.
  644. const nCommonFR = nCommonF + nCommonR;
  645. if (aLength !== nCommonFR && bLength !== nCommonFR) {
  646. const nChange = 0; // number of change items is not yet known
  647. const transposed = false; // call the original unwrapped functions
  648. const callbacks = [
  649. {
  650. foundSubsequence,
  651. isCommon
  652. }
  653. ]; // Indexes in sequence a of last points in furthest reaching paths
  654. // from outside the start at top left in the forward direction:
  655. const aIndexesF = [NOT_YET_SET]; // from the end at bottom right in the reverse direction:
  656. const aIndexesR = [NOT_YET_SET]; // Initialize one object as output of all calls to divide function.
  657. const division = {
  658. aCommonFollowing: NOT_YET_SET,
  659. aCommonPreceding: NOT_YET_SET,
  660. aEndPreceding: NOT_YET_SET,
  661. aStartFollowing: NOT_YET_SET,
  662. bCommonFollowing: NOT_YET_SET,
  663. bCommonPreceding: NOT_YET_SET,
  664. bEndPreceding: NOT_YET_SET,
  665. bStartFollowing: NOT_YET_SET,
  666. nChangeFollowing: NOT_YET_SET,
  667. nChangePreceding: NOT_YET_SET,
  668. nCommonFollowing: NOT_YET_SET,
  669. nCommonPreceding: NOT_YET_SET
  670. }; // Find and return common subsequences in the trimmed index intervals.
  671. findSubsequences(
  672. nChange,
  673. aStart,
  674. aEnd,
  675. bStart,
  676. bEnd,
  677. transposed,
  678. callbacks,
  679. aIndexesF,
  680. aIndexesR,
  681. division
  682. );
  683. }
  684. if (nCommonR !== 0) {
  685. foundSubsequence(nCommonR, aEnd, bEnd);
  686. }
  687. }
  688. };
  689. exports.default = _default;