forked from WinMerge/winmerge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiffList.cpp
969 lines (913 loc) · 25 KB
/
DiffList.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
/////////////////////////////////////////////////////////////////////////////
// License (GPLv2+):
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
/////////////////////////////////////////////////////////////////////////////
/**
* @file DiffList.cpp
*
* @brief Implementation file for DiffList class
*/
#include "pch.h"
#include "DiffList.h"
#include <cassert>
#include <string>
#include <sstream>
#include <algorithm>
using std::swap;
using std::vector;
/**
* @brief Swap diff sides.
*/
void DIFFRANGE::swap_sides(int index1, int index2)
{
swap(begin[index1], begin[index2]);
swap(end[index1], end[index2]);
swap(blank[index1], blank[index2]);
}
/**
* @brief Initialize DiffMap.
* @param [in] nlines Lines to add to the list.
*/
void DiffMap::InitDiffMap(int nlines)
{
// sentry value so we can check later that we set them all
m_map.assign(nlines, BAD_MAP_ENTRY);
}
/**
* @brief Default constructor, initialises difflist to 64 items.
*/
DiffList::DiffList()
: m_firstSignificant(-1)
, m_lastSignificant(-1)
, m_firstSignificantLeftMiddle(-1)
, m_firstSignificantLeftRight(-1)
, m_firstSignificantMiddleRight(-1)
, m_firstSignificantLeftOnly(-1)
, m_firstSignificantMiddleOnly(-1)
, m_firstSignificantRightOnly(-1)
, m_firstSignificantConflict(-1)
, m_lastSignificantLeftMiddle(-1)
, m_lastSignificantLeftRight(-1)
, m_lastSignificantMiddleRight(-1)
, m_lastSignificantLeftOnly(-1)
, m_lastSignificantMiddleOnly(-1)
, m_lastSignificantRightOnly(-1)
, m_lastSignificantConflict(-1)
{
m_diffs.reserve(64); // Reserve some initial space to avoid allocations.
}
/**
* @brief Removes all diffs from list.
*/
void DiffList::Clear()
{
m_diffs.clear();
m_firstSignificant = -1;
m_lastSignificant = -1;
m_firstSignificantLeftMiddle = -1;
m_firstSignificantLeftRight = -1;
m_firstSignificantMiddleRight = -1;
m_firstSignificantLeftOnly = -1;
m_firstSignificantMiddleOnly = -1;
m_firstSignificantRightOnly = -1;
m_firstSignificantConflict = -1;
m_lastSignificantLeftMiddle = -1;
m_lastSignificantLeftRight = -1;
m_lastSignificantMiddleRight = -1;
m_lastSignificantLeftOnly = -1;
m_lastSignificantMiddleOnly = -1;
m_lastSignificantRightOnly = -1;
m_lastSignificantConflict = -1;
}
/**
* @brief Returns count of items in diff list.
* This function returns total amount of items (diffs) in list. So returned
* count includes significant and non-significant diffs.
* @note Use GetSignificantDiffs() to get count of non-ignored diffs.
*/
int DiffList::GetSize() const
{
return (int) m_diffs.size();
}
/**
* @brief Returns count of significant diffs in the list.
* This function returns total count of significant diffs in list. So returned
* count doesn't include non-significant diffs.
* @return Count of significant differences.
*/
int DiffList::GetSignificantDiffs() const
{
int nSignificants = 0;
const int nDiffCount = (int) m_diffs.size();
for (int i = 0; i < nDiffCount; i++)
{
const DIFFRANGE * dfi = DiffRangeAt(i);
if (dfi->op != OP_TRIVIAL)
{
++nSignificants;
}
}
return nSignificants;
}
/**
* @brief Adds given diff to end of the list.
* Adds given difference to end of the list (append).
* @param [in] di Difference to add.
*/
void DiffList::AddDiff(const DIFFRANGE & di)
{
DiffRangeInfo dri(di);
// Allocate memory for new items exponentially
if (m_diffs.size() == m_diffs.capacity())
m_diffs.reserve(m_diffs.size() * 2);
m_diffs.push_back(dri);
}
/**
* @brief Checks if diff in given list index is significant or not.
* @param [in] nDiff Index of DIFFRANGE to check.
* @return true if diff is significant, false if not.
*/
bool DiffList::IsDiffSignificant(int nDiff) const
{
const DIFFRANGE * dfi = DiffRangeAt(nDiff);
if (dfi->op != OP_TRIVIAL)
return true;
else
return false;
}
/**
* @brief Get significant difference index of the diff.
* This function returns the index of diff when only significant differences
* are calculated.
* @param [in] nDiff Index of difference to check.
* @return Significant difference index of the diff.
*/
int DiffList::GetSignificantIndex(int nDiff) const
{
int significants = -1;
for (int i = 0; i <= nDiff; i++)
{
const DIFFRANGE * dfi = DiffRangeAt(i);
if (dfi->op != OP_TRIVIAL)
{
++significants;
}
}
return significants;
}
/**
* @brief Returns copy of DIFFRANGE from diff-list.
* @param [in] nDiff Index of DIFFRANGE to return.
* @param [out] di DIFFRANGE returned (empty if error)
* @return true if DIFFRANGE found from given index.
*/
bool DiffList::GetDiff(int nDiff, DIFFRANGE & di) const
{
const DIFFRANGE * dfi = DiffRangeAt(nDiff);
if (dfi == nullptr)
{
DIFFRANGE empty;
di = empty;
return false;
}
di = *dfi;
return true;
}
/**
* @brief Return constant pointer to requested diff.
* This function returns constant pointer to DIFFRANGE at given index.
* @param [in] nDiff Index of DIFFRANGE to return.
* @return Constant pointer to DIFFRANGE.
*/
const DIFFRANGE * DiffList::DiffRangeAt(int nDiff) const
{
if (nDiff >= 0 && nDiff < (int) m_diffs.size())
{
return &m_diffs[nDiff];
}
else
{
assert(false);
return nullptr;
}
}
/**
* @brief Replaces diff in list in given index with given diff.
* @param [in] nDiff Index (0-based) of diff to be replaced
* @param [in] di Diff to put in list.
* @return true if index was valid and diff put to list.
*/
bool DiffList::SetDiff(int nDiff, const DIFFRANGE & di)
{
if (nDiff < (int) m_diffs.size())
{
m_diffs[nDiff] = DiffRangeInfo(di);
return true;
}
else
return false;
}
/**
* @brief Checks if line is before, inside or after diff
* @param [in] nLine Linenumber to text buffer (not "real" number)
* @param [in] nDiff Index to diff table
* @return -1 if line is before diff, 0 if line is in diff and
* 1 if line is after diff.
*/
int DiffList::LineRelDiff(int nLine, int nDiff) const
{
const DIFFRANGE * dfi = DiffRangeAt(nDiff);
if (static_cast<int>(nLine) < dfi->dbegin)
return -1;
else if (static_cast<int>(nLine) > dfi->dend)
return 1;
else
return 0;
}
/**
* @brief Checks if line is inside given diff
* @param [in] nLine Linenumber to text buffer (not "real" number)
* @param [in] nDiff Index to diff table
* @return true if line is inside given difference.
*/
bool DiffList::LineInDiff(int nLine, int nDiff) const
{
const DIFFRANGE * dfi = DiffRangeAt(nDiff);
if (static_cast<int>(nLine) >= dfi->dbegin && static_cast<int>(nLine) <= dfi->dend)
return true;
else
return false;
}
/**
* @brief Returns diff index for given line.
* @param [in] nLine Linenumber, 0-based.
* @return Index to diff table, -1 if line is not inside any diff.
*/
int DiffList::LineToDiff(int nLine) const
{
const int nDiffCount = static_cast<int>(m_diffs.size());
if (nDiffCount == 0)
return -1;
// First check line is not before first or after last diff
if (nLine < DiffRangeAt(0)->dbegin)
return -1;
if (nLine > DiffRangeAt(nDiffCount-1)->dend)
return -1;
// Use binary search to search for a diff.
int left = 0; // Left limit
int right = nDiffCount - 1; // Right limit
while (left <= right)
{
int middle = (left + right) / 2; // Compared item
int result = LineRelDiff(nLine, middle);
switch (result)
{
case -1: // Line is before diff in file
right = middle - 1;
break;
case 0: // Line is in diff
return middle;
break;
case 1: // Line is after diff in file
left = middle + 1;
break;
default:
{
std::stringstream s;
s << "Invalid return value " << result << " from LineRelDiff(): -1, 0 or 1 expected!";
throw s.str();
}
}
}
return -1;
}
/**
* @brief Return previous diff from given line.
* @param [in] nLine First line searched.
* @param [out] nDiff Index of diff found.
* @return true if line is inside diff, false otherwise.
*/
bool DiffList::GetPrevDiff(int nLine, int & nDiff) const
{
bool bInDiff = true;
int numDiff = LineToDiff(nLine);
// Line not inside diff
if (nDiff == -1)
{
bInDiff = false;
const int size = (int) m_diffs.size();
for (int i = (int) size - 1; i >= 0 ; i--)
{
if ((int)DiffRangeAt(i)->dend <= nLine)
{
numDiff = i;
break;
}
}
}
nDiff = numDiff;
return bInDiff;
}
/**
* @brief Return next difference from given line.
* This function finds next difference from given line. If line is inside
* difference, that difference is returned. If next difference is not found
* param @p nDiff is set to -1.
* @param [in] nLine First line searched.
* @param [out] nDiff Index of diff found.
* @return true if line is inside diff, false otherwise.
*/
bool DiffList::GetNextDiff(int nLine, int & nDiff) const
{
bool bInDiff = true;
int numDiff = LineToDiff(nLine);
// Line not inside diff
if (numDiff == -1)
{
bInDiff = false;
const int nDiffCount = (int) m_diffs.size();
for (int i = 0; i < nDiffCount; i++)
{
if ((int)DiffRangeAt(i)->dbegin >= nLine)
{
numDiff = i;
break;
}
}
}
nDiff = numDiff;
return bInDiff;
}
/**
* @brief Check if diff-list contains significant diffs.
* @return true if list has significant diffs, false otherwise.
*/
bool DiffList::HasSignificantDiffs() const
{
if (m_firstSignificant == -1)
return false;
return true;
}
/**
* @brief Return previous diff index from given line.
* @param [in] nLine First line searched.
* @return Index for next difference or -1 if no difference is found.
*/
int DiffList::PrevSignificantDiffFromLine(int nLine) const
{
int nDiff = -1;
const int size = (int) m_diffs.size();
for (int i = size - 1; i >= 0 ; i--)
{
const DIFFRANGE * dfi = DiffRangeAt(i);
if (dfi->op != OP_TRIVIAL && dfi->dend <= static_cast<int>(nLine))
{
nDiff = i;
break;
}
}
return nDiff;
}
/**
* @brief Return next diff index from given line.
* @param [in] nLine First line searched.
* @return Index for previous difference or -1 if no difference is found.
*/
int DiffList::NextSignificantDiffFromLine(int nLine) const
{
int nDiff = -1;
const int nDiffCount = static_cast<int>(m_diffs.size());
for (int i = 0; i < nDiffCount; i++)
{
const DIFFRANGE * dfi = DiffRangeAt(i);
if (dfi->op != OP_TRIVIAL && dfi->dbegin >= static_cast<int>(nLine))
{
nDiff = i;
break;
}
}
return nDiff;
}
/**
* @brief Construct the doubly-linked chain of significant differences
*/
void DiffList::ConstructSignificantChain()
{
m_firstSignificant = -1;
m_lastSignificant = -1;
m_firstSignificantLeftMiddle = -1;
m_firstSignificantLeftRight = -1;
m_firstSignificantMiddleRight = -1;
m_firstSignificantConflict = -1;
m_lastSignificantLeftMiddle = -1;
m_lastSignificantLeftRight = -1;
m_lastSignificantMiddleRight = -1;
m_lastSignificantConflict = -1;
ptrdiff_t prev = -1;
const ptrdiff_t size = (int) m_diffs.size();
// must be called after diff list is entirely populated
for (int i = 0; i < size; ++i)
{
if (m_diffs[i].op == OP_TRIVIAL)
{
m_diffs[i].prev = -1;
m_diffs[i].next = -1;
}
else
{
m_diffs[i].prev = prev;
if (prev != -1)
m_diffs[prev].next = (size_t) i;
prev = i;
if (m_firstSignificant == -1)
m_firstSignificant = i;
m_lastSignificant = i;
if (m_diffs[i].op != OP_TRIVIAL && m_diffs[i].op != OP_3RDONLY)
{
if (m_firstSignificantLeftMiddle == -1)
m_firstSignificantLeftMiddle = i;
m_lastSignificantLeftMiddle = i;
}
if (m_diffs[i].op != OP_TRIVIAL && m_diffs[i].op != OP_2NDONLY)
{
if (m_firstSignificantLeftRight == -1)
m_firstSignificantLeftRight = i;
m_lastSignificantLeftRight = i;
}
if (m_diffs[i].op != OP_TRIVIAL && m_diffs[i].op != OP_1STONLY)
{
if (m_firstSignificantMiddleRight == -1)
m_firstSignificantMiddleRight = i;
m_lastSignificantMiddleRight = i;
}
if (m_diffs[i].op == OP_1STONLY)
{
if (m_firstSignificantLeftOnly == -1)
m_firstSignificantLeftOnly = i;
m_lastSignificantLeftOnly = i;
}
if (m_diffs[i].op == OP_2NDONLY)
{
if (m_firstSignificantMiddleOnly == -1)
m_firstSignificantMiddleOnly = i;
m_lastSignificantMiddleOnly = i;
}
if (m_diffs[i].op == OP_3RDONLY)
{
if (m_firstSignificantRightOnly == -1)
m_firstSignificantRightOnly = i;
m_lastSignificantRightOnly = i;
}
if (m_diffs[i].op == OP_DIFF)
{
if (m_firstSignificantConflict == -1)
m_firstSignificantConflict = i;
m_lastSignificantConflict = i;
}
}
}
}
/**
* @brief Return index to first significant difference.
* @return Index of first significant difference.
*/
int DiffList::FirstSignificantDiff() const
{
return m_firstSignificant;
}
/**
* @brief Return index of next significant diff.
* @param [in] nDiff Index to start looking for next diff.
* @return Index of next significant difference.
*/
int DiffList::NextSignificantDiff(int nDiff) const
{
return (int)m_diffs[nDiff].next;
}
/**
* @brief Return index of previous significant diff.
* @param [in] nDiff Index to start looking for previous diff.
* @return Index of previous significant difference.
*/
int DiffList::PrevSignificantDiff(int nDiff) const
{
return (int)m_diffs[nDiff].prev;
}
/**
* @brief Return index to last significant diff.
* @return Index of last significant difference.
*/
int DiffList::LastSignificantDiff() const
{
return m_lastSignificant;
}
/**
* @brief Return pointer to first significant diff.
* @return Constant pointer to first significant difference.
*/
const DIFFRANGE * DiffList::FirstSignificantDiffRange() const
{
if (m_firstSignificant == -1)
return nullptr;
return DiffRangeAt(m_firstSignificant);
}
/**
* @brief Return pointer to last significant diff.
* @return Constant pointer to last significant difference.
*/
const DIFFRANGE * DiffList::LastSignificantDiffRange() const
{
if (m_lastSignificant == -1)
return nullptr;
return DiffRangeAt(m_lastSignificant);
}
/**
<<<<<<< .mine
* @brief Return previous diff index from given line.
* @param [in] nLine First line searched.
* @return Index for next difference or -1 if no difference is found.
*/
int DiffList::PrevSignificant3wayDiffFromLine(int nLine, int nDiffType) const
{
for (int i = static_cast<int>(m_diffs.size()) - 1; i >= 0 ; i--)
{
const DIFFRANGE * dfi = DiffRangeAt(i);
switch (nDiffType)
{
case THREEWAYDIFFTYPE_LEFTMIDDLE:
if (dfi->op != OP_TRIVIAL && dfi->op != OP_3RDONLY && dfi->dend <= static_cast<int>(nLine))
return i;
break;
case THREEWAYDIFFTYPE_LEFTRIGHT:
if (dfi->op != OP_TRIVIAL && dfi->op != OP_2NDONLY && dfi->dend <= static_cast<int>(nLine))
return i;
break;
case THREEWAYDIFFTYPE_MIDDLERIGHT:
if (dfi->op != OP_TRIVIAL && dfi->op != OP_1STONLY && dfi->dend <= static_cast<int>(nLine))
return i;
break;
case THREEWAYDIFFTYPE_LEFTONLY:
if (dfi->op == OP_1STONLY && dfi->dend <= static_cast<int>(nLine))
return i;
break;
case THREEWAYDIFFTYPE_MIDDLEONLY:
if (dfi->op == OP_2NDONLY && dfi->dend <= static_cast<int>(nLine))
return i;
break;
case THREEWAYDIFFTYPE_RIGHTONLY:
if (dfi->op == OP_3RDONLY && dfi->dend <= static_cast<int>(nLine))
return i;
break;
case THREEWAYDIFFTYPE_CONFLICT:
if (dfi->op == OP_DIFF && dfi->dend <= nLine)
return i;
break;
}
}
return -1;
}
/**
* @brief Return next diff index from given line.
* @param [in] nLine First line searched.
* @return Index for previous difference or -1 if no difference is found.
*/
int DiffList::NextSignificant3wayDiffFromLine(int nLine, int nDiffType) const
{
const int nDiffCount = static_cast<int>(m_diffs.size());
for (int i = 0; i < nDiffCount; i++)
{
const DIFFRANGE * dfi = DiffRangeAt(i);
switch (nDiffType)
{
case THREEWAYDIFFTYPE_LEFTMIDDLE:
if (dfi->op != OP_TRIVIAL && dfi->op != OP_3RDONLY && dfi->dbegin >= static_cast<int>(nLine))
return i;
break;
case THREEWAYDIFFTYPE_LEFTRIGHT:
if (dfi->op != OP_TRIVIAL && dfi->op != OP_2NDONLY && dfi->dbegin >= static_cast<int>(nLine))
return i;
break;
case THREEWAYDIFFTYPE_MIDDLERIGHT:
if (dfi->op != OP_TRIVIAL && dfi->op != OP_1STONLY && dfi->dbegin >= static_cast<int>(nLine))
return i;
break;
case THREEWAYDIFFTYPE_LEFTONLY:
if (dfi->op == OP_1STONLY && dfi->dbegin >= static_cast<int>(nLine))
return i;
break;
case THREEWAYDIFFTYPE_MIDDLEONLY:
if (dfi->op == OP_2NDONLY && dfi->dbegin >= static_cast<int>(nLine))
return i;
break;
case THREEWAYDIFFTYPE_RIGHTONLY:
if (dfi->op == OP_3RDONLY && dfi->dbegin >= static_cast<int>(nLine))
return i;
break;
case THREEWAYDIFFTYPE_CONFLICT:
if (dfi->op == OP_DIFF && dfi->dbegin >= nLine)
return i;
break;
}
}
return -1;
}
/**
* @brief Return index to first significant difference.
* @return Index of first significant difference.
*/
int DiffList::FirstSignificant3wayDiff(int nDiffType) const
{
switch (nDiffType)
{
case THREEWAYDIFFTYPE_LEFTMIDDLE:
return m_firstSignificantLeftMiddle;
case THREEWAYDIFFTYPE_LEFTRIGHT:
return m_firstSignificantLeftRight;
case THREEWAYDIFFTYPE_MIDDLERIGHT:
return m_firstSignificantMiddleRight;
case THREEWAYDIFFTYPE_LEFTONLY:
return m_firstSignificantLeftOnly;
case THREEWAYDIFFTYPE_MIDDLEONLY:
return m_firstSignificantLeftOnly;
case THREEWAYDIFFTYPE_RIGHTONLY:
return m_firstSignificantRightOnly;
case THREEWAYDIFFTYPE_CONFLICT:
return m_firstSignificantConflict;
}
return -1;
}
/**
* @brief Return index of next significant diff.
* @param [in] nDiff Index to start looking for next diff.
* @return Index of next significant difference.
*/
int DiffList::NextSignificant3wayDiff(int nDiff, int nDiffType) const
{
while (m_diffs[nDiff].next != -1)
{
nDiff = static_cast<int>(m_diffs[nDiff].next);
switch (nDiffType)
{
case THREEWAYDIFFTYPE_LEFTMIDDLE:
if (m_diffs[nDiff].op != OP_3RDONLY)
return nDiff;
break;
case THREEWAYDIFFTYPE_LEFTRIGHT:
if (m_diffs[nDiff].op != OP_2NDONLY)
return nDiff;
break;
case THREEWAYDIFFTYPE_MIDDLERIGHT:
if (m_diffs[nDiff].op != OP_1STONLY)
return nDiff;
break;
case THREEWAYDIFFTYPE_LEFTONLY:
if (m_diffs[nDiff].op == OP_1STONLY)
return nDiff;
break;
case THREEWAYDIFFTYPE_MIDDLEONLY:
if (m_diffs[nDiff].op == OP_2NDONLY)
return nDiff;
break;
case THREEWAYDIFFTYPE_RIGHTONLY:
if (m_diffs[nDiff].op == OP_3RDONLY)
return nDiff;
break;
case THREEWAYDIFFTYPE_CONFLICT:
if (m_diffs[nDiff].op == OP_DIFF)
return nDiff;
break;
}
}
return -1;
}
/**
* @brief Return index of previous significant diff.
* @param [in] nDiff Index to start looking for previous diff.
* @return Index of previous significant difference.
*/
int DiffList::PrevSignificant3wayDiff(int nDiff, int nDiffType) const
{
while (m_diffs[nDiff].prev != -1)
{
nDiff = static_cast<int>(m_diffs[nDiff].prev);
switch (nDiffType)
{
case THREEWAYDIFFTYPE_LEFTMIDDLE:
if (m_diffs[nDiff].op != OP_3RDONLY)
return nDiff;
break;
case THREEWAYDIFFTYPE_LEFTRIGHT:
if (m_diffs[nDiff].op != OP_2NDONLY)
return nDiff;
break;
case THREEWAYDIFFTYPE_MIDDLERIGHT:
if (m_diffs[nDiff].op != OP_1STONLY)
return nDiff;
break;
case THREEWAYDIFFTYPE_LEFTONLY:
if (m_diffs[nDiff].op == OP_1STONLY)
return nDiff;
break;
case THREEWAYDIFFTYPE_MIDDLEONLY:
if (m_diffs[nDiff].op == OP_2NDONLY)
return nDiff;
break;
case THREEWAYDIFFTYPE_RIGHTONLY:
if (m_diffs[nDiff].op == OP_3RDONLY)
return nDiff;
break;
case THREEWAYDIFFTYPE_CONFLICT:
if (m_diffs[nDiff].op == OP_DIFF)
return nDiff;
break;
}
}
return -1;
}
/**
* @brief Return index to last significant diff.
* @return Index of last significant difference.
*/
int DiffList::LastSignificant3wayDiff(int nDiffType) const
{
switch (nDiffType)
{
case THREEWAYDIFFTYPE_LEFTMIDDLE:
return m_lastSignificantLeftMiddle;
case THREEWAYDIFFTYPE_LEFTRIGHT:
return m_lastSignificantLeftRight;
case THREEWAYDIFFTYPE_MIDDLERIGHT:
return m_lastSignificantMiddleRight;
case THREEWAYDIFFTYPE_LEFTONLY:
return m_lastSignificantLeftOnly;
case THREEWAYDIFFTYPE_MIDDLEONLY:
return m_lastSignificantLeftOnly;
case THREEWAYDIFFTYPE_RIGHTONLY:
return m_lastSignificantRightOnly;
case THREEWAYDIFFTYPE_CONFLICT:
return m_lastSignificantRightOnly;
}
return -1;
}
/**
* @brief Return pointer to first significant diff.
* @return Constant pointer to first significant difference.
*/
const DIFFRANGE * DiffList::FirstSignificant3wayDiffRange(int nDiffType) const
{
switch (nDiffType)
{
case THREEWAYDIFFTYPE_LEFTMIDDLE:
if (m_firstSignificantLeftMiddle == -1) return nullptr;
return DiffRangeAt(m_firstSignificantLeftMiddle);
case THREEWAYDIFFTYPE_LEFTRIGHT:
if (m_firstSignificantLeftRight == -1) return nullptr;
return DiffRangeAt(m_firstSignificantLeftRight);
case THREEWAYDIFFTYPE_MIDDLERIGHT:
if (m_firstSignificantMiddleRight == -1) return nullptr;
return DiffRangeAt(m_firstSignificantMiddleRight);
case THREEWAYDIFFTYPE_LEFTONLY:
if (m_firstSignificantLeftOnly == -1) return nullptr;
return DiffRangeAt(m_firstSignificantLeftOnly);
case THREEWAYDIFFTYPE_MIDDLEONLY:
if (m_firstSignificantMiddleOnly == -1) return nullptr;
return DiffRangeAt(m_firstSignificantMiddleOnly);
case THREEWAYDIFFTYPE_RIGHTONLY:
if (m_firstSignificantRightOnly == -1) return nullptr;
return DiffRangeAt(m_firstSignificantRightOnly);
case THREEWAYDIFFTYPE_CONFLICT:
if (m_firstSignificantConflict == -1) return nullptr;
return DiffRangeAt(m_firstSignificantConflict);
}
return nullptr;
}
/**
* @brief Return pointer to last significant diff.
* @return Constant pointer to last significant difference.
*/
const DIFFRANGE * DiffList::LastSignificant3wayDiffRange(int nDiffType) const
{
switch (nDiffType)
{
case THREEWAYDIFFTYPE_LEFTMIDDLE:
if (m_lastSignificantLeftMiddle == -1) return nullptr;
return DiffRangeAt(m_lastSignificantLeftMiddle);
case THREEWAYDIFFTYPE_LEFTRIGHT:
if (m_lastSignificantLeftRight == -1) return nullptr;
return DiffRangeAt(m_lastSignificantLeftRight);
case THREEWAYDIFFTYPE_MIDDLERIGHT:
if (m_lastSignificantMiddleRight == -1) return nullptr;
return DiffRangeAt(m_lastSignificantMiddleRight);
case THREEWAYDIFFTYPE_LEFTONLY:
if (m_lastSignificantLeftOnly == -1) return nullptr;
return DiffRangeAt(m_lastSignificantLeftOnly);
case THREEWAYDIFFTYPE_MIDDLEONLY:
if (m_lastSignificantMiddleOnly == -1) return nullptr;
return DiffRangeAt(m_lastSignificantMiddleOnly);
case THREEWAYDIFFTYPE_RIGHTONLY:
if (m_lastSignificantRightOnly == -1) return nullptr;
return DiffRangeAt(m_lastSignificantRightOnly);
case THREEWAYDIFFTYPE_CONFLICT:
if (m_lastSignificantConflict == -1) return nullptr;
return DiffRangeAt(m_lastSignificantConflict);
}
return nullptr;
}
/**
* @brief Swap sides of diffrange
*/
void DiffList::Swap(int index1, int index2)
{
vector<DiffRangeInfo>::iterator iter = m_diffs.begin();
vector<DiffRangeInfo>::const_iterator iterEnd = m_diffs.end();
while (iter != iterEnd)
{
(*iter).swap_sides(index1, index2);
++iter;
}
}
/**
* @brief Count number of lines to add to sides (because of synch).
* @param [out] nLeftLines Number of lines to add to left side.
* @param [out] nRightLines Number of lines to add to right side.
*/
void DiffList::GetExtraLinesCounts(int nFiles, int extras[3])
{
extras[0]=0;
extras[1]=0;
extras[2]=0;
const int nDiffCount = GetSize();
for (int nDiff = 0; nDiff < nDiffCount; ++nDiff)
{
DIFFRANGE curDiff;
GetDiff(nDiff, curDiff);
// this guarantees that all the diffs are synchronized
assert(curDiff.begin[0]+extras[0] == curDiff.begin[1]+extras[1]);
assert(nFiles<3 || curDiff.begin[0]+extras[0] == curDiff.begin[2]+extras[2]);
int nline[3] = { 0,0,0 };
int nmaxline = 0;
int file;
for (file = 0; file < nFiles; file++)
{
nline[file] = curDiff.end[file]-curDiff.begin[file]+1;
nmaxline = std::max(nmaxline, nline[file]);
}
for (file = 0; file < nFiles; file++)
extras[file] += nmaxline - nline[file];
}
}
void DiffList::AppendDiffList(const DiffList& list, int offset[] /*= nullptr*/, int doffset /*= 0*/)
{
for (std::vector<DiffRangeInfo>::const_iterator it = list.m_diffs.begin(); it != list.m_diffs.end(); ++it)
{
DiffRangeInfo dr = *it;
for (int file = 0; file < 3; ++file)
{
if (offset != nullptr)
{
dr.begin[file] += offset[file];
dr.end[file] += offset[file];
}
if (doffset != 0)
dr.blank[file] += doffset;
}
if (doffset != 0)
{
dr.dbegin += doffset;
dr.dend += doffset;
}
AddDiff(dr);
}
}
int DiffList::GetMergeableSrcIndex(int nDiff, int nDestIndex) const
{
const DIFFRANGE *pdr = DiffRangeAt(nDiff);
switch (nDestIndex)
{
case 0:
case 2:
if (pdr->op == OP_2NDONLY)
return 1;
return -1;
case 1:
if (pdr->op == OP_1STONLY || pdr->op == OP_2NDONLY)
return 0;
else if (pdr->op == OP_3RDONLY)
return 2;
return -1;
default:
return -1;
}
}