File indexing completed on 2024-03-24 17:26:49

0001 /*
0002     This file is part of the Okteta Gui library, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2003 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "coordrangelist.hpp"
0010 
0011 namespace Okteta {
0012 
0013 CoordRangeList::CoordRangeList() = default;
0014 
0015 CoordRangeList::~CoordRangeList() = default;
0016 
0017 void CoordRangeList::addCoordRange(const CoordRange& coordRange)
0018 {
0019     if (!coordRange.isValid()) {
0020         return;
0021     }
0022 
0023     // we try to insert it by ascending indizes
0024     // if sections are overlapping we combine them
0025     Iterator it = begin();
0026     Iterator endIt = end();
0027     for (; it != endIt; ++it) {
0028         // TODO: add bufferwidth to rangelist so consecutive ranges can be joined, cmp sectionlist
0029         // is next CoordRange behind the new CoordRange?
0030         if (coordRange.endsBefore(*it)) {
0031             // put the new before it
0032             insert(it, coordRange);
0033             return;
0034         }
0035 
0036         // does the next CoordRange overlap?
0037         if ((*it).overlaps(coordRange)) {
0038             CoordRange mergedCoordRange(coordRange);
0039             // Start of the combined sections is the smaller one
0040             mergedCoordRange.extendStartTo((*it).start());
0041             // next we search all the overlapping sections and keep the highest end index
0042             Coord endCoord((*it).end());
0043             iterator it2 = it;
0044             for (++it2; it2 != endIt; ++it2) {
0045                 if (coordRange.endsBefore((*it2).start())) {
0046                     break;
0047                 }
0048                 endCoord = (*it2).end();
0049             }
0050 
0051             // the higher end is the end of the combined CoordRange
0052             mergedCoordRange.extendEndTo(endCoord);
0053             // remove all overlapping sections
0054             it = erase(it, it2);
0055             // and instead insert the combined one
0056             insert(it, mergedCoordRange);
0057             return;
0058         }
0059     }
0060 
0061     // all others are before the new?
0062     if (it == endIt) {
0063         // add it at the end
0064         append(coordRange);
0065     }
0066 }
0067 
0068 }