@vitest/utils 2.1.0 → 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/diff.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { format, plugins } from '@vitest/pretty-format';
2
2
  import c from 'tinyrainbow';
3
- import { s as stringify } from './chunk-display.js';
3
+ import { g as getDefaultExportFromCjs, s as stringify } from './chunk-_commonjsHelpers.js';
4
4
  import { deepClone, getOwnProperties, getType as getType$1 } from './helpers.js';
5
5
  import 'loupe';
6
6
 
@@ -402,803 +402,814 @@ const SIMILAR_MESSAGE = "Compared values serialize to the same structure.\nPrint
402
402
 
403
403
  var build = {};
404
404
 
405
- Object.defineProperty(build, '__esModule', {
406
- value: true
407
- });
408
- var _default = build.default = diffSequence;
409
- /**
410
- * Copyright (c) Meta Platforms, Inc. and affiliates.
411
- *
412
- * This source code is licensed under the MIT license found in the
413
- * LICENSE file in the root directory of this source tree.
414
- *
415
- */
416
-
417
- // This diff-sequences package implements the linear space variation in
418
- // An O(ND) Difference Algorithm and Its Variations by Eugene W. Myers
419
-
420
- // Relationship in notation between Myers paper and this package:
421
- // A is a
422
- // N is aLength, aEnd - aStart, and so on
423
- // x is aIndex, aFirst, aLast, and so on
424
- // B is b
425
- // M is bLength, bEnd - bStart, and so on
426
- // y is bIndex, bFirst, bLast, and so on
427
- // Δ = N - M is negative of baDeltaLength = bLength - aLength
428
- // D is d
429
- // k is kF
430
- // k + Δ is kF = kR - baDeltaLength
431
- // V is aIndexesF or aIndexesR (see comment below about Indexes type)
432
- // index intervals [1, N] and [1, M] are [0, aLength) and [0, bLength)
433
- // starting point in forward direction (0, 0) is (-1, -1)
434
- // starting point in reverse direction (N + 1, M + 1) is (aLength, bLength)
435
-
436
- // The “edit graph” for sequences a and b corresponds to items:
437
- // in a on the horizontal axis
438
- // in b on the vertical axis
439
- //
440
- // Given a-coordinate of a point in a diagonal, you can compute b-coordinate.
441
- //
442
- // Forward diagonals kF:
443
- // zero diagonal intersects top left corner
444
- // positive diagonals intersect top edge
445
- // negative diagonals insersect left edge
446
- //
447
- // Reverse diagonals kR:
448
- // zero diagonal intersects bottom right corner
449
- // positive diagonals intersect right edge
450
- // negative diagonals intersect bottom edge
451
-
452
- // The graph contains a directed acyclic graph of edges:
453
- // horizontal: delete an item from a
454
- // vertical: insert an item from b
455
- // diagonal: common item in a and b
456
- //
457
- // The algorithm solves dual problems in the graph analogy:
458
- // Find longest common subsequence: path with maximum number of diagonal edges
459
- // Find shortest edit script: path with minimum number of non-diagonal edges
460
-
461
- // Input callback function compares items at indexes in the sequences.
462
-
463
- // Output callback function receives the number of adjacent items
464
- // and starting indexes of each common subsequence.
465
- // Either original functions or wrapped to swap indexes if graph is transposed.
466
- // Indexes in sequence a of last point of forward or reverse paths in graph.
467
- // Myers algorithm indexes by diagonal k which for negative is bad deopt in V8.
468
- // This package indexes by iF and iR which are greater than or equal to zero.
469
- // and also updates the index arrays in place to cut memory in half.
470
- // kF = 2 * iF - d
471
- // kR = d - 2 * iR
472
- // Division of index intervals in sequences a and b at the middle change.
473
- // Invariant: intervals do not have common items at the start or end.
474
- const pkg = 'diff-sequences'; // for error messages
475
- const NOT_YET_SET = 0; // small int instead of undefined to avoid deopt in V8
476
-
477
- // Return the number of common items that follow in forward direction.
478
- // The length of what Myers paper calls a “snake” in a forward path.
479
- const countCommonItemsF = (aIndex, aEnd, bIndex, bEnd, isCommon) => {
480
- let nCommon = 0;
481
- while (aIndex < aEnd && bIndex < bEnd && isCommon(aIndex, bIndex)) {
482
- aIndex += 1;
483
- bIndex += 1;
484
- nCommon += 1;
485
- }
486
- return nCommon;
487
- };
488
-
489
- // Return the number of common items that precede in reverse direction.
490
- // The length of what Myers paper calls a “snake” in a reverse path.
491
- const countCommonItemsR = (aStart, aIndex, bStart, bIndex, isCommon) => {
492
- let nCommon = 0;
493
- while (aStart <= aIndex && bStart <= bIndex && isCommon(aIndex, bIndex)) {
494
- aIndex -= 1;
495
- bIndex -= 1;
496
- nCommon += 1;
497
- }
498
- return nCommon;
499
- };
500
-
501
- // A simple function to extend forward paths from (d - 1) to d changes
502
- // when forward and reverse paths cannot yet overlap.
503
- const extendPathsF = (
504
- d,
505
- aEnd,
506
- bEnd,
507
- bF,
508
- isCommon,
509
- aIndexesF,
510
- iMaxF // return the value because optimization might decrease it
511
- ) => {
512
- // Unroll the first iteration.
513
- let iF = 0;
514
- let kF = -d; // kF = 2 * iF - d
515
- let aFirst = aIndexesF[iF]; // in first iteration always insert
516
- let aIndexPrev1 = aFirst; // prev value of [iF - 1] in next iteration
517
- aIndexesF[iF] += countCommonItemsF(
518
- aFirst + 1,
519
- aEnd,
520
- bF + aFirst - kF + 1,
521
- bEnd,
522
- isCommon
523
- );
524
-
525
- // Optimization: skip diagonals in which paths cannot ever overlap.
526
- const nF = d < iMaxF ? d : iMaxF;
527
-
528
- // The diagonals kF are odd when d is odd and even when d is even.
529
- for (iF += 1, kF += 2; iF <= nF; iF += 1, kF += 2) {
530
- // To get first point of path segment, move one change in forward direction
531
- // from last point of previous path segment in an adjacent diagonal.
532
- // In last possible iteration when iF === d and kF === d always delete.
533
- if (iF !== d && aIndexPrev1 < aIndexesF[iF]) {
534
- aFirst = aIndexesF[iF]; // vertical to insert from b
535
- } else {
536
- aFirst = aIndexPrev1 + 1; // horizontal to delete from a
537
-
538
- if (aEnd <= aFirst) {
539
- // Optimization: delete moved past right of graph.
540
- return iF - 1;
541
- }
542
- }
543
-
544
- // To get last point of path segment, move along diagonal of common items.
545
- aIndexPrev1 = aIndexesF[iF];
546
- aIndexesF[iF] =
547
- aFirst +
548
- countCommonItemsF(aFirst + 1, aEnd, bF + aFirst - kF + 1, bEnd, isCommon);
549
- }
550
- return iMaxF;
551
- };
552
-
553
- // A simple function to extend reverse paths from (d - 1) to d changes
554
- // when reverse and forward paths cannot yet overlap.
555
- const extendPathsR = (
556
- d,
557
- aStart,
558
- bStart,
559
- bR,
560
- isCommon,
561
- aIndexesR,
562
- iMaxR // return the value because optimization might decrease it
563
- ) => {
564
- // Unroll the first iteration.
565
- let iR = 0;
566
- let kR = d; // kR = d - 2 * iR
567
- let aFirst = aIndexesR[iR]; // in first iteration always insert
568
- let aIndexPrev1 = aFirst; // prev value of [iR - 1] in next iteration
569
- aIndexesR[iR] -= countCommonItemsR(
570
- aStart,
571
- aFirst - 1,
572
- bStart,
573
- bR + aFirst - kR - 1,
574
- isCommon
575
- );
576
-
577
- // Optimization: skip diagonals in which paths cannot ever overlap.
578
- const nR = d < iMaxR ? d : iMaxR;
579
-
580
- // The diagonals kR are odd when d is odd and even when d is even.
581
- for (iR += 1, kR -= 2; iR <= nR; iR += 1, kR -= 2) {
582
- // To get first point of path segment, move one change in reverse direction
583
- // from last point of previous path segment in an adjacent diagonal.
584
- // In last possible iteration when iR === d and kR === -d always delete.
585
- if (iR !== d && aIndexesR[iR] < aIndexPrev1) {
586
- aFirst = aIndexesR[iR]; // vertical to insert from b
587
- } else {
588
- aFirst = aIndexPrev1 - 1; // horizontal to delete from a
589
-
590
- if (aFirst < aStart) {
591
- // Optimization: delete moved past left of graph.
592
- return iR - 1;
593
- }
594
- }
595
-
596
- // To get last point of path segment, move along diagonal of common items.
597
- aIndexPrev1 = aIndexesR[iR];
598
- aIndexesR[iR] =
599
- aFirst -
600
- countCommonItemsR(
601
- aStart,
602
- aFirst - 1,
603
- bStart,
604
- bR + aFirst - kR - 1,
605
- isCommon
606
- );
607
- }
608
- return iMaxR;
609
- };
610
-
611
- // A complete function to extend forward paths from (d - 1) to d changes.
612
- // Return true if a path overlaps reverse path of (d - 1) changes in its diagonal.
613
- const extendOverlappablePathsF = (
614
- d,
615
- aStart,
616
- aEnd,
617
- bStart,
618
- bEnd,
619
- isCommon,
620
- aIndexesF,
621
- iMaxF,
622
- aIndexesR,
623
- iMaxR,
624
- division // update prop values if return true
625
- ) => {
626
- const bF = bStart - aStart; // bIndex = bF + aIndex - kF
627
- const aLength = aEnd - aStart;
628
- const bLength = bEnd - bStart;
629
- const baDeltaLength = bLength - aLength; // kF = kR - baDeltaLength
630
-
631
- // Range of diagonals in which forward and reverse paths might overlap.
632
- const kMinOverlapF = -baDeltaLength - (d - 1); // -(d - 1) <= kR
633
- const kMaxOverlapF = -baDeltaLength + (d - 1); // kR <= (d - 1)
634
-
635
- let aIndexPrev1 = NOT_YET_SET; // prev value of [iF - 1] in next iteration
636
-
637
- // Optimization: skip diagonals in which paths cannot ever overlap.
638
- const nF = d < iMaxF ? d : iMaxF;
639
-
640
- // The diagonals kF = 2 * iF - d are odd when d is odd and even when d is even.
641
- for (let iF = 0, kF = -d; iF <= nF; iF += 1, kF += 2) {
642
- // To get first point of path segment, move one change in forward direction
643
- // from last point of previous path segment in an adjacent diagonal.
644
- // In first iteration when iF === 0 and kF === -d always insert.
645
- // In last possible iteration when iF === d and kF === d always delete.
646
- const insert = iF === 0 || (iF !== d && aIndexPrev1 < aIndexesF[iF]);
647
- const aLastPrev = insert ? aIndexesF[iF] : aIndexPrev1;
648
- const aFirst = insert
649
- ? aLastPrev // vertical to insert from b
650
- : aLastPrev + 1; // horizontal to delete from a
651
-
652
- // To get last point of path segment, move along diagonal of common items.
653
- const bFirst = bF + aFirst - kF;
654
- const nCommonF = countCommonItemsF(
655
- aFirst + 1,
656
- aEnd,
657
- bFirst + 1,
658
- bEnd,
659
- isCommon
660
- );
661
- const aLast = aFirst + nCommonF;
662
- aIndexPrev1 = aIndexesF[iF];
663
- aIndexesF[iF] = aLast;
664
- if (kMinOverlapF <= kF && kF <= kMaxOverlapF) {
665
- // Solve for iR of reverse path with (d - 1) changes in diagonal kF:
666
- // kR = kF + baDeltaLength
667
- // kR = (d - 1) - 2 * iR
668
- const iR = (d - 1 - (kF + baDeltaLength)) / 2;
669
-
670
- // If this forward path overlaps the reverse path in this diagonal,
671
- // then this is the middle change of the index intervals.
672
- if (iR <= iMaxR && aIndexesR[iR] - 1 <= aLast) {
673
- // Unlike the Myers algorithm which finds only the middle “snake”
674
- // this package can find two common subsequences per division.
675
- // Last point of previous path segment is on an adjacent diagonal.
676
- const bLastPrev = bF + aLastPrev - (insert ? kF + 1 : kF - 1);
677
-
678
- // Because of invariant that intervals preceding the middle change
679
- // cannot have common items at the end,
680
- // move in reverse direction along a diagonal of common items.
681
- const nCommonR = countCommonItemsR(
682
- aStart,
683
- aLastPrev,
684
- bStart,
685
- bLastPrev,
686
- isCommon
687
- );
688
- const aIndexPrevFirst = aLastPrev - nCommonR;
689
- const bIndexPrevFirst = bLastPrev - nCommonR;
690
- const aEndPreceding = aIndexPrevFirst + 1;
691
- const bEndPreceding = bIndexPrevFirst + 1;
692
- division.nChangePreceding = d - 1;
693
- if (d - 1 === aEndPreceding + bEndPreceding - aStart - bStart) {
694
- // Optimization: number of preceding changes in forward direction
695
- // is equal to number of items in preceding interval,
696
- // therefore it cannot contain any common items.
697
- division.aEndPreceding = aStart;
698
- division.bEndPreceding = bStart;
699
- } else {
700
- division.aEndPreceding = aEndPreceding;
701
- division.bEndPreceding = bEndPreceding;
702
- }
703
- division.nCommonPreceding = nCommonR;
704
- if (nCommonR !== 0) {
705
- division.aCommonPreceding = aEndPreceding;
706
- division.bCommonPreceding = bEndPreceding;
707
- }
708
- division.nCommonFollowing = nCommonF;
709
- if (nCommonF !== 0) {
710
- division.aCommonFollowing = aFirst + 1;
711
- division.bCommonFollowing = bFirst + 1;
712
- }
713
- const aStartFollowing = aLast + 1;
714
- const bStartFollowing = bFirst + nCommonF + 1;
715
- division.nChangeFollowing = d - 1;
716
- if (d - 1 === aEnd + bEnd - aStartFollowing - bStartFollowing) {
717
- // Optimization: number of changes in reverse direction
718
- // is equal to number of items in following interval,
719
- // therefore it cannot contain any common items.
720
- division.aStartFollowing = aEnd;
721
- division.bStartFollowing = bEnd;
722
- } else {
723
- division.aStartFollowing = aStartFollowing;
724
- division.bStartFollowing = bStartFollowing;
725
- }
726
- return true;
727
- }
728
- }
729
- }
730
- return false;
731
- };
732
-
733
- // A complete function to extend reverse paths from (d - 1) to d changes.
734
- // Return true if a path overlaps forward path of d changes in its diagonal.
735
- const extendOverlappablePathsR = (
736
- d,
737
- aStart,
738
- aEnd,
739
- bStart,
740
- bEnd,
741
- isCommon,
742
- aIndexesF,
743
- iMaxF,
744
- aIndexesR,
745
- iMaxR,
746
- division // update prop values if return true
747
- ) => {
748
- const bR = bEnd - aEnd; // bIndex = bR + aIndex - kR
749
- const aLength = aEnd - aStart;
750
- const bLength = bEnd - bStart;
751
- const baDeltaLength = bLength - aLength; // kR = kF + baDeltaLength
752
-
753
- // Range of diagonals in which forward and reverse paths might overlap.
754
- const kMinOverlapR = baDeltaLength - d; // -d <= kF
755
- const kMaxOverlapR = baDeltaLength + d; // kF <= d
756
-
757
- let aIndexPrev1 = NOT_YET_SET; // prev value of [iR - 1] in next iteration
758
-
759
- // Optimization: skip diagonals in which paths cannot ever overlap.
760
- const nR = d < iMaxR ? d : iMaxR;
761
-
762
- // The diagonals kR = d - 2 * iR are odd when d is odd and even when d is even.
763
- for (let iR = 0, kR = d; iR <= nR; iR += 1, kR -= 2) {
764
- // To get first point of path segment, move one change in reverse direction
765
- // from last point of previous path segment in an adjacent diagonal.
766
- // In first iteration when iR === 0 and kR === d always insert.
767
- // In last possible iteration when iR === d and kR === -d always delete.
768
- const insert = iR === 0 || (iR !== d && aIndexesR[iR] < aIndexPrev1);
769
- const aLastPrev = insert ? aIndexesR[iR] : aIndexPrev1;
770
- const aFirst = insert
771
- ? aLastPrev // vertical to insert from b
772
- : aLastPrev - 1; // horizontal to delete from a
773
-
774
- // To get last point of path segment, move along diagonal of common items.
775
- const bFirst = bR + aFirst - kR;
776
- const nCommonR = countCommonItemsR(
777
- aStart,
778
- aFirst - 1,
779
- bStart,
780
- bFirst - 1,
781
- isCommon
782
- );
783
- const aLast = aFirst - nCommonR;
784
- aIndexPrev1 = aIndexesR[iR];
785
- aIndexesR[iR] = aLast;
786
- if (kMinOverlapR <= kR && kR <= kMaxOverlapR) {
787
- // Solve for iF of forward path with d changes in diagonal kR:
788
- // kF = kR - baDeltaLength
789
- // kF = 2 * iF - d
790
- const iF = (d + (kR - baDeltaLength)) / 2;
791
-
792
- // If this reverse path overlaps the forward path in this diagonal,
793
- // then this is a middle change of the index intervals.
794
- if (iF <= iMaxF && aLast - 1 <= aIndexesF[iF]) {
795
- const bLast = bFirst - nCommonR;
796
- division.nChangePreceding = d;
797
- if (d === aLast + bLast - aStart - bStart) {
798
- // Optimization: number of changes in reverse direction
799
- // is equal to number of items in preceding interval,
800
- // therefore it cannot contain any common items.
801
- division.aEndPreceding = aStart;
802
- division.bEndPreceding = bStart;
803
- } else {
804
- division.aEndPreceding = aLast;
805
- division.bEndPreceding = bLast;
806
- }
807
- division.nCommonPreceding = nCommonR;
808
- if (nCommonR !== 0) {
809
- // The last point of reverse path segment is start of common subsequence.
810
- division.aCommonPreceding = aLast;
811
- division.bCommonPreceding = bLast;
812
- }
813
- division.nChangeFollowing = d - 1;
814
- if (d === 1) {
815
- // There is no previous path segment.
816
- division.nCommonFollowing = 0;
817
- division.aStartFollowing = aEnd;
818
- division.bStartFollowing = bEnd;
819
- } else {
820
- // Unlike the Myers algorithm which finds only the middle “snake”
821
- // this package can find two common subsequences per division.
822
- // Last point of previous path segment is on an adjacent diagonal.
823
- const bLastPrev = bR + aLastPrev - (insert ? kR - 1 : kR + 1);
824
-
825
- // Because of invariant that intervals following the middle change
826
- // cannot have common items at the start,
827
- // move in forward direction along a diagonal of common items.
828
- const nCommonF = countCommonItemsF(
829
- aLastPrev,
830
- aEnd,
831
- bLastPrev,
832
- bEnd,
833
- isCommon
834
- );
835
- division.nCommonFollowing = nCommonF;
836
- if (nCommonF !== 0) {
837
- // The last point of reverse path segment is start of common subsequence.
838
- division.aCommonFollowing = aLastPrev;
839
- division.bCommonFollowing = bLastPrev;
840
- }
841
- const aStartFollowing = aLastPrev + nCommonF; // aFirstPrev
842
- const bStartFollowing = bLastPrev + nCommonF; // bFirstPrev
843
-
844
- if (d - 1 === aEnd + bEnd - aStartFollowing - bStartFollowing) {
845
- // Optimization: number of changes in forward direction
846
- // is equal to number of items in following interval,
847
- // therefore it cannot contain any common items.
848
- division.aStartFollowing = aEnd;
849
- division.bStartFollowing = bEnd;
850
- } else {
851
- division.aStartFollowing = aStartFollowing;
852
- division.bStartFollowing = bStartFollowing;
853
- }
854
- }
855
- return true;
856
- }
857
- }
858
- }
859
- return false;
860
- };
861
-
862
- // Given index intervals and input function to compare items at indexes,
863
- // divide at the middle change.
864
- //
865
- // DO NOT CALL if start === end, because interval cannot contain common items
866
- // and because this function will throw the “no overlap” error.
867
- const divide = (
868
- nChange,
869
- aStart,
870
- aEnd,
871
- bStart,
872
- bEnd,
873
- isCommon,
874
- aIndexesF,
875
- aIndexesR,
876
- division // output
877
- ) => {
878
- const bF = bStart - aStart; // bIndex = bF + aIndex - kF
879
- const bR = bEnd - aEnd; // bIndex = bR + aIndex - kR
880
- const aLength = aEnd - aStart;
881
- const bLength = bEnd - bStart;
882
-
883
- // Because graph has square or portrait orientation,
884
- // length difference is minimum number of items to insert from b.
885
- // Corresponding forward and reverse diagonals in graph
886
- // depend on length difference of the sequences:
887
- // kF = kR - baDeltaLength
888
- // kR = kF + baDeltaLength
889
- const baDeltaLength = bLength - aLength;
890
-
891
- // Optimization: max diagonal in graph intersects corner of shorter side.
892
- let iMaxF = aLength;
893
- let iMaxR = aLength;
894
-
895
- // Initialize no changes yet in forward or reverse direction:
896
- aIndexesF[0] = aStart - 1; // at open start of interval, outside closed start
897
- aIndexesR[0] = aEnd; // at open end of interval
898
-
899
- if (baDeltaLength % 2 === 0) {
900
- // The number of changes in paths is 2 * d if length difference is even.
901
- const dMin = (nChange || baDeltaLength) / 2;
902
- const dMax = (aLength + bLength) / 2;
903
- for (let d = 1; d <= dMax; d += 1) {
904
- iMaxF = extendPathsF(d, aEnd, bEnd, bF, isCommon, aIndexesF, iMaxF);
905
- if (d < dMin) {
906
- iMaxR = extendPathsR(d, aStart, bStart, bR, isCommon, aIndexesR, iMaxR);
907
- } else if (
908
- // If a reverse path overlaps a forward path in the same diagonal,
909
- // return a division of the index intervals at the middle change.
910
- extendOverlappablePathsR(
911
- d,
912
- aStart,
913
- aEnd,
914
- bStart,
915
- bEnd,
916
- isCommon,
917
- aIndexesF,
918
- iMaxF,
919
- aIndexesR,
920
- iMaxR,
921
- division
922
- )
923
- ) {
924
- return;
925
- }
926
- }
927
- } else {
928
- // The number of changes in paths is 2 * d - 1 if length difference is odd.
929
- const dMin = ((nChange || baDeltaLength) + 1) / 2;
930
- const dMax = (aLength + bLength + 1) / 2;
931
-
932
- // Unroll first half iteration so loop extends the relevant pairs of paths.
933
- // Because of invariant that intervals have no common items at start or end,
934
- // and limitation not to call divide with empty intervals,
935
- // therefore it cannot be called if a forward path with one change
936
- // would overlap a reverse path with no changes, even if dMin === 1.
937
- let d = 1;
938
- iMaxF = extendPathsF(d, aEnd, bEnd, bF, isCommon, aIndexesF, iMaxF);
939
- for (d += 1; d <= dMax; d += 1) {
940
- iMaxR = extendPathsR(
941
- d - 1,
942
- aStart,
943
- bStart,
944
- bR,
945
- isCommon,
946
- aIndexesR,
947
- iMaxR
948
- );
949
- if (d < dMin) {
950
- iMaxF = extendPathsF(d, aEnd, bEnd, bF, isCommon, aIndexesF, iMaxF);
951
- } else if (
952
- // If a forward path overlaps a reverse path in the same diagonal,
953
- // return a division of the index intervals at the middle change.
954
- extendOverlappablePathsF(
955
- d,
956
- aStart,
957
- aEnd,
958
- bStart,
959
- bEnd,
960
- isCommon,
961
- aIndexesF,
962
- iMaxF,
963
- aIndexesR,
964
- iMaxR,
965
- division
966
- )
967
- ) {
968
- return;
969
- }
970
- }
971
- }
972
-
973
- /* istanbul ignore next */
974
- throw new Error(
975
- `${pkg}: no overlap aStart=${aStart} aEnd=${aEnd} bStart=${bStart} bEnd=${bEnd}`
976
- );
977
- };
978
-
979
- // Given index intervals and input function to compare items at indexes,
980
- // return by output function the number of adjacent items and starting indexes
981
- // of each common subsequence. Divide and conquer with only linear space.
982
- //
983
- // The index intervals are half open [start, end) like array slice method.
984
- // DO NOT CALL if start === end, because interval cannot contain common items
985
- // and because divide function will throw the “no overlap” error.
986
- const findSubsequences = (
987
- nChange,
988
- aStart,
989
- aEnd,
990
- bStart,
991
- bEnd,
992
- transposed,
993
- callbacks,
994
- aIndexesF,
995
- aIndexesR,
996
- division // temporary memory, not input nor output
997
- ) => {
998
- if (bEnd - bStart < aEnd - aStart) {
999
- // Transpose graph so it has portrait instead of landscape orientation.
1000
- // Always compare shorter to longer sequence for consistency and optimization.
1001
- transposed = !transposed;
1002
- if (transposed && callbacks.length === 1) {
1003
- // Lazily wrap callback functions to swap args if graph is transposed.
1004
- const {foundSubsequence, isCommon} = callbacks[0];
1005
- callbacks[1] = {
1006
- foundSubsequence: (nCommon, bCommon, aCommon) => {
1007
- foundSubsequence(nCommon, aCommon, bCommon);
1008
- },
1009
- isCommon: (bIndex, aIndex) => isCommon(aIndex, bIndex)
1010
- };
1011
- }
1012
- const tStart = aStart;
1013
- const tEnd = aEnd;
1014
- aStart = bStart;
1015
- aEnd = bEnd;
1016
- bStart = tStart;
1017
- bEnd = tEnd;
1018
- }
1019
- const {foundSubsequence, isCommon} = callbacks[transposed ? 1 : 0];
1020
-
1021
- // Divide the index intervals at the middle change.
1022
- divide(
1023
- nChange,
1024
- aStart,
1025
- aEnd,
1026
- bStart,
1027
- bEnd,
1028
- isCommon,
1029
- aIndexesF,
1030
- aIndexesR,
1031
- division
1032
- );
1033
- const {
1034
- nChangePreceding,
1035
- aEndPreceding,
1036
- bEndPreceding,
1037
- nCommonPreceding,
1038
- aCommonPreceding,
1039
- bCommonPreceding,
1040
- nCommonFollowing,
1041
- aCommonFollowing,
1042
- bCommonFollowing,
1043
- nChangeFollowing,
1044
- aStartFollowing,
1045
- bStartFollowing
1046
- } = division;
1047
-
1048
- // Unless either index interval is empty, they might contain common items.
1049
- if (aStart < aEndPreceding && bStart < bEndPreceding) {
1050
- // Recursely find and return common subsequences preceding the division.
1051
- findSubsequences(
1052
- nChangePreceding,
1053
- aStart,
1054
- aEndPreceding,
1055
- bStart,
1056
- bEndPreceding,
1057
- transposed,
1058
- callbacks,
1059
- aIndexesF,
1060
- aIndexesR,
1061
- division
1062
- );
1063
- }
1064
-
1065
- // Return common subsequences that are adjacent to the middle change.
1066
- if (nCommonPreceding !== 0) {
1067
- foundSubsequence(nCommonPreceding, aCommonPreceding, bCommonPreceding);
1068
- }
1069
- if (nCommonFollowing !== 0) {
1070
- foundSubsequence(nCommonFollowing, aCommonFollowing, bCommonFollowing);
1071
- }
1072
-
1073
- // Unless either index interval is empty, they might contain common items.
1074
- if (aStartFollowing < aEnd && bStartFollowing < bEnd) {
1075
- // Recursely find and return common subsequences following the division.
1076
- findSubsequences(
1077
- nChangeFollowing,
1078
- aStartFollowing,
1079
- aEnd,
1080
- bStartFollowing,
1081
- bEnd,
1082
- transposed,
1083
- callbacks,
1084
- aIndexesF,
1085
- aIndexesR,
1086
- division
1087
- );
1088
- }
1089
- };
1090
- const validateLength = (name, arg) => {
1091
- if (typeof arg !== 'number') {
1092
- throw new TypeError(`${pkg}: ${name} typeof ${typeof arg} is not a number`);
1093
- }
1094
- if (!Number.isSafeInteger(arg)) {
1095
- throw new RangeError(`${pkg}: ${name} value ${arg} is not a safe integer`);
1096
- }
1097
- if (arg < 0) {
1098
- throw new RangeError(`${pkg}: ${name} value ${arg} is a negative integer`);
1099
- }
1100
- };
1101
- const validateCallback = (name, arg) => {
1102
- const type = typeof arg;
1103
- if (type !== 'function') {
1104
- throw new TypeError(`${pkg}: ${name} typeof ${type} is not a function`);
1105
- }
1106
- };
1107
-
1108
- // Compare items in two sequences to find a longest common subsequence.
1109
- // Given lengths of sequences and input function to compare items at indexes,
1110
- // return by output function the number of adjacent items and starting indexes
1111
- // of each common subsequence.
1112
- function diffSequence(aLength, bLength, isCommon, foundSubsequence) {
1113
- validateLength('aLength', aLength);
1114
- validateLength('bLength', bLength);
1115
- validateCallback('isCommon', isCommon);
1116
- validateCallback('foundSubsequence', foundSubsequence);
1117
-
1118
- // Count common items from the start in the forward direction.
1119
- const nCommonF = countCommonItemsF(0, aLength, 0, bLength, isCommon);
1120
- if (nCommonF !== 0) {
1121
- foundSubsequence(nCommonF, 0, 0);
1122
- }
1123
-
1124
- // Unless both sequences consist of common items only,
1125
- // find common items in the half-trimmed index intervals.
1126
- if (aLength !== nCommonF || bLength !== nCommonF) {
1127
- // Invariant: intervals do not have common items at the start.
1128
- // The start of an index interval is closed like array slice method.
1129
- const aStart = nCommonF;
1130
- const bStart = nCommonF;
1131
-
1132
- // Count common items from the end in the reverse direction.
1133
- const nCommonR = countCommonItemsR(
1134
- aStart,
1135
- aLength - 1,
1136
- bStart,
1137
- bLength - 1,
1138
- isCommon
1139
- );
1140
-
1141
- // Invariant: intervals do not have common items at the end.
1142
- // The end of an index interval is open like array slice method.
1143
- const aEnd = aLength - nCommonR;
1144
- const bEnd = bLength - nCommonR;
1145
-
1146
- // Unless one sequence consists of common items only,
1147
- // therefore the other trimmed index interval consists of changes only,
1148
- // find common items in the trimmed index intervals.
1149
- const nCommonFR = nCommonF + nCommonR;
1150
- if (aLength !== nCommonFR && bLength !== nCommonFR) {
1151
- const nChange = 0; // number of change items is not yet known
1152
- const transposed = false; // call the original unwrapped functions
1153
- const callbacks = [
1154
- {
1155
- foundSubsequence,
1156
- isCommon
1157
- }
1158
- ];
1159
-
1160
- // Indexes in sequence a of last points in furthest reaching paths
1161
- // from outside the start at top left in the forward direction:
1162
- const aIndexesF = [NOT_YET_SET];
1163
- // from the end at bottom right in the reverse direction:
1164
- const aIndexesR = [NOT_YET_SET];
1165
-
1166
- // Initialize one object as output of all calls to divide function.
1167
- const division = {
1168
- aCommonFollowing: NOT_YET_SET,
1169
- aCommonPreceding: NOT_YET_SET,
1170
- aEndPreceding: NOT_YET_SET,
1171
- aStartFollowing: NOT_YET_SET,
1172
- bCommonFollowing: NOT_YET_SET,
1173
- bCommonPreceding: NOT_YET_SET,
1174
- bEndPreceding: NOT_YET_SET,
1175
- bStartFollowing: NOT_YET_SET,
1176
- nChangeFollowing: NOT_YET_SET,
1177
- nChangePreceding: NOT_YET_SET,
1178
- nCommonFollowing: NOT_YET_SET,
1179
- nCommonPreceding: NOT_YET_SET
1180
- };
1181
-
1182
- // Find and return common subsequences in the trimmed index intervals.
1183
- findSubsequences(
1184
- nChange,
1185
- aStart,
1186
- aEnd,
1187
- bStart,
1188
- bEnd,
1189
- transposed,
1190
- callbacks,
1191
- aIndexesF,
1192
- aIndexesR,
1193
- division
1194
- );
1195
- }
1196
- if (nCommonR !== 0) {
1197
- foundSubsequence(nCommonR, aEnd, bEnd);
1198
- }
1199
- }
405
+ var hasRequiredBuild;
406
+
407
+ function requireBuild () {
408
+ if (hasRequiredBuild) return build;
409
+ hasRequiredBuild = 1;
410
+
411
+ Object.defineProperty(build, '__esModule', {
412
+ value: true
413
+ });
414
+ build.default = diffSequence;
415
+ /**
416
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
417
+ *
418
+ * This source code is licensed under the MIT license found in the
419
+ * LICENSE file in the root directory of this source tree.
420
+ *
421
+ */
422
+
423
+ // This diff-sequences package implements the linear space variation in
424
+ // An O(ND) Difference Algorithm and Its Variations by Eugene W. Myers
425
+
426
+ // Relationship in notation between Myers paper and this package:
427
+ // A is a
428
+ // N is aLength, aEnd - aStart, and so on
429
+ // x is aIndex, aFirst, aLast, and so on
430
+ // B is b
431
+ // M is bLength, bEnd - bStart, and so on
432
+ // y is bIndex, bFirst, bLast, and so on
433
+ // Δ = N - M is negative of baDeltaLength = bLength - aLength
434
+ // D is d
435
+ // k is kF
436
+ // k + Δ is kF = kR - baDeltaLength
437
+ // V is aIndexesF or aIndexesR (see comment below about Indexes type)
438
+ // index intervals [1, N] and [1, M] are [0, aLength) and [0, bLength)
439
+ // starting point in forward direction (0, 0) is (-1, -1)
440
+ // starting point in reverse direction (N + 1, M + 1) is (aLength, bLength)
441
+
442
+ // The “edit graph” for sequences a and b corresponds to items:
443
+ // in a on the horizontal axis
444
+ // in b on the vertical axis
445
+ //
446
+ // Given a-coordinate of a point in a diagonal, you can compute b-coordinate.
447
+ //
448
+ // Forward diagonals kF:
449
+ // zero diagonal intersects top left corner
450
+ // positive diagonals intersect top edge
451
+ // negative diagonals insersect left edge
452
+ //
453
+ // Reverse diagonals kR:
454
+ // zero diagonal intersects bottom right corner
455
+ // positive diagonals intersect right edge
456
+ // negative diagonals intersect bottom edge
457
+
458
+ // The graph contains a directed acyclic graph of edges:
459
+ // horizontal: delete an item from a
460
+ // vertical: insert an item from b
461
+ // diagonal: common item in a and b
462
+ //
463
+ // The algorithm solves dual problems in the graph analogy:
464
+ // Find longest common subsequence: path with maximum number of diagonal edges
465
+ // Find shortest edit script: path with minimum number of non-diagonal edges
466
+
467
+ // Input callback function compares items at indexes in the sequences.
468
+
469
+ // Output callback function receives the number of adjacent items
470
+ // and starting indexes of each common subsequence.
471
+ // Either original functions or wrapped to swap indexes if graph is transposed.
472
+ // Indexes in sequence a of last point of forward or reverse paths in graph.
473
+ // Myers algorithm indexes by diagonal k which for negative is bad deopt in V8.
474
+ // This package indexes by iF and iR which are greater than or equal to zero.
475
+ // and also updates the index arrays in place to cut memory in half.
476
+ // kF = 2 * iF - d
477
+ // kR = d - 2 * iR
478
+ // Division of index intervals in sequences a and b at the middle change.
479
+ // Invariant: intervals do not have common items at the start or end.
480
+ const pkg = 'diff-sequences'; // for error messages
481
+ const NOT_YET_SET = 0; // small int instead of undefined to avoid deopt in V8
482
+
483
+ // Return the number of common items that follow in forward direction.
484
+ // The length of what Myers paper calls a “snake” in a forward path.
485
+ const countCommonItemsF = (aIndex, aEnd, bIndex, bEnd, isCommon) => {
486
+ let nCommon = 0;
487
+ while (aIndex < aEnd && bIndex < bEnd && isCommon(aIndex, bIndex)) {
488
+ aIndex += 1;
489
+ bIndex += 1;
490
+ nCommon += 1;
491
+ }
492
+ return nCommon;
493
+ };
494
+
495
+ // Return the number of common items that precede in reverse direction.
496
+ // The length of what Myers paper calls a “snake” in a reverse path.
497
+ const countCommonItemsR = (aStart, aIndex, bStart, bIndex, isCommon) => {
498
+ let nCommon = 0;
499
+ while (aStart <= aIndex && bStart <= bIndex && isCommon(aIndex, bIndex)) {
500
+ aIndex -= 1;
501
+ bIndex -= 1;
502
+ nCommon += 1;
503
+ }
504
+ return nCommon;
505
+ };
506
+
507
+ // A simple function to extend forward paths from (d - 1) to d changes
508
+ // when forward and reverse paths cannot yet overlap.
509
+ const extendPathsF = (
510
+ d,
511
+ aEnd,
512
+ bEnd,
513
+ bF,
514
+ isCommon,
515
+ aIndexesF,
516
+ iMaxF // return the value because optimization might decrease it
517
+ ) => {
518
+ // Unroll the first iteration.
519
+ let iF = 0;
520
+ let kF = -d; // kF = 2 * iF - d
521
+ let aFirst = aIndexesF[iF]; // in first iteration always insert
522
+ let aIndexPrev1 = aFirst; // prev value of [iF - 1] in next iteration
523
+ aIndexesF[iF] += countCommonItemsF(
524
+ aFirst + 1,
525
+ aEnd,
526
+ bF + aFirst - kF + 1,
527
+ bEnd,
528
+ isCommon
529
+ );
530
+
531
+ // Optimization: skip diagonals in which paths cannot ever overlap.
532
+ const nF = d < iMaxF ? d : iMaxF;
533
+
534
+ // The diagonals kF are odd when d is odd and even when d is even.
535
+ for (iF += 1, kF += 2; iF <= nF; iF += 1, kF += 2) {
536
+ // To get first point of path segment, move one change in forward direction
537
+ // from last point of previous path segment in an adjacent diagonal.
538
+ // In last possible iteration when iF === d and kF === d always delete.
539
+ if (iF !== d && aIndexPrev1 < aIndexesF[iF]) {
540
+ aFirst = aIndexesF[iF]; // vertical to insert from b
541
+ } else {
542
+ aFirst = aIndexPrev1 + 1; // horizontal to delete from a
543
+
544
+ if (aEnd <= aFirst) {
545
+ // Optimization: delete moved past right of graph.
546
+ return iF - 1;
547
+ }
548
+ }
549
+
550
+ // To get last point of path segment, move along diagonal of common items.
551
+ aIndexPrev1 = aIndexesF[iF];
552
+ aIndexesF[iF] =
553
+ aFirst +
554
+ countCommonItemsF(aFirst + 1, aEnd, bF + aFirst - kF + 1, bEnd, isCommon);
555
+ }
556
+ return iMaxF;
557
+ };
558
+
559
+ // A simple function to extend reverse paths from (d - 1) to d changes
560
+ // when reverse and forward paths cannot yet overlap.
561
+ const extendPathsR = (
562
+ d,
563
+ aStart,
564
+ bStart,
565
+ bR,
566
+ isCommon,
567
+ aIndexesR,
568
+ iMaxR // return the value because optimization might decrease it
569
+ ) => {
570
+ // Unroll the first iteration.
571
+ let iR = 0;
572
+ let kR = d; // kR = d - 2 * iR
573
+ let aFirst = aIndexesR[iR]; // in first iteration always insert
574
+ let aIndexPrev1 = aFirst; // prev value of [iR - 1] in next iteration
575
+ aIndexesR[iR] -= countCommonItemsR(
576
+ aStart,
577
+ aFirst - 1,
578
+ bStart,
579
+ bR + aFirst - kR - 1,
580
+ isCommon
581
+ );
582
+
583
+ // Optimization: skip diagonals in which paths cannot ever overlap.
584
+ const nR = d < iMaxR ? d : iMaxR;
585
+
586
+ // The diagonals kR are odd when d is odd and even when d is even.
587
+ for (iR += 1, kR -= 2; iR <= nR; iR += 1, kR -= 2) {
588
+ // To get first point of path segment, move one change in reverse direction
589
+ // from last point of previous path segment in an adjacent diagonal.
590
+ // In last possible iteration when iR === d and kR === -d always delete.
591
+ if (iR !== d && aIndexesR[iR] < aIndexPrev1) {
592
+ aFirst = aIndexesR[iR]; // vertical to insert from b
593
+ } else {
594
+ aFirst = aIndexPrev1 - 1; // horizontal to delete from a
595
+
596
+ if (aFirst < aStart) {
597
+ // Optimization: delete moved past left of graph.
598
+ return iR - 1;
599
+ }
600
+ }
601
+
602
+ // To get last point of path segment, move along diagonal of common items.
603
+ aIndexPrev1 = aIndexesR[iR];
604
+ aIndexesR[iR] =
605
+ aFirst -
606
+ countCommonItemsR(
607
+ aStart,
608
+ aFirst - 1,
609
+ bStart,
610
+ bR + aFirst - kR - 1,
611
+ isCommon
612
+ );
613
+ }
614
+ return iMaxR;
615
+ };
616
+
617
+ // A complete function to extend forward paths from (d - 1) to d changes.
618
+ // Return true if a path overlaps reverse path of (d - 1) changes in its diagonal.
619
+ const extendOverlappablePathsF = (
620
+ d,
621
+ aStart,
622
+ aEnd,
623
+ bStart,
624
+ bEnd,
625
+ isCommon,
626
+ aIndexesF,
627
+ iMaxF,
628
+ aIndexesR,
629
+ iMaxR,
630
+ division // update prop values if return true
631
+ ) => {
632
+ const bF = bStart - aStart; // bIndex = bF + aIndex - kF
633
+ const aLength = aEnd - aStart;
634
+ const bLength = bEnd - bStart;
635
+ const baDeltaLength = bLength - aLength; // kF = kR - baDeltaLength
636
+
637
+ // Range of diagonals in which forward and reverse paths might overlap.
638
+ const kMinOverlapF = -baDeltaLength - (d - 1); // -(d - 1) <= kR
639
+ const kMaxOverlapF = -baDeltaLength + (d - 1); // kR <= (d - 1)
640
+
641
+ let aIndexPrev1 = NOT_YET_SET; // prev value of [iF - 1] in next iteration
642
+
643
+ // Optimization: skip diagonals in which paths cannot ever overlap.
644
+ const nF = d < iMaxF ? d : iMaxF;
645
+
646
+ // The diagonals kF = 2 * iF - d are odd when d is odd and even when d is even.
647
+ for (let iF = 0, kF = -d; iF <= nF; iF += 1, kF += 2) {
648
+ // To get first point of path segment, move one change in forward direction
649
+ // from last point of previous path segment in an adjacent diagonal.
650
+ // In first iteration when iF === 0 and kF === -d always insert.
651
+ // In last possible iteration when iF === d and kF === d always delete.
652
+ const insert = iF === 0 || (iF !== d && aIndexPrev1 < aIndexesF[iF]);
653
+ const aLastPrev = insert ? aIndexesF[iF] : aIndexPrev1;
654
+ const aFirst = insert
655
+ ? aLastPrev // vertical to insert from b
656
+ : aLastPrev + 1; // horizontal to delete from a
657
+
658
+ // To get last point of path segment, move along diagonal of common items.
659
+ const bFirst = bF + aFirst - kF;
660
+ const nCommonF = countCommonItemsF(
661
+ aFirst + 1,
662
+ aEnd,
663
+ bFirst + 1,
664
+ bEnd,
665
+ isCommon
666
+ );
667
+ const aLast = aFirst + nCommonF;
668
+ aIndexPrev1 = aIndexesF[iF];
669
+ aIndexesF[iF] = aLast;
670
+ if (kMinOverlapF <= kF && kF <= kMaxOverlapF) {
671
+ // Solve for iR of reverse path with (d - 1) changes in diagonal kF:
672
+ // kR = kF + baDeltaLength
673
+ // kR = (d - 1) - 2 * iR
674
+ const iR = (d - 1 - (kF + baDeltaLength)) / 2;
675
+
676
+ // If this forward path overlaps the reverse path in this diagonal,
677
+ // then this is the middle change of the index intervals.
678
+ if (iR <= iMaxR && aIndexesR[iR] - 1 <= aLast) {
679
+ // Unlike the Myers algorithm which finds only the middle “snake”
680
+ // this package can find two common subsequences per division.
681
+ // Last point of previous path segment is on an adjacent diagonal.
682
+ const bLastPrev = bF + aLastPrev - (insert ? kF + 1 : kF - 1);
683
+
684
+ // Because of invariant that intervals preceding the middle change
685
+ // cannot have common items at the end,
686
+ // move in reverse direction along a diagonal of common items.
687
+ const nCommonR = countCommonItemsR(
688
+ aStart,
689
+ aLastPrev,
690
+ bStart,
691
+ bLastPrev,
692
+ isCommon
693
+ );
694
+ const aIndexPrevFirst = aLastPrev - nCommonR;
695
+ const bIndexPrevFirst = bLastPrev - nCommonR;
696
+ const aEndPreceding = aIndexPrevFirst + 1;
697
+ const bEndPreceding = bIndexPrevFirst + 1;
698
+ division.nChangePreceding = d - 1;
699
+ if (d - 1 === aEndPreceding + bEndPreceding - aStart - bStart) {
700
+ // Optimization: number of preceding changes in forward direction
701
+ // is equal to number of items in preceding interval,
702
+ // therefore it cannot contain any common items.
703
+ division.aEndPreceding = aStart;
704
+ division.bEndPreceding = bStart;
705
+ } else {
706
+ division.aEndPreceding = aEndPreceding;
707
+ division.bEndPreceding = bEndPreceding;
708
+ }
709
+ division.nCommonPreceding = nCommonR;
710
+ if (nCommonR !== 0) {
711
+ division.aCommonPreceding = aEndPreceding;
712
+ division.bCommonPreceding = bEndPreceding;
713
+ }
714
+ division.nCommonFollowing = nCommonF;
715
+ if (nCommonF !== 0) {
716
+ division.aCommonFollowing = aFirst + 1;
717
+ division.bCommonFollowing = bFirst + 1;
718
+ }
719
+ const aStartFollowing = aLast + 1;
720
+ const bStartFollowing = bFirst + nCommonF + 1;
721
+ division.nChangeFollowing = d - 1;
722
+ if (d - 1 === aEnd + bEnd - aStartFollowing - bStartFollowing) {
723
+ // Optimization: number of changes in reverse direction
724
+ // is equal to number of items in following interval,
725
+ // therefore it cannot contain any common items.
726
+ division.aStartFollowing = aEnd;
727
+ division.bStartFollowing = bEnd;
728
+ } else {
729
+ division.aStartFollowing = aStartFollowing;
730
+ division.bStartFollowing = bStartFollowing;
731
+ }
732
+ return true;
733
+ }
734
+ }
735
+ }
736
+ return false;
737
+ };
738
+
739
+ // A complete function to extend reverse paths from (d - 1) to d changes.
740
+ // Return true if a path overlaps forward path of d changes in its diagonal.
741
+ const extendOverlappablePathsR = (
742
+ d,
743
+ aStart,
744
+ aEnd,
745
+ bStart,
746
+ bEnd,
747
+ isCommon,
748
+ aIndexesF,
749
+ iMaxF,
750
+ aIndexesR,
751
+ iMaxR,
752
+ division // update prop values if return true
753
+ ) => {
754
+ const bR = bEnd - aEnd; // bIndex = bR + aIndex - kR
755
+ const aLength = aEnd - aStart;
756
+ const bLength = bEnd - bStart;
757
+ const baDeltaLength = bLength - aLength; // kR = kF + baDeltaLength
758
+
759
+ // Range of diagonals in which forward and reverse paths might overlap.
760
+ const kMinOverlapR = baDeltaLength - d; // -d <= kF
761
+ const kMaxOverlapR = baDeltaLength + d; // kF <= d
762
+
763
+ let aIndexPrev1 = NOT_YET_SET; // prev value of [iR - 1] in next iteration
764
+
765
+ // Optimization: skip diagonals in which paths cannot ever overlap.
766
+ const nR = d < iMaxR ? d : iMaxR;
767
+
768
+ // The diagonals kR = d - 2 * iR are odd when d is odd and even when d is even.
769
+ for (let iR = 0, kR = d; iR <= nR; iR += 1, kR -= 2) {
770
+ // To get first point of path segment, move one change in reverse direction
771
+ // from last point of previous path segment in an adjacent diagonal.
772
+ // In first iteration when iR === 0 and kR === d always insert.
773
+ // In last possible iteration when iR === d and kR === -d always delete.
774
+ const insert = iR === 0 || (iR !== d && aIndexesR[iR] < aIndexPrev1);
775
+ const aLastPrev = insert ? aIndexesR[iR] : aIndexPrev1;
776
+ const aFirst = insert
777
+ ? aLastPrev // vertical to insert from b
778
+ : aLastPrev - 1; // horizontal to delete from a
779
+
780
+ // To get last point of path segment, move along diagonal of common items.
781
+ const bFirst = bR + aFirst - kR;
782
+ const nCommonR = countCommonItemsR(
783
+ aStart,
784
+ aFirst - 1,
785
+ bStart,
786
+ bFirst - 1,
787
+ isCommon
788
+ );
789
+ const aLast = aFirst - nCommonR;
790
+ aIndexPrev1 = aIndexesR[iR];
791
+ aIndexesR[iR] = aLast;
792
+ if (kMinOverlapR <= kR && kR <= kMaxOverlapR) {
793
+ // Solve for iF of forward path with d changes in diagonal kR:
794
+ // kF = kR - baDeltaLength
795
+ // kF = 2 * iF - d
796
+ const iF = (d + (kR - baDeltaLength)) / 2;
797
+
798
+ // If this reverse path overlaps the forward path in this diagonal,
799
+ // then this is a middle change of the index intervals.
800
+ if (iF <= iMaxF && aLast - 1 <= aIndexesF[iF]) {
801
+ const bLast = bFirst - nCommonR;
802
+ division.nChangePreceding = d;
803
+ if (d === aLast + bLast - aStart - bStart) {
804
+ // Optimization: number of changes in reverse direction
805
+ // is equal to number of items in preceding interval,
806
+ // therefore it cannot contain any common items.
807
+ division.aEndPreceding = aStart;
808
+ division.bEndPreceding = bStart;
809
+ } else {
810
+ division.aEndPreceding = aLast;
811
+ division.bEndPreceding = bLast;
812
+ }
813
+ division.nCommonPreceding = nCommonR;
814
+ if (nCommonR !== 0) {
815
+ // The last point of reverse path segment is start of common subsequence.
816
+ division.aCommonPreceding = aLast;
817
+ division.bCommonPreceding = bLast;
818
+ }
819
+ division.nChangeFollowing = d - 1;
820
+ if (d === 1) {
821
+ // There is no previous path segment.
822
+ division.nCommonFollowing = 0;
823
+ division.aStartFollowing = aEnd;
824
+ division.bStartFollowing = bEnd;
825
+ } else {
826
+ // Unlike the Myers algorithm which finds only the middle “snake”
827
+ // this package can find two common subsequences per division.
828
+ // Last point of previous path segment is on an adjacent diagonal.
829
+ const bLastPrev = bR + aLastPrev - (insert ? kR - 1 : kR + 1);
830
+
831
+ // Because of invariant that intervals following the middle change
832
+ // cannot have common items at the start,
833
+ // move in forward direction along a diagonal of common items.
834
+ const nCommonF = countCommonItemsF(
835
+ aLastPrev,
836
+ aEnd,
837
+ bLastPrev,
838
+ bEnd,
839
+ isCommon
840
+ );
841
+ division.nCommonFollowing = nCommonF;
842
+ if (nCommonF !== 0) {
843
+ // The last point of reverse path segment is start of common subsequence.
844
+ division.aCommonFollowing = aLastPrev;
845
+ division.bCommonFollowing = bLastPrev;
846
+ }
847
+ const aStartFollowing = aLastPrev + nCommonF; // aFirstPrev
848
+ const bStartFollowing = bLastPrev + nCommonF; // bFirstPrev
849
+
850
+ if (d - 1 === aEnd + bEnd - aStartFollowing - bStartFollowing) {
851
+ // Optimization: number of changes in forward direction
852
+ // is equal to number of items in following interval,
853
+ // therefore it cannot contain any common items.
854
+ division.aStartFollowing = aEnd;
855
+ division.bStartFollowing = bEnd;
856
+ } else {
857
+ division.aStartFollowing = aStartFollowing;
858
+ division.bStartFollowing = bStartFollowing;
859
+ }
860
+ }
861
+ return true;
862
+ }
863
+ }
864
+ }
865
+ return false;
866
+ };
867
+
868
+ // Given index intervals and input function to compare items at indexes,
869
+ // divide at the middle change.
870
+ //
871
+ // DO NOT CALL if start === end, because interval cannot contain common items
872
+ // and because this function will throw the “no overlap” error.
873
+ const divide = (
874
+ nChange,
875
+ aStart,
876
+ aEnd,
877
+ bStart,
878
+ bEnd,
879
+ isCommon,
880
+ aIndexesF,
881
+ aIndexesR,
882
+ division // output
883
+ ) => {
884
+ const bF = bStart - aStart; // bIndex = bF + aIndex - kF
885
+ const bR = bEnd - aEnd; // bIndex = bR + aIndex - kR
886
+ const aLength = aEnd - aStart;
887
+ const bLength = bEnd - bStart;
888
+
889
+ // Because graph has square or portrait orientation,
890
+ // length difference is minimum number of items to insert from b.
891
+ // Corresponding forward and reverse diagonals in graph
892
+ // depend on length difference of the sequences:
893
+ // kF = kR - baDeltaLength
894
+ // kR = kF + baDeltaLength
895
+ const baDeltaLength = bLength - aLength;
896
+
897
+ // Optimization: max diagonal in graph intersects corner of shorter side.
898
+ let iMaxF = aLength;
899
+ let iMaxR = aLength;
900
+
901
+ // Initialize no changes yet in forward or reverse direction:
902
+ aIndexesF[0] = aStart - 1; // at open start of interval, outside closed start
903
+ aIndexesR[0] = aEnd; // at open end of interval
904
+
905
+ if (baDeltaLength % 2 === 0) {
906
+ // The number of changes in paths is 2 * d if length difference is even.
907
+ const dMin = (nChange || baDeltaLength) / 2;
908
+ const dMax = (aLength + bLength) / 2;
909
+ for (let d = 1; d <= dMax; d += 1) {
910
+ iMaxF = extendPathsF(d, aEnd, bEnd, bF, isCommon, aIndexesF, iMaxF);
911
+ if (d < dMin) {
912
+ iMaxR = extendPathsR(d, aStart, bStart, bR, isCommon, aIndexesR, iMaxR);
913
+ } else if (
914
+ // If a reverse path overlaps a forward path in the same diagonal,
915
+ // return a division of the index intervals at the middle change.
916
+ extendOverlappablePathsR(
917
+ d,
918
+ aStart,
919
+ aEnd,
920
+ bStart,
921
+ bEnd,
922
+ isCommon,
923
+ aIndexesF,
924
+ iMaxF,
925
+ aIndexesR,
926
+ iMaxR,
927
+ division
928
+ )
929
+ ) {
930
+ return;
931
+ }
932
+ }
933
+ } else {
934
+ // The number of changes in paths is 2 * d - 1 if length difference is odd.
935
+ const dMin = ((nChange || baDeltaLength) + 1) / 2;
936
+ const dMax = (aLength + bLength + 1) / 2;
937
+
938
+ // Unroll first half iteration so loop extends the relevant pairs of paths.
939
+ // Because of invariant that intervals have no common items at start or end,
940
+ // and limitation not to call divide with empty intervals,
941
+ // therefore it cannot be called if a forward path with one change
942
+ // would overlap a reverse path with no changes, even if dMin === 1.
943
+ let d = 1;
944
+ iMaxF = extendPathsF(d, aEnd, bEnd, bF, isCommon, aIndexesF, iMaxF);
945
+ for (d += 1; d <= dMax; d += 1) {
946
+ iMaxR = extendPathsR(
947
+ d - 1,
948
+ aStart,
949
+ bStart,
950
+ bR,
951
+ isCommon,
952
+ aIndexesR,
953
+ iMaxR
954
+ );
955
+ if (d < dMin) {
956
+ iMaxF = extendPathsF(d, aEnd, bEnd, bF, isCommon, aIndexesF, iMaxF);
957
+ } else if (
958
+ // If a forward path overlaps a reverse path in the same diagonal,
959
+ // return a division of the index intervals at the middle change.
960
+ extendOverlappablePathsF(
961
+ d,
962
+ aStart,
963
+ aEnd,
964
+ bStart,
965
+ bEnd,
966
+ isCommon,
967
+ aIndexesF,
968
+ iMaxF,
969
+ aIndexesR,
970
+ iMaxR,
971
+ division
972
+ )
973
+ ) {
974
+ return;
975
+ }
976
+ }
977
+ }
978
+
979
+ /* istanbul ignore next */
980
+ throw new Error(
981
+ `${pkg}: no overlap aStart=${aStart} aEnd=${aEnd} bStart=${bStart} bEnd=${bEnd}`
982
+ );
983
+ };
984
+
985
+ // Given index intervals and input function to compare items at indexes,
986
+ // return by output function the number of adjacent items and starting indexes
987
+ // of each common subsequence. Divide and conquer with only linear space.
988
+ //
989
+ // The index intervals are half open [start, end) like array slice method.
990
+ // DO NOT CALL if start === end, because interval cannot contain common items
991
+ // and because divide function will throw the “no overlap” error.
992
+ const findSubsequences = (
993
+ nChange,
994
+ aStart,
995
+ aEnd,
996
+ bStart,
997
+ bEnd,
998
+ transposed,
999
+ callbacks,
1000
+ aIndexesF,
1001
+ aIndexesR,
1002
+ division // temporary memory, not input nor output
1003
+ ) => {
1004
+ if (bEnd - bStart < aEnd - aStart) {
1005
+ // Transpose graph so it has portrait instead of landscape orientation.
1006
+ // Always compare shorter to longer sequence for consistency and optimization.
1007
+ transposed = !transposed;
1008
+ if (transposed && callbacks.length === 1) {
1009
+ // Lazily wrap callback functions to swap args if graph is transposed.
1010
+ const {foundSubsequence, isCommon} = callbacks[0];
1011
+ callbacks[1] = {
1012
+ foundSubsequence: (nCommon, bCommon, aCommon) => {
1013
+ foundSubsequence(nCommon, aCommon, bCommon);
1014
+ },
1015
+ isCommon: (bIndex, aIndex) => isCommon(aIndex, bIndex)
1016
+ };
1017
+ }
1018
+ const tStart = aStart;
1019
+ const tEnd = aEnd;
1020
+ aStart = bStart;
1021
+ aEnd = bEnd;
1022
+ bStart = tStart;
1023
+ bEnd = tEnd;
1024
+ }
1025
+ const {foundSubsequence, isCommon} = callbacks[transposed ? 1 : 0];
1026
+
1027
+ // Divide the index intervals at the middle change.
1028
+ divide(
1029
+ nChange,
1030
+ aStart,
1031
+ aEnd,
1032
+ bStart,
1033
+ bEnd,
1034
+ isCommon,
1035
+ aIndexesF,
1036
+ aIndexesR,
1037
+ division
1038
+ );
1039
+ const {
1040
+ nChangePreceding,
1041
+ aEndPreceding,
1042
+ bEndPreceding,
1043
+ nCommonPreceding,
1044
+ aCommonPreceding,
1045
+ bCommonPreceding,
1046
+ nCommonFollowing,
1047
+ aCommonFollowing,
1048
+ bCommonFollowing,
1049
+ nChangeFollowing,
1050
+ aStartFollowing,
1051
+ bStartFollowing
1052
+ } = division;
1053
+
1054
+ // Unless either index interval is empty, they might contain common items.
1055
+ if (aStart < aEndPreceding && bStart < bEndPreceding) {
1056
+ // Recursely find and return common subsequences preceding the division.
1057
+ findSubsequences(
1058
+ nChangePreceding,
1059
+ aStart,
1060
+ aEndPreceding,
1061
+ bStart,
1062
+ bEndPreceding,
1063
+ transposed,
1064
+ callbacks,
1065
+ aIndexesF,
1066
+ aIndexesR,
1067
+ division
1068
+ );
1069
+ }
1070
+
1071
+ // Return common subsequences that are adjacent to the middle change.
1072
+ if (nCommonPreceding !== 0) {
1073
+ foundSubsequence(nCommonPreceding, aCommonPreceding, bCommonPreceding);
1074
+ }
1075
+ if (nCommonFollowing !== 0) {
1076
+ foundSubsequence(nCommonFollowing, aCommonFollowing, bCommonFollowing);
1077
+ }
1078
+
1079
+ // Unless either index interval is empty, they might contain common items.
1080
+ if (aStartFollowing < aEnd && bStartFollowing < bEnd) {
1081
+ // Recursely find and return common subsequences following the division.
1082
+ findSubsequences(
1083
+ nChangeFollowing,
1084
+ aStartFollowing,
1085
+ aEnd,
1086
+ bStartFollowing,
1087
+ bEnd,
1088
+ transposed,
1089
+ callbacks,
1090
+ aIndexesF,
1091
+ aIndexesR,
1092
+ division
1093
+ );
1094
+ }
1095
+ };
1096
+ const validateLength = (name, arg) => {
1097
+ if (typeof arg !== 'number') {
1098
+ throw new TypeError(`${pkg}: ${name} typeof ${typeof arg} is not a number`);
1099
+ }
1100
+ if (!Number.isSafeInteger(arg)) {
1101
+ throw new RangeError(`${pkg}: ${name} value ${arg} is not a safe integer`);
1102
+ }
1103
+ if (arg < 0) {
1104
+ throw new RangeError(`${pkg}: ${name} value ${arg} is a negative integer`);
1105
+ }
1106
+ };
1107
+ const validateCallback = (name, arg) => {
1108
+ const type = typeof arg;
1109
+ if (type !== 'function') {
1110
+ throw new TypeError(`${pkg}: ${name} typeof ${type} is not a function`);
1111
+ }
1112
+ };
1113
+
1114
+ // Compare items in two sequences to find a longest common subsequence.
1115
+ // Given lengths of sequences and input function to compare items at indexes,
1116
+ // return by output function the number of adjacent items and starting indexes
1117
+ // of each common subsequence.
1118
+ function diffSequence(aLength, bLength, isCommon, foundSubsequence) {
1119
+ validateLength('aLength', aLength);
1120
+ validateLength('bLength', bLength);
1121
+ validateCallback('isCommon', isCommon);
1122
+ validateCallback('foundSubsequence', foundSubsequence);
1123
+
1124
+ // Count common items from the start in the forward direction.
1125
+ const nCommonF = countCommonItemsF(0, aLength, 0, bLength, isCommon);
1126
+ if (nCommonF !== 0) {
1127
+ foundSubsequence(nCommonF, 0, 0);
1128
+ }
1129
+
1130
+ // Unless both sequences consist of common items only,
1131
+ // find common items in the half-trimmed index intervals.
1132
+ if (aLength !== nCommonF || bLength !== nCommonF) {
1133
+ // Invariant: intervals do not have common items at the start.
1134
+ // The start of an index interval is closed like array slice method.
1135
+ const aStart = nCommonF;
1136
+ const bStart = nCommonF;
1137
+
1138
+ // Count common items from the end in the reverse direction.
1139
+ const nCommonR = countCommonItemsR(
1140
+ aStart,
1141
+ aLength - 1,
1142
+ bStart,
1143
+ bLength - 1,
1144
+ isCommon
1145
+ );
1146
+
1147
+ // Invariant: intervals do not have common items at the end.
1148
+ // The end of an index interval is open like array slice method.
1149
+ const aEnd = aLength - nCommonR;
1150
+ const bEnd = bLength - nCommonR;
1151
+
1152
+ // Unless one sequence consists of common items only,
1153
+ // therefore the other trimmed index interval consists of changes only,
1154
+ // find common items in the trimmed index intervals.
1155
+ const nCommonFR = nCommonF + nCommonR;
1156
+ if (aLength !== nCommonFR && bLength !== nCommonFR) {
1157
+ const nChange = 0; // number of change items is not yet known
1158
+ const transposed = false; // call the original unwrapped functions
1159
+ const callbacks = [
1160
+ {
1161
+ foundSubsequence,
1162
+ isCommon
1163
+ }
1164
+ ];
1165
+
1166
+ // Indexes in sequence a of last points in furthest reaching paths
1167
+ // from outside the start at top left in the forward direction:
1168
+ const aIndexesF = [NOT_YET_SET];
1169
+ // from the end at bottom right in the reverse direction:
1170
+ const aIndexesR = [NOT_YET_SET];
1171
+
1172
+ // Initialize one object as output of all calls to divide function.
1173
+ const division = {
1174
+ aCommonFollowing: NOT_YET_SET,
1175
+ aCommonPreceding: NOT_YET_SET,
1176
+ aEndPreceding: NOT_YET_SET,
1177
+ aStartFollowing: NOT_YET_SET,
1178
+ bCommonFollowing: NOT_YET_SET,
1179
+ bCommonPreceding: NOT_YET_SET,
1180
+ bEndPreceding: NOT_YET_SET,
1181
+ bStartFollowing: NOT_YET_SET,
1182
+ nChangeFollowing: NOT_YET_SET,
1183
+ nChangePreceding: NOT_YET_SET,
1184
+ nCommonFollowing: NOT_YET_SET,
1185
+ nCommonPreceding: NOT_YET_SET
1186
+ };
1187
+
1188
+ // Find and return common subsequences in the trimmed index intervals.
1189
+ findSubsequences(
1190
+ nChange,
1191
+ aStart,
1192
+ aEnd,
1193
+ bStart,
1194
+ bEnd,
1195
+ transposed,
1196
+ callbacks,
1197
+ aIndexesF,
1198
+ aIndexesR,
1199
+ division
1200
+ );
1201
+ }
1202
+ if (nCommonR !== 0) {
1203
+ foundSubsequence(nCommonR, aEnd, bEnd);
1204
+ }
1205
+ }
1206
+ }
1207
+ return build;
1200
1208
  }
1201
1209
 
1210
+ var buildExports = requireBuild();
1211
+ var diffSequences = /*@__PURE__*/getDefaultExportFromCjs(buildExports);
1212
+
1202
1213
  function formatTrailingSpaces(line, trailingSpaceFormatter) {
1203
1214
  return line.replace(/\s+$/, (match) => trailingSpaceFormatter(match));
1204
1215
  }
@@ -1578,7 +1589,7 @@ function diffLinesRaw(aLines, bLines, options) {
1578
1589
  diffs.push(new Diff(DIFF_EQUAL, bLines[bIndex]));
1579
1590
  }
1580
1591
  };
1581
- _default(aLength, bLength, isCommon, foundSubsequence);
1592
+ diffSequences(aLength, bLength, isCommon, foundSubsequence);
1582
1593
  for (; aIndex !== aLength; aIndex += 1) {
1583
1594
  diffs.push(new Diff(DIFF_DELETE, aLines[aIndex]));
1584
1595
  }
@@ -1627,7 +1638,7 @@ function diffStrings(a, b, options) {
1627
1638
  bIndex = bCommon + nCommon;
1628
1639
  diffs.push(new Diff(DIFF_EQUAL, b.slice(bCommon, bIndex)));
1629
1640
  };
1630
- _default(aLength, bLength, isCommon, foundSubsequence);
1641
+ diffSequences(aLength, bLength, isCommon, foundSubsequence);
1631
1642
  if (aIndex !== aLength) {
1632
1643
  diffs.push(new Diff(DIFF_DELETE, a.slice(aIndex)));
1633
1644
  }