File indexing completed on 2024-05-26 13:37:34

0001 // clang-format off
0002 /*
0003  *  This file is part of KDiff3.
0004  *
0005  * SPDX-FileCopyrightText: 2002-2011 Joachim Eibl, joachim.eibl at gmx.de
0006  * SPDX-FileCopyrightText: 2018-2020 Michael Reeves reeves.87@gmail.com
0007  * SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 // clang-format on
0010 
0011 #include "merger.h"
0012 
0013 #include <list>
0014 #include <memory>
0015 
0016 Merger::Merger(const std::shared_ptr<const DiffList>& pDiffList1, const std::shared_ptr<const DiffList>& pDiffList2):
0017     md1(pDiffList1, 0), md2(pDiffList2, 1)
0018 {
0019 }
0020 
0021 Merger::MergeData::MergeData(const std::shared_ptr<const DiffList>& p, qint32 i)
0022 {
0023     idx = i;
0024     pDiffList = p;
0025     if(p != nullptr)
0026     {
0027         it = p->begin();
0028         update();
0029     }
0030 }
0031 
0032 bool Merger::MergeData::eq() const
0033 {
0034     return pDiffList == nullptr || d.numberOfEquals() > 0;
0035 }
0036 
0037 bool Merger::MergeData::isEnd() const
0038 {
0039     return (pDiffList == nullptr || (it == pDiffList->end() && d.numberOfEquals() == 0 &&
0040                                      (idx == 0 ? d.diff1() == 0 : d.diff2() == 0)));
0041 }
0042 
0043 void Merger::MergeData::update()
0044 {
0045     if(d.numberOfEquals() > 0)
0046         d.adjustNumberOfEquals(-1);
0047     else if(idx == 0 && d.diff1() > 0)
0048         d.adjustDiff1(-1);
0049     else if(idx == 1 && d.diff2() > 0)
0050         d.adjustDiff2(-1);
0051 
0052     while(d.numberOfEquals() == 0 && ((idx == 0 && d.diff1() == 0) || (idx == 1 && d.diff2() == 0)) && pDiffList != nullptr && it != pDiffList->end())
0053     {
0054         d = *it;
0055         ++it;
0056     }
0057 }
0058 
0059 void Merger::next()
0060 {
0061     md1.update();
0062     md2.update();
0063 }
0064 
0065 ChangeFlags Merger::whatChanged()
0066 {
0067     ChangeFlags changed = ChangeFlag::NoChange;
0068     changed |= md1.eq() ? ChangeFlag::NoChange : ChangeFlag::AChanged;
0069     changed |= md2.eq() ? ChangeFlag::NoChange : ChangeFlag::BChanged;
0070     return changed;
0071 }
0072 
0073 bool Merger::isEndReached()
0074 {
0075     return md1.isEnd() && md2.isEnd();
0076 }