summaryrefslogtreecommitdiffstats
path: root/src/merger.cpp
blob: 371548c3725910f9dc6610050c6eb9b9da7df373 (plain)
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
/***************************************************************************
                          merger.cpp  -  description
                             -------------------
    begin                : Sun Mar 24 2002
    copyright            : (C) 2002-2007 by Joachim Eibl
    email                : joachim.eibl at gmx.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "merger.h"
#include <assert.h>
#include <iostream>
#include <iomanip>

Merger::Merger( const DiffList* pDiffListAB, const DiffList* pDiffListCA )
: md1( pDiffListAB, 0 ), md2( pDiffListCA, 1 )
{
}


Merger::MergeData::MergeData( const DiffList* p, int i )
: d(0,0,0)
{
   idx=i;
   pDiffList = p;
   if ( p!=0 )
   {
      it=p->begin();
      update();
   }
}

bool Merger::MergeData::eq()
{
   return pDiffList==0 || d.nofEquals > 0;
}

bool Merger::MergeData::isEnd()
{
   return ( pDiffList==0 || ( it==pDiffList->end() && d.nofEquals==0 && 
      ( idx == 0 ? d.diff1==0 : d.diff2==0 )
      ) );  
}

void Merger::MergeData::update()
{
   if ( d.nofEquals > 0 )
      --d.nofEquals;
   else if ( idx==0 && d.diff1 > 0 )
      --d.diff1;
   else if ( idx==1 && d.diff2 > 0 )
      --d.diff2; 

   while( d.nofEquals == 0  && (idx==0 && d.diff1 == 0 || idx==1 && d.diff2 == 0) 
       && pDiffList!=0  &&  it != pDiffList->end() )
   {
      d = *it;
      ++it;
   }
}

void Merger::next()
{
   md1.update();
   md2.update();
}

int Merger::whatChanged()
{
   int changed = 0;
   changed |= md1.eq()  ?  0 : 1;
   changed |= md2.eq()  ?  0 : 2;
   return changed;
}

bool Merger::isEndReached()
{
   return md1.isEnd() && md2.isEnd();
}